?? 按鍵控制雙機通訊.txt
字號:
#include "reg51.h"
#include "string.h"
sbit P34=P3^4;
sbit P35=P3^5;
sbit P36=P3^6;
sbit P37=P3^7;
main()
{ unsigned int i,j,s=0;
TMOD=0x20; // 初始化,波特率1200
TH1=230;
TL1=230;
TR1=1;
SCON=0xd0;
PCON=0;
for(;;)
{ if(P34==0)
{ for(i=5;i>0;i--)
for(j=248;j>0;j--);
if(P34==0)
{ SBUF=P34;
while(TI!=1);
TI=0;
while(RI!=1);
RI=0;
s++;
SBUF=s;
while(TI!=1);
TI=0;
while(RI!=1);
RI=0;
if(s==3)
s=0;
}
while(P34==0);
}
}
}
從機接收程序:
#include "reg51.h"
#include "string.h"
sbit P34=P3^4;
main()
{ unsigned int i=0,j=0,second=0,count=0;
unsigned char smg[]={0xfe,0xfd},display[2];
TMOD=0x22; // 初始化,波特率1200,定時器1用于設(shè)定波特率;定時器0用于設(shè)初值
TH0=6;
TL0=6;
TH1=230;
TL1=230;
TR1=1;
TR0=1;
SCON=0xd0;
PCON=0;
EA=1;ET1=1;ET0=1;
for(;;)
{ while(RI!=1);
RI=0;
while(SBUF^0==0)
{ while(RI!=1);
RI=0;
if(SBUF==1)
TR0=1;
while(RI!=1);
RI=0;
if(SBUF==2)
TR0=0;
while(RI!=1);
RI=0;
if(SBUF==3)
{ P2=0xfb;
P0=0;
TR0=1;
}
}
}
}
void time() interrupt 1 using 0 //計數(shù)部分
{ count++;
if(count==4000)
{count=0;
second++;
if(second==60)
second=0;
display[0]=second%10;
display[1]=second/10;
}
TH0=6;
TL0=6;
}
void xianshi() interrupt 3 //顯示部分
{ i++;
if(i==60)
{ i=0;
P2=smg[j];
P0=display[j];
j++:
if(j==2)
j=0;
}
TH1=230;
TL1=230;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -