I have a problem which I have not been able to solve and it is making me crazy. I am using an interruption to change the width of the PWM pulse in a ATMEGA328P at every cycle. Actually my code seems to be working for a while, but after some cyclic time, the TIMER1 seems not to respond properly (see the photo attached: the high frequency signal is where my code is changing the PWM every cycle, so it is working. But after ~27ms it keeps in the same state for ~32ms. In other words, it stops working for ~32ms and it happens cyclically). Can anyone please help me to find out what is the problem with my code?
My timer configuration:
cli(); //Disable interrupts
DDRB = (1 << PORTB1); //Set port B1 as output in the in the register DDRB
TCCR1A = 0;
TCCR1B = 0;
TCCR1A |= (1 << COM1A0);
TCCR1B |= (1 << WGM12)|(1 << WGM13); //CTC mode, using ICR1
ICR1 = 511;
TIMSK1 |= (1 << OCIE1A); // OCF1A: Timer/Counter1, Output Compare A Match Flag is enabled. Interruption is executed when the TOV1 Flag is set.
TCCR1B |= (1 << CS11); // clk/8
Sei(); //Enable interrupts
Code running in the interruption:
volatile int InterruptionArray ;
volatile int counter1 = 0;
ICR1 = InterruptionArray [counter1];