hello,
another question with a flashing led... i want to be sure about my understanding of avr programming.
first of all i precise the background. i want to blink a led each second. so i use timer0 with interrupt in CTC mode with ATTiny85. a brand new attiny works at 1mhz, i choose a prescaler of 1024.
so the clock frequency falls at about 976hz.
one cycle last ~0.001024. consequently in one second we have ~4 overflows (max value of the counter is 254). am i right ?
here the code :
#define F_CPU 1000000
#include
#include
#include
volatile int cpt = 0;
int main(void)
{
DDRB = (1<<0);
prescaler of 1024
TCCR0B |= (1<<CS02) | (0<<CS01) | (1<<CS00);
//enable CTC mode
TCCR0A |= (1<<WGM01);
//max value of the counter
OCR0A = 254;
//enable interrupt on timer0
TIMSK = (1<<OCIE0A);
sei();
while(1)
{
//if we reach 4 overflows (~1 second)
if(cpt>=4){
PORTB |= (1 << PB0);
_delay_ms(10);
PORTB &= ~(1 << PB0);
cpt=0;
}
}
}
ISR(TIMER0_COMPA_vect){
cpt++;
}
in order to prevent errors with delay i avoid puting too much things into ISR(), so i put the blink part into the loop. any advices about the organisation or optimization of this code ?