Multi Computer Communication Principle of 51 Single Chip Microcomputer

1、 Principle of multi computer communication

In multi computer communication, the host must be able to identify each slave. In 51 Series MCU, it can be realized by SM2 bit of scon register. When the serial port sends data in mode 2 or mode 3, each frame information is 11 bits, and the 9th bit is the data programmable bit. The address frame and data frame are distinguished by setting 1 or 0 to TB8. When the bit is 1, the address frame is sent; When this bit is 0, the data frame is sent.

In the process of multi machine communication, the host first sends the address of a slave and waits for the response of the slave. After receiving the address frame, all slaves compare it with the local address. If it is the same, set SM2 to 0 to prepare to receive data; If it is different, the current data is discarded and the SM2 bit remains unchanged.

2、 Multi computer communication circuit diagram

Here, U1 is the master, U2 is the slave 1, and U3 is the slave 2.

3、 C language program

(1) Host program

#include#include

#define _ SUCC_ 0x0f / / data transfer succeeded #define_ ERR_ 0xf0 / / data transfer failed. Unsigned char table [9] = 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39; unsigned char Buff[20];// Data buffer unsigned char temp = 0xff; sbit KEY1=P1^6; sbit KEY2=P1^7;// unsigned char addr;

//Delay 1ms function void delay_ 1ms (unsigned int t) unsigned int x, y; for (x = t; x > 0; X --) for (y = 110; Y > 0; Y --); / / buffer initialization void buff_ Init() unsigned char I; / / put the data in the table into the buffer for (I = 0; I Ti = 0; / / send data length TB8 = 0; / / send data frame SBUF = lenth; while (! Ti); Ti = 0; for (I = 0; I; I ) >

TB8 = 0; / / send the check byte SBUF = check; while (! Ti); Ti = 0; / / send data void addr_data (unsigned addr) while (temp! = addr) / / the host waits for the slave to return its address as a response signal Ti = 0; / / send the slave address TB8 = 1; / / send the address frame SBUF = addr; while (! Ti); Ti = 0; RI = 0; while (! RI); temp = SBUF; RI = 0;

Temp = _err_; / / the host waits for the slave data to receive the success signal while (temp! = _succ_) send_data (buff); RI = 0; while (! RI); temp = SBUF; RI = 0;

void main()Buff_init();serial_init();while(1)if(KEY1==0)delay_1ms(5);if(KEY1==0)while(!KEY1);ADDR_data(0x01);if(KEY2==0)delay_1ms(5);if(KEY2==0)while(!KEY2);ADDR_data(0x02);

(2) Slave 1 program

#include#include

#Define addr0x01 / / address of slave 1 #define _succ_0x0f / / data transfer succeeds #define _err_0xf0 / / data transfer fails. Unsigned char AA = 0xff; / / communication flag between host and slave is unsigned char buff [20]; / / data buffer

//Serial port initialization function void serial_init() tmod = 0x20; / / timer 1 works in mode 2th1 = 0xfd; TL1 = 0xfd; / / baud rate is 9600pcon = 0; scon = 0xd0; / / serial port works in mode 3tr1 = 1; / / start timer Ti = 0; RI = 0; / / receive data function unsigned char rece_data (unsigned char * buff) unsigned char I, temp; unsigned char lenth; unsigned char check;

RI = 0; / / received data length while (! RI); if (rb8 = = 1) / / if an address frame is received, 0xfereturn 0xFE; lenth = SBUF; RI = 0; check = lenth; for (I = 0; I; I ) >

While (! RI); / / receive the check byte if (rb8 = = 1) / / if the address frame is received, 0xfereturn 0xFE; temp = SBUF; RI = 0; check = temp ^ check; / / compare the check code received from the host with the check code calculated by yourself if (check! = 0) / / if the check code is inconsistent, indicating that the data is received incorrectly, send an error signal to the host, and the function returns 0xffTi = 0; TB8 = 0; SBUF = _err_; while (! Ti) ; Ti = 0; return 0xff; Ti = 0; / / the check codes are consistent, indicating that the data is received correctly. Send a success signal to the host. The function returns 0x00tb8 = 0; SBUF = _succ_; while (! Ti); Ti = 0; return 0;

Void main() serial_init(); while (1) SM2 = 1; / / receive the address frame while (AA! = addr) / / the slave waits for the host to request its own address RI = 0; while (! RI); AA = SBUF; RI = 0;

Ti = 0; / / once requested, the slave returns its own address as a response and waits for the received data TB8 = 0; SBUF = addr; while (! Ti); Ti = 0;

SM2 = 0; / / receive data frame AA = 0xff; / / receive data from the slave and save the data to the data buffer while (AA = = 0xff) AA = rec_data (buff); if (AA = = 0xFE) continue; P1 = buff [1]; / / view the received data

(3) Slave 2 program

#include#include

#Define addr0x02 / / address of slave 2 #define _succ_0x0f / / data transfer succeeds #define _err_0xf0 / / data transfer fails. Unsigned char AA = 0xff. / / the communication flag between host and slave is unsigned char buff [20]; / / data buffer

//Serial port initialization function void serial_init() tmod = 0x20; / / timer 1 works in mode 2th1 = 0xfd; TL1 = 0xfd; / / baud rate is 9600pcon = 0; scon = 0xd0; / / serial port works in mode 3tr1 = 1; / / start timer Ti = 0; RI = 0; / / receive data function unsigned char rece_data (unsigned char * buff) unsigned char I, temp; unsigned char lenth; unsigned char check;

RI = 0; / / received data length while (! RI); if (rb8 = = 1) / / if an address frame is received, 0xfereturn 0xFE; lenth = SBUF; RI = 0; check = lenth; for (I = 0; I; I ) >

While (! RI); / / receive the check byte if (rb8 = = 1) / / if the address frame is received, 0xfereturn 0xFE; temp = SBUF; RI = 0; check = temp ^ check; / / compare the check code received from the host with the check code calculated by yourself if (check! = 0) / / if the check code is inconsistent, indicating that the data is received incorrectly, send an error signal to the host, and the function returns 0xffTi = 0; TB8 = 0; SBUF = _err_; while (! Ti) ; Ti = 0; return 0xff; Ti = 0; / / the check codes are consistent, indicating that the data is received correctly. Send a success signal to the host. The function returns 0x00tb8 = 0; SBUF = _succ_; while (! Ti); Ti = 0; return 0;

Void main() serial_init(); while (1) SM2 = 1; / / receive the address frame while (AA! = addr) / / the slave waits for the host to request its own address RI = 0; while (! RI); AA = SBUF; RI = 0;

Ti = 0; / / once requested, the slave returns its own address as a response and waits for the received data TB8 = 0; SBUF = addr; while (! Ti); Ti = 0;

SM2 = 0; / / receive data frame AA = 0xff; / / receive data from the slave and save the data to the data buffer while (AA = = 0xff) AA = rec_data (buff); if (AA = = 0xFE) continue; P1 = buff [2]; / / view the received data

Multi Computer Communication Principle of 51 Single Chip Microcomputer 1

get in touch with us
Artikel yang disarankan
Multi Channel Laser Anti-theft Alarm Circuit
The figure shows the circuit of multi-channel laser anti-theft alarm. VT1, vt2, RL1, RP1, R1, K and vd6 constitute an optical control switch. During the day, the photoresistor RL1 is in a low resistance state under light, VT1 is saturated on, vt2 is off, relay K is not energized, normally open contact J is off, and the alarm circuit does not work; At night, RL1 changes to high resistance state, VT1 is cut off, vt2 is saturated and turned on, K is powered on to make j pull in, the circuit supplies power to the whole machine, the red LED is on, the regulated voltage is 6V, and supplies power to the subsequent circuit after voltage reduction by vd7, vd8 and vd9. Laser rods JB1, RL2, VT3, RP2, R3 and R4 form a laser transmitting and receiving network. When the alarm is in the waiting state, the laser rod emits red laser and irradiates it on the photoresist RL2. RL2 is in a low resistance state, and the base potential of VT3 is very low, which provides trigger current for thyristor vs, vs is turned on, IC2 is powered on, and the horn gives an alarm sound. The alarm can be equipped with up to 10 laser transmitting and receiving networks at the same time. The circuit structure is exactly the same, and C3 is an anti-interference capacitor. An is a reset switch. When the power grid is cut off or the gangster cuts off the power supply, the battery immediately supplies power to the whole machine, ensuring the reliability of the alarm device.Component selection: IC1 is voltage stabilizing integrated circuit 7806, and aluminum heat sink shall be added during use. IC2 is KD-9561 four sound chip, and the circuit is connected to the second sound selection terminal sel2 (fire truck alarm sound). The laser source is a red laser rod with voltage of 4.5V and power
How Do I Make My Go Kart Faster, Without Spending too Much Or Nothing?!?
What Solute Will Be Replaced by Liquid Electrolyte in Commercial Lithium Ion Battery?
Pool Stage
Introduction of Tubular Soil Moisture Monitoring Station
From the Perspective of Bridge Lighting System, the Use of LED Technology Has Become a Trend
Olar Energy Phone Charger Stand
Can Boiling Water Purify It? What's a Good Water Medium Sized Water Filter?
Precautions for Installation of Liquid Vortex Flowmeter
How Do I Connect My Desktops to Share the Internet?
Pencarian terkait
Multi Channel Laser Anti-theft Alarm Circuit
How Do I Make My Go Kart Faster, Without Spending too Much Or Nothing?!?
What Solute Will Be Replaced by Liquid Electrolyte in Commercial Lithium Ion Battery?
Pool Stage
Introduction of Tubular Soil Moisture Monitoring Station
From the Perspective of Bridge Lighting System, the Use of LED Technology Has Become a Trend
Olar Energy Phone Charger Stand
Can Boiling Water Purify It? What's a Good Water Medium Sized Water Filter?
Precautions for Installation of Liquid Vortex Flowmeter

Copyright © 2020 Concises YuGa Sports | Sitemap