//按键和键盘组合程序
//P3口键盘扫描,P0口LED显示
#include<reg51.h>
unsigned char table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07};/*定义LED段位码*/
unsigned char temp;/*定义键值变量*/
void delay(unsigned char time)/*扫描延时子程序*/
{
unsigned char i,j;
for(i=0;i<=20;i++)
for(j=0;j<=time;j++);
}
void init(void)/*初始化子程序*/
{
P0=0x00;
P3=0xff;
}
void display(unsigned char key)/*显示子程序*/
{
P0=table[key];
}
bit keyscan()/*键盘扫描子程序*/
{
temp="P3";
if(temp!=0x00)
return 1;
else
return 0;
}
unsigned char keyprocess()/*键盘处理子程序*/
{
unsigned char keyresult;
switch(temp)
{
case 0xfe:keyresult=0x00;break;
case 0xfd:keyresult=0x01;break;
case 0xfb:keyresult=0x02;break;
case 0xf7:keyresult=0x03;break;
case 0xef:keyresult=0x04;break;
case 0xdf:keyresult=0x05;break;
case 0xbf:keyresult=0x06;break;
case 0x7f:keyresult=0x07;break;
default:keyresult=0xff;break;
}
return keyresult;
}
int main()/*主程序*/
{
unsigned char keyout;
init();//初始化
for(;;)
{
if(keyscan()==1)//第一次扫描
delay(250);//延时重扫描
if(keyscan()==1)//第二次扫描
{
keyout="keyprocess"();//取扫描键值
display(keyout);//显示
}
}
}
文章评论(0条评论)
登录后参与讨论