How to design an electronic switch based on the principle of multi-machine serial communication of single chip microcomputer

Publisher:王大雷Latest update time:2023-10-12 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

introduction

Currently, it is the mainstream communication method in the field of communication control. In the fields of data communication, computer network, distributed industrial control system and its measurement and control, serial communication is often used to achieve the purpose of information exchange. The multi-machine serial communication control network is a multi-machine serial communication industrial control equipment network composed of the physical layer using the RS 485 communication interface. RS 485 is both the protocol standard for the physical layer and the electrical standard for the serial communication interface. This communication interface can easily combine many devices into a control network. The RS 485 communication method has many advantages. First of all, its communication distance is relatively long, which can usually be hundreds of meters or even kilometers. It can also implement multi-point communication, so that a small-scale local area network can be established, so it is more convenient. Practical value. RS 485 uses differential mode signal transmission and has little to do with the ground level. Therefore, 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 this system requires that the host machine and the slave machine can perform address confirmation and communicate with each other. The slave machine can accurately detect the measured signal. The host machine can receive the data signal from the slave machine and display it through the LCD and light-emitting diodes. At the same time Long-distance communication can be achieved by using RS 485 bus master-slave. The main core chips used in the master and slave machines are all. Considering the performance of the microcontroller and the needs of the actual operating environment, the AT89S51 microcontroller is selected. The serial communication network structure is shown in Figure 1.

How to design an electronic switch based on the principle of multi-machine serial communication of single-chip microcomputer?

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

(2) The main devices used by the slave: AT89S51 MAX485 RESISTORS CAP CRYSTAL

Main functions of host:

(1) Confirm the address with the slave machine.

(2) Query whether the slave has a signal (the corresponding data signal generated when the slave detects the measurement) and sends it to the host.

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

Main functions of slave machine:

(1) Confirm the address with the host.

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

The host mainly has two functional modules: the liquid crystal display module is used to display the slave address number, and the device used is 1602LCD. The RS 485 serial communication module is used to achieve long-distance serial communication with the slave, using the MAX485 chip. to fulfill.

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

2 Design and implementation principles

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

Both the master and the slave use the 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 working mode of the serial port and monitors the work of the serial port. Status, sending and receiving status control, etc. P3.0/RXD is the serial communication receiving end, and P3.1/TXD is the serial communication sending end.

The host ports P0.0~P0.7 serve as the data lines of the LCD. The P0 port pull-up 10 kΩ resistor is connected to the D0~D7 data terminals of the 1602 LCD. P3.7 is connected to the enable terminal E, and P3.6 is connected to read/write. The signal line RW is connected, 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 P1.0 and P1.1 pins of the slave machine are respectively connected to the primary travel switch and the secondary travel switch to detect the status of the two switches. Finally, the two pins of the slave machine P1.0 and P1.1 will The status of the travel switch is sent to the host, and the host reads the status value and displays it.

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

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 single-chip microcomputer.

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 the sending buffer register SBUF and the sending controller. The receiver is mainly composed of the receive buffer register SBUF, the receive shift register and the receive controller.

2.1.2 Serial port control register

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

How to design an electronic switch based on the principle of multi-machine serial communication of single-chip microcomputer?

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 methods

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. A 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 9600 b/s.

2.1.4 Implementation principles and processes

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.

Implementation process of electronic switch multi-machine serial communication:

(1) Both the master and slave machines initialize working mode 2. The master machine sets SM2=0 and allows interrupts. The slave machine sets SM2=1 and REN=1.

(2) The master sets REN=1, TB8=1, sends the address information to the slave, and waits for the sending to be completed. The slave waits for the master to send the address.

(3) The slave receives the address and checks it with the local address. If the check is correct, set SM2=0, send the response address to the host and wait for the sending to complete. After receiving the address, the host confirms the address and sets TB8=0.

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

(5) The host receives the data and sends the slave number to the LCD display, and sends the signal data to the LED to display the signal level.

2.1.5 Address confirmation between master and slave

Before the master-slave machine performs data communication, it first performs address confirmation. The purpose is to let the host know which slave machine sends data to the host. The master first sends the nth slave address number #0FFH. After receiving the address sent by the master, all slaves check with their respective addresses. The slaves with addresses other than #OFFH will continue to wait for the call from the master and check the correct slave to the master. Send the response address of the local machine. After receiving the response address of the slave, the host further makes a judgment to confirm whether it is the address of the nth slave. After the address is confirmed to be correct, the master communicates with the slave No. n. Until the data communication is completed, the master will call the next slave.

2.1.6 Avoid data conflicts caused by communication between multiple slaves and the host

When a host communicates serially with multiple slaves, if a slave has data and sends it to the host, there may be two, three or more slaves generating data at the same time, which need to be sent to the host at the same time. , which will lead to data communication conflicts. The design of this electronic switch adopts the method of master polling slave, thus effectively avoiding the problem of master-slave data communication conflict. The master first calls the slave machine No. n. After the address is confirmed, the slave machine No. n responds and communicates with the host machine. Communication, the other slaves are in a waiting state, the host receives the data from the nth slave, and determines whether the slave continues to send data. At this time, if other slaves have data, they can only wait in the queue and will not interfere with the host. After receiving the message, the host decrements the address of slave No. n by 1, calls slave No. n-1, and polls each slave repeatedly. The host polling process is very fast, and when the slave detects the signal under test, it can also be processed in time. as shown in picture 2.

[1] [2]
Reference address:How to design an electronic switch based on the principle of multi-machine serial communication of single chip microcomputer

Previous article:Identification method of automatic baud rate detection and design and application of wireless data transmission module
Next article:51 microcontroller development and implementation of perpetual calendar system (alarm clock, stopwatch function)

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号