Simvik

Fix: 100 % brightness still flickers

A duty-cycle conversion uses the auto-reload value instead of the period length. An easy embedded C problem (Timers and PWM), run and graded in the browser.

easy · Timers and PWM · find-the-bug · pwm · off-by-one

Bug report. duty_to_ccr(percent) converts a duty cycle from 0 to 100 % into the compare value for the PWM channel under Provided. At 100 % the LED strip still flickers faintly, and every other setting is slightly dim compared with the spec. Fix it by changing one line.

Starter code

uint32_t duty_to_ccr(unsigned percent) {
  return PWM_ARR * percent / 100;
}

Example tests

Off, half and full

printf("%u %u %u\n", duty_to_ccr(0), duty_to_ccr(50), duty_to_ccr(100));

Expected output:

0 500 1000

Learn it first

  • Timers and PWM · Prescalers, auto-reload, input capture, output compare and dead time.

More Timers and PWM problems