Pages: 1 2 [3]   Go Down
Author Topic: Varying the pwm frequency for timer 0 or timer 2?  (Read 40160 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm slightly confused.  In the code:

Code:
int outputPsuB = 45;  // Timer5-B

void 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... smiley-sad
Logged

France
Offline Offline
Sr. Member
****
Karma: 0
Posts: 262
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

please read my answer on
Quote
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

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

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:
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?
« Last Edit: March 15, 2010, 01:58:20 am by modom » Logged

Pages: 1 2 [3]   Go Up
Jump to: