#define uint unsigned int
#define uchar unsigned char
uint dat;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,
0x7d,0x07,0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x00}; //0-f 共阴极数码管段选
/*串口初始化*/
void init(void)
{
TMOD = 0x20; //T1定时器 工作方式2
SM0 = 0;
SM1 = 1; //串口方式1
TH1 = 0xfa;
TL1 = 0xfa; //T1定时器 装初值
// EA = 1;
// ES = 1;
TR1 = 1; //T1定时器 开
REN = 1;
}
/*接收数据*/
void RI_UART()
{
dat = SBUF;
while(!RI);
RI = 0;
}
/*发送数据*/
void TI_UART(bit DATA)
{
SBUF = DATA;
while(!TI);
TI = 0;
}
/*主函数*/
void main()
{
init() ; //初始化
while(1)
{
RI_UART(); //接收数据
P2 = table[dat]; //数码管显示数据
TI_UART(0);
//delay(50);
}
}
/*发送数据单片机A*/
#include
#include
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,
0x7d,0x07,0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x00}; //0-f 共阴极数码管段选
***it beep = P2^3; //位定义蜂鸣器
bit dat;
uint key;
***it dula = P2^6;
***it wela = P2^7;
***it led1 = P1^0;
/*延时函数*/
void delay(uint x)
{
uint a,b;
for(a=x;a>0;--a)
for(b=0;b<110;++b);
}
/*串口初始化*/
void init(void)
{
TMOD = 0x20; //T1定时器 工作方式2
SM0 = 0;
SM1 = 1; //串口方式1
TH1 = 0xfa;
TL1 = 0xfa; //T1定时器 装初值
// EA = 1;
// ES = 1;
TR1 = 1; //T1定时器 开
REN = 1;
}
/*键盘扫描程序 */
void keyscan(void) //计算出key值
{
uint a;
P3=0xfe;
a=P3;
//a=a&0xf0;
if(a!=0xfe)
{
delay(10);
a=P3;
switch(a) //扫描第一行
{
case 0xee:
key=0;
break;
case 0xde:
key=1;
break;
case 0xbe:
key=2;
break;
case 0x7e:
key=3;
break;
}
}
P3=0xfd;
a=P3;
//a=a&0xf0;
if(a!=0xfd) //扫描第二行
{
delay(10);
a=P3;
switch(a)
{
case 0xed:
key=4;
break;
case 0xdd:
key=5;
break;
case 0xbd:
key=6;
break;
case 0x7d:
key=7;
break;
}
}
P3=0xfb; //扫描第三行
a=P3;
//a=a&0xf0;
if(a!=0xfb)
{
delay(10);
a=P3;
switch(a)
{
case 0xeb:
key=8;
break;
case 0xdb:
key=9;
break;
case 0xbb:
key=10;
break;
case 0x7b:
key=11;
break;
}
}
P3=0xf7;
a=P3;
//a=a&0xf0;
if(a!=0xf7) //扫描第四行
{
delay(10);
a=P3;
switch(a)
{
case 0xe7:
key=12;
break;
case 0xd7:
key=13;
break;
case 0xb7:
key=14;
break;
case 0x77:
key=15;
break;
}
}
}
/*发送数据*/
void TI_UART(void)
{
SBUF = key;
while(!TI);
TI = 0;
}
/*接收数据*/
void RI_UART()
{
while(!RI);
dat = SBUF;
//while(!RI);
RI = 0;
//return(dat);
}
void main()
{
wela = 1; //位选开
P0 = 0; //8位全选
wela = 0; //位选关
dula = 1; //段选开
init(); //初始化
while(1)
{
keyscan(); //键盘扫描
P0 = table[key]; //数码管显示
//TI_UART(); //发送数据
SBUF = key;
while(!TI);
//RI_UART(); //接收数据
dat = SBUF;
while(!RI);
//_nop_();
beep = dat; //蜂鸣器响
//led1= dat;
delay(500);
beep = 1;
//led1 = 1;
delay(500);
}
}
void UART()interrupt 4
{
if(TI==1)
{
TI = 0;
}
if(RI==1)
{
RI = 0;
}
}
/* 接收数据端 单片机B */
#include
//#include
#define uint unsigned int
#define uchar unsigned char
uint dat;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,
0x7d,0x07,0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x00}; //0-f 共阴极数码管段选
/*串口初始化*/
void init(void)
{
TMOD = 0x20; //T1定时器 工作方式2
SM0 = 0;
SM1 = 1; //串口方式1
TH1 = 0xfa;
TL1 = 0xfa; //T1定时器 装初值
// EA = 1;
// ES = 1;
TR1 = 1; //T1定时器 开
REN = 1;
}
/*接收数据*/
void RI_UART()
{
dat = SBUF;
while(!RI);
RI = 0;
}
/*发送数据*/
void TI_UART(bit DATA)
{
SBUF = DATA;
while(!TI);
TI = 0;
}
/*主函数*/
void main()
{
init() ; //初始化
while(1)
{
RI_UART(); //接收数据
P2 = table[dat]; //数码管显示数据
TI_UART(0);
//delay(50);
}
}
举报