I'm having problems with using Arduino Leonardo PWM outputs (I'm actually using YUN board).
I need 4 PWM outputs that generally have the same base frequency of around 10 to 20 Hz or so.
I'm using timer 1 (A-pin-9,B-pin-10,C-pin-11) and timer 4 (A-pin-13)
I get pins 9,10,and 13 PWM output working decent, but for some reason pin 11 PWM frequency is much much higher.
Pins 9,10,11 should be working off TIMER 1 which all use the same pre-scaler, so I dont know how 9 and 10 can have one frequency, and 11 have a different frequency.
Here is my code can anybody see anything wrong? thanks!
#define PWM_4A_PIN 13
#define PWM_1C_PIN 11
#define PWM_1B_PIN 10
#define PWM_1A_PIN 9
void setup() {
TCCR1B = TCCR1B & 0b11111000 | 0x05;
TCCR4B = TCCR1B & 0b11110000 | 0x09;
analogWrite(PWM_4A_PIN,128);
analogWrite(PWM_1C_PIN,128);
analogWrite(PWM_1B_PIN,128);
analogWrite(PWM_1A_PIN,128);
}
void loop()
{
}
1 Answer 1
Pins 9,10,11 should be working off TIMER 1 which all use the same pre-scaler, so I dont know how 9 and 10 can have one frequency, and 11 have a different frequency.
Unless D11 is configured as OC0A instead of OC1C, which means that it will work off timer 0.
TCCR0A &= ~(_BV(COM0A1) | _BV(COM0A0));
TCCR1A |= _BV(COM1C1) | _BV(COM1C0);
...
OCR1C = ...;
-
Thanks Inacio, I tried your code but the same result D11 has much higher freq than 9 and 10, its very strange!rough neck– rough neck2014年09月09日 14:59:15 +00:00Commented Sep 9, 2014 at 14:59
-
1Did you use
analogWrite()
on D11? That will reset it to OC0A.Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2014年09月09日 15:04:13 +00:00Commented Sep 9, 2014 at 15:04 -
if I replace analogWrite(11,128) with OCR1C=128, then no output at allrough neck– rough neck2014年09月09日 15:16:51 +00:00Commented Sep 9, 2014 at 15:16
-
1I need to keep changing the PWM duty cycle, so I cant use analogwrite for any pin, only OC1C, OC1A, OC1B fro then on?rough neck– rough neck2014年09月09日 15:35:51 +00:00Commented Sep 9, 2014 at 15:35
-
1
analogWrite()
does a lot of housekeeping behind the scenes, including preparing the pin for how it thinks it should work. One of these bits is setting D11 as OC0A. Another is setting the PWM frequency. Sometimes the Arduino libraries are handy, and sometimes they interfere.Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2014年09月09日 15:36:59 +00:00Commented Sep 9, 2014 at 15:36
Explore related questions
See similar questions with these tags.