Interrupt dan Timer
DiperkenalkanDalam pelajaran ini
| English | Makna |
|---|---|
| interrupt/ˈɪntərʌpt/ | interupsi |
| interrupt service routine/ˈɪntərʌpt ˈsɜːvɪs ruːˈtiːn/ | rutin layanan interupsi |
| timer/ˈtaɪmə/ | |
| prescaler/ˈpriːskeɪlə/ | preskaler |
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.
Atur penanganan satu interupsi secara berurutan.
Kesulitan 3/5 · 200 koin + bonus kecil pada penyelesaian pertama hari ini
Simpan, lompat, layani, hapus, kembali. Lupakan penghapusan dan langkah terakhir tidak akan pernah terjadi.
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}}$.
Sebuah pewaktu berjalan pada 84 MHz dengan faktor pembagi P = 84 (PSC = 83) dan R = 1000 (ARR = 999). Berapa frekuensi interupsi dalam Hz?
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
$84 \times 10^{6} / (84 \times 1000) = 1000$ Hz: satu detak setiap 1 ms, dan nilai tersimpan 83 serta 999 keduanya muat dalam 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.
Dari clock 84 MHz, faktor pembagi 4200 (pembagi awal) dan 20 000 (muat ulang) menghasilkan interupsi pada
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
$4200 \times 20\,000 = 84 \times 10^{6}$, yaitu frekuensi clock, sehingga tepat 1 Hz; dan nilai ARR = 19 999 yang disimpan muat dalam 16 bit.
Faktor pembagi 1 (pembagi awal) dan 84 000 (muat ulang) menghasilkan rancangan 1 kHz yang valid pada register pewaktu 16 bit.
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
Hasil kalinya benar, tetapi ARR harus menyimpan 83 999, lebih besar daripada 65 535, sehingga tidak muat. Faktor 84 dan 1000 (PSC = 83, ARR = 999) dapat digunakan.
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.
ISR berdurasi 52 siklus pada frekuensi 84 MHz, melayani 1 MHz event, menggunakan sekitar
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
$52 \times 10^{6}$ siklus per detik dari $84 \times 10^{6}$ adalah 62%. ISR yang sama pada frekuensi 1 kHz hanya menggunakan 0.06%: laju event yang menentukan.
ISR yang sama dengan durasi 52 siklus melayani event 10 kHz pada 84 MHz. Berapa persentase CPU yang digunakan? Berikan 1 tempat desimal.
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
$52 \times 10\,000 = 520\,000$ siklus per detik; $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.
ISR yang lupa membersihkan flag interupsinya menyebabkan
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
Flag tetap menunjukkan status pending saat ISR selesai, sehingga CPU langsung masuk kembali. Bersihkan flag dan pastikan ISR pendek.
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 |
Apa yang salah? Hanya tim situs yang dapat melihatnya.