Ngắt và bộ đếm thời gian
Đã giới thiệuTrong bài học này
| English | Ý nghĩa |
|---|---|
| interrupt/ˈɪntərʌpt/ | ngắt |
| interrupt service routine/ˈɪntərʌpt ˈsɜːvɪs ruːˈtiːn/ | hàm phục hồi ngắt |
| timer/ˈtaɪmə/ | bộ đếm thời gian |
| prescaler/ˈpriːskeɪlə/ | bộ chia tần |
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.
Sắp xếp thứ tự xử lý một tín hiệu ngắt.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Lưu, chuyển, phục vụ, xóa, trở về. Bỏ qua bước xóa và bước cuối cùng sẽ không bao giờ xảy ra.
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}}$.
Một bộ định thời chạy từ nguồn xung 84 MHz với các hệ số chia P = 84 (PSC = 83) và R = 1000 (ARR = 999). Tần số ngắt là bao nhiêu Hz?
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
$84 \times 10^{6} / (84 \times 1000) = 1000$ Hz: một chu kỳ 1 ms, và cả hai giá trị được lưu 83 và 999 đều vừa trong 16 bit.
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.
Từ nguồn xung 84 MHz, các hệ số chia 4200 (bộ chia trước) và 20 000 (giá trị nạp lại) tạo ra ngắt tại
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
$4200 \times 20\,000 = 84 \times 10^{6}$, bằng tần số xung nhịp, nên chính xác là 1 Hz; và giá trị ARR = 19 999 được lưu vừa trong 16 bit.
Các hệ số chia 1 (bộ chia trước) và 84 000 (giá trị nạp lại) tạo thành một thiết kế 1 kHz hợp lệ trên các thanh ghi bộ định thời 16 bit.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Tích là đúng, nhưng ARR sẽ phải lưu 83 999, lớn hơn 65 535, nên không vừa. Các hệ số 84 và 1000 (PSC = 83, ARR = 999) sử dụng được.
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.
Một ISR dài 52 chu kỳ ở tốc độ 84 MHz, phục vụ sự kiện 1 MHz, tiêu thụ khoảng
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
$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.
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
$52 \times 10\,000 = 520\,000$ chu kỳ mỗi giây; $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.
Một ISR quên xóa cờ ngắt của nó sẽ gây ra
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Cờ vẫn cho thấy trạng thái chờ khi ISR trả về, nên CPU nhảy thẳng trở vào. Hãy xóa cờ và giữ cho ISR ngắn gọn.
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 |
Đã xảy ra vấn đề gì? Chỉ đội ngũ trang web nhìn thấy thông tin này.