#include
#define uint unsigned int
#define uchar unsigned char
sbit key1 = P1^0;//按键位界说
sbit key2 = P1^1;
sbit key3 = P1^2;
sbit key4 = P1^3;
void send(uchar key_num)//经过串口发送按键值子函数
{
SBUF = key_num;
while(!TI);
TI = 0;
}
void delay(uint z)//延时_ms子函数
{
uint x,y;
for(x = 0;xfor(y = 0;y<125;y++);
}
uint key_scan()//键盘扫描子函数,带返回值num
{
uint num;
if(key1 == 0)//键1按下则数码管将显现1
{
delay(5);
if(key1 ==0)
{num = 1;}
}
if(key2 == 0)//键2按下则数码管将显现2
{
delay(5);
if(key2 ==0)
{num = 2;}
}
if(key3 == 0)//键3按下则数码管将显现3
{
delay(5);
if(key3 ==0)
{num = 3;}
}
if(key4 == 0)//键4按下则数码管将显现4
{
delay(5);
if(key4 ==0)
{num = 4;}
}
return num;//返回值为num
}
void scon_init()//初始化串口子函数
{
TMOD = 0x20;
TH1 = 0xfd;
TL1 = 0xfd;
TR1 = 1;
SM0 = 0;
SM1 = 1;
EA = 1;
ES = 1;
}
void main()
{
scon_init();//初始化串口
while(1)
{
send(key_scan());//不断扫描并发送按键值
}
}
#include
#define uint unsigned int
#define uchar unsigned char
uchar code tab[]={//数码管字形
0x40,0x79,0x24,0x30,
0x19,0x12,0x02,0x78,
0x00,0x10,0x08,0x03,
0x46,0x21,0x06,0x0e};
void scon_init()//串口寄存器初始化
{
TMOD = 0x20;
TH1 = 0xfd;
TL1 = 0xfd;
TR1 = 1;
REN = 1;//留意REN的方位
SM0 = 0;
SM1 = 1;
EA = 1;
ES = 1;
}
void main()
{
scon_init();//调用串口初始化子函数
while(1);//等候串口中止
}
void ser() interrupt 4//串口中止子函数
{
uchar a;
RI = 0;
a = SBUF;//将接收到在SBUF中的key值送给变量a
P2 = tab[a];//在串口中止中显现字形
}
声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/yingyong/261226.html