proteus與keil Cx51的單片機(jī)仿真(串行口單工通信2)
電路圖:
本文引用地址:http://butianyuan.cn/article/201611/320717.htm
U1程序:
#include
sbit p=PSW^0;
unsigned char receive(void)//接收一個(gè)字節(jié)數(shù)據(jù)
{
unsigned char dat;
while(RI==0);
RI=0;//只要接收中斷標(biāo)志位RI沒有被置1,等待,直至接收完(RI1)
ACC=SBUF;//將接收緩沖器中的數(shù)據(jù)存于dat
if(RB8==P)//奇校驗(yàn)
{dat=ACC;return dat;}
}
void main(void)
{
unsigned char i;
TMOD=0x20;//定時(shí)器T1工作于方式2
SCON=0xd0;//SCON=1101 0000B,串行口工作方式1,允許接收(REN=1)
PCON=0x00;//PCON=0000 0000B,波特率9600
TH1=0xfd;TL1=0xfd;//根據(jù)規(guī)定給定時(shí)器T1賦初值
TR1=1;//啟動(dòng)定時(shí)器T1
REN=1;//允許接收
while(1)
{
for(i=0;i<9;i++)P1=receive();//將數(shù)據(jù)送P1口顯示
for(i=0;i<9;i++)P0=receive();//將數(shù)據(jù)送P0口顯示
for(i=0;i<9;i++)P2=receive();//將數(shù)據(jù)送P2口顯示
}
}
U2程序:
#include
sbit p=PSW^0;
unsigned char code Tab[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff};//流水燈燈控制碼,該數(shù)據(jù)被定義為全局變量
void send(unsigned char dat)//向PC發(fā)一個(gè)字節(jié)數(shù)據(jù)
{
ACC=dat;
TB8=p;
SBUF=dat;
while(TI==0);//發(fā)送等待
TI=0;
}
void delay(void)//延時(shí)約150ms
{
unsigned char m,n;
for(m=0;m<200;m++)for(n=0;n<250;n++);
}
void main(void)
{
unsigned char i;
TMOD=0x20;//TMOD=0010 0000B,定時(shí)器T1工作于方式2
SCON=0xc0;//SCON=1100 0000B,串行口工作方式3,SM2置0,不使用多機(jī)通信,TB8置0
PCON=0x00;//PCON=0000 0000B,波特率9600
TH1=0xfd;TL1=0xfd;//給定時(shí)器T1賦初值
TR1=1;//啟動(dòng)定時(shí)器T1
while(1)
{
for(i=0;i<27;i++)//模擬檢測數(shù)據(jù)
{
send(Tab[i]);//發(fā)送數(shù)據(jù)i
delay();//50ms發(fā)送一次檢測數(shù)據(jù)
}
}
}
評論