📄 串行.c
字号:
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uint z,num,numduan;
uchar temp,numwei=0,numwei1;
static west=0;
uchar code tableduan[]={ //键盘表
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code tablewei[]={
0xfe,0xfd,0xfb,0xf7,
0xef,0xdf,0xbf,0x7f};
void init();
void sent(uchar aa);
void receive();
uchar jianpan();
void display(uchar dis);
uchar cc;
void main()
{
init();
while(1)
{
numduan=jianpan();
display(cc);
if(west) {
cc=numduan;
sent(numduan);
west=0; }
receive();
}
}
void init()
{
SM0=0;
SM1=1;
REN=1;
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
TR1=1;
TI=0;
}
//------------------------------------------------
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=120;y>0;y--);
}
void display(uchar dis)
{
P0=tableduan[dis];
P2=0;
}
//--------------------------------键盘子程序:
uchar jianpan( )
{
P1=0xfe;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xee: num=1; break;
case 0xde: num=2; break;
case 0xbe: num=3; break;
case 0x7e: num=4; break;
}
}
while(temp!=0xf0) //松手检测
{
temp=P1;
temp=temp&0xf0;
west=1;
}
}
//-------------------------------------------
P1=0xfd;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xed: num=5; break;
case 0xdd: num=6; break;
case 0xbd: num=7; break;
case 0x7d: num=8; break;
}
}
while(temp!=0xf0) //松手检测
{
temp=P1;
temp=temp&0xf0;
west=1;
}
}
//-------------------------------------------
P1=0xfb;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xeb: num=9; break;
case 0xdb: num=10; break;
case 0xbb: num=11; break;
case 0x7b: num=12; break;
}
}
while(temp!=0xf0) //松手检测
{
temp=P1;
temp=temp&0xf0;
west=1;
}
}
//-----------------------------------------------
P1=0xf7;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xe7: num=12; break;
case 0xd7: num=13; break;
case 0xb7: num=14; break;
case 0x77: num=15; break;
}
}
while(temp!=0xf0) //松手检测
{
temp=P1;
temp=temp&0xf0;
west=1;
}
}
return num;
}
void sent(uchar aa)
{
SBUF=aa;
while(1)
{
if(TI)
{TI=0;
break;
}
}
}
void receive()
{ uchar bb;
while(RI)
{cc=SBUF;
RI=0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -