Skip to main content

Subsection 11.6.7 Polling or an Interrupt

When polling is fast enough. Noticing a pulse takes two reads, not one: a read that catches the line HIGH and a later read that catches it LOW, so the loop can see that it changed. So a polled read catches every pulse only if the gap between reads is shorter than the shorter of the pulse’s HIGH time and its LOW time. If either one is shorter than the gap, it can fall entirely between two reads and the change is never seen. Those two times are set by how wide the slots are compared with the spokes between them, and by the shaft speed. On a wheel whose slots and spokes are about equally wide, a pulse rate of \(f\) gives a HIGH time of about \(1/(2f)\text{,}\) so 60 pulses a second means about 8 ms HIGH and 8 ms LOW and a loop reading every 10 ms will miss changes. The short version, that a 100 Hz loop cannot see a 60 Hz signal, is not quite true and points at the wrong quantity.
The interrupt. Counting the pulses on a GPIO interrupt is Day 9’s five writes with the line number changed. On the STM32C031C6 the port selection for line 15 is in EXTI_EXTICR4 (EXTI is the extended interrupts and events controller), whose offset is \(0\text{x}060 + 4(x-1)\) for \(x = 4\text{,}\) so 0x06C; the field is EXTI15[7:0], in bits 31:24, and 0x00 there selects PA15. The table below calls that same register EXTI->EXTICR[3], because the CMSIS array is indexed from zero. RM0490 §12.5.6 is the register; §12.5.9 is only the map. The counter the handler increments must be declared volatile.
Table 11.6.4. The five writes, on PB4’s line and on PA15’s
What it does The button on PB4 The sensor on PA15
Choose which port drives the line EXTI->EXTICR[1], EXTI_PB EXTI->EXTICR[3], EXTI_PA
Trigger on the falling edge EXTI_FTSR1, bit 4 EXTI_FTSR1, bit 15
Unmask the line EXTI_IMR1, bit 4 EXTI_IMR1, bit 15
Clear the pending bit, in the handler EXTI_FPR1, bit 4 EXTI_FPR1, bit 15
Enable it in the NVIC EXTI4_15_IRQn EXTI4_15_IRQn, unchanged
The handler’s name EXTI4_15_IRQHandler EXTI4_15_IRQHandler, unchanged
The last two rows are the ones to be careful with. One vector covers lines 4 through 15 together, so the 4 in EXTI4_15_IRQn and in EXTI4_15_IRQHandler is part of a name and stays a 4. Every other 4, the ones that identify the line, becomes a 15.