Design of electronic switch based on RS485 single chip multi-machine serial communication

Publisher:陈熙琼Latest update time:2012-06-04 Source: 现代电子技术 Keywords:RS485 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

0 Introduction

At present, multi-machine serial communication is the mainstream communication method in the field of communication control. Serial communication is often used to achieve the purpose of information exchange in data communication, computer network, distributed industrial control system and its measurement and control field. The multi-machine serial communication control network is a multi-machine serial communication industrial control equipment network composed of RS 485 communication interface at the physical layer. RS 485 is both the protocol standard of the physical layer and the electrical standard of the serial communication interface. This communication interface can easily form a control network with many devices. The RS 485 communication method has many advantages. First of all, its communication distance is relatively long, usually hundreds of meters or even more than kilometers, and it can also realize multi-point communication, so that a local area network within a small range can be established, which is more practical. RS 485 adopts differential signal transmission, which has little to do with the ground level, so it has better anti-interference ability, and can obtain stable transmission even when the signal voltage is relatively small.

1 Overall plan and hardware design

The design of the electronic switch system requires that the host can confirm the address with the slave and communicate with each other, the slave can accurately detect the measured signal, the host can receive the data signal from the slave and display it through the LCD and light-emitting diode, and the RS 485 bus master and slave can achieve long-distance communication. The main core chips used by the master and slave are all single-chip microcomputers. Considering the performance of the single-chip microcomputer and the needs of the actual operating environment, the AT89S51 single-chip microcomputer is selected, and the serial communication network structure is shown in Figure 1.

(1) Main components used by the host: AT89S51 MAX4851602LCD LED RESISTORS CAP CRYSTAL;

(2) Main components used by the slave: AT89S51 MAX485 RESISTORS CAP CRYSTAL

Main functions of the host:

(1) Address confirmation with the slave device.

(2) Query whether the slave has a signal (the corresponding data signal generated when the slave detects the measured value) to send to the master.

(3) The slave address of the sending signal is displayed through the LCD, and the signal level can be displayed through the light-emitting diode (the two light-emitting diodes here represent the on and off of the two travel switches respectively).

Main functions of slave:

(1) Address confirmation with the host.

(2) Detect the measured quantity and send the measured signal data to the host.

The host has two main functional modules: the LCD display module is used to display the slave address number, the device used is 1602LCD, and the RS 485 serial communication module is used to realize long-distance serial communication with the slave, which is realized using the MAX485 chip.

The main functional module of the slave is the RS 485 serial communication module, which is used to send and receive data with the host.

2 Design Implementation Principles

The master and slave devices both use the AT89S51 microcontroller, which is a low-power, high-performance CMOS 8-bit microcontroller with 4 KB of FLASH read-only program memory that can be repeatedly erased and written 1,000 times. XTAL1 of the microcontroller: input of the reverse oscillator amplifier and input of the internal clock working circuit. XTAL2: output from the reverse oscillator. This electronic switch is designed to be connected to an external 12MHz crystal oscillator.

The master and slave use two full-duplex serial communication ports of the microcontroller to connect to the MAX485 chip to complete the serial communication with the slave. The serial port control register SCON sets the serial port working mode, monitors the serial port working status, and controls the sending and receiving status. P3.0/RXD is the serial communication receiving end, and P3.1/TXD is the serial communication sending end.

The host port P0.0~P0.7 is used as the data line of the LCD display. The 10 kΩ pull-up resistor of the P0 port is connected to the D0~D7 data terminal of the 1602 LCD, P3.7 is connected to the enable terminal E, P3.6 is connected to the read/write signal line RW, and P3.5 is connected to the register selection terminal RS. The P1.0 and P1.1 pins are connected to two light-emitting diodes to display the level of the signal sent by the slave.

The slave's P1.0 and P1.1 pins are connected to the primary and secondary travel switches respectively to detect the status of the two switches. Finally, the slave's P1.0 and P1.1 pins send the status of the travel switches to the host, and the host reads the status value and displays it.

The host uses polling. When the host queries slave No. n, it confirms the address with slave No. n and determines whether slave No. n generates a signal. If not, the host will query slave No. n-1. If a signal is generated, the host receives the signal and stores the host number of the slave, which is displayed through the LED and LCD respectively. If slave No. n-1 continues to generate a signal, the host continues to receive until the reception is completed. The host will query slave No. n-2, and so on. The number of slaves can be set in the host program.

2.1 Multi-machine serial communication and communication protocol

The design core of the electronic switch is mainly realized through multi-machine serial communication of the microcontroller.

2.1.1 Transmitter and Receiver

The AT89S51 serial port is mainly composed of a transmitter, a receiver and a serial control register. The transmitter is mainly composed of a transmit buffer register SBUF and a transmit controller. The receiver is mainly composed of a receive buffer register SBUF, a receive shift register and a receive controller.

2.1.2 Serial Port Control Registers

The serial port control register SCON is used to set the serial port working mode, monitor the serial port working status, and control the sending and receiving status. It is a special function register that can be addressed by both byte and bit. The format of the control register SCON is as follows:

The settings of the microcontroller serial port control register SCON in the electronic switch are as follows:

Host SCON setting: 1 0 0 1 1 0 0 0 =#98H

Slave SCON setting: 1 0 1 1 0 0 0 0 = #0B0H

2.1.3 Choice of working mode

The serial communication working mode of this electronic switch microcontroller is selected as working mode 2. In mode 2, the serial port works in 11-bit asynchronous communication mode. One frame of information contains a start bit "0", eight data bits, a programmable ninth data bit and a stop bit "1". The programmable bit is the TB8 bit in SCON. After the eight data bits, it can be used as a parity bit or a flag bit of the address/data frame, which is determined by the user.

Baud rate selection: The baud rate of mode 2 is fixed.

If SMOD=0 in PCON, rate=fosc/32

If SMOD=1 in PCON, rate=fosc/64

In this design: the baud rate is 9 600 b/s.

2.1.4 Implementation Principle and Process

Multi-machine communication mainly relies on the correct setting and judgment of the multi-machine communication control bit SM2 and the 9th data bit (D8) sent or received between the master and slave machines.

The implementation process of multi-machine serial communication of electronic switches:

(1) Both the master and slave are initialized to working mode 2. The master sets SM2 = 0 to enable interrupts, and the slave sets SM2 = 1 and REN = 1.

(2) The host sets REN=1, TB8=1, sends the address information to the slave, and waits to see if the sending is complete. The slave waits for the host to send the address.

(3) The slave receives the address and compares it with the local address. If the comparison is correct, it sets SM2 = 0, sends a response address to the host and waits for the sending to be completed. After receiving the address, the host confirms the address and sets TB8 = 0.

(4) After the slave address is sent, it sends the signal data and waits for the sending to be completed.

(5) After receiving the data, the host sends the slave number to the LCD display and the signal data to the LED to display the signal level. [page]

2.1.5 Address confirmation between master and slave

Before the master and slave communicate, they first confirm the address, the purpose of which is to let the host know which slave is sending data to the host. The host first sends the address number #0FFH of slave n. After receiving the address sent by the host, all slaves check it with their own addresses. Slaves whose addresses are not #OFFH will continue to wait for the host's call. The correct slave sends its own response address to the host. After receiving the slave's response address, the host makes further judgments to confirm whether it is the address of slave n. After the address is confirmed to be correct, the host communicates data with slave n until the data communication is completed, and the host will call the next slave.

2.1.6 Avoiding data conflicts between multiple slaves and the master

When a host communicates serially with multiple slaves, if a slave sends data to the host when it has data, two, three or more slaves may generate data at the same time and need to send it to the host at the same time, which will cause data communication conflicts. The design of this electronic switch adopts the method of host polling slaves, which effectively avoids the problem of master-slave data communication conflicts. The host first calls slave No. n. After the address is confirmed, slave No. n responds and communicates with the host, and the other slaves are in a waiting state. The host receives the data of slave No. n and determines whether the slave continues to send data. At this time, if other slaves have data, they can only be in queue and will not interfere with the host. After the host receives the data, it will reduce the address of slave No. n by 1 and call slave No. n-1. It will repeatedly poll each slave. The host polling process is very fast, and the slave can be processed in time when it detects the measured signal. As shown in Figure 2.

2.2 Multiple RS 485 serial communication modules

RS 485 uses balanced transmission and differential reception to achieve communication: at the transmitting end TXD, the TTL level signal of the serial port is converted into two differential signal outputs A and B, and after transmission, the differential signal is restored to a TTL level signal at the receiving end. The two transmission lines usually use twisted pair cables, which are differential transmissions, so they have strong resistance to common mode interference and high receiving sensitivity. If the data is transmitted at a rate of 10 Kb/s, the transmission distance can reach 12 m, and when the data is transmitted at a rate of 100 Kb/s, the transmission distance can reach 1.2 km. If the baud rate is reduced, the transmission distance can be further increased.

To realize RS 485 serial communication, you only need to connect a MAX485 chip to the host and each slave. The output terminal RO of the receiver and the input terminal DI of the driver only need to be connected to the RXD and TXD of the microcontroller respectively; the and DE terminals are the enable terminals for receiving and transmitting respectively. When it is logic 0, the device is in the receiving state; when DE is logic 1, the device is in the transmitting state. Because MAX485 works in half-duplex state, only one pin of the microcontroller needs to be used to control these two pins; the P3.4 pin of the microcontroller is used in the host electronic switch to control the and DE terminals of MAX485, as shown in Figure 3.

The A and B terminals are the differential signal terminals for receiving and sending respectively. When the level of the A pin is higher than that of the B pin, it means that the data sent is 1; when the level of the A pin is lower than that of the B terminal, it means that the data sent is 0. The wiring is very simple when connected to the microcontroller. Only one signal is needed to control the reception and transmission of the MAX485.

The reliability measures are as follows:

When reset, both the master and slave should be in the receiving state.

On the control side, the effective pulse width of the DE signal should be greater than the width of a frame of signal sent or received.

At the same time, add a matching resistor between the A and B terminals. Generally, a 120 Ω resistor can be selected to absorb the reflected signal on the bus and ensure that the normal transmission signal is clean and burr-free. The value of the matching resistor should be equivalent to the characteristic impedance of the bus.

The receive and send enable and DE pin control are implemented in the program. The host part program is as follows:

2.3 LCD display module

The LCD display module uses 1602 LCD. 1602 LCD has many advantages, such as low power consumption, small size, rich display content, ultra-thin and light weight, and is increasingly widely used in pocket instruments and low-power application systems. The 1602 LCD display is used on electronic switches to display the slave number that sends the signal data. When a slave detects a signal, it will display: [page]

Fault Machine is: *** (No. 001, No. 002, No. 003...)

The main pin definitions of 1602 are as follows:

RS is the register selection. When it is high, the data register is selected. When it is low, the instruction register is selected. RW is the read/write signal line. When it is high, the read operation is performed. When it is low, the write operation is performed. When RS and RW are both low, the instruction or display address can be written. When RS is low and RW is high, the busy signal can be read. When RS is high and RW is low, data can be written. The E terminal is the enable terminal. When the E terminal jumps from high to low, the LCD module executes the command. D0~D7 are 8-bit bidirectional data lines.

When the host receives a primary or secondary signal from a slave, it sends the slave number to the LCD to display the slave number. The host first separates the address number value into hundreds of bits, and then displays it bit by bit on the LCD.

The procedure for separating hundreds of bits is as follows:

The schematic diagram of the host RS 485 communication and LCD display circuit is shown in Figure 3.

3 Functions and applications of electronic switches

The electronic switch is functionally divided into two parts: a master electronic switch and a slave electronic switch.

The host is mainly responsible for receiving and processing data, and displaying the final result status. The slave electronic switch is mainly responsible for detecting signals and sending data to the host. The entire system is composed of a host and one or more slave electronic switches. The number of slave electronic switches can be determined according to actual needs.

Because the electronic switch is used to detect the deviation of the conveyor belt position, the slave electronic switch is installed in a lever mechanism (as shown in Figure 4 and Figure 5). Each lever mechanism has two travel switches inside. According to the required signal level, they are set as a primary travel switch and a secondary travel switch respectively. The lever is connected to the travel switch through a cam mechanism, and the two data input lines of the slave electronic switch are connected to the two travel switches.

This electronic switch is mainly designed for the detection of position deviation during the transmission process of large and medium-sized conveyor belts. It can also be applied to the detection of displacement and deviation of other related equipment and occasions. Slave electronic switches are placed at determined positions on both sides of the conveyor belt. When a certain amount of position deviation occurs at a certain point during the transmission process of the conveyor belt, the first-level travel switch will be triggered through the gear lever to generate a first-level trigger signal, and the signal will be transmitted to the slave electronic switch. The slave electronic switch detects it and sends it to the host for processing. The host is responsible for displaying the slave number and fault level of the fault, so that the staff can understand where the conveyor belt has a first-level deviation and make corresponding adjustments to the conveyor belt. Similarly, when a second-level deviation occurs at a certain point on the conveyor belt, the slave electronic switch will send the second-level signal to the host for display.

The schematic diagram of the installation structure on the conveyor belt is shown in Figure 5.

4 Operation status and results

When the level 1 travel switch in the travel switch lever mechanism connected to slave No. 1 is pressed, slave No. 1 detects a level 1 fault signal, confirms the address with the host, and sends data to the host. At this time, the LCD display on the host displays: Fault Machine is: 001 (the faulty slave number is 001), and one of the LEDs is off (under normal conditions, both LEDs are in a light state). Similarly, if slave No. 2 detects a fault signal, the operating result is shown in Figure 6.

5 Conclusion

This electronic switch is designed based on the principle of single-chip multi-machine serial communication. It is a small electronic switch system composed of RS 485 module and 1602 LCD display module. In industrial control, the application of multi-machine serial communication is becoming more and more extensive. On the basis of this electronic switch design, more powerful electronic switch related products based on RS 485 bus multi-machine serial communication can be developed for application in industrial control, detection and other fields.

Keywords:RS485 Reference address:Design of electronic switch based on RS485 single chip multi-machine serial communication

Previous article:Anti-interference design of C8051f064 in power monitoring instrument
Next article:Design and implementation of automobile anti-theft alarm system based on single chip microcomputer

Recommended ReadingLatest update time:2024-11-17 02:28

MCU RS485 serial communication
RS-485 is a half-duplex communication mode, where sending and receiving share the same physical channel. Only one machine is allowed to be in the sending state at any time. Therefore, the machine that requires a response can only respond when it hears that the call signal on the bus has been sent and no other machine h
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号