Skip to main content

Subsection 11.3.1 Part 1: The Timer That Makes the Waveform

The hardware we built on Tuesday controls both a motor’s direction of rotation and its speed of rotation. The H-bridge inside the TB6612 chooses which way current crosses the motor, which is what sets the direction; pulse-width modulation of the switches in that bridge sets what fraction of each period the supply is connected at all, which sets the average voltage across the terminals and so the speed. The wiring that does both is the circuit you built for the second exercise (Figure 11.2.28): motor power from the regulator, logic power from the Nucleo’s 3.3 V, and three signal wires from PA5 (D13), PA6 (D12) and PA7 (D11).
The PWM signal itself comes out of a peripheral we have already studied. TIM14 is the same timer we set up to interrupt once a millisecond, and there we looked only at its timing core: the prescaler that divides the system clock down, the counter CNT that counts the divided pulses, and the auto-reload register that sets how far the counter runs before it starts again (Figure 8.3.3). PWM is built into that same timer, in the machinery around that core that we left aside then.
Figure 11.3.1 is the whole of TIM14 as the reference manual, RM0490, prints it in Figure 165. The core we already know runs across the middle. What is new is the block located just below the counter (the capture/compare register, CCR1) and the path leading from it out to the pin marked TIMx_CH1, which for us is PA7. CCR1 holds the compare value we introduced on Tuesday: the counter is compared against it on the way up, so CCR1 sets how long the output stays HIGH in each period, while the auto-reload register sets how long the whole period is. Today we’ll learn about the registers that are needed to configure the timer so that the result of the comparison reaches the pin.
Figure 11.3.1. TIM14 in full, as RM0490 Figure 165 prints it. When we used it for periodic interrupts we looked only at this timer’s core (the prescaler, the counter, and the auto-reload value it counts up to, Figure 8.3.3) and said the capture/compare machinery around it would come back in the motor chapters. This is it: now we introduce a new timer mode that allows us to use the timer to produce a PWM signal on a pin. The clock still enters at the left and the counter CNT still runs up to the auto-reload register, but now the capture/compare register below it is compared against the counter on the way up, and the result leaves through the output control block at the right, on the pin marked TIMx_CH1, which for us is PA7.