Các bác cho em hỏi em dùng ngắt ngoài để đếm xung và dùng timer1 để tạo khoảng thời gian trễ 1s để đo tốc độ động cơ nhưng có điều em chưa biết cách gán thế nào cho các thanh ghi để được 1s mong các bác chỉ bảo
/************************************************** ***
This program was produced by the
CodeWizardAVR V2.03.4 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
HP InfoTech, Development Tools for Microcontrollers, C Compilers, In-System Programmers
Project :
Version :
Date : 11/21/2011
Author :
Company :
Comments:
Chip type : ATmega16
Program type : Application
Clock frequency : 7.372800 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
************************************************** ***/
#include <mega16.h>
#include <delay.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>
// Standard Input/Output functions
#include <stdio.h>
unsigned int k=0,h;
unsigned int num; // Bien dem so xung vao chan INT0
unsigned int TD,tocdo,MENU,pass; // Toc do dong co
eeprom int CB1=150,CB3=450, ;// ,CB2,CB3,TANG,GIAM;
eeprom int CB2=300;
eeprom unsigned int ranh=2;
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Place your code here
TCNT0=0; // Ð?t l?i giá tr?
}
// Timer 1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
k++; h++;
if (h==100) // Da du thoi gian dat (chua tinh chinh xac)
{
num=TCNT0; // Gan so xung tu thanh ghi vao bien num ????????????????????
TD=num/ranh ; // Toc do dong co thuc te (vi la mo phong nen coi nhu encoder co 1 ranh)
//PORTA=PORTA^0xFF;
h=0;
TCNT0=0 ; // Ðat lai so xung tren thanh ghi.
}
}
void HIEN_THI()
{
unsigned char a,b,c;
tocdo=TCNT0*60 ;
a=tocdo/100;
// lay fan tram
b=(tocdo-100*a)/10;
// lay fan chuc
c=(tocdo-100*a-10*b);
// lay hang don vi
lcd_gotoxy(8,0);
// ve vi tri x,y
lcd_putchar(a+48);
// day ra hang tram, ma ascii
lcd_gotoxy(9,0);
lcd_putchar(b+48);
// day ra hang chuc, ma ascii
lcd_gotoxy(10,0);
lcd_putchar(c+48);
// day ra hang don vi, ma ascii
}
void CANHBAO1()
{
unsigned char d,e,f,d1,e1,f1,d2,e2,f2;
d=CB1/100;
// lay fan tram
e=(CB1-100*d)/10;
// lay fan chuc
f=(CB1-100*d-10*e);
// lay hang don vi
lcd_gotoxy(5,1);
// ve vi tri x,y
lcd_putchar(d+48);
// day ra hang tram, ma ascii
lcd_gotoxy(6,1);
lcd_putchar(e+48);
// day ra hang chuc, ma ascii
lcd_gotoxy(7,1);
lcd_putchar(f+48);
d1=CB2/100;
// lay fan tram
e1=(CB2-100*d1)/10;
// lay fan chuc
f1=(CB2-100*d1-10*e1);
// lay hang don vi
lcd_gotoxy(9,1);
// ve vi tri x,y
lcd_putchar(d1+48);
// day ra hang tram, ma ascii
lcd_gotoxy(10,1);
lcd_putchar(e1+48);
// day ra hang chuc, ma ascii
lcd_gotoxy(11,1);
lcd_putchar(f1+48);
// day ra hang don vi, ma ascii
d2=CB3/100;
// lay fan tram
e2=(CB3-100*d2)/10;
// lay fan chuc
f2=(CB3-100*d2-10*e2);
// lay hang don vi
lcd_gotoxy(13,1);
// ve vi tri x,y
lcd_putchar(d2+48);
// day ra hang tram, ma ascii
lcd_gotoxy(14,1);
lcd_putchar(e2+48);
// day ra hang chuc, ma ascii
lcd_gotoxy(15,1);
lcd_putchar(f2+48);
// day ra hang don vi, ma ascii
}
// Declare your global variables here
void main(void)
{
/// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=1 State6=1 State5=1 State4=1 State3=1 State2=1 State1=1 State0=1
//PORTA=0x00;
//DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTC=0x00;
DDRC=0xFF;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=P State6=P State5=P State4=P State3=P State2=P State1=P State0=P
PORTD=0x00;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 7372.800 kHz
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x01;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x05;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x2F;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// LCD module initialization
lcd_init(16);
// Global enable interrupts
DDRB = 0x00;
PORTB = 0xFF;
DDRA = 0x00;
PORTA = 0xFF;
TCCR0 = 0x06;
TCNT0 = 0;
#asm("sei")
while (1)
{
if (PINA.0==0) { PORTD.5==1; lcd_gotoxy(0,0);// Dua con tro ve goc, cot 0, dong 0
lcd_putsf("SET UP:"); }
{ lcd_gotoxy(0,0);// Dua con tro ve goc, cot 0, dong 0
lcd_putsf("Speeds:");}
lcd_gotoxy(0,0);// Dua con tro ve goc, cot 0, dong 0
lcd_putsf("Speeds:");// Hien thi dong chu1
HIEN_THI(); // Hien thi gia tri toc do dong co }
CANHBAO1 ();
lcd_gotoxy(11,0);// Dua con tro ve goc, cot 0, dong 0
lcd_putsf("(Rpm)");// Hien thi dong chu
lcd_gotoxy(0,1);// Dua con tro ve goc, cot 0, dong 0
lcd_putsf("(Set)");// Hien thi dong chu
// dieu khien role
}
}
/************************************************** ***
This program was produced by the
CodeWizardAVR V2.03.4 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
HP InfoTech, Development Tools for Microcontrollers, C Compilers, In-System Programmers
Project :
Version :
Date : 11/21/2011
Author :
Company :
Comments:
Chip type : ATmega16
Program type : Application
Clock frequency : 7.372800 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
************************************************** ***/
#include <mega16.h>
#include <delay.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>
// Standard Input/Output functions
#include <stdio.h>
unsigned int k=0,h;
unsigned int num; // Bien dem so xung vao chan INT0
unsigned int TD,tocdo,MENU,pass; // Toc do dong co
eeprom int CB1=150,CB3=450, ;// ,CB2,CB3,TANG,GIAM;
eeprom int CB2=300;
eeprom unsigned int ranh=2;
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Place your code here
TCNT0=0; // Ð?t l?i giá tr?
}
// Timer 1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
k++; h++;
if (h==100) // Da du thoi gian dat (chua tinh chinh xac)
{
num=TCNT0; // Gan so xung tu thanh ghi vao bien num ????????????????????
TD=num/ranh ; // Toc do dong co thuc te (vi la mo phong nen coi nhu encoder co 1 ranh)
//PORTA=PORTA^0xFF;
h=0;
TCNT0=0 ; // Ðat lai so xung tren thanh ghi.
}
}
void HIEN_THI()
{
unsigned char a,b,c;
tocdo=TCNT0*60 ;
a=tocdo/100;
// lay fan tram
b=(tocdo-100*a)/10;
// lay fan chuc
c=(tocdo-100*a-10*b);
// lay hang don vi
lcd_gotoxy(8,0);
// ve vi tri x,y
lcd_putchar(a+48);
// day ra hang tram, ma ascii
lcd_gotoxy(9,0);
lcd_putchar(b+48);
// day ra hang chuc, ma ascii
lcd_gotoxy(10,0);
lcd_putchar(c+48);
// day ra hang don vi, ma ascii
}
void CANHBAO1()
{
unsigned char d,e,f,d1,e1,f1,d2,e2,f2;
d=CB1/100;
// lay fan tram
e=(CB1-100*d)/10;
// lay fan chuc
f=(CB1-100*d-10*e);
// lay hang don vi
lcd_gotoxy(5,1);
// ve vi tri x,y
lcd_putchar(d+48);
// day ra hang tram, ma ascii
lcd_gotoxy(6,1);
lcd_putchar(e+48);
// day ra hang chuc, ma ascii
lcd_gotoxy(7,1);
lcd_putchar(f+48);
d1=CB2/100;
// lay fan tram
e1=(CB2-100*d1)/10;
// lay fan chuc
f1=(CB2-100*d1-10*e1);
// lay hang don vi
lcd_gotoxy(9,1);
// ve vi tri x,y
lcd_putchar(d1+48);
// day ra hang tram, ma ascii
lcd_gotoxy(10,1);
lcd_putchar(e1+48);
// day ra hang chuc, ma ascii
lcd_gotoxy(11,1);
lcd_putchar(f1+48);
// day ra hang don vi, ma ascii
d2=CB3/100;
// lay fan tram
e2=(CB3-100*d2)/10;
// lay fan chuc
f2=(CB3-100*d2-10*e2);
// lay hang don vi
lcd_gotoxy(13,1);
// ve vi tri x,y
lcd_putchar(d2+48);
// day ra hang tram, ma ascii
lcd_gotoxy(14,1);
lcd_putchar(e2+48);
// day ra hang chuc, ma ascii
lcd_gotoxy(15,1);
lcd_putchar(f2+48);
// day ra hang don vi, ma ascii
}
// Declare your global variables here
void main(void)
{
/// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=1 State6=1 State5=1 State4=1 State3=1 State2=1 State1=1 State0=1
//PORTA=0x00;
//DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTC=0x00;
DDRC=0xFF;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=P State6=P State5=P State4=P State3=P State2=P State1=P State0=P
PORTD=0x00;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 7372.800 kHz
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x01;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x05;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x2F;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// LCD module initialization
lcd_init(16);
// Global enable interrupts
DDRB = 0x00;
PORTB = 0xFF;
DDRA = 0x00;
PORTA = 0xFF;
TCCR0 = 0x06;
TCNT0 = 0;
#asm("sei")
while (1)
{
if (PINA.0==0) { PORTD.5==1; lcd_gotoxy(0,0);// Dua con tro ve goc, cot 0, dong 0
lcd_putsf("SET UP:"); }
{ lcd_gotoxy(0,0);// Dua con tro ve goc, cot 0, dong 0
lcd_putsf("Speeds:");}
lcd_gotoxy(0,0);// Dua con tro ve goc, cot 0, dong 0
lcd_putsf("Speeds:");// Hien thi dong chu1
HIEN_THI(); // Hien thi gia tri toc do dong co }
CANHBAO1 ();
lcd_gotoxy(11,0);// Dua con tro ve goc, cot 0, dong 0
lcd_putsf("(Rpm)");// Hien thi dong chu
lcd_gotoxy(0,1);// Dua con tro ve goc, cot 0, dong 0
lcd_putsf("(Set)");// Hien thi dong chu
// dieu khien role
}
}