Abstract: This paper describes in detail the design and implementation of a CAN bus and RS 232 interface card using the AT89S51 microcontroller and the SJAl000 bus controller; a synchronous serial communication protocol is designed by strengthening the serial communication protocol. This interface card can easily establish communication between the computer and the CAN bus, and enables the designer of the CAN bus to easily observe the operation of the bus and the data sent by each node. The actual operation confirms its reliability and ease of use.
Keywords: CAN bus; RS 232; serial communication protocol; bus controller
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 Hardware design of interface card
The hardware part mainly includes the main controller, CAN bus controller, CAN bus transceiver and RS 232 communication interface chip with computer. The overall structure of the system is shown in Figure 1. The main controller receives and caches data through the CAN bus interface. Selective data transmission is performed according to the command of the PC.
1.1 Interface with CAN bus
As shown in Figure 2, the controller of the interface card is Atmel's AT89S51, which is pin- and function-compatible with 8051 and has 4 KB of FLASH ROM inside, supporting in-system programming (ISP), so that the application program can be easily downloaded to the microcontroller.
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 performs 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 capabilities for the bus and differential reception capabilities for the CAN controller, with a maximum speed of 1 Mb/s. Due to the 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 and improve the anti-interference and reliability of data communication. The specific circuit is shown in Figure 2.
1.2 RS 232 interface
RS 232 standard is a serial physical interface standard developed by the Electronic Industry Association (EIA) of the United States. It is suitable for communication with data transmission rates in the range of 0 to 20,000 b/s. It is currently widely used for near-end connections between computers and terminals or peripherals. In actual operation, the voltage level should be between 5 and 15V. Therefore, in order to communicate with the microcontroller, its voltage 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.
2 Software Design of Interface Card
2.1 Software Design of RS 232 Interface
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.
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 MCU to PC. The frame starts with 0X11H 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 a specific node, the MCU starts with 0X10H as the response, 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 MCU to the PC, with the starting byte of 0X22H. Every time SJA1000 receives valid data, it will cause the MCU to generate a receive interrupt. The MCU will first read the data into the RAM for temporary storage during the interrupt processing, and then calculate its data length and checksum, encapsulate the data into frames, and then send the frames to the PC for processing and display through the serial port.
Various 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 receiving the data, the PC 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 Transparency Analysis 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 has the same data 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. This is the transparency problem of data transmission. There is a better solution for serial port data transmission. This method converts data into ASCII characters for transmission, that is, splitting each byte of data, splitting a hexadecimal byte into two bytes, such as 5AH, 5BH, 5CH, ..., can be split into 50H, 0AH, 50H, 0BH, 50H, OCH, ..., and after the PC receives the data, it will combine the data in pairs. In this way, only 32 bytes are needed to combine into any data from 00H to 0FFH. However, 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 adopted another method of escaping characters. First, an escape character identifier OXDBH is defined. For the CAN bus, the maximum data length of each frame is 8. Even if the CAN frame information and identification code are all sent as data, the maximum length is only 13 B. It can be confirmed that under normal circumstances, the high 4 bits of the data length byte in the data frame must be 0. Therefore, before sending data, first check whether there is data identical to the start byte 0X22H, the end byte 0X33H and the escape identifier 0XDBH in the data. If not, send it normally; if yes, detect the number of data identical to 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 before these data, and escape the data. For example, if the data is 0X22H, 0X33H, 0XDBH..., it will be escaped 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 traffic will not be very high.
2.3 CAN interface program design
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 packaged into a frame and sent to the PC.
2.4 PC-side program design
The PC-side program is developed using VB 6.0. VB 6.0 is more convenient in the production of graphical interfaces. Using the Mscomm component, you can easily create a serial port monitoring environment. Just add the above protocol during software design. Figure 5 shows the debugging of software developed using VB 6.0.
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.
Previous article:Design of uncooled infrared focal plane temperature control circuit based on ADN8830
Next article:Application of telemetry technology in rocket engine overload test
- Popular Resources
- Popular amplifiers
- Molex leverages SAP solutions to drive smart supply chain collaboration
- Pickering Launches New Future-Proof PXIe Single-Slot Controller for High-Performance Test and Measurement Applications
- CGD and Qorvo to jointly revolutionize motor control solutions
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Nidec Intelligent Motion is the first to launch an electric clutch ECU for two-wheeled vehicles
- Bosch and Tsinghua University renew cooperation agreement on artificial intelligence research to jointly promote the development of artificial intelligence in the industrial field
- GigaDevice unveils new MCU products, deeply unlocking industrial application scenarios with diversified products and solutions
- Advantech: Investing in Edge AI Innovation to Drive an Intelligent Future
- CGD and QORVO will revolutionize motor control solutions
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Play with smart speaker design: Hold TI system solutions and look forward to unlimited possibilities in the future!
- Things you don’t know about Qorvo’s acquisition of Active-Semi!
- The problem of program running away
- Raspberry Pi Speaker Pirate Audio
- Will a short circuit between 3.3V and GND in a microcontroller system burn the microcontroller?
- Summary of DIY Bing Dwen Dwen works
- [Shanghai Hangxin ACM32F070 development board + touch function evaluation board evaluation] + development environment construction and download test
- Do you know the future star material in the RF field?
- There is no virtual serial port when the STM32F0discovery development board is plugged into the computer?
- Please advise everyone!