嵌入式——独立键盘扫描

 

#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;
}

 

posted @ 2016-02-16 15:51  冰糖胡芦娃哈哈  阅读(326)  评论(0编辑  收藏  举报