Design of RS485 Serial Communication Based on C8051F020 Single Chip Microcomputer

Publisher:真诚友谊Latest update time:2014-11-23 Source: 互联网Keywords:C8051F020 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

  1 Introduction

  With the continuous development of computer technology and measurement and control technology, in the data acquisition system with single-chip microcomputer as the core, it is necessary to realize data exchange between single-chip microcomputer and computer, so as to give full play to the respective strengths of single-chip microcomputer and computer and improve the performance-price ratio of the whole system.

  In computer networks and industrial control systems, serial communication is often required to achieve remote data transmission. At present, there are many interface standards available for serial communication, including RS232, RS422, RS485, etc. RS232 is the earliest serial interface standard and has been widely used in short-distance, low-baud-rate serial communication. However, RS232 communication has shortcomings such as slow transmission speed, short transmission distance, and signal susceptibility to interference, and its application limitations have become increasingly prominent. RS485 communication uses a differential method to eliminate noise, that is, the signal will be decomposed into two positive and negative lines before sending. When it reaches the receiving end, the signal will be subtracted to cancel each other out and restore the original signal. This method has a strong ability to suppress common-mode interference and has been widely used in industrial control and other fields.

  To achieve RS485 communication between the microcontroller and the computer, there are generally two methods: one method is to use RS232 and RS485 level conversion devices at both ends of the microcontroller and the computer respectively; the other method is to use an RS485 communication card and insert it on the computer motherboard. The advantage of the former method is that the hardware device is easy to install and the software programming is relatively simple; the disadvantage is that the communication rate is limited to 20 kb/s. The advantage of the second method is that the communication distance is longer and the rate is higher, up to 10 Mb/s; the disadvantage is that the communication card and driver need to be installed, and the necessary settings need to be made. This article adopts the second method.

  2 Overall Architecture

  In the development of a certain type of electronic equipment, an 8-bit single-chip microcomputer C8051F020 produced by Cygnal Company of the United States was selected to collect data for the measurement and control system. This single-chip microcomputer is a sub-series of the F02X series of the C8051F series, and its cost performance is highly competitive in the field of industrial control. It is a fully integrated mixed-signal system-level chip with a CIP-51 core that is fully compatible with the 8051 instruction set. For technicians familiar with the 51 single-chip microcomputer, it greatly improves the development efficiency in hardware design and software programming.

  The measurement and control computer uses Advantech's IPC-610 industrial computer, and uses a PCL-846B communication card for RS485 serial data communication. The communication card contains 4 channels, and each channel can communicate with 32 nodes.

  In order to realize RS485 serial communication between the microcontroller and the industrial computer, the UART output level of the microcontroller must be converted to meet the RS485 serial communication requirements. The MAX485 interface chip is selected, which is an RS485 interface chip of Maxim Company. The structure and pins of the chip are relatively simple.

  Theoretically, this design can realize RS485 serial communication between one industrial computer (installed with one RS485 communication card) and 128 single-chip microcomputers. The overall design block diagram is shown in Figure 1.

  

 

  Figure 1 Overall structure

  3 System Hardware Design

  3.1 Hardware Introduction

  1) C8051F020 microcontroller

  The C8051F020 has two enhanced full-duplex UARTs, an SPI bus, and an SMBus/IC. These serial buses are implemented in hardware and can generate interrupts to the CIP-51 core with very little CPU intervention; these serial buses do not "share" resources such as timers, interrupts, or I/O ports, so any one or all of them can be used simultaneously.

  2) MAX485 chip

  The MAX485 chip uses a +5V power supply and has a rated current of 300"A. It can convert the UART output level to the RS485 level. The chip has 8 pins and contains 1 receiver and 1 driver. R0 is the output end of the receiver, connected to the TXD pin of the microcontroller; DI is the input end of the driver, connected to the RXD pin of the microcontroller. /RE is the receive enable end. When /RE=0, the MAX485 chip is in the receive state; DE is the transmit enable end. When DE=1, the MAX485 chip is in the transmit state.

  The A and B ends of the MAX485 chip are the differential signal ends for receiving and sending respectively. When VA is less than VB, it means the sent signal is "0"; when VA is greater than VB, it means the sent signal is "1". When the MAX485 chip is working, a matching resistor should be added between the A and B ends, generally 120 Ω; when the communication distance is long, 300 Ω can be used.

  3) RS485 communication card

  RS485 communication card has strong anti-interference ability, high communication rate and low price, and has strong competitiveness and practicality in the fields of industrial control. Advantech's PCL-846B communication card is selected. This card supports two serial communication interface standards, RS422 and RS485, and needs to be set through jumpers. In addition, welding holes for welding terminal matching resistors are reserved on the four channels of the communication card.

  3.2 Circuit Design

  The UART serial bus is used for communication because UART is a serial transmission interface widely used in long-distance, low-speed, low-cost communication. Due to its characteristics of few data lines, it has been widely used in digital system design. Basic UART communication only needs two data lines (RXD, TXD) to complete the mutual communication of data. Both reception and transmission are full-duplex, where RXD is the receiving end and TXD is the transmitting end.

  The C8051F020 microcontroller has two UARTs (UART0 and UART1). Taking UART0 as an example, its TxD and RXD are multiplexed with the digital I/0 pins PO.O and PO.1 respectively, and are selected through the cross switch configuration register. Since MAX485 works in half-duplex state, the wiring when it is connected to the microcontroller is relatively simple. Only one pin of the microcontroller (such as PO.2) is needed to control the two pins RE and DE. The PCL-846B communication card has 4 channels. Channel 1 is selected to communicate with the microcontroller, and channel 2 and channel 4 are connected to realize the self-test of the communication card in a self-transmitting and self-receiving manner. The connection relationship between the microcontroller and the external circuit is shown in Figure 2.

  

 

  Figure 2 Hardware circuit principle

  When using RS485 communication card for communication, when the signal is transmitted to both ends of the communication line, if the impedance does not match, signal reflection problems may occur. Signal reflection will cause signal distortion and deformation, resulting in communication errors. The solution is to connect a terminal matching resistor at both ends of the communication line to ensure impedance matching. When the communication distance is short, generally less than 300 m, the terminal resistor can be omitted. When the communication distance is greater than 300 m, a terminal resistor should be used, and its resistance must be the same as the linear impedance of the communication line. The resistance value is generally selected to be around 120 Ω, and when the communication distance is longer, 300 Ω can be selected.

  4 Software Design

  4.1 Communication method

  The UARTo of the C8051F020 microcontroller provides 4 working modes (1 synchronous mode and 3 asynchronous modes). Take mode 1 as an example: Mode 1 provides standard asynchronous, full-duplex communication. Each data byte contains 10 bits: 1 start bit, 8 data bits and 1 stop bit. Data is sent from the TX pin and received at the RX pin. 2 In multi-machine communication, mode 2 and mode 3 are usually selected. By using the 9th data bit and the built-in UARTo address recognition hardware, multi-machine communication between a master processor and multiple slave processors is supported.

  In order to simplify the use of UART0 and software programming, select method 1 for multi-machine communication and make an agreement between the two communicating parties by writing a serial port interrupt program. The specific steps are:

  1) Agree on the format of the sent data. This article agrees to use "#...*" as the legal data format, that is, starting with "#" and ending with "*" is used as the basis for judging the validity of the data.

  2) The data between "#" and "*" is required. This article agrees to use the second character (when there are many devices, 2 characters can be used) as the identification code of the receiving device. The receiving device (microcontroller or host computer) judges the character to determine whether it needs to receive the data.

  3) When the receiving device needs to know the source of the sent data, it can also agree on the identification code of the sending device (since each device can be both a sending device and a receiving device, a fixed identification code can be agreed upon), and the identification code can be inserted into the sent data (the insertion position must be agreed upon in advance) to know the source device of the data.   4.2 Serial port baud rate

  The C8051F020 microcontroller has five 16-bit general-purpose counters/timers (T0~T4) and an on-chip programmable counter/timer array (PCA). The baud rate of mode 1 is a function of the timer overflow time. UART0 can use timer 1 to work in 8-bit auto-reload mode or timer 2 to work in baud rate generator mode to generate baud rate. Taking timer 2 as an example, the baud rate calculation formula is:

  

 

  Where: SySCLK is the system clock of the microcontroller, RCAP2H and RCAP2L are the high 8 bits and low 8 bits of the reload register of timer 2 respectively.

  The baud rate is determined according to the actual needs of the data transmission rate. After the baud rate is determined, the initial value T2 of the reload register of timer 2 can be calculated.

  

 

  Using a 22.1184 MHz crystal oscillator as the system clock and a baud rate of 9600 b/s, we can calculate T2=FFB8H (hexadecimal).

  4.3 Software Process

  The C8051F020 microcontroller has JTAG and debugging circuits inside, and the MCU can be debugged non-invasively, at full speed, and in-system through the JTAG interface. According to the system functional requirements, the software is modularly designed, and the microcontroller is programmed and debugged using the Silicon Labs IDE integrated development environment. Using the interrupt system of the C8051F020 microcontroller, the UART interrupt service program is directly written in C language.

  The serial communication program of the microcontroller can be composed of two modules: data sending and receiving. The data sending module first needs to set the UART to the sending state, clear the sending flag and write data to SBF, start sending characters one by one, and after the data is sent, restore the UART to the receiving state. The data receiving module clears the receiving flag and reads the data in SBF, starts receiving characters, first determines whether the received data is valid, then analyzes and processes the data, and exits the receiving program after receiving the data. The program flow of the UART serial port sending and receiving modules is shown in Figure 3.

  

 

  Figure 3 Serial communication program flow

  5 Experimental analysis

  The designed system has been applied to the development of a real-time measurement and control device. The measurement and control device contains two single-chip microcomputers, and its main control host computer is equipped with an RS485 communication card. The specific verification process is: one measurement and control device and one host computer are 50 meters apart for serial communication, and the communication is normal and reliable. Two measurement and control devices and two host computers (both equipped with RS485 communication cards) are 50 meters apart, and the communication lines are connected in parallel. The RS485 communication between the four communication nodes is normal and reliable. By analogy, when multiple measurement and control devices communicate with multiple host computers over long distances, RS485 communication is normal and reliable.

  The serial port can be easily debugged on the host computer through the serial port debugging assistant. This debugging tool can send and receive serial data in real time, with adjustable data format and transmission rate, and can dynamically observe the data transmission and reception of each communication node. The serial port debugging interface is shown in Figure 4.

  

 

  Figure 4 Serial port debugging interface

  This design method successfully realizes RS485 serial communication between the MCU and the host computer. When the distance between the MCU and the host computer is less than 50 m, high-speed communication of 115.2 k/ps can be achieved, and the data transmission bit error rate is low, which fully meets the system design indicators.

  6 Conclusion

  By establishing an RS485 serial communication network between the single-chip microcomputer and the industrial computer, and taking advantage of the RS485 serial communication, data can be transmitted stably, quickly and over long distances. The designed system has been successfully applied in the development of a certain type of real-time measurement and control device. Experimental verification shows that data transmission is normal and reliable, meeting the design requirements.

  Due to the low development cost, simple software design, and easy use and maintenance of the design method, its communication design method can also be slightly modified and transplanted to other measurement and control systems, and will be increasingly used in fields such as computer communication and industrial control.

Keywords:C8051F020 Reference address:Design of RS485 Serial Communication Based on C8051F020 Single Chip Microcomputer

Previous article:A new intelligent air conditioner remote controller based on Atmega16 microcontroller
Next article:Low-cost, highly flexible MIPI CSI-2 and DSI connection solutions based on FPGA

Latest Power Management Articles
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号