Design of CAN bus intelligent node based on C8051F040

Publisher:TranquilMindLatest update time:2011-06-08 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Introduction

The "X-type fire control system" uses the CAN bus to realize the communication between its internal monomers according to actual needs. The CAN bus is a field bus used for the detection and control of various equipment. It is a multi-master bus and is widely used in high-speed networks and low-cost node systems. Compared with general communication buses, the data communication of the CAN bus has outstanding reliability, real-time and flexibility. Its characteristics are as follows:

* It can work in multi-master mode.
* CAN nodes can send and receive data in point-to-point, point-to-multipoint and global broadcast modes by filtering the identifier of the message.
* The CAN bus communication format adopts the short frame format.
* It adopts non-destructive bus arbitration technology.
* The maximum direct communication distance can reach 10km (rate below 5kb/s), the maximum communication rate can reach 1Mb/s (the maximum distance is 40m at this time), the number of nodes can reach 110, and the communication medium can be twisted pair, coaxial cable or optical fiber.
* The CAN bus adopts CRC check and can provide corresponding error handling functions to ensure the reliability of data communication.

C8051F040 and its internal CAN controller

C8051F040 has an internal integrated CAN controller, which is mainly composed of CAN core, message RAM, message processing unit and control register. Figure 1 is the structural diagram of C8051F040 integrated CAN.

In Figure 1, the CAN core is composed of CAN controller and internal registers responsible for message transmission and reception. The message RAM is used to store message targets and arbitration masks of each target, and each message target has its own identification mask. All data transmission and reception filtering are completed by the CAN controller. The message processing unit is used to control the data transfer between the shift register and the message RAM in the CAN core according to the information in the register. It can also be used to manage the generation of interrupts.

Figure 1 Structure of C8051F040 integrated CAN

All CAN control and configuration information is stored in the internal registers of CAN, including control registers, status registers, bit timing registers for setting baud rate, test registers, error counters and message interface registers. Usually, the CAN core cannot directly access the message RAM, but must access it through the interface registers IF1 or IF2. In addition, the SFR of CIP-51 cannot directly access all units of the internal registers of CAN. Its configuration of CAN, message target, reading CAN status, obtaining received data, and transmitting sent data are all completed by 6 special registers in SFR. Among them, the CAN0CN, CAN0TST and CAN0STA three registers can directly obtain or modify the corresponding registers in the CAN controller, while the CAN0DATH, CAN0DATL and CAN0ADR three registers are mainly used to access and modify other CAN internal registers that cannot be directly accessed. Among them, CAN0ADR is used to indicate the address of the register to be accessed. At this time, CAN0DATH and CAN0DATL are equivalent to the mapping registers of the high and low bytes of the 16-bit register to be accessed, and reading and writing them is equivalent to reading and writing the pointed registers. Figure 2 shows how CIP-51 accesses the control registers and each message path in CAN.

Figure 2. CIP-51 access path diagram for control registers and each message in CAN

Design of CAN intelligent node based on C8051F040

Since CAN bus works in multi-master mode, CAN bus system has a different topology from DCS (distributed control system). Figure 3 shows the bus system structure of a certain type of fire control system. The biggest feature of this system structure is that all nodes can be connected to the bus with equal status. CAN bus intelligent nodes can communicate with each other to complete data exchange. An intelligent node usually includes at least three parts, namely, the microcontroller responsible for node task control, the bus controller and the bus transceiver. Since the C8051F040 microcontroller has an internal integrated CAN controller, the intelligent node is mainly composed of C8051F040 and CAN transceiver TJA1050.

Figure 3 CAN bus structure of a fire control system

Figure 4 is a hardware block diagram of an intelligent node with C8051F040 as the core. Since C8051F040 integrates a CAN controller, an external CAN bus transceiver is required to enable the CAN bus to operate. Commonly used CAN bus transceivers include Philips' PCA82C250 transceiver and high-speed TJA1050 transceiver. This system uses the TJA1050 high-speed CAN transceiver to replace the traditional PCA82C250 transceiver. TJA1050 has the characteristics of low electromagnetic radiation, short circuit protection, and no effect on the bus when not powered on. Its pin S can select high-speed or silent modes, and can be controlled by P4.0 of C8051F040. The nodes 8 at both ends of the CAN bus are generally added with two 60W resistors and 47nf capacitors to absorb the signal to avoid signal reflection. In order to further improve the anti-interference ability of the system, the CAN controller pins CANTX, CANRX and the transceiver TJA1050 are not directly connected, but connected to TJA1050 through an isolation circuit composed of high-speed optical coupler 6N137, so that the electrical isolation of each node on the bus can be well achieved. This part increases the complexity of the node, but it improves the stability and security of the node.

Figure 4 Smart node hardware block diagram

The main task of the CAN smart node is to transmit relevant data to other nodes and receive the data required by the node from the bus. Therefore, the CAN communication of the smart node mainly includes system initialization program, sending program, receiving program, etc. In this example, the system software adopts a structured program design scheme, which makes it have good modularity and portability. For different system functions or different application environments, it can be easily reorganized by programming. System initialization

program The

initialization program mainly completes the initialization of all message objects (generally all values ​​are set to zero), sets the CAN control register (CAN0CN) and the bit timing register (BITREG), and also initializes the sending message object and the receiving message object respectively. Among them, the setting of the bit timing register is more complicated. Here we use an external crystal oscillator of 8MHz and a CAN communication rate of 500k/s, and the initial value of BITREG is 0x2301. The main program specifies object initialization, transmission and reception initialization, and finally starts the CAN processing mechanism (initialization of BITREG and CAN0CN). The following is the CAN startup program:

void start_CAN(void){
SFPRAGE=CAN0_PAGE; //Point to CAN0
page
CAN0CN|=0x41; //Set CCE and Init to "1"
to start initialization
CAN0ADR=BITREG; //Point to the bit timing register
Configure
CAN0DAT=0x2301; //The bit rate is 500k/s
CAN0CN|=0x06; //Enable global interrupts,
set IE and SIE to
CAN0CN &=~0x41; //Clear CCE and INIT
bits, start CAN status mechanism
}

Sending program

CAN message sending is automatically completed by the CAN controller. The user only needs to transfer the corresponding data to the send buffer register according to the identifier of the received remote frame, and then write the encoding of this message object into the command request register to start sending, and the sending is completed by hardware. Here, we use the timed update to send the data in the message object. The data is sent automatically by the controller. When it receives a remote frame, it sends out the data frame with the same identifier. The sending program structure is as follows:

void transmit_message(char MsgNum) {
uchar i;
SFRPAGE=CAN0_PAGE; //Point to CAN0
page
CAN0ADR=IF1CMDMSK; //Write command to IF1 command
mask register
CAN0DAT =0x0083; //Bit rate is 500k/s
CAN0ADR=IF1ARB2; //Point to IF1 arbitration
register 2
CAN0DATH |= 0x80;
CAN0ADR=IF1DATA1; //Point to
the first byte of the data field
for(i=0;i<4;i++){
CAN0DATH=can_temp[i]; //Write 4 bytes of data
to the send buffer
}
CAN0ADR=IF1CMDRQST;
CAN0DATL=MsgNum; //Write the message object number
, then the data is sent
to Corresponding message object
}

Receiving program

CAN message reception is the same as sending, which is automatically completed by the CAN controller. The receiving program only needs to read the received data from the receiving buffer and then perform corresponding processing. Its basic method is the same as the sending program, but the receiving program uses interrupt mode. In this application, the receiving program mainly receives the parameter setting data of the host computer for the intelligent node. It only needs to receive data when modifying, so it is more appropriate to use interrupt mode for processing. The receiving program structure is as follows:
void receive_data(void) {
SFRPAGE=CAN0_PAGE; //Point to CAN0 page
CAN0ADR=IF2CMDMSK; //
Write command to IF2 command mask register
CAN0DATH =0x00; //Bit rate is 500k/s
CAN0DATL =0x3F;
CAN0ADR=IF2CMDRQST; //Write the message object
number to the command request register
, corresponding to the receiving CAN0DATL=MsgNum; //
Move the data from the message RAM
to the data buffer CAN0ADR=IF2DATA1; //Point to the first byte
of the data field for(i=0;i<4;i++) {CAN_RX[i]=CAN0DAT; //Read 4 bytes of data } } Conclusion During the development and testing of the CAN bus, after actual testing, the CAN bus intelligent node we designed based on C8051F040 has the characteristics of high integration, stable performance, and strong anti-electromagnetic interference ability. When the communication baud rate is set to 500kbps, the communication is smooth, realizing the reliability, real-time performance, and flexibility of the communication between the internal units of a certain type of fire control system.









Reference address:Design of CAN bus intelligent node based on C8051F040

Previous article:A new type of high-range micro-acceleration sensor for penetration testing
Next article:Design of CAN bus intelligent node based on LPC2292

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号