Interrupts and timers
引入
7:37
微处理器与嵌入式系统
数字逻辑课程构建了逻辑门和触发器;本课程要构建一台运行程序的机器。嵌入式系统的工作要从三个方面验证。位宽:每个值都能放进它的寄存器吗?时钟:时序能满足要求吗?执行轨迹:代码是否完成了你声称的任务?本课程中的每一次寄存器计算、每一个定时器设置和每一段代码,都要从这三个方面检查。
英文讲解 · 可切换中英文字幕
本课内容
| English | 释义 | 拼音 |
|---|---|---|
| interrupt/ˈɪntərʌpt/ | 中断 | zhōng duàn |
| interrupt service routine/ˈɪntərʌpt ˈsɜːvɪs ruːˈtiːn/ | 中断服务程序 | zhōng duàn fú wù chéng xù |
| timer/ˈtaɪmə/ | 定时器 | dìng shí qì |
| prescaler/ˈpriːskeɪlə/ | 分频器 | fēn pín qì |
| divider factor | 分频系数 | fēn pín xì shù |
| polling/ˈpəʊlɪŋ/ | 轮询 | lún xún |
一周还是一年
- 传感器节点需依靠一块电池运行一年。若CPU全天持续轮询,电池大约在一周内就会耗尽。
- 中断机制使CPU能够在无任务时进入休眠状态,仅在需要时唤醒。本节展示其工作原理及开销。
中断
- 中断 中断会暂停主程序的当前执行位置。硬件保存返回状态并跳转到 中断服务程序 中断服务程序 (ISR)。
- NVIC(嵌套向量中断控制器)负责选择ISR并设定优先级。ISR执行完毕后清除标志位并返回。
Put the handling of one interrupt in order.
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Save, jump, serve, clear, return. Forget the clear and the last step never comes.
定时器
- 定时器 定时器是一个通过分频器计数的计数器,即 预分频器 分频器。在此使用的 STM32 风格向上计数器中,当计数从 ARR 回绕到零时会触发更新事件;此时已启用的中断可调用 ISR。
- 两级分频可将快速时钟转换为精确的慢速节拍。

定时器公式
- 中断频率取决于 $f_{\text{int}} = \dfrac{f_{\text{clock}}}{P R}$,其中 $P = PSC + 1$ 和 $R = ARR + 1$。
- 存储的寄存器 PSC 和 ARR 是 16 位值,最多为 65 535。由于零也被计入,其分频因子可达 65 536。请检查寄存器是否适配且 $PR = f_{\text{clock}}/f_{\text{int}}$。
A timer runs from 84 MHz with divider factors P = 84 (PSC = 83) and R = 1000 (ARR = 999). What is the interrupt rate in Hz?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
$84 \times 10^{6} / (84 \times 1000) = 1000$ Hz: a 1 ms tick, and the stored values 83 and 999 both fit 16 bits.
示例:一个1 Hz的心跳
- 从 84 MHz 出发,分频因子 8400 产生 10 kHz。进一步乘以 10 000 恰好得到 1 Hz:写入 PSC = 8399 和 ARR = 9999。
- 检查:两个存储值均符合 16 位,且 $8400 \times 10\,000 = 84 \times 10^{6}$。若分频因子为 84 000,则需要 PSC = 83 999,这将超出容量。
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.
From an 84 MHz clock, divider factors 4200 (prescaler) and 20 000 (reload) give an interrupt at
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
$4200 \times 20\,000 = 84 \times 10^{6}$, the clock, so exactly 1 Hz; and the stored ARR = 19 999 fits 16 bits.
Divider factors 1 (prescaler) and 84 000 (reload) make a valid 1 kHz design on 16-bit timer registers.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
The product is right, but ARR would have to store 83 999, more than 65 535, so it does not fit. Factors 84 and 1000 (PSC = 83, ARR = 999) work.
示例:中断还是轮询?
- 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.
A 52-cycle ISR at 84 MHz, serving 1 MHz of events, uses about
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
$52 \times 10^{6}$ cycles a second out of $84 \times 10^{6}$ is 62%. The same ISR at 1 kHz uses only 0.06%: the event rate decides.
The same 52-cycle ISR serves 10 kHz of events at 84 MHz. What percentage of the CPU does it use? Give 1 decimal place.
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
$52 \times 10\,000 = 520\,000$ cycles a second; $100 \times 520\,000 / (84 \times 10^{6}) = 0.62\%$.
三个常见误区
- 忘记清除标志位。 CPU返回后会再次进入ISR,陷入死循环。
- 长ISR。 仅执行最少操作,设置标志位,让主循环完成后续工作。
- 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.
An ISR that forgets to clear its interrupt flag causes
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
The flag still shows pending when the ISR returns, so the CPU jumps straight back in. Clear the flag and keep the ISR short.
复习要点
- 中断:向量号、短ISR、清除标志位。
- 定时器:将 $f_{\text{clock}}$ 除以 $(PSC+1)(ARR+1)$;检查存储的寄存器值和分频乘积。
- 低事件率下中断更优;极高事件率下轮询更优。
出了什么问题?只有网站团队能看到这些信息。