Re: 請問如何解決按鍵彈跳問題?
|
||||
---|---|---|---|---|
資深會員
|
感謝版主清楚的回答,照您的關念只要程式寫對,
應該是個很好的方式
發表於: 2009/9/17 15:32
|
|||
|
Re: 請問如何解決按鍵彈跳問題?
|
||||
---|---|---|---|---|
資深會員
|
感謝nicecookie大大清楚的說明,畢竟例上總是比較無法適用於實際的應用,經你這麼說就了解了!
發表於: 2009/9/17 9:46
|
|||
|
Re: 請問如何解決按鍵彈跳問題?
|
||||
---|---|---|---|---|
資深會員
|
範例程式就只是提供參考而已
實際應用還是要自己做變化 實作時後,例如像是這種程式寫法一定要避免 while(~PORTAbits.RA4); //等待按鍵放開 用個while()在那邊等待按鍵放開是非常危險的行為 要是user故意不放開呢?要是按鍵故障卡死了呢? 主程式不就當在那邊了嗎 用delay和 loop式的計數只能用在ms以內時使用 不然會耗掉太多不必要時間 彈跳處理很簡單 只要在主程式的while()迴圈裡 每次都去檢查1次按鍵 主程式迴圈run個10次,不就檢查10次了嗎 何必要用delay和 while(~PORTAbits.RA4) 呢 若要讓檢查按鍵時間固定點,那就搭配timer 中斷即可 範例程式的做法,用來練習是很方便的 用來實做,還是避免為上
發表於: 2009/9/17 9:23
|
|||
|
Re: 請問如何解決按鍵彈跳問題?
|
||||
---|---|---|---|---|
資深會員
|
請教版主:
在實際的應用上,可能因為主程式很複雜,且在判斷按鍵時沒有辦法做其它工作,這樣有辦法解決嗎?
發表於: 2009/9/17 8:30
|
|||
|
Re: 請問如何解決按鍵彈跳問題?
|
||||
---|---|---|---|---|
版主
|
貼上一段 dsPIC30F Peripheral -- Interrupt Module 的教育訓練裡的 Lab2 裡的處理按鍵彈跳的問題:
while(1)
debunce 為 0 時,代表彈跳處理已完成進入一般偵測按鍵模式,如果SW5 & SW6 有被按下就立即處理按鍵事項並設定 debunce time = 30 也就是要連續檢查 30 次案件是否都放開了,如果檢查一次的延遲為 2mS 那就連續檢查 60mS。 如果 SW5 &SW6 都放開了,每2mS 檢查時就將 debunce -1 直到為 0,如果在檢查期間 SW5 & SW6 又有 Low 的現象就表示還在桃跳階段,以前的 debunce 不算重設為30
發表於: 2009/9/16 11:17
|
|||
|
Re: 請問如何解決按鍵彈跳問題?
|
||||
---|---|---|---|---|
資深會員
|
用timer中斷
//使用Microchip APP025實驗板 // Writer : Edison //2009/07/09 23:39 #include <p18f4520.h> #include <timers.h> #pragma config OSC=HS,BOREN=OFF,LVP=OFF,BORV=2,WDT=OFF,PWRT=ON #define TMR1_VAL 55536 //中斷時間1ms==>65536-55536)*1/10MHz #define OSC_CLOCK 10 #define key PORTAbits.RA4 unsigned char keydelay=0; void scankey(); struct { unsigned TMR1INT:1; unsigned keyfirst:1; unsigned kin:1; unsigned kconti:1; }FLAGbits; void timer1_isr(void); #pragma code high_vector=0x08 void high_interrupt(void) { _asm GOTO timer1_isr _endasm } #pragma code #pragma interrupt timer1_isr void timer1_isr(void) { PIR1bits.TMR1IF=0; WriteTimer1(TMR1_VAL); FLAGbits.TMR1INT=1; } void main(void) { PORTD=0x01; TRISD=0; TRISAbits.TRISA4=1; // 設定RA4為數位輸入腳位 OpenTimer1( TIMER_INT_ON & T1_16BIT_RW & T1_SOURCE_INT & T1_PS_1_1 & T1_OSC1EN_ON & T1_SYNC_EXT_OFF ); WriteTimer1(TMR1_VAL); PIR1bits.TMR1IF = 0; // 清除中斷旗標 IPR1bits.TMR1IP = 1; // 設定為高優先中斷 RCONbits.IPEN=1; // 開啟中斷優先功能 INTCONbits.GIEL = 1; // 開啟低優先中斷功能 INTCONbits.GIEH = 1; // 開啟高優先中斷功能 FLAGbits.TMR1INT=0; while(1) { if(PORTAbits.RA4==0) FLAGbits.kin=1; if(FLAGbits.kin==1) { if(FLAGbits.TMR1INT==1) //判斷timer1中斷旗標 { keydelay++; // keydelay FLAGbits.TMR1INT=0; if(keydelay==20) // delay 20ms { while(~PORTAbits.RA4); //等待按鍵放開 PORTDbits.RD0=!PORTDbits.RD0; //LED0反向 keydelay=0; // 重設delay時間 FLAGbits.kin=0; // 按鍵旗標重設為0 } } } } }
發表於: 2009/9/15 18:11
|
|||
|
請問如何解決按鍵彈跳問題?
|
||||
---|---|---|---|---|
新會員
|
小弟寫了一段hi-tech的程式
if(!SW3) { count=count+1; } !SW3是APP001上的SW開關 發現COUNT並非正常的每一次都加1 請問該如何解決這樣的問題呢?
發表於: 2009/9/15 17:49
|
|||
|