Go Down

Topic: PWM Freq. I can't change it - please help :-( (Read 4808 times) previous topic - next topic


"sbi()" sets a bit in a register.

Which timer(s) do you want to modify?  You realize that different timers affect different pairs of PWM pins, right?  Altering the behavior of Timer0 has side effects, eg, millis(), that you may not want.  The other timers are fair game as long as they aren't used by a library you've included in your sketch.


i have the same problem. any help would be appreciated


Feb 20, 2011, 02:15 am Last Edit: Feb 20, 2011, 02:17 am by thefatmoop Reason: 1
here's some actually useful information. I didnt read all the posts, but yes you can change the pwm frequencies. There are 8bit and 16bit pwm options. I really haven't looked into it too much since i just had to change it for one job.

is a great article. Really you want phase correct pwm if possible, unless you need pwm above about 30khz.

for the 8 bit timers, start reading page 94 of the 328P datasheet
you can set N on the equations by looking at page 110. Really the whole 8bit pwm section is only 9 pages.

If you need more resolution look at the 16bit.

The closest you can get to 500hz without using the cpu's time is 490hz.

Code: [Select]

 pinMode(3, OUTPUT);
 pinMode(11, OUTPUT);
 TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM20);
 TCCR2B = _BV(CS22);
 OCR2A = 180;
 OCR2B = 50;

and just set OCR registers to whatever you want your duty to be

another useful setup for pwm that's about 32khz:
Code: [Select]

 //ultrasound pwm setup
 pinMode(3, OUTPUT); //3 and 11 are fans
 pinMode(11, OUTPUT);
  TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM20);

 TCCR2B = _BV(CS20);
 OCR2A = 255;
 OCR2B = 255;

really read those about 15 pages and look through the equations and divisors to find the frequency you want. If that doesn't work then use the 16bit pwm options

To the guy who suggested using a software cpu controlled pwm:... seriously?

Go Up