Interrupts and timers
แนะนำใหม่
7:37
ไมโครโปรเซสเซอร์และระบบฝังตัว
The digital logic course built gates and flip-flops; this course builds the machine that runs programs. Embedded work is verified from three directions.…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| interrupt/ˈɪntərʌpt/ | สัญญาณขัดจังหวะ (Interrupt) |
| interrupt service routine/ˈɪntərʌpt ˈsɜːvɪs ruːˈtiːn/ | โปรแกรมบริการขัดจังหวะ (ISR) |
| timer/ˈtaɪmə/ | ตัวจับเวลา |
| prescaler/ˈpriːskeɪlə/ | ตัวหารความถี่ |
| divider factor | |
| polling/ˈpəʊlɪŋ/ | Having software check a device's status again and again to see whether it needs attention. |
สัปดาห์หนึ่งหรือหนึ่งปี
- Sensor node ต้องทำงานต่อเนื่องตลอดหนึ่งปีด้วยแบตเตอรี่เพียงก้อนเดียว ถ้า CPU ทำ polling ตลอดทั้งวัน แบตเตอรี่จะหมดภายในประมาณสัปดาห์แรก
- Interrupts ช่วยให้ CPU นอนหลับจนกว่าจะมีสิ่งที่ต้องใช้งาน บทเรียนนี้จะแสดงวิธีการทำงานและค่าใช้จ่ายที่เกี่ยวข้อง
Interrupt
- Interrupt จะหยุดโปรแกรมหลักที่กำลังทำงานอยู่ Hardware จะบันทึกสถานะก่อนหน้าแล้ว jump ไปยัง interrupt service routine (ISR)
- NVIC (nested vectored interrupt controller) เลือก ISR และจัดลำดับความสำคัญ ISR จะทำงาน ปิด flag ของมันเอง แล้วกลับมาทำงานต่อ
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.
Timer
- Timer คือตัวนับที่ถูก clock ผ่านตัวหาร ซึ่งเรียกว่า prescaler ใน upcounter แบบ STM32 ที่ใช้ที่นี่ มันจะส่งสัญญาณ update event เมื่อการนับวนกลับจาก ARR กลับไปเป็นศูนย์; Interrupt ที่เปิดใช้งานสามารถเรียก ISR ได้ทันที
- การหารสองขั้นตอนเปลี่ยนนาฬิกาความเร็วสูงให้เป็น tick ที่ช้าลงและแม่นยำ

สมการของ Timer
- อัตราการเกิด interrupt คือ $f_{\text{int}} = \dfrac{f_{\text{clock}}}{P R}$, โดยที่ $P = PSC + 1$ และ $R = ARR + 1$.
- ค่าที่เก็บใน PSC และ ARR เป็นตัวเลข 16-bit สูงสุดคือ 65 535 ตัวหาร (divider factors) สามารถถึง 65 536 เพราะเลข 0 ก็ถูกนับด้วย ตรวจสอบว่าค่าใน registers เข้ากันได้ และ $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.
ตัวอย่างคำนวณ: Heartbeat ที่ 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.
การ์ดนี้ได้รับการแก้ไขแล้ว จึงแสดงเป็นภาษาอังกฤษจนกว่าคำแปลจะได้รับการอัปเดต
Worked example: interrupt or poll?
- An ISR with 12 cycles of entry, 30 instructions taking one cycle each, 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\%$.
ข้อผิดพลาดทั่วไปสามประการ
- ลืมล้างสถานะ (flag) CPU จะกลับไปยังโปรแกรมหลักแล้วเข้าสู่ ISR อีกครั้งโดยไม่หยุดยั้ง
- ISR ที่ใช้เวลานาน ควรทำเพียงสิ่งที่จำเป็นที่สุด ตั้งค่า flag แล้วให้ลูปหลักทำงานต่อ
- อ่านค่าไทม์เมอร์ 16-บิต เป็นสองส่วน หากมีการ carry ระหว่างการอ่านจะทำให้ค่าผิดเพี้ยน; ให้อ่าน byte สูงสองครั้งหรือใช้ 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.
สรุปทบทวน
- การรบกวน: ใช้ vector, ISR สั้น, ล้าง flag
- ไทม์เมอร์: หาร $f_{\text{clock}}$ ด้วย $(PSC+1)(ARR+1)$; ตรวจสอบค่าที่บันทึกในรีจิสเตอร์และผลคูณของตัวหาร
- การรบกวนชนะที่อัตราการเหตุการณ์ต่ำ ส่วนการสุ่มตรวจสอบชนะที่อัตราสูงมาก
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้