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;
}
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. \$\endgroup\$