Interrupciones y temporizadores
IntroducidoEn esta lección
| English | Significado |
|---|---|
| interrupt/ˈɪntərʌpt/ | Interrupción |
| interrupt service routine/ˈɪntərʌpt ˈsɜːvɪs ruːˈtiːn/ | Rutina de servicio de interrupción |
| timer/ˈtaɪmə/ | Temporizador |
| prescaler/ˈpriːskeɪlə/ | predivisor |
A week or a year
- The sensor node must run for a year on one battery. If the CPU polls all day, the battery is empty in about a week.
- Interrupts let the CPU sleep until something needs it. This lesson shows how they work and what they cost.
The interrupt
- An interrupt 中断 stops the main program where it is. The hardware saves the return state and jumps to an interrupt service routine 中断服务程序 (ISR).
- The NVIC (nested vectored interrupt controller) picks the ISR and ranks priorities. The ISR runs, clears its flag and returns.
Coloque el manejo de una interrupción en orden.
Dificultad 3/5 · 200 monedas + un pequeño bono en el primer intento de hoy
Guardar, saltar, servir, limpiar, regresar. Olvidar la limpieza hace que el último paso nunca ocurra.
The timer
- A timer 定时器 is a counter clocked through a divider, the prescaler 分频器. In the STM32-style upcounter used here, it raises an update event when the count wraps from ARR back to zero; an enabled interrupt can then call the ISR.
- Two divisions turn a fast clock into a slow, exact tick.

The timer equation
- The interrupt rate is $f_{\text{int}} = \dfrac{f_{\text{clock}}}{P R}$, where $P = PSC + 1$ and $R = ARR + 1$.
- The stored registers PSC and ARR are 16-bit values, at most 65 535. Their divider factors can reach 65 536 because zero is counted too. Check that the registers fit and that $PR = f_{\text{clock}}/f_{\text{int}}$.
Un temporizador funciona a 84 MHz con factores divisores P = 84 (PSC = 83) y R = 1000 (ARR = 999). ¿Cuál es la frecuencia de interrupción en Hz?
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
$84 \times 10^{6} / (84 \times 1000) = 1000$ Hz: un intervalo de 1 ms, y los valores almacenados 83 y 999 caben ambos en 16 bits.
Worked example: a 1 Hz heartbeat
- From 84 MHz, divider factor 8400 gives 10 kHz. A further factor of 10 000 gives exactly 1 Hz: write PSC = 8399 and ARR = 9999.
- Check: both stored values fit 16 bits, and $8400 \times 10\,000 = 84 \times 10^{6}$. Divider factor 84 000 would need PSC = 83 999, which does not fit.
The divider factor includes the zero count
$f_{update}=f_{clock}/[(PSC+1)(ARR+1)]$
Start with the worked 84 MHz to 1 Hz heartbeat. Divider factors 8400 and 10000 require stored PSC = 8399 and ARR = 9999 in this STM32-style upcounter. Advance exact clock edges, counter ticks and update events, then use a small 4 × 5 example to see zero counted. Compare writing the factors directly to the registers and observe the changed rate.
A partir de un reloj de 84 MHz, los factores divisores 4200 (preescalador) y 20 000 (recarga) generan una interrupción a
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
$4200 \times 20\,000 = 84 \times 10^{6}$, la frecuencia del reloj, por lo que es exactamente 1 Hz; y el valor almacenado ARR = 19 999 cabe en 16 bits.
Los factores divisores 1 (preescalador) y 84 000 (recarga) permiten un diseño válido de 1 kHz con registros de temporizador de 16 bits.
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
El producto es correcto, pero ARR tendría que almacenar 83 999, más de 65 535, por lo que no cabe. Los factores 84 y 1000 (PSC = 83, ARR = 999) funcionan.
Worked example: interrupt or poll?
- An ISR with 12 cycles of entry, 30 instructions and 10 cycles of return costs 52 cycles, or 0.62 µs at 84 MHz.
- That cost is paid on every event: 0.06% of the CPU at 1 kHz of events, but 62% at 1 MHz. At very high rates, polling wins.
Una ISR de 52 ciclos a 84 MHz, que sirve eventos de 1 MHz, utiliza aproximadamente
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
$52 \times 10^{6}$ ciclos por segundo de $84 \times 10^{6}$ es el 62%. La misma ISR a 1 kHz solo usa el 0.06%: la tasa de eventos decide.
La misma ISR de 52 ciclos sirve eventos de 10 kHz a 84 MHz. ¿Qué porcentaje de la CPU utiliza? Proporcione 1 decimal.
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
$52 \times 10\,000 = 520\,000$ ciclos por segundo; $520\,000 / 84 \times 10^{6} = 0.62\%$.
Three common slips
- Forgetting to clear the flag. The CPU returns and enters the ISR again, for ever.
- Long ISRs. Do the minimum, set a flag, and let the main loop do the work.
- Reading a 16-bit timer in two halves. A carry between the reads corrupts the value; read the high byte twice or use a latch.
Una ISR que olvida limpiar su bandera de interrupción causa
Dificultad 1/5 · 100 monedas + un pequeño bono en el primer intento de hoy
La bandera sigue mostrando pendiente cuando la ISR retorna, por lo que la CPU salta directamente de nuevo. Limpie la bandera y mantenga la ISR corta.
Recap
- Interrupt: vector, short ISR, clear the flag.
- Timer: divide $f_{\text{clock}}$ by $(PSC+1)(ARR+1)$; check the stored register values and the divider product.
- Interrupts win at low event rates; polling wins at very high ones. The interrupt overhead in CPU time:
| Events per second | 1 kHz | 1 MHz |
|---|---|---|
| CPU used by interrupts | 0.06% | 62% |
| Better choice | interrupts | polling |
¿Qué salió mal? Solo el equipo del sitio ve esta información.