Hey I'm trying to do blinking led using interrupts on 16 bit timer. My atmega model is Atmega168A. Chip clock rate is 12Mhz.
Here is my code:
#include <avr/io.h>
#include <avr/interrupt.h>
ISR(TIMER1_COMPA_vect)
{
PORTC ^= (1 << PC5);
}
int main()
{
DDRC |= (1 << PC5);
PORTC |= (1 << PC5); //led on
sei();
TIMSK1 |= (1 << OCIE1A); // cmp interruupt enable
TCCR1B |= (1 << CS12) | (1 << CS10); //prescalser 64
OCR1A = 11718; // value to compare
TCCR1A |= (1 << WGM12); // set ctc mode
while(1)
{
}
return 0;
}
When the program startup led turn on. After compare match it should turn off. And it works, but with one strange problem.
If I change OCR1A, only the first peroid of time before led on and off changes.
When I set OCR1A e.g 0x100 the led turns off very quickly.
When I set OCR1A e.g 0xfffa the led turns off slowly.
But only the first state change.
Another led blinks do not take over about changes of OCR1A.
Whats wrong?
Do I need reset some flags or something like this?