Can (controller area network) is one of the most widely used field buses in the world. Compared with general bus communication, can data communication has outstanding reliability, real-time and flexibility. This paper designs a monitoring system based on CAN bus, and expounds the key problem of bit timing in detail. With the help of the system, users can realize the real-time monitoring of can network and optimize the system parameters.

Overall structure of monitoring system

The whole monitoring system (as shown in Figure 1) is a distributed control system, which is composed of three parts: upper computer, conversion module and can node. The upper computer is responsible for monitoring the operation of the whole system; As the communication intermediary between each node and the host computer, the conversion module realizes the two-way communication between the host computer and the node; Each can node can complete the tasks of uploading information and executing control commands at any time according to the communication protocol.

Design of distributed monitoring system based on single chip microcomputer and can controller SJA1000

Design of CAN node interface circuit

Each node on the CAN bus uses the 8-bit single chip microcomputer produced by ATMEL company as the main controller. In addition to completing the control function of the node itself, it also cooperates with the CAN controller SJA1000 to realize the can network communication function.

In the design, SJA1000 controller is selected and its Pelican expansion mode with superior performance is adopted. Can transceiver tja1050 is a high-speed can bus driver produced by Philips company. Tja1050 has the characteristics of high speed, low power consumption and superior electromagnetic performance. The can interface circuit is shown in Figure 2.

Design of distributed monitoring system based on single chip microcomputer and can controller SJA1000

The node module adopts high-speed optocoupler to realize the electrical isolation between the transceiver and the controller and anti-interference. The optocoupler selects the high-speed device tlp113 to meet the electrical response at the highest rate of 500 Kbps. DC / DC module is used to provide 5V power supply and realize electrical isolation between system power supply and network power supply. In order to ensure the safety of the system, TVs protection is added to the DC / DC module and the input and output terminals of the system.

Realization of system communication

Can controller protocol

The serial communication network implemented by CAN controller follows OSI model and is divided into two layers: data link layer and physical layer. The physical layer is in accordance with IEEE 802.1 The 3lan standard structure realizes the functions of sending data to the transmission medium and receiving data stream. The assembly of bit stream is realized in the data link layer. Four different frame formats are agreed in the can communication protocol. The standard frame format is used in this system, and its format is shown in Table 1. Can first receives the arbitration field, judges which frame format the received signal is according to the content of the arbitration field, and the user writes the corresponding data into the data field for transmission, or reads the received data from the data field.

Can application layer protocol

In can 2.0 specification, only physical layer and data link layer are specified. Users need to formulate application layer protocol according to their own needs. This design takes each node as the control object, and designs its application layer communication protocol. Generally, the information transmitted on CAN bus can be divided into two categories:

Command information: including the number command of the upper computer and the control command of the upper computer. In each cycle, the upper computer sends the command to each node through the can-rs232 conversion module. After receiving the status information, the upper computer sends the control command according to the situation and sends it to each node module through the conversion module.

Status information. After receiving the command information, each node collects the field data information, sends it to the can-rs232 conversion module, and then uploads it to the upper computer. Information includes: equipment switching status, voltage, electric flow, etc.

According to the actual situation of the system, the system can2 0 standard frame format (see Table 2), an application layer protocol for multi frame transmission is formulated. In the protocol, practical software filtering, that is, shielding the acceptance filter, allocates the second and third bytes including 11 bit identifiers in addition to the frame information, as shown in Table 3.

Design of distributed monitoring system based on single chip microcomputer and can controller SJA1000

The frame information can be determined according to the actual situation.

The identifiers ID10 ~ ID3 represent the module address, so the protocol can theoretically meet 256 control nodes. The identifiers Id2, id0 and the last six bits of the byte are defined as the frame type: command frame or status frame. The division of data information is simple and clear. The upper computer collects and judges the node information according to the module address and issues the corresponding command. The protocol also stipulates that the module is assigned an address from high to low and from low to high according to the priority, so as to ensure that the node with high priority can occupy the bus first in the bus competition.

System software design

Based on the overall design requirements and hardware composition, combined with the model structure of CAN bus protocol, the software design of the whole system can be divided into two parts: node part and host computer part.

The node part includes initialization module, can communication module, serial communication module and data processing module. Correct can initialization can make full use of the advantages of CAN bus and ensure the correct and reliable operation of CAN communication. The CAN node can be initialized only in the reset mode. The initialization mainly includes the setting of working mode, the setting of receiving filter mode, the setting of receiving mask register (AMR), the setting of receiving code register (ACR), the setting of baud rate parameter and the setting of interrupt permission register (IER). After completing the initialization setting of the CAN controller, the CAN controller can return to the working state and perform normal communication tasks. Other modules, no more details.

Host computer part: the host computer software is written by VC + + 6.0 software, and the human-computer interaction interface with windows style is written and generated by MFC class library. Software application MSComm control transmits and receives data through serial port, which provides serial communication function for application program. Microsoft communications control (MSComm) is an ActiveX control provided by Microsoft company to simplify serial communication programming under windows. It provides a simple method for applications to send and receive data through serial interface. Specifically, it provides two methods to deal with communication problems: event driven method and query method. We adopt an event driven approach. Users can monitor the whole system in real time through a good man-machine interface.

Bit period parameter determination

When initializing the CAN controller, the configuration of bus timing register and other important problems should be considered. In fact, the communication baud rate, the sampling point of bit cycle and the number of samples can be set independently, which provides space for users to optimize the network communication performance. If the bit period sampling point is offset, a large signal transmission delay can be accepted, and the transmission distance of the corresponding bus can be extended; If the sampling point of the cycle is close to the middle, the reference clock error between system nodes can be tolerated. These contradictions directly affect the performance of the network system, so the bus bit timing is very important. Reasonable bit timing can improve the overall performance of the system.

The CAN bus cycle consists of four parts: sync_seg, propagation delay, phase_seg1 and phase_seg2, as shown in Table 4.

Design of distributed monitoring system based on single chip microcomputer and can controller SJA1000

Synchronization mechanism

Can is a serial communication network that effectively supports distributed real-time control. Considering the synchronization mode of bit timing, it essentially belongs to asynchronous communication protocol. Each frame transmitted starts with the frame start bit, ends with the frame and the subsequent intermittent field. This requires that the receiver / transmitter must keep each bit in the intra frame information code strictly synchronized from the start bit of the frame. Considering the bit timing coding, it adopts the non return to zero coding mode. The bit stream transmission is not like the differential code, which can directly represent the synchronization signal with the change of level. It belongs to the self synchronization mode (the receiver tries to extract the synchronization information from the received signal), and the can node extracts the clock information from the transformation from one bit value to another bit value. In order to ensure the synchronization quality, can protocol defines its own bit synchronization methods: hard synchronization and resynchronization.

Through the synchronization mechanism, the influence caused by phase error can be eliminated to ensure the correct decoding of information. After hard synchronization, the internal bit time restarts from the synchronization period. Therefore, hard synchronization forces the synchronization edge to be within the restarted bit time synchronization period. As a result of resynchronization, phase buffer segment 1 is increased or phase buffer segment 2 is shortened. There is an upper limit on the number of phase buffer segments lengthened or shortened, which is given by the resynchronization jump width.

The phase error of an edge is given by the position of the edge of the relevant synchronization section. Phase error is defined as follows:

Le = 0 if the edge is in the synchronization segment;

Le “0” if the edge is before the acquisition point;

Le “0” if the edge is after the acquisition point in the previous bit.

When the amplitude of the phase error causing the resynchronization edge is less than or equal to the set value of the resynchronization jump width, the functions of resynchronization and hard synchronization are the same. When the magnitude of phase error is greater than the resynchronization jump width, there are two cases:

If the phase error is positive, the phase buffer section 1 is increased in the same range as the resynchronization jump width;

If the phase error is negative, the phase buffer section 2 is shortened by the same range as the resynchronization jump width.

Hard synchronization and resynchronization are two forms of synchronization, which follow the following rules:

Only one synchronization is allowed in a bit time;

Only when the value detected before the acquisition point is inconsistent with the bus value immediately after the edge, it can be used as synchronization;

During the idle period of the bus, there is a transition from “implicit” to “explicit”, and hard synchronization will be performed at any time.

Bit time parameter calculation rules

The bit time parameter calculation rule is to ensure that the system can correctly receive and decode the information frame on the network between two nodes in extreme conditions. Extreme conditions mean that the clock vibration deviation of the two nodes is at both ends of the system tolerance deviation limit, and there is the maximum transmission delay between the two nodes. In normal communication without noise interference, the worst case of phase error accumulation is that there are 10 bit cycles (5 dominant bits followed by 5 recessive bits) between resynchronization edges. The actual system operates in a noisy environment. Due to noise interference, the interval between resynchronization edges may exceed 10 bit cycles. In this case, it must be strictly adopted, otherwise it may enter the error processing mode. The node modules of the monitoring system take SJA1000 as can controller. Therefore, considering the influence of all aspects, the formula of bit timing parameter when there is only one sampling point is set as follows:

( sjw) min =max{(20 × nbt ×δ f)/(1 – δ f) ,((nbt(1-25 ×δ f )- prop max -(1 – δ f)+prop min/2))/(1- δ f)}

( s j w) max = 4

t(seg2) min = max{2 , s j w}

t(seg2) max =min{8,((n b t (1-25 ×δ f ) – ( pro p) max)/

(1 – δ f), (nb t(1 – 25 ×δ f) – ( pro p)

max – (1- δ f) + prop min )/ 2) / (1- δ f )

The above is the calculation rule of bit time parameters. In the actual monitoring system, the bit timing parameters are determined and optimized according to this rule according to appropriate methods to optimize the system performance.


Fieldbus system is a real-time network control system that uses an open fieldbus control communication network to interconnect the lowest field controller and field intelligent instrument equipment. It is a new stage in the development of industrial control system. The monitoring system based on CAN bus designed in this paper has simple and convenient design and stable performance. It can well meet the requirements of real-time and reliability of the control system, and runs in the field with good results.

Responsible editor: GT

Leave a Reply

Your email address will not be published. Required fields are marked *