Skip to main content

Subsection 11.6.2 The PWM Arithmetic

The counter is clocked at the timer clock divided by the prescaler, and it resets after a fixed number of counts. With the 12 MHz default clock, a prescaler factor \(P\) and a top value \(M\text{,}\) the switching frequency is \(f_{PWM} = 12\,\text{MHz} / (P \times M)\text{.}\)
TTmotor_ramp.c uses \(P = 6\) and \(M = 1250\text{,}\) so the counter is clocked at 2 MHz and the waveform is at \(2\,000\,000 / 1250 = 1600\) Hz, a period of 625 µs. The registers are set with one less than each: TIM14_PSC gets 5 and TIM14_ARR gets 1249, because both count from zero.
Resolution. A period divided into \(M\) steps gives \(M\) distinct duty cycles, so one count of TIM14_CCR1 is \(1/M\) of the period and \(V/M\) of average voltage. At \(M = 1250\) on the 5 V motor supply that is 500 ns of pulse width and 4 mV of average voltage. Resetting at 100 instead would make one step 50 mV, which is coarse enough to hear as steps in the motor.
The 50 Hz alternative. At 50 Hz the period is 20 ms rather than 625 µs, long enough for the motor to accelerate and decelerate within each period rather than averaging across it. That torque ripple is a cost at speed and can help starting torque at very low speed, which is what Lab 6’s optional section asks you to try.