The H-bridge sets the motor’s direction. In this part we turn to its speed, which we set with pulse-width modulation (PWM). We have done this once already: the SevenSeg_dim() function we wrote for the display pulsed the display’s LEDs, and a wider pulse gave a brighter digit. The same idea sets a motor’s speed. The reading showed the difficulty: a GPIO pin is either fully HIGH or fully LOW, with nothing in between, so it cannot hold an in-between voltage. Nor does the STM32C031C6 have a true digital-to-analog converter (DAC) to do it for us: the DAC from Day 7 was part of the ADC, making test voltages the program never sees, not a pin we can drive to an analog level. So rather than hold the motor at some steady in-between voltage, we switch the full supply on and off quickly and let the duty cycle, the fraction of each period spent HIGH, set the average. The motor’s inertia smooths the pulses, and it turns as though driven by that average voltage.
We have done this once already: dimming the seven-segment display pulsed its LEDs, and a wider pulse gave a higher duty cycle and a brighter light. The same idea sets a motor’s speed.
A GPIO pin is either fully HIGH or fully LOW, so it cannot hold an in-between voltage, and the STM32C031C6 has no true digital-to-analog converter (DAC) to create one for us.
Figure11.2.24.PWM at three duty cycles, drawn for a 5 V supply. The output is a fixed-frequency square wave; the wider the HIGH pulse, the higher the average voltage (dashed line) and the faster the motor. Here a 75% duty cycle averages 3.75 V, a 50% duty cycle averages 2.5 V, and a 25% duty cycle averages 1.25 V.
Same period in all three; only the width of the HIGH pulse changes. The average voltage is proportional to the duty cycle: V_PWM = V_SUPPLY × T_HIGH / T.
The waveform is produced with almost no work. We use a counter that counts up from zero and resets when it reaches a fixed top value, over and over. A second number, the compare value, is checked against the counter: while the counter is below the compare value the output is HIGH, and once it reaches the compare value the output goes LOW until the counter resets. A larger compare value keeps the output HIGH for more of each period, a larger duty cycle. The entire process runs in the timer hardware, so once it is set up the CPU does nothing to keep it going. The output is directed to one of the microcontroller pins from where it can direct the motor speed.
Figure11.2.25.How the timer makes the waveform. A counter counts up and resets at its top value, over and over (the blue ramp). A compare value (the dashed line) is checked against it: while the counter is below the compare value the output is HIGH, and above it the output is LOW. Raising the compare value keeps the ramp below the line for more of each period, which widens the HIGH pulse and so raises the duty cycle.
A counter counts up from zero and resets when it reaches a fixed top value, over and over. That top value is the timer’s ARR, the same auto-reload register we set for periodic interrupts.
A second number, the compare value, is checked against the counter. While the counter is below it the output is HIGH, and once the counter reaches it the output goes LOW until the counter resets. That number lives in the timer’s CCR1, the capture/compare register.
The entire process runs in the timer hardware, so once it is set up the CPU does nothing to keep it going. The output goes to a microcontroller pin, and from there it sets the motor’s speed.
We’ll use timer TIM14 for this, the same timer we set up for periodic interrupts, and route its output onto a pin. Not every pin can carry a given timer’s output; the STM32C031C6 datasheet’s pin table lists which can.
Almost every pin can carry a PWM output, driven by one counter or another; the datasheet’s Table 12 shows which counter to use for a particular pin, and which alternate function mode.
In the STM32C031C6 datasheet (not the TB6612 one), find Table 12, Pin assignment and description, on page 30. Look up PA7 and read off which timer channel it can output, and which of the pin’s alternate functions it is.
A pin only carries a timer’s output once it has been switched away from being an ordinary GPIO output and handed to the timer peripheral, which is the same alternate-function machinery we used to give pins to the UART and the ADC. How would you configure PA7 to use the alternate function you identified above? Write the necessary register operations, naming the registers and bit positions. You can ignore the timer’s own registers.
To configure the timer’s counting rate, recall that our system clock runs at 12 MHz. We want to create a PWM signal at a switching frequency of about 1.6 kHz. Furthermore, each period should be divided into 1250 steps of duty cycle. What must the prescaler divide the clock by, and what value does the counter have to count to? Remember from our earlier work with timers what actually gets written into the prescaler and auto-reload registers, as opposed to the number you have in mind.