您的位置 首页 5G

单片机键盘扫描试验

代码如下:实例7includereg51h>led字模charled_mod[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,

代码如下:
//实例7
#include
//led字模
char led_mod[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x58,0x5e,0x79,0x71};
//键值
char key_buf[] = {0x11,0x21,0x41,0x81,0x12,0x22,0x42,0x82,
0x14,0x24,0x44,0x84,0x18,0x28,0x48,0x88};
char getKey(void);
void delay(unsigned int time){
unsigned int j = 0;
for(;time>0;time–)
for(j=0;j<125;j++);
}
void main(void) {
char key = 0;
P2 = 0x00;
//P0=0x11;
while(1) {
key = getKey();
if (key != -1) {
P2 = led_mod[key];
}
delay(10);
}
}
//检测按键
//回来-1表明没有检测到按键按下
char getKey(void) {
char i = 0;
unsigned char keycode=0xff;
unsigned char scancode=0xff;
scancode=0xf7;
P0=0xf0;
while(scancode!=0xff) {
P0=scancode;
//delay(10);
keycode=P0;
if((keycode&0xf0)!=0xf0)break;
scancode=(keycode>>1)|0xf0;
//if(keycode==0xff)scancode=scancode&0xf0;
}
keycode=~keycode;
for (i = 0 ; i < 16 ; i++) {
if (key_buf[i] == keycode) {
return i;
}
}
return-1;
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/yingyong/5g/262757.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部