• slider image 442
  • slider image 483
  • slider image 484
  • slider image 485
  • slider image 486
  • slider image 487
  • slider image 488
  • slider image 491
:::


Browsing this Thread:   1 Anonymous Users






Re: 請問如何解決按鍵彈跳問題?
#8
資深會員
資深會員


查看用戶資訊
感謝版主清楚的回答,照您的關念只要程式寫對,
應該是個很好的方式

發表於: 2009/9/17 15:32
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 請問如何解決按鍵彈跳問題?
#7
版主
版主


查看用戶資訊
所提供的按鍵彈跳處理只是一個基本觀念而已。實際上按鍵的偵測掃描與彈跳處理實際上都在背景程式下(Timer 中斷)執行的,主程式一點都不會受按鍵按住不放就無法執行的情形。
按鍵背景處理程式只會再有新按鍵按下時產生一個按鍵碼 (不為 0) 存在 RAM 裡,主程式看到有新按鍵碼輸入就去執行按鍵動作並將按鍵碼清為零。在此同時中斷背景程式持續作彈跳偵測直到按鍵完全放開為止,這時才進入下一按鍵的掃描動作...

基本的動作與範例程式雷同,只是程式移到中斷下處理,for 迴圈 delay 變成了 Timer 的中斷時間了。

發表於: 2009/9/17 10:07
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 請問如何解決按鍵彈跳問題?
#6
資深會員
資深會員


查看用戶資訊
感謝nicecookie大大清楚的說明,畢竟例上總是比較無法適用於實際的應用,經你這麼說就了解了!

發表於: 2009/9/17 9:46
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 請問如何解決按鍵彈跳問題?
#5
資深會員
資深會員


查看用戶資訊
範例程式就只是提供參考而已
實際應用還是要自己做變化

實作時後,例如像是這種程式寫法一定要避免

while(~PORTAbits.RA4); //等待按鍵放開


用個while()在那邊等待按鍵放開是非常危險的行為
要是user故意不放開呢?要是按鍵故障卡死了呢?
主程式不就當在那邊了嗎


用delay和 loop式的計數只能用在ms以內時使用
不然會耗掉太多不必要時間

彈跳處理很簡單
只要在主程式的while()迴圈裡
每次都去檢查1次按鍵
主程式迴圈run個10次,不就檢查10次了嗎
何必要用delay和 while(~PORTAbits.RA4) 呢

若要讓檢查按鍵時間固定點,那就搭配timer 中斷即可


範例程式的做法,用來練習是很方便的
用來實做,還是避免為上

發表於: 2009/9/17 9:23
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 請問如何解決按鍵彈跳問題?
#4
資深會員
資深會員


查看用戶資訊
請教版主:

在實際的應用上,可能因為主程式很複雜,且在判斷按鍵時沒有辦法做其它工作,這樣有辦法解決嗎?

發表於: 2009/9/17 8:30
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 請問如何解決按鍵彈跳問題?
#3
版主
版主


查看用戶資訊
貼上一段 dsPIC30F Peripheral -- Interrupt Module 的教育訓練裡的 Lab2 裡的處理按鍵彈跳的問題:
while(1)
    {
        if (
debounce==0)
            {
                if (!
SW5
                    {
                    
SRbits.IPL++;
                    
setcurLCD(15,1);
                    
putcLCD('0'SRbits.IPL);
                    
debounce=30;
                    }
                if (!
SW6
                    {
                    
SRbits.IPL--;
                    
setcurLCD(15,1);
                    
putcLCD('0'SRbits.IPL);
                    
debounce=30;
                    }
            }
        else
            {
                if (
SW5 SW6
                    {
                        
debounce--;
                        for (
i=0;i<100;i++);
                    }
                else 
                    
debounce=30;    
            }         
    }


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
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 請問如何解決按鍵彈跳問題?
#2
資深會員
資深會員


查看用戶資訊
用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
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


請問如何解決按鍵彈跳問題?
#1
新會員
新會員


查看用戶資訊
小弟寫了一段hi-tech的程式
if(!SW3)
{
count=count+1;
}

!SW3是APP001上的SW開關
發現COUNT並非正常的每一次都加1
請問該如何解決這樣的問題呢?

發表於: 2009/9/15 17:49
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部







You can view topic.
不可以 發起新主題
You cannot reply to posts.
You cannot edit your posts.
You cannot delete your posts.
You cannot add new polls.
You cannot vote in polls.
You cannot attach files to posts.
You cannot post without approval.
You cannot use topic type.
You cannot use HTML syntax.
You cannot use signature.
You cannot create PDF files.
You cannot get print page.

[進階搜尋]


:::

Microchip連結

https://www.facebook.com/microchiptechnologytaiwan/
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=13
https://mu.microchip.com/page/tmu
http://elearning.microchip.com.tw/modules/tad_link/index.php?cate_sn=1
https://page.microchip.com/APAC-PrefCenters-TW.html
http://www.microchip.com/
http://www.microchip.com/treelink
http://www.microchipdirect.com/
http://www.microchip.com.cn/newcommunity/index.php?m=Video&a=index&id=103
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=2
http://www.microchip.com.tw/Data_CD/eLearning/index.html
http://www.microchip.com.tw/RTC/RTC_DVD/
https://www.microchip.com/development-tools/
https://www.youtube.com/user/MicrochipTechnology
[ more... ]

教育訓練中心

!開發工具購買
辦法說明 [業界客戶] [教育單位]
----------------------------------
!校園樣品申請
辦法說明 [教師資格] [學生資格]
----------------------------------