分类: C/C++
2009-04-16 15:32:46
//=====================================================================
// 4*4键盘扫描测试程序
//硬件连接: 4*4键盘的8个引脚接51的P1口
//扫描返回值为0—15,串口收到的值为a—p
//=====================================================================
#include
#define uint unsigned int
#define uchar unsigned char
uchar Key_Get()
{
uchar temp;
uchar a;
a=0xff;
P1=0xfe;
temp=P1&0xf0;
if(temp==0xe0) while(temp==0xe0){ P1=0xfe;temp=P1&0xf0; a=0x00; }
if(temp==0xd0) while(temp==0xd0){ P1=0xfe;temp=P1&0xf0; a=0x04; }
if(temp==0xb0) while(temp==0xb0){ P1=0xfe;temp=P1&0xf0; a=0x08; }
if(temp==0x70) while(temp==0x70){ P1=0xfe;temp=P1&0xf0; a=0x0c; }
P1=0xfd;
temp=P1&0xf0;
if(temp==0xe0) while(temp==0xe0){ P1=0xfd;temp=P1&0xf0; a=0x01; }
if(temp==0xd0) while(temp==0xd0){ P1=0xfd;temp=P1&0xf0; a=0x05; }
if(temp==0xb0) while(temp==0xb0){ P1=0xfd;temp=P1&0xf0; a=0x09; }
if(temp==0x70) while(temp==0x70){ P1=0xfd;temp=P1&0xf0; a=0x0d; }
P1=0xfb;
temp=P1&0xf0;
if(temp==0xe0) while(temp==0xe0){ P1=0xfb;temp=P1&0xf0; a=0x02; }
if(temp==0xd0) while(temp==0xd0){ P1=0xfb;temp=P1&0xf0; a=0x06; }
if(temp==0xb0) while(temp==0xb0){ P1=0xfb;temp=P1&0xf0; a=0x0a; }
if(temp==0x70) while(temp==0x70){ P1=0xfb;temp=P1&0xf0; a=0x0e; }
P1=0xf7;
temp=P1&0xf0;
if(temp==0xe0) while(temp==0xe0){ P1=0xf7;temp=P1&0xf0; a=0x03; }
if(temp==0xd0) while(temp==0xd0){ P1=0xf7;temp=P1&0xf0; a=0x07; }
if(temp==0xb0) while(temp==0xb0){ P1=0xf7;temp=P1&0xf0; a=0x0b; }
if(temp==0x70) while(temp==0x70){ P1=0xf7;temp=P1&0xf0; a=0x0f; }
return(a);
}
//=====================================================================
void init_serial() //初始化串口
{
TMOD=0x20; //定时器T1使用工作方式2
TH1=250; //设置初值
TH0=250;
TR1=1; //开始计时
PCON=0x80; //SMOD=1;
SCON=0x50; //工作方式1,波特率9600bit/s,允许接收
TI=1;
}
void main()
{
uchar key;
init_serial();
IE=0; //屏蔽中断
while(1)
{
key=Key_Get();
if(key!=0xff)
{
key+=97;
if(TI==1)
{
SBUF=key;
TI=0;
}
}
}
}