Pages: [1]   Go Down
Author Topic: Fast PWM causing some problems  (Read 501 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys,

I noticed when I use Arduino's PWM to control DC motors, I can hear some annoying "singing", and I've been told this is because Arduino's PWM frequency is somewhere between 0-22,000Hz where the human ear can hear it.

I want to change this PWM frequency to something higher, so I looked up the datasheet for the Atmega328P and changed the registers as follows (Fast PWM, No prescaler, PWM frequency around 62kHz):

Code:
      TCCR1A=TCCR1A&0xA1;
      TCCR1B=TCCR1B&0x09;
      TCCR0A=TCCR0A&0xA3;
      TCCR0B=TCCR0B&0x01;
      TCCR2A=TCCR2A&0xA3;
      TCCR2B=TCCR2B&0x01;


I can't hear the motors anymore which is good, but the rest of my code has gone completely bonkers and doesn't do what it's supposed to do anymore.  Could anyone shed some light onto what could possibly be the matter?

Thankyou smiley
Andrew
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45978
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I noticed when I use Arduino's PWM to control DC motors, I can hear some annoying "singing"
Coming from where? The Arduino? The motors?

Quote
I can't hear the motors anymore which is good, but the rest of my code has gone completely bonkers and doesn't do what it's supposed to do anymore.  Could anyone shed some light onto what could possibly be the matter?
Without seeing the rest of the code? Without knowing what "completely bonkers" means to you? Without knowing what it is supposed to do, and what it does instead? No, I don't think so.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I can't hear the motors anymore which is good, but the rest of my code has gone completely bonkers and doesn't do what it's supposed to do anymore. 

Completely bonkers, huh? My code does that sometimes too. Can you be a little bit more specific?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24286
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
completely bonkers
Jerry Lewis comedic bonkers, or Harold Camping barking-mad bonkers?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Left Coast, USA
Offline Offline
Sr. Member
****
Karma: 5
Posts: 499
Sometimes I just can't help myself.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
       TCCR0B=TCCR0B&0x01;
...bonkers...

Well, bonkers is as bonkers does.

However...

If anything in your code depends on the standard Arduino setup of Timer 0, it won't work if you change the settings of Timer 0.  This includes the functions micros(), millis(), delay(), maybe others.


Regards,

Dave

Footnote:
Normal setup is TCCR0A = 0x03 and TCCR0B = 0x03.  You are changing the Timer 0 prescaler division value from 64 to 1.  
« Last Edit: May 23, 2011, 09:02:24 am by davekw7x » Logged

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

Quote
completely bonkers
Jerry Lewis comedic bonkers, or Harold Camping barking-mad bonkers?

Actually more along the lines of Dizzee Rascal - Bonkers.  Sorry to not be more specific.

I copied the these following two lines of code from somewhere else, and they worked:

Code:
//    TCCR1B=TCCR1B&0xf8|0x01;    // Pin9,Pin10 PWM 31250Hz
//    TCCR2B=TCCR2B&0xf8|0x01;    // Pin3,Pin11 PWM 31250Hz

Which modifies just Timer 1 and Timer 2 prescalers (I think?).  Pin3 and Pin11 are not connected to my motor drivers, so I just used some jumper wires and now it works.

I guess Timer 0 is off limits to change the prescaler.
« Last Edit: May 24, 2011, 12:02:30 am by andyjjones » Logged

Pages: [1]   Go Up
Jump to: