想用Timer0定时1s,可定时时间总是不准确,不知什么原因,希望大家帮忙看看!!
用的是TIMER0比较匹配,这是我的定时计算式:定时器时钟为1Mhz/64,指令周期为1/(1MHZ/64)=0.064ms,0.064*125*125=1000ms
#define FOSC 1000000UL
int cnt;
int main(void)
{
DDRD = 0x03;
PORTD = 0x00;
ATmega88P_Timer0_Init();
SREG |= 0x80;
while(1);
}
void ATmega88P_Timer0_Init()
{
TCCR0A = 0x02;
TCCR0B = 0x03;
TIMSK0 = 0x02;
OCR0A = 124;
}
ISR(TIMER0_COMPA_vect)
{
cnt++;
if(cnt >= 124)
{
cnt = 0;
PORTD ^= 0x03;
}
}
|