Realizing CAN Bus Communication Using 87C196NT Single Chip Microcomputer[Copy link]
1 Introduction to CAN bus CAN (Controller Area Network) is a field bus mainly used for detection and control of various equipment. CAN bus was originally designed by Bosch, Germany, for automobile detection and control systems. CAN bus has a unique design concept, good functional characteristics and extremely high reliability, and strong anti-interference ability on site. Specifically, CAN bus has the following characteristics:
* Simple structure, only 2 wires connected to the outside, and internal error detection and management modules. * Flexible communication mode. It can work in multi-master mode, and any node on the network can actively send information to other nodes on the network at any time, regardless of master and slave. * Data can be sent and received in point-to-point, point-to-multipoint and global broadcast modes. * Node information on the network can be divided into different priorities to meet different real-time requirements.
* The CAN bus communication format adopts a short frame format, with a maximum of 8 bytes per frame, which can meet the general requirements of control commands, working status and test data in the general industrial field. At the same time, 8 bytes will not occupy the bus for too long, thus ensuring the real-time nature of communication. * Adopts non-destructive bus arbitration technology. When two nodes send data to the bus at the same time, the node with low priority will actively stop sending data, while the node with high priority can continue to transmit data without being affected. This greatly saves the bus arbitration conflict time, and the network will not be paralyzed when the network load is heavy.
* The maximum direct communication distance can reach 10km (rate below 5kb/s), and the maximum communication rate can reach 1Mb/s (the maximum distance is 40m); the number of nodes can reach 110, and the communication medium can be twisted pair, coaxial cable or optical fiber. * The CAN bus communication interface integrates the physical layer and data link layer functions of the CAN protocol, which can complete the framing of communication data, including bit filling, data block encoding, cyclic redundancy check, priority discrimination and other tasks. * The CAN bus uses CRC check and can provide corresponding error handling functions to ensure the reliability of data communication.
* The above characteristics of the CAN bus provide a new solution for high-reliability data transmission in industrial control systems. It has been widely used in the field of industrial control abroad, and many domestic industrial control fields have also begun to use CAN-based field buses. CAN bus has become one of the four most promising field buses. 2 Hardware design of single-chip control system
This design uses the 87C196NT single-chip microcomputer in the Intel 196 series. The 87C196NT has an addressing space of 1MB. It does not have a CAN controller, so to achieve communication with the CAN bus, an external CAN controller and CAN driver chip are required. In this design, the author uses LTL-CAN. LTL-CAN is a CAN bus control and drive isolation transceiver, which is made of CAN controller chip PCA82C200, CAN driver chip PCA82C250, 16MHz crystal oscillator and optical isolation circuit in thick film packaging. It only supports standard information frame format, and its internal logic function diagram is omitted (see "Single-chip Microcomputer and Embedded System Application" 2002.8 for details). The interface circuit diagram between the single-chip microcomputer and the CAN bus is omitted (see "Single-chip Microcomputer and Embedded System Application" 2002.8 for details). In the figure, LTL-CAN is the interface between the CAN controller with optical isolation and the physical bus, providing differential transmission and reception functions for the bus. Resistor R1 is used as the matching resistor of the CAN terminal. The chip select signal CSCAN of the LTL-CAN chip is generated by GAL16V8 decoding, and its address is 08000H-08FFFH. The P3 port of 87C196NT is a data/address multiplexing port. 3 Software Design of Single-Chip Microcomputer Control System 3.1 CAN Information Packet Format Description A valid CAN data frame consists of a frame start, arbitration field, control field, data field, check field, response field and frame end. The CAN controller has two different frame formats: standard format and extended format. Their main difference lies in the different arbitration field formats: the standard frame arbitration field consists of an 11-bit flag and a remote transmission request bit RTR; the extended frame arbitration field consists of a 29-bit flag that replaces the remote request SRR bit, flag bit and remote transmission request bit RTR, the figure is omitted (see "Single-Chip Microcomputer and Embedded System Application" 2002.8 for details). The identifier is the name of the message and is first sent to the bus during the arbitration process. It is used in the acceptance judgment of the receiver and in determining the access priority during the arbitration process. The remote transmission request bit (RTR) is used to determine whether to send a remote frame or a data frame: when RTR is high, the CAN controller sends a remote frame; when it is low, it sends a data frame. The data length code (DLC) is used to determine how many bytes of data to send per frame, up to 8 bytes. 3.2 Introduction to CAN controller PCA82C200
The PCA82C200 chip is a CAN controller produced by Philips. It is a microcontroller based on memory addressing for I/O devices. The independent operation of the device is achieved by modifying the on-chip registers like RAM. It only supports the standard information frame format. The address area of PCA82C200 includes the control segment and the information buffer control segment. It can be programmed to configure communication parameters (for example, bit timing) during initialization loading. The microcontroller also controls communication on the CAN bus through this segment. PCA82C200 has two working modes: reset mode and working mode. In reset mode, the receive code, receive mask, bus timing registers 0 and 1, and output control registers can be set. Generally, the above registers are set when CAN is initialized. When CAN enters working mode, their values will no longer change. Data can be sent and received in working mode. It should be noted that when the hardware is reset or the controller is offline, it will automatically enter the reset mode, so that normal CAN communication cannot be carried out, which requires monitoring the reset bit. When the hardware is reset or the controller is offline and enters the reset mode, it is required to set the reset bit to 0 to enter the working mode, so that CAN can send and receive normally.
3.3 Network communication rules
The CAN bus is a multi-master working mode. Any node on the network can actively send information to other nodes on the network at any time, regardless of master and slave. The communication method is flexible and does not require node information such as address. In order to prevent bus conflicts, the CAN bus adopts non-destructive bus arbitration technology, sets different priorities for each node as needed, and calibrates it with an identifier ID. The smaller the value, the higher the priority. In the actual application of this system, the master station is responsible for the entire network. It sends various control commands to the slave station, and the slave station operates according to the commands from the master station. The master station can select any slave station to exchange data information. Once any slave station is selected, it can receive information from the master station and can also send information back to the master station according to the command of the slave station. When a slave station fails, it can actively send relevant information to the slave station. When any slave station exchanges information with the master station, it must bring the slave station address information. 3.4 Lower computer software programming The CAN communication interface program between the single-chip microcomputer 87C196NT and the upper computer is omitted. The single-chip microcomputer sends data to the upper computer at a baud rate of 500Kbps. When the single-chip microcomputer receives the data transmitted by the upper computer, it forwards the received data to the upper computer. There are many CAN cards available. Conclusion
Applying CAN fieldbus to the power system line segment protection monitoring unit is conducive to achieving real-time and reliability of detection in the power system. On the basis of existing equipment, it further improves the level of automatic detection and management of the power grid, which is of great significance to improving the reliability of power supply and increasing the economic benefits of the power grid.