32u4 timer 4 pwm frequency help
Yeah... sometimes it's easier to scrap what's already there and start fresh.

We need to:


Pick a suitable prescaler to get close to the desired cycle length
Fine-tune the value in TOP in order to get an exact value


Assuming this is a 16MHz Leonardo, and we have Fast 10-bit PWM (PWM4x = 1, WGM4x = 0b00) available:

16MHz / 50Hz / (1 &lt;&lt; 10) = 312.5

Therefore we need a prescaler of 512 (CS4x = 0b1010).

16MHz / 50Hz / 512 = 625

Therefore we need a TOP of 624 (OCR4C = 624).

TCCR4B &amp;= ~(_BV(CS43) | _BV(CS42) | _BV(CS41) | _BV(CS40));
TCCR4B |= _BV(CS43) | _BV(CS41);
TCCR4D &amp;= ~(_BV(WGM41) | _BV(WGM40));
TC4H = 624 &gt;&gt; 8;
OCR4C = 624 &amp; 0xff;


And then you set COM4xn and PWM4x to appropriate values for whichever pin(s) you want to output PWM on, and set OCR4x to a value between 0 and 624 (using TC4H appropriately; see the datasheet for details) to control the duty cycle.