Go Down

### Topic: Varying the pwm frequency for timer 0 or timer 2? (Read 88888 times)previous topic - next topic

#### jdumbaugh

#30
##### Nov 08, 2009, 12:30 am
I'm slightly confused.  In the code:

Code: [Select]
`int outputPsuB = 45;  // Timer5-Bvoid setup(){// PSU outputs via timer5  pinMode(outputPsuB, OUTPUT);  // select Pin as ch-B    TCCR5A = B00100010; // Phase correct PWM change at OCR5A  TCCR5B = B10010;  // prescaling by 8 the system clock  OCR5A = 14970; // 66,8002672Hz  OCR5B = 1497; // 10% PWM  }   `

We set pin 45 as output.  How would this actually run?  If I do analogWrite(outputPsuB, 25) it gives me a distorted sine wave.  I don't understand this and nobody can seem to explain this stuff to me...

#### selfonlypath

#31
##### Nov 09, 2009, 05:26 am
Quote
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1252073278/15#15

#### jfunk

#32
##### Jan 23, 2010, 07:08 pm
I would also like to know how to change the frequencies on the Arduino Mega....

I have changed them on my regular Arduino with good success, but the Mega has more timers and I am having trouble linking how MaceGR came up with the commands to change the frequencies from the Atmega168 datasheet.

Has anyone done this yet?

#### modom

#33
##### Mar 15, 2010, 06:37 amLast Edit: Mar 15, 2010, 07:58 am by modom Reason: 1
So I am trying to create PWM using Timer4 on the Arduino Mega. I know that Timer4 uses pins 6, 7, and 8 on the Mega, and I'm trying to use output pin 7.
I want to use Fast PWM mode with a frequency of 62.5 kHz. (Ideally I would like 80kHz, but it seemed easier to get 62.5kHz by setting the clock prescale value to 1.)
Here is my code:
Code: [Select]
`void setup(){  pinMode(7, OUTPUT); //Within the TCCR4A register: //COM4A1:0 = 10 for non-inverted output //WGM41:0 = 11 for the LSBs of WGM setting to fast PWM mode  TCCR4A = _BV(COM4A1) | _BV(WGM41) | _BV(WGM40); //Within the TCCR4B register: //CS42:0 = 001 for clock prescale of 1, which should make a frequency of 62.5 kHz //WGM43:2 = 01 for the MSBs of WGM setting to fast PWM mode  TCCR4B = _BV(CS40) | _BV(WGM42);  OCR4A = 14970;          // change the frequency by some (arbitrary) amount  OCR4B = 819;              // set 80% duty cycle on channel 4B (this is supposed to show up on output pin 7!)}void loop(){}`

I don't completely understand how to use the OCR registers, but from what I've seen it seems like you use OCR4A to set the timer compare value to determine the frequency you want. Then, you use the OCR4B and OCR4C registers to set the values for the duty cycles of your respective output channels.

BUT, this code just gives me a noisy sine wave on output pin 7.  What am I doing wrong?

Go Up

Please enter a valid email to subscribe