Pages: 1 [2]   Go Down
Author Topic: PWM Freq. I can't change it - please help :-(  (Read 2290 times)
0 Members and 1 Guest are viewing this topic.
Plano, TX
Offline Offline
Full Member
***
Karma: 0
Posts: 101
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i have the same problem. any help would be appreciated
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 82
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

http://arduino.cc/en/Tutorial/SecretsOfArduinoPWM
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:
 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:
 //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?
« Last Edit: February 19, 2011, 08:17:34 pm by thefatmoop » Logged

Pages: 1 [2]   Go Up
Jump to: