Subsection11.5.4Part 4: A Loop That Does Three Things at Three Rates
By the end of Lab 6 your program has three jobs to do, and they run at three different rates: sample the potentiometer a hundred times a second, notice every pulse from the sensor as it arrives, and put a new number on the display once a second. Before any of that, though, the sensor’s signal wire has to reach a pin, and we have to find out which pin that is.
A quick review. The board’s user manual translates a header’s Arduino name into a port pin: UM2953, Table 11, ARDUINO connector pinout, on page 20.
We’ll plug the sensor’s output into the header pin labeled D7 on the Nucleo (an Arduino name). Find the row for D7 in that table: which STM32C031C6 pin is it?
Table 12 of the same manual lists the board’s I/O assignment the other way round, by port pin. Find your answer there as well, and check that the two tables agree.
Now that we know D7 is PA15, look at the three rates together. Figure 11.5.14 draws them over one second. Two of the three jobs are on a schedule the program itself sets: it decides when 10 ms have passed and when a second has passed. The pulses are on no schedule at all (one arrives when the shaft brings a slot around, which lines up with none of the three rates), so the loop has to be free to notice one at any moment. That is what rules out delay_ms(): it can serve exactly one of the three jobs, because while it is counting out its milliseconds the program is doing nothing else whatsoever, the same problem we watched a polled button counter have. In Lab 6 you are asked not to use delay_ms() for timing at all, and to use milliseconds() instead.
Figure11.5.14.The three rates the Lab 6 program has to keep, drawn over one second: the loop asks the time on every pass, the potentiometer is read every 10 ms, and the count becomes an rpm once a second. The pulses along the bottom arrive when the shaft brings a slot around, which lines up with none of the three. The tick marks are schematic.
By the end of Lab 6 your program has three jobs, and they run at three different rates: sample the potentiometer 100 times a second, notice every pulse from the sensor as it arrives, and put a new number on the display.
delay_ms() can only do one of them at a time. While it is counting out its milliseconds, the program is doing nothing else at all, which is the same problem we watched a polled button counter have.
The pulses along the bottom line up with none of the three rates.
milliseconds() is a function you have actually had all term, in sysinit.c, and we just have never opened it. SysTick is a counter built into the processor core itself rather than a peripheral like TIM14, and it is what delay_ms() uses to produce a (blocking) delay. SysTick itself is interrupt based, and its handler, which is called every time one millisecond has passed, simply increases the currentMilliseconds count by one. All milliseconds() does is return that count to you.
void SysTickInit(void) {
// Assumes 12MHz clock...could be better
SysTick->LOAD = 12000 - 1;
SysTick->CTRL |= SysTick_CTRL_CLKSOURCE_Msk; // Select the processor clock
SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk; // Enable the interrupt
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; // Turn it on.
}
SysTick is another timer, with its own registers (Day 8’s was TIM14), but it has the same shape as Day 8’s: a counter, an interrupt when it reaches the end, and a handler that runs each time. SysTick->LOAD gets 12000 − 1 because the clock is 12 MHz, and 12 MHz divided by 12000 is 1 kHz, which is one interrupt every millisecond. SystemInit() calls SysTickInit() before main() starts, so the count is already running before your first line of code does. And milliseconds() does not wait for anything: it returns a number immediately, so asking what time it is costs one memory read, and you can afford to ask on every pass.
milliseconds() is a function you have actually had all term, in sysinit.c, and we just have never opened it. SysTick is a counter built into the processor core itself rather than a peripheral like TIM14, and it is what delay_ms() uses to produce a (blocking) delay. SysTick itself is interrupt based, and its handler, which is called every time one millisecond has passed, simply increases the currentMilliseconds count by one. All milliseconds() does is return that count to you. SystemInit() calls SysTickInit() before main() starts, so the count is already running when your code begins.
static volatile uint32_t currentMilliseconds = 0;
void SysTick_Handler(void) { // runs once every millisecond
currentMilliseconds++;
}
inline uint32_t milliseconds(void) { // milliseconds since the board came up
return currentMilliseconds;
}
void SysTickInit(void) {
SysTick->LOAD = 12000 - 1; // 12 MHz / 12000 = 1 kHz
SysTick->CTRL |= SysTick_CTRL_CLKSOURCE_Msk; // Select the processor clock
SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk; // Enable the interrupt
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; // Turn it on.
}
Here is a first attempt at the loop. The potentiometer is sampled at 100 Hz, which is what Lab 6 asks for, and the sensor’s pin is read in the same beat.
So the loop asks the time on every pass; when 10 ms have gone by it samples the potentiometer and commands the motor, and when a second has gone by it converts the pulse count and sends it to the display. What is left to place is the read of the sensor’s pin, and the obvious place (inside the 10 ms step, where everything else already happens) does not work. Reading a pin tells you only what that pin is doing at the instant you read it, so a pulse that arrives and leaves between two reads is never seen at all. As Figure 11.5.14 shows, the pulses line up with none of the loop’s three rates, so the count comes out low by a different amount each time, not by a fixed fraction you could divide back out. Polling fast enough is a condition on the pulse’s HIGH and LOW times rather than on its rate; see Subsection 11.6.7.
Both answers can work. Reading the pin on every pass. Move the read out of the 10 ms beat so that the loop looks at the pin as fast as it can go, and use milliseconds() only to decide when to sample the potentiometer and when to close the one-second counting window. At the rates this motor produces, this works. What it costs is a constraint on everything you add later: nothing in that loop may block, ever.
We’ll use the interrupt. Whether polling is fast enough depends on the pulse’s HIGH and LOW times rather than on the pulse rate you measured, and those times depend on how wide the slots are compared with the spokes between them, which nobody has measured, and which changes if you swap a wheel. The interrupt does not depend on any of that: the pulse is counted whatever else the loop is doing, at any speed this motor reaches.
Read the pin every pass. Move the read out of the 10 ms beat so the loop looks at it as fast as it can go, and use milliseconds() only to decide when to sample the potentiometer and when to close the one-second counting window. This works, and it costs a promise: nothing anywhere in the loop may ever block, for as long as the program lives.
Take a falling-edge interrupt on PA15. This is what we set up for the button on PB4, with 15 in place of 4, and PA15 is on the same handler you already wrote, EXTI4_15_IRQHandler. The port selection is in EXTI_EXTICR4, which the Reference Manual gives in section 12.5.6; section 12.5.9 there is only the register map.
The second one is the one this course goes with, and the reason is not the arithmetic. The arithmetic depends on how fast this particular motor turns and how wide these particular slots are. The interrupt does not: the pulse is counted whatever else the loop is doing, at any speed.