PWM with the Arduino Mega

Hi Guys,

I need to write some PWM signals with higher resolution than the built-in functions allow, so I tried to use the 16-bit timers of the ATMega to generate the signal, but I dont get an output.

I used Timer3 in Mode 8 (Phase and frequency correct PWM) and Comparator Mode 3. With a counting frequency of clk/8 I use 10000 as TOP to get a PWM frequency of 100Hz and I set OCR3A to 8500 to get a duty cycle of 1500µs.

Does anyone see, why this doesn’t work?

void setup() {
  pinMode(2,OUTPUT);

  ICR3 = 10000;
  TCCR3A = (1<<COM3A1)|(1<<COM3A0);
  TCCR3B = (1<<WGM33)|(1<<CS31);
  OCR3A = 8500;
  TCNT3 = 0;
  sei();
}

void loop() {
  
}

Thanks for replies

http://playground.arduino.cc/Code/PwmFrequency

 pinMode(2,OUTPUT);
TCCR3A = (1<<COM3A1)|(1<<COM3A0);

You have set up for output on channel 3A and and compare OCR3A. 3A is output on Digital pin 5

Pin2 is OC3B. You need to change either the setup to match pin 2 or set pin5

 pinMode(5,OUTPUT)

Yup, that was it. Classical mistake :slight_smile:

Thanks