Now weβll work out what value goes into each of those registers. RM0490 Β§17.4 gives every register as a row of bits, and the value our program needs is assembled from the position of a field and the pattern that goes in it. Three of the registers we need are the easy case, because the thing we set is a single bit; TIM14_CCMR1 is the harder case, because there is more than one bitfield we need to set in it, one of which consists of four bits not all of which are adjacent to each other.
Activity11.3.2.From a register name to a register value.
Here are four lines from TTmotor_ramp.c. ES28.h defines TIM_OC1_PWM1 as 6 and TIM_CC1_OUTPUT as 0. Every _Pos name is the bit position of the field it names.
TIM14->CCMR1 |= TIM_OC1_PWM1 << TIM_CCMR1_OC1M_Pos;: Which bits of CCMR1 does this set? Which PWM mode does this put into the OC1M field (see Section 17.3.8 of the Reference Manual)? The line before this statement is TIM14->CCMR1 &= ~TIM_CCMR1_OC1M_Msk;. What could go wrong if we left that line out?
TIM14->CCER |= TIM_CCER_CC1E;: find TIM14_CCER, the capture/compare enable register, in the Reference Manual, Β§17.4, and read which bit CC1E is. What is the numeric value of TIM_CCER_CC1E? Why is this line written with |= and no clearing line before it, when the OC1M line above needed one?
TIM14->CR1 |= TIM_CR1_CEN;: find TIM14_CR1, control register 1, in the same section, and read which bit CEN is. What value is that, and what happens on the pin the instant this line runs?
TIM14->CCMR1 |= TIM_CC1_OUTPUT << TIM_CCMR1_CC1S_Pos;: the same register as the first question, and a second field of it. Which bits is CC1S, and what does 00 select? The comment in TTmotor_ramp.c calls it the default value. So why write it at all?
TIM14_CCMR1, the capture/compare mode register, is different, because neither of the things we set in it is a single bit. OC1M, which chooses what the channel does, is four bits wide and split across the register, three of them together and a fourth up at bit 16. CC1S, which decides whether the channel is an output or an input, is two bits at the bottom. A field several bits wide cannot be reliably set by OR-ing a value into it, because OR can only turn bits on: if the field already holds a 1 where the new pattern wants a 0, that bit stays set, and the result is the two patterns mixed together. That is why each of these two gets a clearing line before its setting line, and why the three single-bit registers do not.
Figure11.3.18.TIM14_CCMR1 laid out bit by bit, with the first of our two fields marked. OC1M selects what the channel does, and it is four bits wide and not in one piece: three of them at 6:4, and the fourth up at bit 16. Four bits is what makes it different from CC1E, CEN and UG, which are one bit each.
Figure11.3.19.The same register, with our other field marked. CC1S is two bits at 1:0, and it decides whether the channel is an output or an input. Two fields in one register is why the initialization writes CCMR1 twice.
Two of your four answers, as RM0490 Β§17.4 prints them: the register CC1E lives in, and the register CEN lives in. The other two are both CCMR1, on the next two slides.
CC1E is bit 0 of CCER, CEN is bit 0 of CR1. One bit each, which is why |= on its own is safe here, and why the two CCMR1 fields are not.
Figure11.3.21.Everything our program does to TIM14_CCMR1, in one place: the bit map at the top, then RM0490 Β§17.4βs description of each field with the value we write boxed, and beside it the line that writes it. In blue, OC1M gets 0110, PWM mode 1, which the manual defines as channel 1 being active as long as the counter is below the compare value. In magenta, CC1S gets 00, which is what keeps the channel an output. Two fields, one register, which is why the initialization writes CCMR1 twice.
Reference Manual Β§17.3.8 on 0110: βPWM mode 1: Channel 1 is active as long as TIMx_CNT < TIMx_CCR1 else inactive.β HIGH until the counter reaches the compare value, then LOW.
Reference Manual Β§17.4 on 00: βCC1 channel is configured as output.β That is what keeps the channel an output, and it is what the second CCMR1 pair writes.
The answer to the checkpoint question that comes later: CCMR1 is the register written twice, because two of its fields are ours.
That is every register the initialization touches, so we can now read it straight through. The function runs in three stretches: it sets up the GPIO pin in the mode the timer needs, it sets the timerβs own numbers, and it puts the channel into PWM mode and starts it. Nothing in it is new at this point, which is the whole idea. By the time you reach the last line every register in it should be one you have already looked up.
The pin first. These are the same three operations we used before: give the port a clock, put the pin in alternate-function mode, and select which alternate function.
void tim14_pa7_pwm_init(void) {
// Enable clock access to GPIOA
RCC->IOPENR |= RCC_IOPENR_GPIOAEN;
// Set PA7 in alternate function mode (10)
GPIOA->MODER = (GPIOA->MODER & ~GPIO_MODER_MODE7_Msk)
| (GPIO_ALTERNATE << GPIO_MODER_MODE7_Pos);
// Set PA7 alternate function type (AF4, 0100)
GPIOA->AFR[0] = (GPIOA->AFR[0] & ~GPIO_AFRL_AFSEL7_Msk)
| (GPIO_AF4 << GPIO_AFRL_AFSEL7_Pos);
Then come the timerβs own numbers. PSC_FACTOR and PWM_TIMER_MAX are the two constants at the top of the file, 6 and 1250, and both are written one less than the value we have in mind because the counter starts at zero. The last of these four lines is the only one in the function that is there for safety rather than for configuration: TIM14->CCR1 = 0; sets the starting speed to zero, so the motor cannot jump the moment the counter starts.
// Enable clock access to Timer 14
RCC->APBENR2 |= RCC_APBENR2_TIM14EN;
// Set prescaler
TIM14->PSC = PSC_FACTOR-1;
// Set autoreload value
TIM14->ARR = PWM_TIMER_MAX-1;
// Set the compare register value - start with a speed of 0
TIM14->CCR1 = 0;
This is the part that makes it a PWM channel. The two CCMR1 pairs are the read-modify-write we decoded above; CC1E lets the channel reach the pin; UG forces the update event that loads the buffered prescaler; and CEN starts the counter.
// Set pwm mode 1 (0110): pin is high up to capture value, then goes low
TIM14->CCMR1 &= ~TIM_CCMR1_OC1M_Msk;
TIM14->CCMR1 |= TIM_OC1_PWM1 << TIM_CCMR1_OC1M_Pos;
// Make sure timer is in output mode; this is default value
TIM14->CCMR1 &= ~TIM_CCMR1_CC1S_Msk;
TIM14->CCMR1 |= TIM_CC1_OUTPUT << TIM_CCMR1_CC1S_Pos;
// Enable TIM14_CH1 in output mode
TIM14->CCER |= TIM_CCER_CC1E;
// Generate an update event - this clears counter, prescaler counter, updates registers
TIM14->EGR |= TIM_EGR_UG;
// Enable timer
TIM14->CR1 |= TIM_CR1_CEN;
}
Here is the function that controls the motor speed while it is running: the clamp keeps the value inside the range the counter can represent, and then one assignment sets the speed. The value < 0 test is technically not necessary here since a uint16_t value will never be less than zero, but it will help ensure you never set the compare register to a value outside the correct range by accident: when you write motor_speed() for Lab 6, taking a signed parameter is the natural choice, and then that line is all that stands between a negative speed and a compare value of sixty-five thousand.
void tim14_pwm_set(uint16_t value) {
// Safety first: Allow no pulse values outside the (0, PWM_TIMER_MAX-1 range!
if (value>PWM_TIMER_MAX-1) {
value = PWM_TIMER_MAX-1;
} else if (value < 0) {
value = 0;
}
TIM14->CCR1 = value;
}
We now have every register we need, so here is the whole initialization, in the order it runs. Weβll take it in three pieces, and the first is getting the pin ready for PWM mode, which is where Day 11 left off.
void tim14_pa7_pwm_init(void) {
// Enable clock access to GPIOA
RCC->IOPENR |= RCC_IOPENR_GPIOAEN;
// Set PA7 in alternate function mode (10)
GPIOA->MODER = (GPIOA->MODER & ~GPIO_MODER_MODE7_Msk)
| (GPIO_ALTERNATE << GPIO_MODER_MODE7_Pos);
// Set PA7 alternate function type (AF4, 0100)
GPIOA->AFR[0] = (GPIOA->AFR[0] & ~GPIO_AFRL_AFSEL7_Msk)
| (GPIO_AF4 << GPIO_AFRL_AFSEL7_Pos);
Clock, mode, function: the same three steps as every pin we have configured.
Second piece: getting the timer to count at the correct rate, and initializing the compare register value. Two of the numbers come from constants set at the top of the file.
// Enable clock access to Timer 14
RCC->APBENR2 |= RCC_APBENR2_TIM14EN;
// Set prescaler
TIM14->PSC = PSC_FACTOR-1;
// Set autoreload value
TIM14->ARR = PWM_TIMER_MAX-1;
// Set the compare register value - start with a speed of 0
TIM14->CCR1 = 0;
Rate, range, and a starting speed of zero, so the motor cannot jump when the counter starts.
The last piece is what makes it a PWM channel rather than a plain counter.
A reminder of what the compare register does: the counter counts up from zero to the value in TIM14_ARR and starts over, and TIM14_CCR1 holds the compare value. The output is HIGH while the counter is below the compare value and LOW once the counter reaches it, so raising the compare value lengthens the HIGH part of each period and raises the duty cycle.
void tim14_pwm_set(uint16_t value) {
// Safety first: Allow no pulse values outside the (0, PWM_TIMER_MAX-1 range!
if (value>PWM_TIMER_MAX-1) {
value = PWM_TIMER_MAX-1;
} else if (value < 0) {
value = 0;
}
TIM14->CCR1 = value;
}
The counter counts at 2 MHz, so adding 1 to the compare value lengthens each HIGH pulse by 500 ns.
Once that function has run, the motorβs speed is changed by writing one register and nothing else. tim14_pwm_set() limits the value it is given to the range the counter can represent and then assigns it to CCR1; the prescaler, the auto-reload value, the mode bits and the output enable are all left exactly as the initialization set them, and the timer keeps producing the waveform without any further involvement from the CPU.
In Lab 6 you are asked to turn this into a device driver of your own, with an initialization function, a mode-select function and a speed-select function in a file called tb6612.c. Two of the three are almost in front of you already: tim14_pa7_pwm_init() becomes the initialization once you fold in the two lines that make PA5 and PA6 outputs, and tim14_pwm_set() becomes the speed-select nearly unchanged.
What is notmotor_mode(): the direction logic in main(). That code decides when to brake as the ramp turns around, which is your programβs job, not the driverβs.
Write the body of motor_mode(). The signature is given in Lab 6 (void motor_mode(uint8_t mode);, with the four modes FWD, REV, BRAKE and STOP), and the TB6612 truth table from Day 11, on page 4 of the driverβs datasheet, gives you IN1 and IN2 for each. Note that TTmotor_ramp.c only ever produces three of the four: it never puts the driver in stop. What do IN1 and IN2 have to be for that fourth case, and how is what the motor does then different from brake?
You predicted earlier what would change if this motor ran at 50 Hz instead of 1.6 kHz. Now work out the numbers: from a 12 MHz clock, what does the prescaler have to divide by, and what does the counter have to run up to, to give a 50 Hz PWM with 2000 steps of duty cycle? The two commented-out #define lines just below the active ones in TTmotor_ramp.c are that setting. Check them against your answer, then try them and listen to the difference.