双机串口通信

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33248019/article/details/78426030

内容描述:

有A.B两个机,以方式一进行串口通信,其中A机发送信息,B机接收信息。双方的晶振频率为11.0592MHZ,通信波特率为9600。
通信协议:通信开始时,A机首先发送一个启动信号AA,B机接收到后发送一个应答信号BB表示同意接收。

附仿真电路图:
这里写图片描述

C语言:

#include <reg52.h>
#define uchar   unsigned char
void time(unsigned int _time);      //延时函数
void InitArray();                   //初始化数组
void SendData();                    //发送数据
void Receive();                     //接收数据
sbit    P10=P1^0;
sbit    P17=P1^7;
uchar databuff[32]      _at_ 0x30;
uchar bufR[32]          _at_ 0x50;

void InitArray()
{
    uchar i;
    for(i=0;i<32;i++)
    databuff[i]=i;
}

void init()
{
    SCON=0X50;                      //串口工作在方式1
    PCON=0;                         //波特率不加倍
    TMOD=0X20;
    TH1=0XFd;
    TL1=0XFd;
    TCON=0X40;
}

void SendData()
{
    uchar jiaoyan;                  //校验和
    uchar i;
    leap:
        P2=0XAA;                   //显示AA,表示就绪,等待它机
        SBUF=0XAA;  
        while(TI==0);
        TI=0;
        while(RI==0);
        RI=0;
        if(SBUF!=0xbb)  goto leap;
        P2=0XBB;
        time(20);
    leap1:jiaoyan=0;
    //发送数据
        for(i=0;i<32;i++)
    {
        P2=databuff[i];
        time(200);
        SBUF=databuff[i];
        jiaoyan+=databuff[i];
        while(TI==0);
            TI=0;
    }
    if(P17)
    {
        P2=jiaoyan;
        time(200);
        SBUF=jiaoyan;
    }
    else
    {
        jiaoyan++;
        P2=jiaoyan;
        time(200);
        SBUF=jiaoyan;
    }
        while(TI==0);
        TI=0;
        while(RI==0);
        RI=0;
        P2=SBUF;
        time(200);
        //校验是否正确
    if(SBUF==0) goto leap1;
}

void Receive()
{
    uchar i,pf;
    do{
        //等待接收数据
        while(RI==0);RI=0;
        P2=SBUF;
        time(200);
    }while((SBUF^0XAA)!=0);
    P2=0XBB;
    time(200);
    SBUF=0XBB;
    while(TI==0);TI=0;
    while(1)
    {
        pf=0;
        for(i=0;i<32;i++)
    {
        //接收所有传递过来的数据
        while(RI==0);
        RI=0;
        bufR[i]=SBUF;       
        P2=bufR[i];
        time(200);
        pf+=bufR[i];
    }
    P2=SBUF;
    //判断接收数据是否等于校验和
    if((SBUF^pf)==0)
    {
        P2=0;
        time(200);
        SBUF=0X00;
        while(TI==0);TI=0;
    }
    else
    {
        P2=0XFF;
        time(200);
        SBUF=0XFF;
        while(TI==0);TI=0;
    }
}
    }

void main()
{

    uchar count=0;
    time(1);
    init();
    InitArray();
    if(P10)
    {                       
        SendData();         //传送端
    }
    else
    {
        Recive();          //接收端
    }
    while(1);
}

void time(unsigned int _time)
{
    uchar ucCounter,i;
    for(ucCounter=0;ucCounter<110;ucCounter++)
    for(i=0;i<_time;i++);

}

汇编:

ORG 0000H
BUFS    EQU 30H
BUFR    EQU 50H
NUMB    EQU 10H
SUMS    EQU 7EH
SUMR    EQU 7FH
MAIN: 
    LCALL InitArray
    LCALL DELAY
    LCALL InitUart 
MAIN2: 
        JB P1.0,SEND
        LCALL RECEIVE 
HERE:   SJMP HERE
SEND:   LCALL SEND1
HERE1:  SJMP HERE1
InitArray: 
    MOV R2,#NUMB
    MOV R0,#BUFS
    MOV A,#00H
LOOP: 
    MOV @R0,A
    INC R0  
    INC A       
    DJNZ    R2,LOOP
    RET 
InitUart:
    MOV SCON,#50H
    MOV TMOD,#20H;
    MOV PCON,#00H; 
    MOV TH1,#0FDH 
    MOV TCON,#40H; 
    RET
SEND1:
    MOV P2,#0AAH    
    MOV SBUF,#0AAH 
LOOP1: JNB TI,LOOP1
        CLR TI
LOOP2:
    JNB RI,LOOP2 
    CLR RI
    MOV A,SBUF
    CJNE A,#0BBH,LOOP2 
    MOV P2,#0BBH 
    LCALL DELAY
SEND2:
    MOV SUMS,#00H 
    MOV R0,#BUFS 
    MOV R2,#NUMB
LOOP3:  
    MOV A,@R0 
    MOV P2,A
    LCALL DELAY 
    MOV SBUF,A
LOOP4: 
    JNB TI,LOOP4
    CLR TI
    ADD A,SUMS 
    MOV SUMS,A 
    INC R0 
    DJNZ R2,LOOP3
LOOP5: 
    JB P1.7,LOOP6
    ADD A,#01H 
    MOV SUMS,A
LOOP6: 
    MOV P2,A
    LCALL DELAY 
    MOV SBUF,A
LOOP7: 
    JNB TI,LOOP7
    CLR TI 
LOOP8: 
    JNB RI,LOOP8
    CLR RI
    MOV P2,SBUF 
    LCALL DELAY 
    MOV A,SBUF
    CJNE A,#00H,LOOP9
ENDS: RET
LOOP9: 
LJMP SEND2
RECEIVE:
    JNB RI,RECEIVE 
    CLR RI
    MOV P2,SBUF 
    LCALL DELAY 
    MOV A,SBUF
    CJNE A,#0AAH,RECEIVE 
    MOV P2,#0BBH
    LCALL DELAY
    MOV SBUF,#0BBH 
LOOP11: 
    JNB TI,LOOP11
    CLR TI
LOOP12: 
    MOV SUMR,#00H
    MOV R1,#BUFR
    MOV R3,#NUMB
LOOP13: 
    JNB RI,LOOP13
    CLR RI  
    MOV P2,SBUF
    LCALL DELAY
    MOV A,SBUF
    MOV @R1,A
    ADD A,SUMR
    MOV SUMR,A
    INC R1  
    DJNZ R3,LOOP13
    MOV P2,SUMR
LOOP14: 
    JNB RI,LOOP14
    CLR RI  
    MOV P2,SBUF
    LCALL   DELAY
    MOV A,SBUF
    CJNE A,SUMR,LOOP16
    MOV P2,#00H
    LCALL   DELAY
    MOV SBUF,#00H
LOOP15: 
    JNB TI,LOOP15
    CLR TI  
    RET 
LOOP16: 
    MOV P2,#0FFH
    LCALL   DELAY
    MOV SBUF,#0FFH
LOOP17: 
    JNB TI,LOOP17
    CLR TI
    LJMP LOOP12
DELAY:
    MOV R7,#04H
    DL: MOV R5,#0FFH
    DL1: MOV R5,#0FFH
    DL5: DJNZ R5,DL5
    DJNZ R6,DL1
    DJNZ R7,DL
    RET 
    END 

效果图:

这里写图片描述

阅读更多
换一批

没有更多推荐了,返回首页