# Timer1 on ATtiny85 not working as expected

I am trying to use Timer1 on the ATtiny85 (Adafruit Trinket) to blink an LED every second. The prescaler value is 1024, and the compare register value is 28. I loop through this 279 times to get a delay of 1 second.

$$\frac{1024\ . 28\ . 279}{8\ .10^6}\simeq1$$

With the code below I get something like 8.5 seconds. I set -mmcu=attiny85 and -DF_CPU=8000000UL when compiling. What could I be missing.

#include <stdint.h>
#include <avr/interrupt.h>

#define TIMER_OVERFLOW_COUNT 279

volatile uint16_t timerCount = TIMER_OVERFLOW_COUNT;
ISR(TIMER1_OVF_vect)
{
timerCount++;
}

int main(void)
{
DDRB = 1 << DDB1;
OCR1C = 28;
TIMSK = 1 << TOIE1;
sei();
TCCR1 = (1 << CS13) | (1 << CS11) | (1 << CS10);
while( 1)
{
if( timerCount >= TIMER_OVERFLOW_COUNT)
{
timerCount = 0;
PORTB ^= 1 << PORTB1;
}
}
return 0;
}

• Updating the timerCount in both interrupt handler and the loop is a bad idea. You have a race condition here. Why not put all of the logic in the handler? It's not that heavy. – Eugene Sh. Aug 30 '16 at 14:42
• Wait. You don't want to interrupt on overflow, you want to interrupt on compare match – Eugene Sh. Aug 30 '16 at 14:56
• @SubaThomas I am right for sure. But the way to do it should be described in the datasheet an demonstrated in some tutorials around. – Eugene Sh. Aug 30 '16 at 15:17
• @EugeneSh and Bence Kaulics, thanks for your help. I read the datasheet with a bit more care and found this: "In the normal mode an overflow interrupt (TOV1) is generated when Timer/Counter1 counts from $FF to$00..." Essentially the compare match option is not available in the normal mode. I changed TIMER_OVERFLOW_COUNT to 31 and set OCR1C to 255 (which is really superfluous) and things work as expected. – Suba Thomas Aug 30 '16 at 16:51
• @BenceKaulics, this timer does not have a CTC mode. There is a CTC1 bit that resets the timer counter when there is a match with OCR1C, but no interrupt to capture when it happens. – Suba Thomas Aug 30 '16 at 18:41