Skip to main content

Subsection 13.2.3 Part 3: Designing the Timer for the Servo’s Pulse

We worked out what the prescaler and the auto-reload value would have to be for a 50 Hz PWM (Task 11.3.4.b): the 12 MHz clock has to be divided down by a factor of 240,000 to get 50 Hz (12,000,000 clock cycles per second divided by 50 periods per second is 240,000 cycles per period), and the prescaler and the counter split that factor between them. Today we’ll repeat that computation for the servo, with a new constraint: the servo only uses pulse widths between 1 ms and 2 ms, and its dead band sets the finest change in width it can use. The numbers we settle here become the five #define lines of the program we’ll build afterwards.
Recall how TIM14 produces the pulse (Figure 11.3.1): the prescaler divides the 12 MHz clock down, the counter counts the divided pulses up to the auto-reload value ARR and starts again. ARR is a 16-bit register, so the largest count it can hold is 65,535.
We need a 50 Hz PWM frequency, so one period is 1/(50 Hz) = 20 ms, so the counter needs to take 20 ms to count from 0 to its auto-reload value. The counter is clocked from the 12 MHz system clock through the prescaler, so there are many ways to accomplish this.

Activity 13.2.4. Which of these are usable?

Fill in the table below. For each prescaler, what does the counter’s clock frequency become, and how many counts does it take to get to 20 ms? Then decide which rows are usable, and why the others are not.
Prescaler Timer clock Counts for 20 ms (must fit)
1  
 
 
 
2  
 
 
 
6  
 
 
 
60  
 
 
 
240  
 
 
 
A 50 Hz frequency gives a pulse period of \(T_p = 1/(50\text{ Hz}) = 20\text{ ms}\text{,}\) and the counter divides that period into ARR + 1 bins, where ARR is the auto-reload value the counter counts up to (Figure 13.2.10). So 20 ms/(ARR + 1) is the smallest pulse width the timer can produce, \(T_0\text{,}\) and it is also the smallest change in pulse width it can make. The servo’s protocol specifies pulse widths between 1 ms and 2 ms, so the number of steps between 1 ms and 2 ms is the number of distinct positions the timer can command: the resolution. By convention 1 ms and 2 ms correspond to −90° and +90°, so 180° divided by the number of steps is the angle per step. Putting a number in: on the table’s first row the period is divided into 240,000 bins, so \(T_0\) would be 20 ms/240,000, about 83 ns.
Figure 13.2.10. The pulse period \(T_p\) and the smallest pulse width \(T_0\text{.}\) The counter divides each period into ARR + 1 bins, so \(T_0 = T_p/(\text{ARR}+1)\) is both the narrowest pulse the timer can make and the step between one pulse width and the next.

Activity 13.2.5. Which one do you like best?

For the three usable rows, fill in the smallest pulse width, the number of steps between 1 ms and 2 ms, and the angle per step. Then choose a row you want to use in your design and explain why.
Prescaler ARR + 1 \(T_0 = 20\text{ ms}/(\text{ARR}+1)\) Steps, 1 ms to 2 ms Angle per step
6  
 
 
 
 
 
 
 
60  
 
 
 
 
 
 
 
240  
 
 
 
 
 
 
 
Timer 14 will count continuously from 0 to the value stored in TIM14_ARR. It will drive the connected pin HIGH while the counter is below the value stored in TIM14_CCR1, and drive the pin LOW while the counter is between TIM14_CCR1 and TIM14_ARR. TIM14_ARR and TIM14_CCR1 hold counts, not times. To convert a pulse width to a count, divide the width by the duration of one count, \(T_0\text{,}\) from your table. For the 240 row, for example, one count is 20 µs, so a 1 ms pulse is 1000 µs ÷ 20 µs = 50 counts. Now that you have chosen a row, you can work out the numbers the program needs.

Activity 13.2.6. Choosing TIM14_ARR and TIM14_CCR1.

With your chosen values, complete the table below to help you prepare for your code. The names in capitals are the #define lines of the program we’ll build next.
The STM32C031C6’s system clock runs at                                                                       
 
The prescaler you chose, PWM_PSC_FACTOR                                                                       
 
The counter’s clock frequency after the prescaler                                                                       
 
ARR + 1, the counts in one 20 ms period, PWM_TIMER_MAX                                                                       
 
CCR1 for a 1 ms pulse, SERVO_MIN                                                                       
 
CCR1 for a 1.5 ms pulse, SERVO_MID                                                                       
 
CCR1 for a 2 ms pulse, SERVO_MAX