我有一个atmega128L的开发板。我已将其Timer1配置为CTC模式,将Timer 0配置为溢出模式,并启用两个定时器的中断。 我的CPU时钟频率为8Mhz。我正在为两个计时器使用两个计数器变量,它们将跟踪计时器执行其isr的次数,并基于我在主代码中执行操作。根据我比较计时器0的计数器变量的值,它应该每16秒后执行一次。但它每4秒就会采取一次行动。我尝试过不同的预分频器,我已经能够成功地为timer1采取行动。保险丝字节设置为高:1F,低:E4,扩展:FF我附上了我的代码的相关部分以供参考:
int main()
{
cli();
timer1_init();
timer0_init();
sei();
while (1)
{
if(meter_interrupt_flag==1)
{
meter_interrupt_flag=0;
transmitData(meter_interrupt_msg,strlen(meter_interrupt_msg));
//send some string "reading meter"
}
if(connection_interrupt_flag==1);
{
connection_interrupt_flag=0;
checkNetworkConnection();
transmitData(connection_interrupt_msg,strlen(connection_interrupt_msg));
//send some string "connection interrupt"
}
}
}
void timer1_init(void)
{
overflow_counter=0;
// set up timer with prescaler = 1024
TCCR1B |= (1 << CS12) |(1 << CS10) | (1 << WGM12);
// initialize counter
TCNT1 = 0;
OCR1A = 39062;
TIMSK |= (1 << OCIE1A);
}
void timer0_init(void)
{
overflow_counter_3=0;
TCCR0 |= (1 << CS02) |(1 << CS01); //prescalar 256
TCNT0 = 0;
TIMSK |= (1 << TOIE0);
}
ISR(TIMER1_COMPA_vect)
{
overflow_counter++;
if(overflow_counter>=12)
{
meter_interrupt_flag=1;
overflow_counter=0;
}
}
ISR(TIMER0_OVF_vect)
{
overflow_counter_3++;
if(overflow_counter_3>=4000)
{
connection_interrupt_flag=1;
overflow_counter_3=0;
}
}