Hello all, I've spent the last several hours going through the ATMega32u4 datasheet trying to set the appropriate registers to give myself a PWM output at 25khz. This is part of my first microcontroller project so its all a bit foreign to me.
I'm using a Pro Micro which is Leonardo based running at 16MHz.
Here's my code:
void setup() {
pinMode(10, OUTPUT);
//Set WGM1 bits 3:0 to 0b1110 corresponding to "Fast PWM" with Top set to the value stored in OCR1A
TCCR1A |= 0b00000011;
TCCR1B |= 0b00011000;
//Set timer 1 prescaler to 1
TCCR1B |= 0b00000001;
//Set COM1B0 and COM1B1 to 0b10 to select the compare mode: "Clear OCnA/OCnB/OCnC on compare match, set OCnA / OCnB / OCnC at TOP"
//1B corresponds to OC1B, which is pin 10 on the pro micro
TCCR1A |= 0b00100000;
//Set Top to 640 which is 16Mhz/25khz
OCR1A = 640;
//Compare match value every 320 clocks = 50% duty cycle
OCR1B = 320;
...
}
Changing OCR1B changes the duty cycle as expected, but my PWM frequency is only 390Hz. It's like my Timer1 is being prescaled but I set the register to tell it to not use prescalaing so I don't understand what's happening.
In addition to that, if I set the prescaling to a higher value it actually has no effect on the output frequency...
Thanks for taking the time to help.