嵌入式——獨立鍵盤掃描

 

#include <reg51.h>
#define IO_KEY P1

void Delay(unsigned int c);
unsigned char KeyScan();

void main(){
    unsigned char key = 0;
    while(1){
        key = KeyScan();
        Delay(100);
    }
}

void Delay(unsigned int c){
    unsigned char a,b;
    for(;c>0;c--)
        for(b=13;b>0;b--)
            for(a=38;a>0;a--);
}

unsigned char KeyScan(){
    unsigned char key = 0;
    if(IO_KEY != 0xff){
        Delay(10);        //延時10ms,消除抖動
        if(IO_KEY != 0xff){
            key = IO_KEY;
        }
    }
    return key;
}
相關文章
相關標籤/搜索