Multi-machine serial communication and machine serial communication

Source: Internet
Author: User

Multi-machine serial communication and machine serial communication
★Devices Used

Three 80c51 single-chip microcomputer are used, in which U1 controls the other two slave machines U2 and u3. Each single-chip microcomputer has a digital tube to display data. The host has two keys, KEY_1 and KEY_2, respectively, to control different slave servers.

★Goals

The main goal is to understand their protocols and write simple protocols through multi-host communication! This program mainly achieves the following results: the host can control the slave machine by sending commands: send data to and receive data from the slave machine. Then, the data displayed on the slave or host is displayed on the digital tube.

★Protocol requirements 1. Address: the host address is set to 0x01, the slave machine 1 (U3) address is 0x03, and the slave machine 2 (U2) 0x022, command:

0x01 indicates that the host sends data to the slave

0x02 indicates that the number of dramas sent from the slave to the host

3. basic steps:

● When the host sends an address frame, after receiving the address frame from the host, It will compare it with the local address. If the address frame is correct, it will send its own address to respond. If the address frame is incorrect, the data will be discarded, continue monitoring and waiting for data;

● After the Receiving address is correct, the host returns its own address as the response signal. If the data received by the host is the same as the address data sent by the host, the command will be sent (whether to allow the slave machine to receive data or send data); if it is different from the address data sent by itself, it will send 0xff, when RB8 = 1 is obtained from the server, the system continues monitoring and waiting for data.

● When the host sends a command, after receiving the command from the host, it will return the received command as a response (no exception is handled). When the host detects that the response data is correct, it will send data, wait for the response signal from the slave machine (0x11), receive the response signal, and then send the second data (the host does not handle exceptions in this region) to instruct the sending of data to end.

● After the data is sent and received, the host sends a Checksum, Which is compared with the checksum of the data received from the slave. If the checksum is the same, it indicates that the data is sent correctly and then exits the program; if the verification and sending are incorrect, the data will be resent.

4. Specific implementation

The serial port has a very important register:


SM2 is a multi-host communication control bit. When the slave machine SM2 is 1, the received RB8 can be used to control the RI. When RB8 = 1 receives data and RI = 1, when RB8 = 0, it does not accept data. When SM2 = 0, it receives data regardless of whether RB8 is 1, and set RI to 1. Therefore, we can differentiate addresses and Data Based on this feature. The slave machine first sets SM2 to 1, and then sets TB8 to 1 for the data to be sent. After the slave machine receives the data, it will compare whether the address is the same as the local address, if it is the same, set SM2 to 0 and then send the data (TB = 0). Other slave machines are still in the SM = 1 status and cannot receive the data.
★Circuit Diagram


★Program Implementation Host:

# Include <reg52.h> # define uint unsigned int # define uchar unsigned charsbit key_1 = P1 ^ 0; sbit key_2 = P1 ^ 1; sbit Led = P1 ^ 7; # define master_U1 0x01 // host address # define slave_U2 0x02 // slave address # define slave_U3 0x03 // ar tbuf [16] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79,0x71}; uchar rbuf [16]; // array of data to be received void DelayUs2x (unsigned char t) {while (-- t);} void DelayMs (unsigned Char t) {while (t --) {// approximate latency: 1 mS DelayUs2x (245); DelayUs2x (245) ;}} void Uart_init (void) {SCON | = 0xf8; // Working Mode 1, T1, R1 0, allowing the serial port to receive data TMOD | = 0x20; // timer 1, Mode 2 TH1 = 0xFD; TL1 = 0xFD; TR1 = 1; EA = 1; // elasticsearch = 1; // enable Serial Port Interrupt} void master_send (uchar addr, uchar command) {uchar status, check, I = 0; SBUF = addr; // Send the address to be operated while (! TI); DelayMs (10); TI = 0; // send slave address while (! RI); RI = 0; // wait for the slave address to reply if (addr! = SBUF) // If the returned address is different from the address to be operated {SBUF = 0xff; while (! TI); TI = 0;} else {TB8 = 0; SBUF = command; while (! TI); TI = 0; // send the command while (! RI); RI = 0; // wait for the slave to reply status = SBUF; // send the confirmation command from the slave if (status = 0x80) {TB8 = 1 ;} else {if (status = 0x01) // when the host knows that the slave is ready to receive data, it enters the sending status {while (1) {check = 0; for (I = 0; I <16; I ++) {SBUF = tbuf [I]; while (! TI); TI = 0; while (! RI); // wait for the return confirmation signal from the slave machine 0x11 RI = 0; check + = tbuf [I]; // checksum DelayMs (2000);} SBUF = check; // send the checksum and while (! TI); // send the checksum to the slave address TI = 0; while (! RI); RI = 0; // receives data returned from the address (0x00 or 0xff) if (SBUF = 0x00) {break ;} // if 0x00 is received, it indicates that the verification is correct, and the sending function is displayed. if 0xff is received, it indicates that the verification is incorrect. resend} if (status = 0x02) // The host knows that it wants to receive data from the slave. {while (1) {check = 0; for (I = 0; I <16; I ++) {while (! RI); RI = 0; rbuf [I] = SBUF; P2 = rbuf [I]; SBUF = 0x11; // send 0x11while (! TI); // indicates that the validation TI = 0 after receiving the data; check + = rbuf [I];} while (! RI); RI = 0; if (check = SBUF) // if the host data checksum is equal to the slave data checksum, the host 0x00 {// otherwise, you need to wait for receiving data again. SBUF = 0x00; while (! TI); TI = 0; P2 = 0; break ;}}}} void main () {Uart_init (); P2 = 0; Led = 1; if (! Key_1) {DelayMs (20); if (! Key_1) {master_send (slave_U2, 0x02) ;}} if (! Key_2) {DelayMs (20); if (! Key_2) {master_send (slave_U3, 0x01) ;}led = 0 ;}}

Slave 1:

/** Slave machine 1 U3 **/# include <reg52.h> # define uint unsigned int # define uchar unsigned char // # define slave_U2 0x02 # define slave_U3 0x03sbit key = P1 ^ 0; static uchar tbuf [] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79,0x71}; static uchar rbuf [16]; void Uart_receive (); void Send_receive (); void DelayUs2x (unsigned char t) {while (-- t);} void DelayMs (unsigned char t) {while (t --){// Approximate latency: 1 mS DelayUs2x (245); DelayUs2x (245) ;}} void Uart_init () // serial port initialization {PCON = 0; SCON | = 0xf8; TMOD | = 0x20; TH1 = 0xfd; TL1 = 0xfd; TR1 = 1; EA = 1; ES = 1;} void main () {P2 = 0; uart_init (); while (1);} void Ser_uart () interrupt 4 {uchar save; RI = 0; ES = 0; if (SBUF! = Slave_U3) {ES = 1; goto end; // If the sent slave address does not match the current slave address, run this statement .} SM2 = 0; // set to click mode SBUF = slave_U3; // send the slave address to the host while (! TI); TI = 0; while (! RI); // receives the command RI = 0 from the host; if (RB8 = 1) // if the returned address is incorrect, 0xff, RB8 = 1 is sent; {SM2 = 1; ES = 1; goto end; // enter the waiting address frame mode} save = SBUF; // save the received command in save if (save = 0x01) // if the received command is 0x01 {SBUF = 0x01; // send the 03 message to the host, which indicates confirmation. You are ready for the while (! TI); TI = 0; Uart_receive (); // enter the receiving status wait} else {if (save = 0x02) {SBUF = 0x02; // tell the host that the while (! TI); TI = 0; Send_receive (); // enter the data sending function} else {SBUF = 0x80; while (! TI); TI = 0; SM2 = 1; ES = 1; goto end ;}end :;} void Uart_receive () {uchar check, I; while (1) {check = 0; for (I = 0; I <16; I ++) {while (! RI); RI = 0; rbuf [I] = SBUF; P2 = rbuf [I]; // display received data SBUF = 0x11; // send 0x11while (! TI); // indicates that the validation TI = 0 after receiving the data; check + = rbuf [I];} while (! RI); RI = 0; if (SBUF = check) // if the host data checksum is equal to the slave data checksum, returns the host 0x00 {SBUF = 0x00; while (! TI); TI = 0; P2 = 0; break;} else // if the host data checksum is not the same as the Slave Data checksum, resend the data {SBUF = 0xff; while (! TI); TI = 0 ;}}void Send_receive () {uchar check, I; while (1) {check = 0; for (I = 0; I <11; I ++) {SBUF = tbuf [I]; while (! TI); TI = 0; check + = tbuf [I];} SBUF = check; while (! TI); // send verification data TI = 0; while (! RI); RI = 0; // wait for the verification result to receive the data (0x00 or 0xff) returned by the Master Address if (SBUF = 0x00) break; // If 0x00 is received, it indicates that the verification is correct, and the sending function is displayed. If 0xff is received, it indicates that the verification is incorrect. resend} while (1) {for (I = 0; I <16; I ++) P2 = rbuf [I]; DelayMs (1000 );}}

Slave 2 ::

/** Slave 2, circuit Diagram U2 **/# include <reg52.h> # define uint unsigned int # define uchar unsigned char # define slave_U2 0x02 // # define slave_U3 0x03sbit Key = P1 ^ 0; static uchar tbuf [] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79,0x71}; static uchar rbuf [16]; void Uart_receive (); void Send_receive (); static uchar rbuf [16]; void DelayUs2x (unsigned char t) {while (-- t);} void DelayMs (unsigne D char t) {while (t --) {// approximate latency: 1 mS DelayUs2x (245); DelayUs2x (245) ;}} void Uart_init () // serial port initialization {PCON = 0; SCON | = 0xf8; TMOD | = 0x20; TH1 = 0xfd; TL1 = 0xfd; TR1 = 1; EA = 1; ES = 1 ;}void main () {P2 = 0; Uart_init (); while (1) ;}void Ser_uart () interrupt 4 {uchar save; RI = 0; ES = 0; if (SBUF! = Slave_U2) {ES = 1; goto end; // If the sent slave address does not match the current slave address, run this statement .} SM2 = 0; // set to click mode SBUF = slave_U2; // send the slave address to the host while (! TI); TI = 0; while (! RI); // receives the command RI = 0 from the host; if (RB8 = 1) // if the returned address is incorrect, 0xff, RB8 = 1 is sent; {SM2 = 1; ES = 1; goto end; // enter the waiting address frame mode} save = SBUF; // save the received command in save if (save = 0x01) // if the received command is 0x01 {SBUF = 0x01; // send 01 to the host, which indicates confirmation. You are ready. while (! TI); TI = 0; Uart_receive (); // enter the receiving status wait} else {if (save = 0x02) {SBUF = 0x02; // tell the host that the while (! TI); TI = 0; Send_receive (); // enter the data sending function // DelayMs (1000);} else {SBUF = 0x80; while (! TI); TI = 0; SM2 = 1; ES = 1; goto end ;}end :;} void Uart_receive () {uchar check, I; while (1) {check = 0; for (I = 0; I <11; I ++) {while (! RI); RI = 0; tbuf [I] = SBUF; SBUF = 0x11; // send 0x11while (! TI); // indicates that the validation TI = 0 after receiving the data; check + = tbuf [I];} while (! RI); RI = 0; if (SBUF = check) // if the host data checksum is equal to the slave data checksum, returns the host 0x00 {SBUF = 0x00; while (! TI); TI = 0; break;} else // if the host data checksum is not the same as the Slave Data checksum, resend the data {SBUF = 0xff; while (! TI); TI = 0 ;}}void Send_receive () {uchar check, I; while (1) {check = 0; for (I = 0; I <16; I ++) {SBUF = tbuf [I]; while (! TI); TI = 0; while (! RI); RI = 0; check + = tbuf [I]; DelayMs (2000);} SBUF = check; while (! TI); // send verification data TI = 0; while (! RI); RI = 0; // wait for the verification result to receive the data (0x00 or 0xff) returned by the Master Address if (SBUF = 0x00) break; // If 0x00 is received, it indicates that the verification is correct, and the sending function is displayed. If 0xff is received, it indicates that the verification is incorrect. resend }}




Single-Chip Microcomputer multi-host Serial Communication System

One server sends one character, and the other server receives interrupted data through the serial port. After verification, the server receives the data as an action instruction,

Single-Chip Microcomputer: Serial Port Mode 1: cannot be used for multi-host communication

It can be used for multi-host communication, and single-host communication uses RS232 level. Multi-host communication uses RS485 level. Duplex.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.