Skip to main content

Subsection 11.2.6 Part 6: Second Exercise, Ramping the Motor with PWM

For the last exercise we hand the driver’s inputs over to the Nucleo and run a program that ramps the motor’s speed up and down and reverses it. The wiring continues from the first exercise: leave the power, the motor, and the breakout as they are, and move three wires. The PWM pin, which you tied HIGH by hand, now goes to PA7 (D11); the AIN1 and AIN2 wires, which you moved by hand, now go to PA5 (D13) and PA6 (D12), so the program controls all three.
Figure 11.2.28. Wiring for the second exercise, changed from the first only at the driver’s three logic inputs: PWM to PA7 (D11), AIN1 to PA5 (D13), AIN2 to PA6 (D12). The motor and its 5 V supply are unchanged.
Make a copy of TemplateProject, rename it DCmotor, and add the demonstration program TTmotor_ramp.c to its Src folder. Compile and run it. The program sets up TIM14 for PWM on PA7, drives AIN1 and AIN2 for direction, and repeatedly ramps the duty cycle up and back down, reversing the motor at the ends of the ramp. For homework and tomorrow, we will look closely at the code and TIM14 registers: How does TIM14 generate the PWM signal that drives the motor?

Activity 11.2.8. Measure the PWM, and account for what the motor does.

With the program running and the motor turning, put the oscilloscope on the PWM signal: channel 1 (the orange lead) on PA7, and its ground lead to a ground on the board. If the motor does not move when the program runs, confirm the build succeeded and that the three wires reached PA5, PA6, and PA7.

(a)

Measure the period of the waveform, and from it the PWM frequency. The timer’s counter is fed a 2 MHz clock (the 12 MHz system clock divided by 6) and counts to 1250 before resetting. Work out the frequency those two numbers predict, and compare it with what you measured.

(b)

Freeze the trace at a moment when the motor is running at a middling speed and measure the width of the HIGH pulse as well as the period. Give the duty cycle as a percentage, and then convert it into the compare value out of 1250 that the program must have written to produce it.

(c)

Watch what happens at the end of a ramp, where the motor reverses. The program does not simply swap the direction inputs: it brakes the motor first, then applies the opposite direction. Give two independent reasons why this is worth doing: one about the switches inside the driver, and one about the current through the motor. What you would expect to go wrong if the program reversed the direction inputs at full speed instead?
Figure 11.2.29. The PWM signal from TTmotor_ramp.c on the oscilloscope, while the ramp runs. The window is narrow enough to hold only a few periods of the 1.6 kHz waveform, so what you are watching is one pulse train changing shape: the width of the HIGH portion grows and shrinks as the motor speeds up and slows down, while the spacing from one rising edge to the next, the period, does not move at all. That is duty cycle changing while frequency stays fixed, which is the whole of how this program sets the speed.

Activity 11.2.9. Going further.

These two questions go further than the measurements above, and are optional.

(a)

The counter resets at 1250, so the duty cycle can only take 1250 distinct values. Work out the smallest change in average motor voltage the program can ask for. If we had instead let the counter reset at 100, what would the PWM frequency have become, and what would the smallest voltage step have been? What are the trade-offs between the two choices?

(b)

In Lab 6 there is an optional experiment: running the same motor with a 50 Hz PWM instead of 1.6 kHz. Work out the period of each, and then think about what the motor does between one HIGH pulse and the next: recall from the truth table that during the LOW part of a cycle the driver is braking the motor rather than letting it coast. Predict what changes about the motor’s behavior at 50 Hz, both what gets worse and what might get better, before you ever try it.
For next class, work through TTmotor_ramp.c with the reference manual open (RM0490, “General-purpose timers (TIM14)”, section 17.3.8, PWM mode, and section 17.4, TIM14 registers) and see how the program sets the timer’s registers to produce the PWM you watched today. You have already looked up the pin and worked out the timer’s prescaler and auto-reload values, so you already know much of what the program has to do. Look at the remaining parts of the code: What do the initialization settings accomplish, and how does the main loop change the motor speed and direction?