AT89S52两单片机之间的通信 C程序
时间:01-20 15:27 阅读:740次
*温馨提示:点击图片可以放大观看高清大图
简介:本文给大家分享了一个AT89S52两单片机之间的通信 C程序。
#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;x for(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]; //在串口中断中显示字形
}