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
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!
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.
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 Varying the pwm frequency for timer 0 or timer 2? - #5 by macegr - Interfacing - Arduino Forum 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!
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.
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)
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:
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??