In many data acquisition systems such as instruments and meters, intelligent devices, etc., which are based on single-chip microcomputers, it is often necessary to realize data exchange between computers and single-chip microcomputers, that is, communication between single-chip microcomputers and computers, so as to give full play to the respective strengths of single-chip microcomputers and computers and improve the performance and cost performance of the entire application system. The traditional communication based on RS-232 protocol has increasingly prominent application limitations due to its shortcomings such as short transmission distance, slow speed, and signal susceptibility to interference. In view of this, the author discusses how to build an RS-485 communication network based on RS-485 communication protocol to realize remote communication between several single-chip microcomputers and computers.
2. Hardware Design
1. Introduction to RS-485 communication
RS-485 is a standard asynchronous serial bus for balanced transmission and balanced reception established by EIA (Electronic Industries Association of the United States). It has the advantages of long transmission distance, high sensitivity, and strong multi-point communication capability. See Table 1 for RS-485 interface standards.
2. MAX485 chip introduction
The RS-485 conversion interface chip we use is MAXIM's MAX485 (see Figure 1). This chip uses a single power supply +5V, a rated current of 300uA, and a half-duplex communication mode. It converts the TTL level into
RS-485 level. The chip has 8 pins, 2 drivers and receivers inside. RO and DI are the output of the receiver and the input of the driver, respectively, which are connected to the MCU TXD and RXD. /RE and DE are the receiving and transmitting enable terminals, respectively. When /RE=0, MAX485 is in the receiving state; when DE=1, MAX485 is in the transmitting state. Because MAX485 works in half-duplex state, it is very simple to connect it to the MCU. Only one pin of the MCU is needed to control these two pins. The A terminal and the B terminal are the differential signal terminals for receiving and transmitting, respectively. When VA is greater than VB, it indicates that the transmitted data is "1", and when VA is less than VB, it indicates that the transmitted data is "0". When working, matching resistors should be added between A and B, which can generally be selected from 100 to 120 ohms.
.Level conversion interface
When the PHILIPS P89C51RD2 microcontroller communicates with the computer, the level must be converted. Because the microcontroller and the computer are connected via an RS-485 network, the microcontroller side signal is converted from a TTL level to an RS-485 level signal after passing through MAX485. Therefore, the RS-485 signal must be converted to an RS-232 level signal on the computer side so that the two can communicate correctly. In this system, a DAC-8520 converter is used to convert RS485 signals to RS-232 signals. The converter has a fast data conversion and transmission speed and does not require the user to initialize or modify parameters.
Table 1 RS-485 interface standards
4. Computer interface
The computer uses a programmable serial asynchronous communication controller 8251A to implement it. By initializing 8251A, the serial data transmission format, speed and working mode can be controlled to make it consistent with the communication mode of the microcontroller, thereby realizing communication between the computer and the microcontroller.
3. Multipoint Communication
3.1 Multipoint Communication Principle
The RS485 bus standard is developed on the basis of RS-422. It adds multi-point two-way communication capability, making multi-point communication in distributed networks possible. After we understand the driving capability of the device, we can add the required nodes according to the needs and driving capability. This system uses a computer to control up to 32 single-chip microcomputers. The computer determines the communication connection with the required single-chip microcomputer by sending control words and working mode words. The single-chip microcomputer collects data according to the instructions sent by the computer, and exchanges data with the computer or adjusts the production process parameters and the status of the connected equipment as needed. After the computer analyzes and calculates the data sent back by the single-chip microcomputer, it transmits the relevant control information to the single-chip microcomputer.
When the 51 series microcontroller is used for multi-point communication, it must work in mode 2 or mode 3. Taking mode 3 as an example, each frame of data sent is 11 bits: 1 start bit (0), 8 data bits and 1 stop bit (1). The additional 9th bit of data is a parity bit in a non-multi-point communication system. It is generated by TB8 of SCON at the sending end and transmitted to RB8 at the receiving end. It can also be set to "0" or "1" as a mark to distinguish data frames (0 mark) or address frames (1 mark) in multi-point communication. In the 51 series microcontroller multi-point communication system, there is the following communication protocol: All slaves are in the listening state, that is, SM2=1, so as to receive the address sent by the host. When RB8 of a received frame of information is "1", it means that the host sends address information, and all slaves have a receiving interrupt, otherwise the interrupt is masked. When a slave enters the corresponding interrupt service program, it compares the received address with the address of the local machine. If they match, it sets SM2=0 and sends the local address back to the host as a response. The slave is connected to the host and is ready to receive commands or data information from the host, while other slaves that are not addressed keep SM2=1 and exit their respective interrupt service programs. In this way, only the slave with SM2=0 can receive the data information sent by the host, and the separation of the address frame and the data frame is successfully realized. After the communication is completed, the addressed slave sets SM2=1 again, exits the interrupt service program, and waits for the next communication.
3.2 Multipoint Communication Protocol
To achieve normal communication between the computer and the microcontroller, the working mode between the two must be set correctly to ensure that both parties use the same baud rate, start bit, stop bit, parity bit, and establish a response signal for communication between the two parties.
The microcontroller can work in synchronous shift register mode or in UART (Universal Asynchronous Receiver Transmitter). The communication mode of the serial port is controlled by the special function register SCON. Its bits are defined as follows:
SM0, SM1: working mode setting bits; SM2: multi-machine communication control bits that allow use of modes 2 and 3; RB8: 9th bit of received data; TB8: 9th bit of transmitted data; TI transmit interrupt flag; RI: receive interrupt flag. The working mode in this article is mode 3, that is, the 9-bit UART timer T1 is used as a baud rate generator and works in mode 2. If the selected baud rate is 9600bps, the initial count value is 0FAH (SMOD=0, clock frequency Fosc is 11.0592MHz)
The asynchronous communication interface of the computer is INTEL8251A, which can set 1, 1.5 or 2 stop bits, the data can be selected between 5 and 8 bits, the communication frequency is 0 to 9600bps, and there are three modes of parity check, frame check and overflow check. It can be initialized by writing certain mode control words and command control words to it. Its working status is stored in the status register and can be read by the CPU of P89C51RD2. The communication protocol is as follows: (1) The serial communication baud rate is 9600bps; (2) The frame format is 8 data bits, a start bit, a programmable 9th bit (i.e., the flag bit of the address/data bit sent and received), and a stop bit; (3) The host and the slave follow the master-slave principle. The host selects the slave by calling. Data is transmitted bidirectionally between the host and the slave. The mutual communication between the slaves needs to be mediated by the host; (4) The master and the slave should also transmit some commands and status words for them to identify, such as 00H indicating that the host sends a slave receive command, and 01H indicating that the slave sends a master receive command, etc.
4. Software Design
This part mainly includes the host program, the slave and its interrupt service program. The host program mainly completes the initialization of the serial port and the communication between the host and the slave. The slave main program mainly completes the initialization of the serial port and the setting of the baud rate (should be consistent with the baud rate of the host end), and its interrupt program mainly completes the data communication between P89C51RD2 and the computer.
1. Single chip microcomputer part
The microcontroller program is written in C language in uVISION2, an excellent visual integrated development environment provided by KEIL. The communication baud rate is 9600bps and the crystal oscillator is 11.0592Mhz. It should be noted that one machine cycle of PHILIPS P89C51RD2 is 6 clock cycles, which is different from the standard 8051 microcontroller, which has 12 clock cycles. Therefore, this difference should be paid attention to when setting relevant parameters, otherwise, the communication connection cannot be established correctly. In order to cooperate with the multi-machine communication mode, we use working mode 3.
The microcontroller communication flow chart is shown in Figure 2.
The microcontroller serial port initialization procedure is as follows:
main()
{TMOD = 0x20;
/*Timer 1 mode 2*/
TH1=0xfa;
/*9600 baud rate crystal oscillator 11.0592MHz*/
TCON=0x40;
/* Start timer 1*/
SCON=0x50;
/* Allow to receive*/
IE=0x90;
/* Enable serial port interrupts */
………..
2. Computer part
We use the excellent visualization software Visual Basic to write the computer program. The key part is to reference the serial communication control MSComm of Visual Basic. The sending and receiving are completely managed by this control, without the need for the cumbersome driver writing in the past. This undoubtedly greatly improves work efficiency and reduces development costs.
The computer communication process is shown in Figure 3.
The following is a communication program compiled with VB6:
Dim inbyte() AS Byte 'Define a dynamic byte array
Private Sub txd_click() 'Communication establishment and data sending control button
If MSComm1.PortOpen = True Then GoTo jj
If the communication port is open, jump to jj
MSComm1.CommPort = 1
'The communication port is COM1
MSComm1.Settings = "9600,n,8,1"
'Serial port initialized to 9600 baud, 8 data bits, 1 stop bit, no parity
MSComm1.PortOpen = True
'Open the communication port
jj: Mscomm1.output=Text1.text
'Send address code
………..
Mscomm1.output=text2.text 'Send working mode control word, acquisition channel and check code
End Sub
Private Sub rxd_click() 'Data receiving button
Inbyte() = Mscomm1.input 'The received data is stored in a dynamic byte array
……. 'Omit the data processing part
End Sub
V. Conclusion
RS-485 serial communication can ensure fast, stable and long-distance data transmission. The system designed in this paper has been successfully applied in intelligent data acquisition network instruments, and its communication design method can be transplanted to other industrial measurement and control systems with slight modifications. Due to the low development cost and simple design of the design method in this paper, it will be more and more used in current and future industrial control and other aspects.
Previous article:51 MCU experiment DIY welding power supply--serial port detailed steps
Next article:Design of Capacitance Tester Based on 555 Timer
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Chapter7 Analog-to-digital converter ADC12_A
- Open source hardware small project: Anxinke ESP-C3-12F controls WS2812
- Principle and design of flyback switching power supply
- EEWORLD University Hall----Live Replay: MPS Inductor Solutions Help Better Switching Power Supply Design
- High-precision humidity and temperature sensor
- 【NUCLEO-L552ZE Review】+ Planning & Preparation
- After cc2530 successfully creates a network, p1_0 immediately becomes low level
- RF and Microwave Passive Component Design Considerations and Limitations
- Let's see! Detailed explanation of the components of a switching power supply
- SCI Interface and Hardware Implementation of TMS320C6711D