//该程序实现了,按一次按键将其对应的数值以9600波特率发送到串口上,注意用的是4X4键盘
//将JP8跳线帽联接到P1。0
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define shuju P0
#define bus P1
sbit beep="P3"^4;
void delayms(int i)
{
int j;
for(j=0;j<i;j++);
}
void chushi()//初始化串口
{//***晶振:11.0592MHZ,波特率:9600***
TMOD="0x20";
SCON="0x50";
TH1=0xfA;
TL1=0xfA;
PCON="0x80";
TR1=1;
EA="1";
ES="0";
RI="0";
TI="0";
}
void display(uchar i)
{
SBUF=i;
while(!TI);
TI=0;
}
void jianpan()//键盘检测
{
uchar a;
uchar k;
bus="0xff";//清P1口
bus="0xef";
delayms(6000);
a="bus";
a="a"&0x0f;
if(a!=0x0f)
{
delayms(2000);
if(a!=0x0f)
{
switch(a)
{
case 0x0e:
k="0x01";
break;
case 0x0d:
k="0x02";
break;
case 0x0b:
k="0x03";
break;
case 0x07:
k="0x04";
break;
}
display(k);
while(a!=0x0f)
{beep=0;
bus="0x0f";
delayms(2000);
a="bus";
}
}
}
beep="1";
bus="0xdf";
delayms(6000);
a="bus";
a="a"&0x0f;
if(a!=0x0f)
{
delayms(2000);
if(a!=0x0f)
{
switch(a)
{
case 0x0e:
k="0x05";
break;
case 0x0d:
k="0x06";
break;
case 0x0b:
k="0x07";
shuju="0x07";
break;
case 0x07:
k="0x08";
break;
}
display(k);
while(a!=0x0f)
{ beep="0";
bus="0x0f";
delayms(2000);
a="bus";
}
}
}
beep="1";
bus="0xbf";
delayms(6000);
a="bus";
a="a"&0x0f;
if(a!=0x0f)
{
delayms(2000);
if(a!=0x0f)
{
switch(a)
{
case 0x0e:
k="0x09";
break;
case 0x0d:
k="0x0a";
break;
case 0x0b:
k="0x0b";
break;
case 0x07:
k="0x0c";
break;
}
display(k);
while(a!=0x0f)
{ beep="0";
bus="0x0f";
delayms(2000);
a="bus";
}
}
}
beep="1";
bus="0x7f";
delayms(6000);
a="bus";
a="a"&0x0f;
if(a!=0x0f)
{
delayms(2000);
if(a!=0x0f)
{
switch(a)
{
case 0x0e:
k="0x0d";
break;
case 0x0d:
k="0x0e";
break;
case 0x0b:
k="0x0f";
break;
case 0x07:
k="0x00";
break;
}
display(k);
while(a!=0x0f)
{ beep="0";
bus="0x0f";
delayms(2000);
a="bus";
}
}
}
}
void main()
{
chushi();//初始化串口
while(1)
{
jianpan();
}
}
文章评论(0条评论)
登录后参与讨论