Skip to content

Commit 9bc4f62

Browse files
committed
fix off-by-one in PWM frequency setting
1 parent 95dbb18 commit 9bc4f62

File tree

1 file changed

+2
-2
lines changed
  • ports/raspberrypi/common-hal/pwmio

1 file changed

+2
-2
lines changed

ports/raspberrypi/common-hal/pwmio/PWMOut.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ void common_hal_pwmio_pwmout_set_frequency(pwmio_pwmout_obj_t *self, uint32_t fr
218218
pwm_set_clkdiv_int_frac(self->slice, div16 / 16, div16 % 16);
219219
pwm_set_wrap(self->slice, self->top);
220220
} else {
221-
uint32_t top = common_hal_mcu_processor_get_frequency() / frequency;
222-
self->actual_frequency = common_hal_mcu_processor_get_frequency() / top;
221+
uint32_t top = common_hal_mcu_processor_get_frequency() / frequency - 1;
222+
self->actual_frequency = common_hal_mcu_processor_get_frequency() / (top + 1);
223223
self->top = MIN(MAX_TOP, top);
224224
pwm_set_clkdiv_int_frac(self->slice, 1, 0);
225225
// Set TOP register. For 100% duty cycle, CC must be set to TOP+1.

0 commit comments

Comments
 (0)