Phase and frequency correct PWM mode Arduino Mega 2560 I wrote a code to generate 1kHz Phase and frequency correct PWM signal from Arduino mega 2560 using timer 4. When I try the code with fast PWM mode it works perfectly. But phase correct mode didn't provide the expected outcome. What is wrong with my code?


unsigned int b=7999;
void setup() {
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);

TCCR4A=0;
TCCR4B=0;
//Timer 1 1kHz mode 8: phase and frequency correct pwm oc1a non inverting

TCCR4A=(1&lt;&lt;COM4A1)|(0&lt;&lt;COM4A0)|(0&lt;&lt;WGM41) ;//| (0&lt;&lt;WGM40)
TCCR4B=(1&lt;&lt;WGM43)|(0&lt;&lt;WGM42);//
ICR4=b;
OCR4A=int(b*1/5);
TCCR4B|=(1&lt;&lt;CS10);
TIMSK4 |=(1&lt;&lt;OCIE4A);
}

void loop() {
  } 

ISR(TIMER_COMPA_vect){

  }   


The output of the code for different duty cycles are as below
Duty cycle:0.2 (OCR4A=b/5)


Duty cycle:0.2 (OCR4A=b/2)
You wrote:


  TIMSK4 |= (1&lt;&lt;OCIE4A);


and then never defined ISR(TIMER4_COMPA_vect). As soon as you get a
compare match, you get a bad interrupt, which defaults to resetting your
program and restarting again from reset vector.