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 service routine/ˈɪntərʌpt ˈsɜːvɪs ruːˈtiːn/ | 割り込みサービスルーチン |
| timer/ˈtaɪmə/ | タイマ |
| prescaler/ˈpriːskeɪlə/ | プリスケーラ |
一週間か一年か
- センサーノードは単一バッテリーで一年間動作する必要があります。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からゼロに wrap した際に更新イベントを発生させます。有効な割り込みは 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.
このカードは修正されたため、翻訳が更新されるまで英語で表示されます。
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\%$.
三つの一般的な誤解
- フラグクリアの忘れた処理。 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)$で割り、保存されたレジスターの値と分周器の積を確認します。
- 低いイベントレートでは中断が有利であり、非常に高いレートではポーリングが有利である。
何が問題でしたか?この内容はサイトの運営チームだけが確認します。