Design and Implementation of CAN/RS 232 Interface Card

Publisher:雅致书香Latest update time:2010-04-10 Source: 现代电子技术 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Controller Area Network (CAN) belongs to the category of fieldbus. Its bus specification has been formulated as an international standard by ISO International Organization for Standardization and is recognized as one of the most promising fieldbuses. CAN bus is widely used for data communication between various detection and actuators in control systems. Because the transmission and processing of CAN bus data are completed by the node's single-chip microcomputer. This makes it impossible for designers and observers to grasp the operation of the bus in a more intuitive way. There are two main CAN interface cards on the market, using PCI bus and USB interface. The former has the characteristics of fast transmission speed, but it takes up a computer slot and is very difficult to design; the latter has a fast transmission speed and is relatively cheap, but it takes a lot of effort to develop and maintain USB firmware and write complex drivers. Based on this, it is necessary to design an interface card that can easily read CAN bus data and send the data to the PC for processing and display through the RS 232 serial port; in addition, it is necessary to strengthen its serial transmission protocol so that it can effectively reduce errors in the transmission process.

1 Interface Card Hardware Design

The hardware part mainly includes the main controller, CAN bus controller, CAN bus transceiver and RS 232 communication interface chip with the computer. The overall structure of the system is shown in Figure 1. The main controller receives and caches data through the CAN bus interface and performs selective data transmission according to the command of the PC.

Overall structure of the system

1.1 Interface with CAN bus

As shown in Figure 2, the controller of the interface card uses Atmel's AT89S51, which is pin- and function-compatible with 8051 and contains 4 KB of FLASH ROM to support in-system programming (ISP), so that the application can be easily downloaded to the microcontroller.

Interface card controller

The CAN bus controller uses the SJA1000 produced by Philips. This chip is a substitute for the PCA82C200 CAN controller, and a new PeliCAN working mode is added to support the CAN 2.0B protocol. It also supports error interrupts, alarm limits, acceptance filter extensions, and self-receive requests. In the system, the interrupt output pin INT of the SJA1000 is connected to the interrupt input pin of the microcontroller, so that an interrupt can be generated when receiving data or bus errors, and the controller will perform corresponding processing. The chip select CS of the SJA1000 is connected to the P2.7 pin of the microcontroller, so that the address of the SJA1000 is determined to be 7F00H~7FFFH.

The chip 82C250 is selected as the driver interface between the CAN controller and the physical bus, which can provide differential transmission capability for the bus and differential reception capability for the CAN controller, with a maximum speed of 1 Mb/s. Due to interference in the actual system, a high-speed optical isolation 6N137 is added to play an isolation role. The electrical interface is very simple, with two wires, CANH and CANL, to complete CAN data transmission. There is a 120Ω resistor at both ends of the CAN bus, which is used to match the bus impedance to improve the anti-interference and reliability of data communication. The specific circuit is shown in Figure 2.

1.2 Interface with RS 232

RS 232 standard is a serial physical interface standard established by the Electronic Industry Association (EIA) of the United States. It is suitable for communication with data transmission rate in the range of 0 to 20,000 b/s. It is currently widely used for near-end connection between computers and terminals or peripherals. In actual operation, the level should be guaranteed to be between 5 and 15V. Therefore, in order to communicate with the microcontroller, its level must be converted. The commonly used conversion chip is MAX232. MAX232 has a voltage multiplier circuit and a conversion circuit inside. It only needs a 5V power supply to realize the conversion between TTL level and RS 232 level, which is very convenient to use. The serial port interface circuit is shown in Figure 3.

Serial port interface circuit

2 Interface Card Software Design

2.1 RS 232 interface software design

The data communication between the microcontroller and the PC requires the design of a communication protocol, which is carried out in a synchronous communication mode. The communication protocol is divided into three formats: command frame, response frame and data frame. Each frame contains a start byte, data length, check byte and frame end byte. The specific format is shown in Figure 4.

Communication Protocol

The command frame has 6 bytes in total, and the transmission direction is from PC to MCU. 0X00H is used as the frame start, and the data length is 2. When data 1 and data 2 are both 0X00, it means starting the interface card. After receiving the signal, the MCU will initialize SJA1000 and feedback the result to the PC (success or failure); when data 1 and data 2 are both 0XFFH, it means that the interface card is required to stop working, and the MCU will put SJA1000 into sleep mode to exit the bus; when data 1 is 0X01H, it means that the PC requires to receive only the data of a certain node, and data 2 indicates the node number of the contact. The MCU will reset the acceptance filter of SJA1000 to shield other unnecessary node data.

The response frame has 6 bytes in total, and the transmission direction is from the MCU to the PC. 0X11H is used as the frame start, and the data length is 2. When the MCU receives the start or stop command from the PC and is successfully set, it sends two bytes of data with the same value of 0X00H to the PC. If the setting fails, it sends two bytes of data with the same value of 0XFFH to the PC. For commands from the PC to read specific nodes, the MCU uses 0X10H as the response start, and two bytes with the same value of 0X00H indicate success, and two bytes with the same value of 0XFFH indicate failure.

The data frame is the data read by the interface card from the bus. Because the data on the bus is divided into standard frames, remote frames, etc., and the length of the data loaded in each frame is unknown, the length of the data frame is uncertain, and its transmission direction is also from the microcontroller to the PC, and the starting byte is 0X22H. Every time SJA1000 receives valid data, it will cause the microcontroller to generate a receiving interrupt. During the interrupt processing, the microcontroller first reads the data into the RAM for temporary storage, then calculates its data length and checksum, encapsulates the data into frames, and then sends the frames to the PC through the serial port for processing and display.

All types of frames use 0X33H as the frame end mark. The checksum byte is used to ensure the accuracy of the data. The microcontroller adds each byte before sending the data and fills the result into the checksum byte. After the PC receives the data, it recalculates the checksum and compares it with the checksum sent by the microcontroller. If they are the same, it is received correctly, otherwise the microcontroller is required to resend.

2.2 Analysis of the transparency of the protocol

In the process of synchronous data transmission, the start byte and end byte of the frame are very important, which can ensure the accuracy of the data frame. However, because of the existence of these two bytes, another problem to be solved is also introduced. When the data field is the same as the start byte, it is very likely to cause the PC to regard it as a new frame start, resulting in data processing and display errors, which is the transparency problem of data transmission. There is a better solution for serial port data transmission. This method converts the data into ASCII characters for transmission, that is, splits each byte of data, and splits a hexadecimal byte into two bytes, such as 5AH, 5BH, 5CH, ..., which can be split into 50H, 0AH, 50H, 0BH, 50H, OCH, ..., and the PC will then combine the data in pairs after receiving the data. In this way, only 32 bytes are needed to combine into any data from 00H to 0FFH. But the disadvantages of this method are also obvious. The amount of effective data sent by it is twice that of normal transmission. For a communication medium such as a serial port with a low transmission speed, it is not an advisable method when the amount of data is large.

In this interface card, the author adopts another method of escaping characters. First, define an escape character identifier OXDBH. For the CAN bus, the maximum data length of each frame is 8. Even if the frame information and identification code of CAN are all sent as data, the maximum length is only 13 B. It can be confirmed that under normal circumstances, the upper 4 bits of the data length byte in the data frame must be 0. Therefore, before sending the data, first check whether there is data that is the same as the start byte 0X22H, the end byte 0X33H and the escape identifier 0XDBH in the data. If not, send it normally; if so, detect the number of data that is the same as these two bytes, and fill the number into the upper 4 bits of the data length byte; at the same time, add the escape character identifier to the front of these data, and escape the data. For example, the data 0X22H. 0X33H, 0XDBH..., then escape to 0XDBH. 0XD2H. 0XDBH, 0XD3H, 0XDBH, 0XDDH... After receiving the data frame, the PC first checks the data length byte to see if its upper 4 bits are 0. If they are 0, it means that there is no escape character in the data group and it can be processed directly; if they are N, it means that there are N escape characters in the data group, and each escape character starts with 0XDB, so they need to be modified back to the original data first. In comparison, this method has good stability and interface utilization, and its data flow will not be very high.

2.3 CAN interface programming

The interface program of the CAN bus is mainly to perform the initialization process of SJA1000 and the subsequent receiving interrupt processing process. CAN initialization mainly sets the communication parameters of CAN. The CAN control registers that need to be initialized are: mode register (MOD), acceptance code register (ACR), acceptance mask register (AMR), bus timing register (RTR), output control register (OC), interrupt register (IR) and interrupt enable register (IER). These registers can only be written and accessed when SJA1000 is in reset state. When SJA1000 enters the working mode, it can automatically receive the data on the bus and put it into the receiving buffer. At the same time, it generates a receiving interrupt. After the microcontroller enters the interrupt, it reads the data from the buffer and releases the buffer. After the interrupt returns, the data is encapsulated into a frame and sent to the PC.

2.4 PC programming

The PC-side program is developed using VB 6.0. VB 6.0 is more convenient in creating graphical interfaces, and the use of Mscomm components can easily create a serial port monitoring environment. Just add the above protocol when designing the software. Figure 5 shows the debugging of the software developed using VB 6.0.

Software debugging status

3 Conclusion

The CAN bus interface card has been verified by experiments and proved to meet the expected requirements, can monitor the data on the bus well, and has a simple structure and great utilization value. When the data flow of the CAN bus system is large, you can consider using the USB interface to communicate with the PC, which can better solve the problems of speed matching and limited application occasions.

Reference address:Design and Implementation of CAN/RS 232 Interface Card

Previous article:Research on MAC Protocol for Wireless Multimedia Sensor Networks
Next article:Design of infrared remote control keyboard for industrial control computers

Recommended ReadingLatest update time:2024-11-16 18:04

A brief discussion on ERS system and applicable liquid level gauges
  ERS (Electronic Remote Sensor) electronic remote transmission system solves common problems in level measurement on high containers and towers. The ERS system is a multi-parameter system that provides additional process optimization control information. In addition to level calculation, ERS provides real-time access
[Test Measurement]
A brief discussion on ERS system and applicable liquid level gauges
Remote Online Upgrade of Embedded System Software Based on GPRS
With the rapid development of electronic technology, computer technology and communication technology, embedded systems have been widely used in the fields of industry, military, communication, information appliances, etc. However, in the face of the continuous emergence of new technologies and the continuous improv
[Analog Electronics]
Remote Online Upgrade of Embedded System Software Based on GPRS
Tektronix USB spectrum analyzer RSA306 application details
     The RSA306 is the first USB spectrum analyzer from Tektronix , and similar instruments will be introduced that can take advantage of desktop and laptop computers with high-speed USB3.0 interfaces and processing power to handle complex test and measurement analysis. The RSA306 is priced much lower than traditional
[Test Measurement]
Brief Analysis of the Principle of Dual-machine Communication Based on Serial Port RS485 in STM32
I believe everyone knows about RS485 communication. When learning about RS232, we will compare it with 485 (RS485 will be replaced by 485 below). Without talking about the advantages and disadvantages of 485, there are some articles on the Internet that I learned 485 communication using the STM32 library function, so
[Microcontroller]
Brief Analysis of the Principle of Dual-machine Communication Based on Serial Port RS485 in STM32
Solutions for high-frequency transient interference of RS-232/485 (Figure)
RS-232/485 is a widely used serial interface in the industrial control industry. RS-232/485 is defined as a single-ended standard that increases the communication distance in low-speed serial communication. However, in actual circuit applications, it is often affected by interference and surges. There are many reasons
[Power Management]
Solutions for high-frequency transient interference of RS-232/485 (Figure)
Summary of C52 serial communication RS232
1. The microcontroller has two pins, rxd and txd, which are responsible for receiving data. 2. There are four serial communication modes: 0123, which can also be divided into adjustable baud rate and fixed baud rate. They can be set as a whole in SCON or bit-set. 3. Adjustable baud rate is handled by timer 1. Please
[Microcontroller]
AARONIA Real-time Spectrum Analyzer V6-RSA250X
product description AARONIA V6-RSA250X is a new generation of high-performance real-time spectrum analyzer developed and produced by AARONIA. Like all AARONIA products, SPECTRAN V6 X is developed and manufactured in Germany according to high quality standards. With the launch of V6, AARONIA's real-time spectrum analyz
[Test Measurement]
AARONIA Real-time Spectrum Analyzer V6-RSA250X
Design of non-polarity wiring for RS422/RS485 network
1. Problem Many current measurement and control systems are interconnected using RS422 or RS485 buses. RS422/485 bus signals are transmitted by 4 (2) polarized differential signals, which cannot be reversed. When the network transmission distance is long or there are many nodes, there will be many junction boxe
[Industrial Control]
Latest Industrial Control 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号