Electrical – Timer1 on ATtiny85 not working as expected

attinyavrinterruptsmicrocontrollertimer

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;
}

Best Answer

The CKDIV8 fuse is programmed on devices fresh from the factory. If you forget to unprogram this fuse then the device will run at 1/8 the expected clock speed.