#define uint unsigned int
#define uchar unsigned char
sbit key1 = P1^0;//按鍵位定義
sbit key2 = P1^1;
sbit key3 = P1^2;
sbit key4 = P1^3;
本文引用地址:http://m.butianyuan.cn/article/201611/320335.htmvoid send(uchar key_num)//通過串口發(fā)送按鍵值子函數(shù)
{
SBUF = key_num;
while(!TI);
TI = 0;
}
void delay(uint z)//延時(shí)_ms子函數(shù)
{
uint x,y;
for(x = 0;xfor(y = 0;y<125;y++);
}
uint key_scan()//鍵盤掃描子函數(shù),帶返回值num
{
uint num;
if(key1 == 0)//鍵1按下則數(shù)碼管將顯示1
{
delay(5);
if(key1 ==0)
{num = 1;}
}
if(key2 == 0)//鍵2按下則數(shù)碼管將顯示2
{
delay(5);
if(key2 ==0)
{num = 2;}
}
if(key3 == 0)//鍵3按下則數(shù)碼管將顯示3
{
delay(5);
if(key3 ==0)
{num = 3;}
}
if(key4 == 0)//鍵4按下則數(shù)碼管將顯示4
{
delay(5);
if(key4 ==0)
{num = 4;}
}
return num;//返回值為num
}
void scon_init()//初始化串口子函數(shù)
{
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());//不停掃描并發(fā)送按鍵值
}
}
#include
#define uint unsigned int
#define uchar unsigned char
uchar code tab[]={//數(shù)碼管字形
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();//調(diào)用串口初始化子函數(shù)
while(1);//等待串口中斷
}
void ser() interrupt 4//串口中斷子函數(shù)
{
uchar a;
RI = 0;
a = SBUF;//將接收到在SBUF中的key值送給變量a
P2 = tab[a];//在串口中斷中顯示字形
}
評(píng)論