Japanino ArduinoISPで ATtiny13 で 割込みでスイッチ入力 (18) (9/21)
これは前回の外部割込みで、飛び先で25ms待っているのと同じ。
Isr_pcint:
' GIFR.5 = 1
Waitms 25
:
:
Return
しかし、次のURLからピンによる外部割込みの場合、チャタリングで発生した割込みが現在の割り込み終了を待ってから処理されるとの内容があり、GIFR I/Oレジスタの、ビット5のPCIFビットを0(他の割り込み要求を解除)するために1を書き込む必要があるらしい(コメントのGIFR.5=1)。それはポート単位で割り込みを解除するため、ポートBしかないATtiny13では外部割込み要求はチャタリングを防止するため1ピンしか使えないようだ。もちろんスイッチじゃなく外部回路とのやり取りならチャタリングはないのでこのビットは0にしなくてもいい。つまり2つスイッチを外部割込みで同じポートBにつなぐのは勧められない。
[AVR] 割り込み処理でのチャタリング対策 - Let's Try It!
別のチャタリング防止策として、外部割込みではなく、ポーリングで行う方法もある。
ソース:
'****************************************
' Copyright (c) 2016 k1segawa
' License : free
' Program : Internal Interrupt Control
' (Compare OC0A SW Bounce)
'****************************************
$regfile = "attiny13.dat"
$crystal = 9600000
$hwstack = 32
$swstack = 8
$framesize = 16
Dim S As Byte , W As Byte 'sampling
S = 0
Config Portb.3 = Input
Set Portb.3 'Pullup
'--- Compare OC0A ---
Config Portb.4 = Output
Config Timer0 = Timer , Prescale = 1024 , Clear Timer = 1 ' REMEMBER comma !
Ocr0a = 94 '10ms
On Oc0a Isr
Enable Oc0a
Enable Interrupts
Do
Loop
End
Isr:
'--- Store Pin Status with 3 times ---
W = Pinb.3 'polling Pin
S = S And &B11 'bit1,0 mask
Shift S , Left , 1 '1:bit2, 2:bit1, 3:bit0
S = S + W 'bit0 append
If S = 0 Then '3 times = 000
Portb.4 = 0
Elseif S = 7 Then '3 times = 111
Portb.4 = 1
End If
Return
' History
' [2016/09/21]
' URL=http://www.palettesoft.co.jp/technology/pic/pic_sample_sw0.htm
どちらの方法も長所短所があり、スイッチ入力が一筋縄ではいかないことがわかる。