Communication interface design between CAN bus and RS-485 bus
2026-04-06 04:49:44··#1
1. Introduction to CAN Bus and RS-485 Bus CAN bus (Controller Area Network) is a serial data communication protocol developed by Bosch in Germany in the 1980s specifically for automotive electronic control systems. It is particularly suitable for distributed control or real-time control applications. Its applications span high-speed networks and low-cost distributed control systems. CAN has the following characteristics: (1) Non-destructive arbitration based on priority; (2) Multiple master stations access the bus based on priority; (3) Strict error detection and definition; (4) Support for remote data requests; (5) Transmission of multi-address frames with the aid of receiver filtering; (6) Diverse communication media and flexible combination methods; (7) Full system data compatibility and system flexibility; (8) Maximum transmission distance of 10Km (bus transmission rate of 5kbps), maximum transmission rate of 1Mbps (bus transmission distance of 40m). The RS-485 standard is a serial communication protocol published by the EIA (Electronic Industries Association) of the United States. It boasts a longer transmission distance, higher speed, and better anti-interference performance than the RS-232C standard signal, making it widely used in various industrial, building, and energy fields. In practical applications, many industrial control devices (such as PLCs) are equipped with RS-485 interfaces. Therefore, it is necessary to design a communication interface circuit between the CAN bus and the RS-485 bus. 2. Hardware Design The entire communication interface circuit consists of an AT89C52 microcontroller, an SJA1000 CAN controller, an 82C250 CAN transceiver, a 6N137 high-speed optocoupler, and a MAX485 RS-485 transceiver, as shown in Figure 1. The AT89C52 microcontroller primarily handles the initialization of the SJA1000 and the data communication between the CAN bus and the RS-485 bus. The SJA1000 is a replacement for the 82C200 CAN controller. It mainly consists of the following functional modules: interface management logic, transmit buffer, receive buffer, receive filter, bit stream processor, bit timing logic, and error management logic. In addition to being compatible with the 82C200, it adds many new features: PeliCAN working mode, error counter for read/write access, reception and transmission of standard and extended structure information, programmable error limit alarm, listen-only mode and arbitration loss interrupt. The microcontroller controls the internal registers of the SJA1000 by accessing external registers. The CAN transceiver 82C250 is the interface between the CAN bus controller and the physical bus, mainly responsible for providing differential transmission capability to the bus and differential reception capability to the CAN controller. Its pin 8 (RS) controls the selection of three different working modes: standby, slope control and high speed. (1) When RS is connected to a high level, the 82C250 operates in low-current standby mode; (2) If RS is grounded through a connecting resistor, it enters the slope control mode, which can adjust the slope by changing the resistance value, suitable for low-speed and short bus length applications; (3) If RS is grounded, the high-speed working mode is selected. The high-speed optocoupler 6N137 achieves electrical isolation between the CAN controller and the transceiver, enhancing system stability and anti-interference capabilities. The RS-485 transceiver MAX485 is a differential balanced low-power transceiver containing one driver and one receiver, powered by a single +5V supply. It is specifically designed for conversion between the TTL protocol (a communication protocol applicable to various CPUs) and the RS-485 standard, enabling microcontrollers to directly perform serial communication using the RS-485 serial communication protocol. The entire conversion process can be divided into: CAN bus to RS-485 bus conversion and RS-485 bus to CAN bus conversion. The CAN bus to RS-485 bus conversion process is as follows: the microcontroller receives the message sent by the CAN bus node, extracts the useful information from the message according to the data format specified by the CAN bus, and then sends the information to the RS-485 bus through the MAX485. The RS-485 bus to CAN bus conversion is the reverse process. 3. Software Design The main task of the software design is to realize the transmission of data and control commands between the CAN bus network and the RS-485 bus. It mainly includes the initialization program for the RS-485 and CAN interfaces, message sending and receiving programs, interrupt service routines, and error handling programs. Among these, the initialization program for the SJA1000 is the most challenging part of the software design, requiring the setting of various operating parameters. The control register is used to change the behavior of the CAN controller, including entering the reset state and activating different types of interrupt sources, such as receive interrupt, transmit interrupt, and data overflow interrupt. The acceptance code register and acceptance mask register work together to define the conditions under which a message can be received by the node: the corresponding bits of the identifier of the received information are equal to the corresponding bits of the acceptance code in the acceptance code register. The acceptance mask register determines which bits of the acceptance code are relevant or irrelevant to the acceptance filter. Bus timing register 0 defines the preset baud rate and synchronization jump width; bus timing register 1 defines the length of the bit period, the position of the sampling point, and the number of samples. The output control register can set different output modes (such as normal output mode, bi-phase output mode, and clock output mode). The clock divider register not only controls the output frequency of the SJA1000 and disables the CLKOUT pin, but also allows selection between Basic CAN and Peli CAN modes. The specific SJA1000 initialization procedure flow is shown in Figure 2. The CAN bus to RS-485 bus conversion program is as follows: CAN-RS485_TRANSITION: MOV DPTR, #SR; Status register MOVX A, @DPTR; JNB ACC.0, CAN_QUIT; Check if information has been received; exit the conversion program if no data is received. CAN-RS485_HANDLE: MOV DPTR, #CANR; Receive buffer start address MOVX A, @DPTR; Receive frame format JNB ACC.6, RECEIVE_FRAME; RTR=1 indicates a remote request frame LCALL TRANSMIT_DATA; Send the requested data LJMP CAN_QUIT; Exit the conversion program RECEIVE_FRAME: MOVX A, @DPTR; ANL A, #0FH; ADD A, #05H; Get the length of the received data MOV R7, A MOV R0, #43H; Set the MCU receive data buffer DATAFRAME: MOVX A, @DPTR ; Receive data MOV @R0, A LCALL RS485_SEND ; Forward valid information to the RS-485 bus via MAX485 INC DPTR INC R0 DJNZ R7, DATAFRAME ; Determine if reception is complete RECOUT: MOV DPTR, #CMR MOV A, #04H ; Release the receive buffer MOVX @DPTR, A CAN_QUIT: RET ; Exit the conversion program 4 Conclusion CAN bus is one of the most widely used fieldbuses, while RS-485 bus is a commonly used serial bus. The mutual conversion between the two has high practical value. The CAN-RS485 communication interface circuit designed in this paper can effectively solve the mutual conversion problem between these two serial data buses and has been successfully applied to the intelligent traffic control system in a certain region.