Share this

Design and Implementation of CAN Bus System

2026-04-06 08:49:47 · · #1
In the nearly 20 years since the concept of fieldbus emerged, several fieldbus technologies have appeared and matured. Among them, CAN bus is widely recognized as one of the most promising fieldbuses. CAN is a high-performance serial data local area communication network composed of CAN controllers, and it is one of the most widely used fieldbuses internationally. Initially, CAN was designed for microcontroller communication in automotive environments, exchanging information between various electronic control units (ECUs) to form an automotive electronic control network. Due to its outstanding advantages such as high communication speed, high reliability, and good performance-price ratio, it is increasingly widely used in automotive, machinery industry, textile machinery, agricultural machinery, robotics, CNC machine tools, medical devices, home appliances, and sensors. Figure 1 shows a typical system block diagram of a CAN bus node. [align=center]Figure 1 CAN Bus Node System Block Diagram[/align] System Hardware Design The SJA1000 standalone CAN controller is a replacement for the PHILIPS PCA82C200 CAN controller. While fully compatible with the PCA82C200, it adds a new operating mode, PeliCAN. The SJA1000 fully supports the CAN 2.0B protocol with many new features. The operating mode of the SJA1000 is selected through the CAN mode setting in its internal clock divider register. The SJA1000 can support various timing characteristics of the microprocessor, such as Intel mode or Motorola mode. The interface between the SJA1000 and the microprocessor is very simple; the microprocessor accesses the SJA1000 by accessing external memory. The TJA1050 is the interface between the CAN protocol controller and the physical bus in the Controller Area Network (CAN) system. The TJA1050 can provide different transmit performance for the bus and different receive performance for the CAN controller. The TJA1050 has the following main features: full compliance with the ISO 11898 standard, a maximum speed of 1Mb/s, compatibility with 3.3V and 5V input devices, and the ability to connect at least 110 nodes. The microprocessor used in this design is an 89C51 microcontroller responsible for initializing the SJA1000 and controlling it to perform data reception and transmission tasks. The system circuit diagram is shown in Figure 2. [align=center] Figure 2 System Circuit Diagram[/align] The data lines AD0 to AD7 of the CAN controller SJA1000 are connected to the P0 port of the 51 microcontroller and to the chip select signal of the external memory with a base address of 0xFA00. When accessing addresses 0xFA00 to 0xFA31, the CPU can perform corresponding read and write operations on the SJA1000. The pins of the SJA1000 are connected to the corresponding pins of the 51 microcontroller, allowing the 51 to access the SJA1000 via interrupts. System Software Design This system consists of four nodes. One node is a data transceiver connected to a host computer via a parallel port to a CAN bus. The other three nodes are composed of a microcontroller-based CAN bus transceiver system as shown in Figure 2. The microcontroller system sends one frame (8 bytes) of data per second. The CAN bus transceiver connected to the host computer has corresponding host computer testing software support. This paper mainly introduces the program design of the microcontroller-based CAN bus transceiver. Figure 3 is a flowchart of the lower-level software. [align=center] Figure 3 Flowchart of the lower-level main program[/align] Part of the system design code is as follows: main() { Sja_1000_Init(); // Initialize SJA1000 Init_Cpu(); // Initialize CPU Init_T0(); // Initialize timer flag_init=0x00; while(1) { if(rcv_flag) //rcv_flag is the receive flag bit, if there is a receive, the microcontroller will process it { rcv_flag=0; BCAN_DATA_RECEIVE(rcv_data); BCAN_CMD_PRG(0X04); disp_rec(); } if(flag_sec) // The timer interrupt flag is set, if the timer expires, the data frame will be sent { flag_sec=0; send_data[0]=0xaa; send_data[1]=0x08; send_data[2]=DA1; send_data[3]=DA2; send_data[4]=DA3; send_data[5]=DA4; send_data[6]=DA5; send_data[7]=DA6; send_data[8]=DA7; send_data[9]=DA8; BCAN_DATA_WRITE(send_data); BCAN_CMD_PRG(0X01); } if(err_flag) } err_flag=0; disp_err(); Sja_1000_Init(); } display(a); // Loop to display the received data The initialization process of SJA1000 includes requesting to enter the reset state, setting the bus baud rate, setting the output mode, enabling error interrupt, receive and send interrupt. When sending data, the first two bytes of the data packet 0Xaa and 0X08 are descriptors, including an 11-bit ID (flag)\1-bit RTR\4-bit DLC describing the data length, a total of 16 bits. `BCAN_DATA_RECEIVE(rcv_data)` is the function for reading data from the SJA1000 using the 89C51 microcontroller. Its specific function definition is: `bit BCAN_DATA_RECEIVE(unsigned char *RcvDataBuf) { unsigned char TempCount; SJA_BCANAdr = REG_STATUS; // Access address points to the status register if ((*SJA_BCANAdr&0x01) == 0) // Check if the message is valid { return 1; } SJA_BCANAdr = REG_RxBuffer2; // Access address points to receive buffer 2 if ((*SJA_BCANAdr&0x10) == 0) // If it's a data frame { TempCount = (*SJA_BCANAdr&0x0f) + 2; // Calculate the number of data items in the message } else { TempCount = 2; } SJA_BCANAdr = REG_RxBuffer1;` // Access address points to receive buffer 1 memcpy(RcvDataBuf, SJA_BCANAdr, TempCount); // Read the message from the receive buffer return 0; } This function is only for CAN controllers to receive data. A return value of 0 indicates successful reception, and a return value of 1 indicates failure. The BCAN_DATA_WRITE(send_data) function is the write data function of 89C51 to SJA1000. Its specific definition is as follows: bit BCAN_DATA_WRITE(unsigned char *SendDataBuf) { unsigned char TempCount; SJA_BCANAdr = REG_STATUS; // Access address points to status register if ((*SJA_BCANAdr&0x08) == 0) // Check if the last transmission is complete { return 1; } if ((*SJA_BCANAdr&0x04) == 0) // Check if the transmit buffer is locked { return 1; } SJA_BCANAdr = REG_TxBuffer1; // Access address points to transmit buffer 1 if ((SendDataBuf[1]&0x10) == 0) // Check RTR to determine whether it is a data frame or a remote frame { TempCount = (SendDataBuf[1]&0x10) == 0) Buf[1]&0x0f)+2; //Input data frame} else { TempCount =2; //Remote frame memcpy(SJA_BCANAdr,SendDataBuf,TempCount); return 0; } This function sends the data of the specific frame to be sent into the SJA1000 transmission buffer, and then starts. The function returns 0 to indicate that the data has been successfully sent to the transmission buffer, and returns 1 to indicate that the data from the previous transmission is being sent. The system networking is relatively easy. Just connect each node to the same twisted pair cable and start the CAN transceiver of the host computer to monitor the bus data status. Whenever a lower-level CAN transceiver is started, the test software of the host computer can receive the data frame sent by the same CAN transceiver every second. The experimental results show that when the CAN bus of 3 lower-level computers transmits data at the same time, there is no data loss or bus conflict at the data receiving end. Conclusion Fieldbus has huge development potential and will have a profound impact on the transformation of the automatic control field. Our designed CAN bus transceiver is versatile; based on this system design, only the data transmission protocol needs to be modified accordingly to apply it to various CAN bus data transmission systems. References: 1. Chen Liyuan (ed.). Visual Basic Implementation of Serial and Parallel Communication Technology. Tsinghua University Press, 2001. 2. Zhang Xuezhong and Wang Fucheng (eds.). Visual Basic Control Application Programming Example Tutorial. Beijing Hope Electronic Press, 2002. 3. Ma Xirong (ed.). Visual Basic 6.0 Programming. Machinery Industry Press, 2004. 4. Peng Yuhao. Embedded Systems Based on Microcontrollers and CAN Controllers. Microcomputer Information, 2007, 4-2: 33-35.
Read next

CATDOLL 139CM Vivian (TPE Body with Soft Silicone Head)

Height: 139cm Weight: 23kg Shoulder Width: 33cm Bust/Waist/Hip: 61/56/69cm Oral Depth: 3-5cm Vaginal Depth: 3-15cm Anal...

Articles 2026-02-22