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.
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.
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.
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.
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.
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: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
- Popular Resources
- Popular amplifiers
- Siemens S7-1200-PLC Programming and Application Tutorial (3rd Edition) (Edited by Shi Shouyong)
- Modern Compiler Principles C Language Description (Ampel)
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Research on motion planning and tracking control methods for emergency collision avoidance of driverless vehicles
- 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
- EEWORLD University Hall----Live Replay: Detailed Explanation of NXP Embedded Human Machine Interface Solutions
- Analysis of four constant current source circuits
- C54x has a small problem with my_sect when configuring CMD
- Sorting out the msp430f149 port functions and settings
- FPGA introductory series experimental tutorial——
- Grounding of electronic equipment
- What is the difference between the full-port and half-port of Zhongying SH367309 5-16 series lithium battery protection chip?
- 【DIY Creative LED】The soldered board has normal charging function
- ESP32 SD/MMC hardware connection method
- Domestic chips are amazing. I just applied for a board. CH579 is so small but it integrates network, USB, and Bluetooth.