51单片机多机通信Word文档下载推荐.docx
《51单片机多机通信Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《51单片机多机通信Word文档下载推荐.docx(12页珍藏版)》请在冰点文库上搜索。
ucharmiao=0,fen=0,shis=0;
/******************************
共阴数码管0——15译码表
****************************************/
uchartable[]={
0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f,0X40};
voiddelay(uintj)
{
uchari;
for(;
j>
0;
j--)
for(i=110;
i>
i--);
}
voidHC138(uchari)
a=i&
0x01;
b=i&
0x02;
c=i&
0x04;
voidxianshi(intk,longtemp)//八位数字数码管显示函数
{
uintj,ge,shi;
for(j=0;
j<
1;
j++)
shi=temp%100/10;
ge=temp%10;
HC138(k);
P2=table[shi];
delay
(2);
HC138(k+1);
P2=table[ge];
}
}
voidsend(ucharc)//甲机发送字符
SBUF=c;
while(TI==0);
TI=0;
voidkey()
if(K1==0)
{
delay(5);
{
while(!
K1);
miao=miao+1;
if(miao>
59){miao=0;
}
}
if(K2==0)
delay(5);
if(K2==0)
{
K2);
fen=fen+1;
if(fen>
59){fen=0;
}
}
if(K3==0)
{
if(K3==0)
K3);
shis++;
if(shis>
23){shis=0;
if(K4==0)
if(K4==0)
K4);
miao--;
if(miao==1){miao=2;
if(K5==0)
if(K5==0)
K5);
fen--;
if(fen<
0){fen=0;
if(K6==0)
if(K6==0)
K6);
shis--;
if(shis<
0){shis=0;
voidmain()
TH0=(65536-5000)/256;
TL0=(65536-5000)%256;
EA=1;
ET0=1;
TR0=1;
SCON=0x50;
//设置串口方式1允许控制位1
PCON=0x00;
//波特率不倍增
TMOD=0x21;
//定时器1设置工作方式2
TH1=0xfd;
//波特率设定9600
TL1=0xfd;
TI=RI=0;
TR1=1;
//启动定时器T1
EA=1;
delay(100);
while
(1)
key();
//
/*****************本机显示************************************/
xianshi(6,miao);
HC138
(2);
P2=table[10];
//分隔符号
xianshi(3,fen);
HC138(5);
xianshi(0,shis);
/*************************************************************/
send(shis);
send(fen);
send(miao);
if(K8==0)
delay(3);
if(K8==0)
while(!
K8);
while
(1)
/***********************************************************************/
if(flag==1)
flag=0;
miao++;
if(miao>
fen++;
if(fen>
59){fen=0;
shis++;
if(shis>
23){shis=0;
send(shis);
}
xianshi(6,miao);
xianshi(3,fen);
/********************************************************************/
}
}
}
/************************************************/
voidtimer0(void)interrupt1
TH0=(65536-5000)/256;
TL0=(65536-5000)%256;
count++;
if(count>
=200)//1S时间
count=0;
flag=1;
从机程序
sbita=P1^5;
sbitb=P1^6;
sbitc=P1^7;
uintj,n=1;
uintmiao=0,fen=0,shis=0;
longshijian=0;
voidxianshi(intk,longtemp)
//串口模式1,允许接收
TMOD=0x20;
//T1工作模式2
//波特率设定:
9600
IE=0x90;
//允许串口中断1001
{
xianshi(0,miao);
xianshi(6,shis);
voidserial_int()interrupt4//乙机串口接收中断函数
{if(RI)
{RI=0;
if(n==1){miao=SBUF;
if(n==2){fen=SBUF;
if(n>
=3){shis=SBUF;
n=0;
n++;
}