微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > [C51代码]4*4键盘扫描

[C51代码]4*4键盘扫描

时间:11-10 来源:互联网 点击:
程序如下:

#include
#define uint unsigned int
#define uchar unsigned char

sbit key3 =P2^0; //按键的行检测信号
sbit key2 =P2^1;
sbit key1 =P2^2;
sbit key0 =P2^3;

sbit dis0 =P2^4;
sbit dis1 =P2^5;
sbit dis2 =P2^6;
sbit dis3 =P2^7;
sbit dis4 =P3^3;
uchar keyx,key;
uchar code disp[17]={0x00,0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

void delay(uint i)
{for(;i>0;i--)
;
}

uchar keyscan() //数码管显示扫描与键盘检测
{
static dis=0;
dis=(dis+1)%5;
dis0=1;dis1=1;dis2=1;dis3=1;dis4=1;
switch(dis) //位选码输出
{
case 0: dis0=0; break;
case 1: dis1=0; break;
case 2: dis2=0; break;
case 3: dis3=0; break;
case 4: dis4=0; break;
}
if(!key0) keyx=dis+1; //键盘的行检测与译码
if(!key1) keyx=dis+5;
if(!key2) keyx=dis+9;
if(!key3) keyx=dis+13;
return keyx;
}
void main(void)
{ while(1)
{
key=keyscan();
P0=disp[key];
delay(3000);

}

}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top