I am trying to use Timer2 on an Elegoo Nano to produce a "tick" interrupt every 10uS. My plan was to set Timer2 to CTC mode with a /8 prescaler (to get a timer clock of 2MHz from the 16MHz chip clock) and a compare value of 19 (to reset the timer every 20 counts) which should trigger the compareA ISR at 100KHz (every 10us). The problem is that only prescale values of 64 or greater work as expected. Setting the prescaler to any value lower than 64 actually results in the "tick" interval getting much longer (10 times the interval or more). I have carefully studied the ATmega628 datasheet to see if I am configuring every register correctly, but I don't see anything wrong with what I am doing. Here is my code...
void initTimer() {
// ---Disable interrupts during timer initialization
cli();
// ---Counter compare value (make CTC interrupt fire every 10uS)
OCR2A = 19;
// ---Clear TCCR A register (not used)
TCCR2A = 0;
// ---Enable CTC mode with divide-by-8 prescaler
TCCR2B = _BV( WGM22 ) | _BV( CS21 );
// ---Enable Timer2 CTC interrupts for compare A match
TIMSK2 |= _BV( OCIE2A );
// ---Initialize timer 2 to zero (not really needed in my situation)
TCNT2 = 0;
// ---Turn interrupts back on
sei();
}
SIGNAL( TIMER2_COMPA_vect ) {
/*
** This should execute every 10uS, but for prescaler values
** below 64 the interval is many times longer than expected.
*/
}
Can anyone point out my mistake?