Design and implementation of single-chip peer-to-peer network based on RS485 bus

Publisher:骄阳少年Latest update time:2012-02-07 Source: 现代电子技术 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1 Introduction

With the application and development of automatic control technology, control systems are becoming more and more complex. The application system of single-chip microcomputers has gradually developed from single-machine to multi-machine networking, such as data acquisition, fire protection, access control, consumption and other control systems, which requires the formation of a network between single-chip microcomputers or between single-chip microcomputers and microcomputers for mutual communication. The existing various bus products, such as Foundation Bus (FF), LonWorks Bus, ProfiBus Bus, HART Bus and CAN Bus, have their own characteristics, but due to their poor compatibility, high cost, inconvenient maintenance and other reasons, they have not been widely used.

RS485 bus is a simple and cheap communication technology. Its transceiver adopts balanced drive and differential reception, and has the ability to suppress common mode interference. The sensitivity of RS485 receiver can reach ±200 mV, which greatly improves the communication distance. The cable length can reach 1200 m at a rate of 100 kb/s. If the communication distance is shortened, the maximum rate can reach 10 Mb/s. RS485 bus allows multiple drivers and receivers. The latest transceiver can carry 128 nodes for building multi-point communication network. Because RS485 bus has the characteristics of simple equipment, low price and long-distance communication, it has been widely used in engineering.

Usually in the application of RS485 bus products, the master-slave control mode is adopted, that is, one microcomputer or one single-chip microcomputer is used as the master, and the rest are slaves. The master and slaves can communicate directly, and the slaves must communicate through the master. The communication method is that the master polls each slave. This method greatly simplifies the software design, but it also brings many disadvantages.

(1) Since the host polling method is used for communication, the communication throughput is low, which is not suitable for occasions with large communication requirements or low average communication volume but bursty, such as: consumption system, attendance system, etc.
(2) Since the host polling method is used for communication, the real-time performance is poor when the system is large.
(3) Since the host polling method is used for communication, the host is constantly querying each slave, and each slave must respond to the host's query to decide whether to communicate with it. This will inevitably increase the system overhead of each slave.
(4) When slaves need to communicate with each other, they must go through the host, which increases the difficulty of communication between slaves.

In view of these shortcomings of the master-slave network, this paper proposes a design method for a single-chip microcomputer peer-to-peer network using the RS485 bus. In this network, there is no master-slave distinction between nodes (single-chip microcomputers), which is completely high in throughput and flexible. Finally, the C51 language implementation method of the network is given.

2 Network Principles

In a master-slave network, the channel is controlled by the host, and there is no problem of channel competition. In a peer-to-peer network, it is different. Since each node can send information at any time according to its own needs, it will inevitably compete for the use of the channel. Therefore, how to reasonably allocate the right to use the channel becomes a key issue in a peer-to-peer network. Similar to computer networks [1], static allocation schemes and dynamic allocation schemes can be used to solve the channel allocation problem. In the static allocation scheme, time division multiple access (TDMA) is a commonly used method. In TDMA, time is evenly divided into N time slots, and each node statically occupies one. If each node can make full use of its own time slot to send data, the scheme is very flexible and efficient.

However, in practice, some nodes have a lot of data and insufficient time slots, while some nodes have very little data and cannot fully utilize the time slots allocated to them, leaving this time idle. Therefore, the static channel allocation scheme cannot effectively handle the burstiness of communication. In the dynamic allocation scheme, the carrier sense multiple access with collision detection CSMA/CD (carrier sense multiple access with collision detection) protocol is a commonly used method.

In this protocol, when a node wants to send data, it first listens to the channel to see if there are other nodes sending. If no other node is sending, it starts sending, otherwise, it waits for a random time and repeats the above process. When two nodes hear that the channel is idle and start transmitting at the same time, a conflict is bound to occur. At this time, they will detect the conflict almost at the same time. Once a conflict is detected, instead of continuing to transmit their frames, they stop as soon as possible, which saves both time and bandwidth. After waiting for a random time, the above transmission process is restarted.

CSMA/CD can effectively solve the burstiness of communication. When the load is low, the node that wants to send data can send it immediately. When the load is heavy, it can also ensure the stability of communication. This article will use this protocol to realize network communication.

3 Hardware Circuit Design

The key to implementing the CSMA/CD protocol is carrier sensing and collision detection, which requires each node to be able to receive data sent by all nodes, including itself.

In the network composed of RS485 bus, since the serial port of the single-chip microcomputer is TTL level, it is necessary to complete the level conversion and data transmission and reception through the 485 dedicated transceiver chip. The more commonly used SN75176 is selected here. The chip combines the 3-state differential line driver and the differential input line receiver. The driver and the receiver have high-level effective and low-level effective enable terminals respectively, as shown in Figure 1. In the master-slave network, the enable terminals of the driver and the receiver are generally connected together to play a role in direction control, so that the transceiver works in half-duplex state. Here the transceiver must work in full-duplex state so that the node can receive the information it sends. Therefore, the enable terminal of the receiver is connected to a low level, and the enable terminal of the driver is controlled by the IO port of the single-chip microcomputer. It is usually low level, releasing the bus, and converted to high level when data is to be sent. The single-chip microcomputer peer-to-peer network formed in this way is shown in Figure 2.


4 Software Implementation

This paper uses Franklin C51 to implement the CSMA/CD network protocol. Using C language to write the microcontroller target system software can shorten the development cycle, increase the readability of the software, and facilitate improvement and expansion, thereby developing a larger-scale and more complete system [2].

Timer 0 is set as a delay timer in the system. It has two sets of delay parameters. One set is used to monitor whether the network is idle. Each time an interrupt is received, the delay parameters are reloaded to Timer 0 and the network busy flag is set. The value of this parameter should ensure that the timer will not overflow during normal data transmission. In this way, if a node is sending data, Timer 0 will be repeatedly reloaded and the network busy flag will be set. If all nodes stop sending data, the timer will overflow. At this time, the timing will stop and the network busy flag will be cleared, indicating that the network is idle. The sending program can start sending data after detecting this flag. Every time the sending program sends 1 B of data, the data is stored in a temporary variable. At this time, the receiving program will also receive a data and compare the received data with the data in the temporary variable. If they are equal, it means that the data is sent successfully. Otherwise, it means that a conflict occurs. At this time, the transmission is stopped immediately and a random delay value is set for Timer 0. After the delay ends, the above process is repeated. The C51 program that implements the above algorithm is as follows [3]:
[page]






5 Conclusion

This paper designs a single-chip microcomputer peer-to-peer network based on RS485 bus and implements the network with C51 language. Compared with the commonly used master-slave single-chip microcomputer network, the peer-to-peer network has the characteristics of large data throughput and direct communication between nodes. Therefore, it is particularly suitable for occasions with large communication volume, burst communication or frequent communication between nodes. The network has been actually applied in the access control system. Practice shows that this method is very effective and flexible.

References

[1]Andrew S. Tanenbaum Computer Networks[M]. 3rd Edition. Beijing: Tsinghua University Press,1998.
[2]Ma Zhongmei. C Language Application Design for Microcontrollers[M]. Revised Edition. Beijing: Beijing Aeronautics and Astronautics Press,1999.
[3]KeilC51 Usage Skills and Practice[DB]. http://www.zlgmcu.com.
Reference address:Design and implementation of single-chip peer-to-peer network based on RS485 bus

Previous article:Realizing Sun Tracking Using 80C196KC Single Chip Microcomputer
Next article:I2C Bus Extension and I2C Virtual Technology for MCU

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号