thanks for reading in advance. Please have a look at the code below and note that TCCR0's interrupt is being triggered every now and then, which means what's inside ISR(TIMER0_OVF_vect) is run. However, I have set up TCCR2 the same way, but ISR(TIMER2_OVF_vect) will not be called. Also, what's inside ISRs are just temporary codes for testing purposes.
void init_timer0(void)
{
TCCR0 = 0x07;// 32 prescaler
// 0000 0111b
// clkI/O/1024 (From prescaler)
TCNT0 = 10;
TCCR2 = 0x07;
//TCCR2 = 0x0E;
TCNT2 = 20;
TIMSK |= 0x01;
TIMSK |= 0x40;
}
volatile int i = 0;
ISR(TIMER2_OVF_vect)
{
TCNT2 = 20;
i = 1;
i = 2;
}
ISR(TIMER0_OVF_vect)
{
TCNT0 = 10;
}
CodePudding user response:
Table 68 of the data sheet says for TCCR2 (excerpt):
CS22 | CS21 | CS20 | Description |
---|---|---|---|
1 | 0 | 1 | clkI/O/1024 (From prescaler) |
1 | 1 | 1 | External clock source on T2 pin. Clock on rising edge. |
You tell the AVR to use an external clock (TCCR2 = 0x07;
) but you want to use the system clock.
Use TCCR2 = 0x05;
instead.