Go Down

Topic: Adjusting PWM frequency on Leonardo (Read 3046 times) previous topic - next topic

octopus

I have some code running on an UNO that I need to port to a Leonardo.

One of the lines adjusts the PWM frequency of pin 3 to 31250Khz
Code: [Select]
TCCR2B = TCCR2B & 0b11111000 | 0x01; //set PWM frequency for pins 3 & 11 to 31250Khz

I'm having a hard time figuring out how to translate this to the Leonardo since I believe the timers and prescaler values are different but I can't really make sense of the 32U4 data sheet (I am new to microcontrolers).

I'd need to keep using pin 3 if possible.

Can anyone help me out or point me in the right direction?  Thanks!

pylon

Pin 3 of the Leonardo is bundled to timer 0 and not timer 2 as on the UNO. Your code does just set the prescaler, so for that alone it does not output anything on pin 3. Take care because timer 0 is responsible for the Arduino's time keeping (millis() and delay()), so if you change the prescaler you cannot rely on these functions. The prescaler for timer 0 is usually at 64.

octopus

Thanks for the info

I need to use delay() so I'll have to resolder the shield to use a different pin.

Is there some info somewhere about which pins are on which timers on the Leonardo?


DuaneB

Yes, its on this site, but google will find it as fast as i can, search for leonardo pin mapping.

Duane B
Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

octopus

I've seen the pin mapping table here: http://arduino.cc/en/Hacking/PinMapping32u4 but I'm still slightly unclear.

For example digital pin 10 shows (PCINT6/OC1B/OC4B/ADC13) PB6 I assume that "OC1B" tells me it is timer 1, or is it timer 4 (OC4B)??

My second question is how to work out what prescale factor to use and what settings are available. I know that on the Uno different timers have a different settings available and the same setting can result in a different frequency depending on the timer. I used the data referenced in this post http://arduino.cc/forum/index.php/topic,16612.msg121031.html#msg121031 for the Uno so was trying to figure out the same for the Leonardo.

Sorry if these are dumb questions - this pin mapping stuff just seems a bit cryptic to a novice like me!

pylon

Quote
For example digital pin 10 shows (PCINT6/OC1B/OC4B/ADC13) PB6 I assume that "OC1B" tells me it is timer 1, or is it timer 4 (OC4B)??


Both. You can configure this pin to be altered by either timer 1 or timer 4.

Quote
My second question is how to work out what prescale factor to use and what settings are available.


The prescaler defines the PWM frequency while the OCRX(AB) register defines the pulse width. The value of the prescaler says how much clock cycles it needs for the timer to be increased by one. An 8bit timer counts from 0 to 255 and back again (in PWM mode), every time it matches the value in the OCRX(AB) register, it toggles the corresponding output pin (if it's configured so). This results in the simple formula for the PWM frequency:

clock speed / prescaler value / 2

in the example:

16'000'000 / 256 / 2 = 31250Hz (not kHz as in your comment)

octopus

Thanks for the info pylon, I understand this a lot better now :)

I have switched to using pin 9 so as not to affect the Arduino timekeeping. I can confirm the pins 9 and 10 on the Leonardo behave same as the Uno with regarding timer / pwm frequency settings, that is:

Pins 9 and 10: controlled by timer 1

Setting    Divisor    Frequency
0x01        1        31250
0x02        8        3906.25
0x03     64        488.28125
0x04     256        122.0703125
0x05        1024        30.517578125

TCCR1B = TCCR1B & 0b11111000 | <setting>;

Interestingly I have noticed that the speed of a fan will change when the PWM frequency is altered even although the duty cycle remains unchanged. Could to do with the characteristics of the fan or perhaps something to do with the MOSFET that I am driving it from??

Go Up