Subsection11.3.2Part 2: Which Register Does Each Job
TTmotor_ramp.c, which we ran on Tuesday, drives exactly one pin: PA7, which you found is where TIM14’s channel 1 comes out. Getting that pin to carry a PWM waveform takes a number of steps of setup, many of which we already know how to do:
MODER in blinky, AFR in UART and I2C; both again for PA7 in the last class
Give TIM14 a clock
RCC_APBENR2
in timers (Day 8)
Set the counting rate
TIM14_PSC
in timers (Day 8)
Set the value the counter runs up to
TIM14_ARR
in timers (Day 8)
Start the counter
TIM14_CR1
in timers (Day 8)
TTmotor_ramp.c drives one pin: PA7, where TIM14’s channel 1 comes out. Getting that pin to carry a PWM waveform takes a number of steps of setup, many of which we already know how to do:
We’ll need a few more registers to set the timer up for PWM mode.
That leaves four things the table does not cover. Three of them are registers we have never written: one holds the value the counter is compared against, one puts the channel into PWM mode, and one lets the channel’s output reach the pin at all. The fourth is not a missing register so much as a line whose reason is not obvious: the program writes one more register just before it starts the counter, and the interesting part is why that write has to happen before rather than after starting the counter. The compare register is the subject of the next part; the other three are what we’ll look up now, in the reference manual (RM0490) §17.4.
Open the reference manual and navigate to Section 17.4, TIM14 registers, on page 482, and keep section 17.3.8, PWM mode, on page 479 within reach; you will want it for the first question. Both are in the reference manual’s Section 17, the one titled “General-purpose timers (TIM14)”.
The reference manual, in §17.3.8, says PWM mode is selected by writing 0110 or 0111 into a field called OC1M, and RM0490 §17.4 tells you which register that field lives in. Find the register. Then read what the two values do, and work out which of them holds the pin HIGH from the start of each period until the counter reaches the compare value, and then drops it. If we had picked the other one, what would the trace on the oscilloscope have looked like?
Find the bit named CC1E, and the register it lives in. How does the manual describe what it does? Our program sets it in one line. If you deleted that one line and changed nothing else (the pin still in alternate-function mode, the counter still running, the compare value still being written every two seconds), what would you see on PA7?
The program writes one more register that Table 11.3.3 does not list and neither of the questions above asks about. Find it in the reference manual, §17.4. It is written near the end of the setup, just before the counter is started. What is it, what does the bit that our program sets do, and why would you want to do that before starting the counter rather than after?
If your group has answered all three of the above questions, work this out: our program calls tim14_pwm_set(625). Given that the counter runs up to 1249, what fraction of each period is the pin HIGH, and what average voltage does the motor see from a 5 V supply?
The reference manual’s section 17.3.8 selects PWM mode by writing 0110 or 0111 into a field called OC1M. Which register is that field in? Which of the two choices holds the pin HIGH until the counter reaches the compare value, and then drops it? What would the other one have looked like on the scope?
Find the bit named CC1E, and the register it lives in. If you were to delete the one line of tim14_pa7_pwm_init() that sets this bit, and changed nothing else, what would you see on PA7?
tim14_pa7_pwm_init() writes one more register that neither of the first two questions asked about, just before it starts the counter. Find it in the file, then look it up in the reference manual. What does the bit that our program sets do, and why do we set it before starting the counter rather than after?