Single chip microcomputer realizes point-to-point data transmission-serial communication

Publisher:hzx312895379Latest update time:2016-09-07 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
When the single-chip microcomputer is controlling local peripherals, information exchange is carried out through an 8-bit parallel data bus. However, in larger-scale modern control systems, the single-chip microcomputer also needs to control remote devices. The serial communication module of the single-chip microcomputer can be used. After learning the principles and timing of RS-232 serial communication, we learned to use the RS-232 interface to realize point-to-point communication of serial data transmission.

 

Main components:

1. AT89C52 single-chip microcomputer chip, used to control serial port communication.

2. Interface level conversion chip MAX3232 is used to realize the conversion between TTL level and RS-232 level.

 

Test flow chart: host part                             


 

 Slave part:


 

Test circuit diagram:
 

Test program code:

//PPDataR.h program

#ifndef _PPDATAR_H // Prevent PPDataT.h from being referenced repeatedly

 

#define _PPDATAR_H

 

#include

#include

 

#define uchar unsigned char

#define uint unsigned int

 

/* Handshake signal macro definition */

#define CALL 0x24 // Host call

#define BUSY 0x15 // Slave is busy

#define OK 0x00 // Slave is ready

#define SUCC 0x2A // Receive successfully

#define ERR 0xF0 // Receive error

 

#define MAXLEN 64 // Maximum length of the buffer

 

uchar buf[MAXLEN];

 

#endif

 

//PPDataR.c program

#include "PPDataR.h"

 

/* Send data function */

uchar recvdata(uchar *buf)

{

       uchar i,tmp;

       uchar len; // Save data length

       uchar ecc; // Save the check byte

      

       /* Receive data length bytes */

       RI = 0;

       while(!RI);

       len = SBUF;

       RI = 0;

      

       /* Use the value of len to initialize the check byte ecc */

       ecc = len;                                  

 

       /* Receive data */

       for (i=0;i

       {

              while(!RI);

              *buf = SBUF;    

              ecc = ecc^(*buf); // Perform byte check

              RI = 0;      

              buf++;             

       }

       *buf = 0; // indicates the end of data

      

       /* Receive checksum byte */

       while(!RI);

       tmp = SBUF;

       RI = 0;

 

       /* Perform data verification */

       ecc = tmp^ecc;

       if (ecc!=0) // If the check is wrong

       {

              *(buf-len) = 0; // Clear the data buffer

             

              /* Send verification error signal ERR */

              TI = 0;      

              SBUF = ERR;

              while(!TI);

              TI = 0;

 

              return 0xff; // Return 0xff to indicate verification error

       }

 

       /* Send verification success signal SUCC */

       TI = 0;

       SBUF = SUCC;

       while(!TI);

       TI = 0;

 

       return 0; // Verification successful, return 0

}

 

void init_serial()

{

       TMOD = 0x20; // Timer T1 uses working mode 2

       TH1 = 250;

       TL1 = 250;

       TR1 = 1; // Start timing

       PCON = 0x80; // SMOD = 1

       SCON = 0x50; // Working mode 1, baud rate 9600kbit/s, receiving is allowed

}

 

/* Main program */

void main()

{

       uchar tmp = 0;

       /* Serial port initialization */

       init_serial();

 

       EA = 0; // Disable all interrupts

 

       while(1)

       {

              /* If the received data is not CALL, continue waiting */

              while (tmp!=CALL)

              {

                     RI = 0;

                     while(!RI)

                     tmp = SBUF;

                     RI = 0;

              }

      

              /* Check the P0 port to determine whether it is currently busy. If P0=0xBB, it is busy */

              P0 = 0xff;

              tmp = P0;

              if(tmp==0xBB) // If the P0 port is 0xBB, send a BUSY signal

              {

                     TI = 0;

                     SBUF = BUSY;

                     while(!TI);

                     TI = 0;

                     continue;

              }

 

              /* Otherwise, send an OK signal, indicating that the slave can receive data */

              TI = 0;                           

              SBUF = OK;

              while(!TI);

              TI = 0;

 

              /* Data receiving */

              tmp = 0xff;

              while(tmp==0xff)

              {

                     tmp = recvdata(buf); // If verification fails, 0xff is returned; if reception succeeds, 0 is returned

              }

       }

}

Keywords:MCU Reference address:Single chip microcomputer realizes point-to-point data transmission-serial communication

Previous article:Single chip microcomputer realizes short distance wireless transmission
Next article:Realizing Intelligent Signal Generator Using Integrated Chip MAX038

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号