Share this

Design of a USB-based CAN bus adapter

2026-04-06 06:20:26 · · #1
Abstract: With the development of fieldbus technology and computer peripheral interface technology, more solutions have emerged for the fast and efficient connection between fieldbus and computer. USB, as a new type of interface technology, is favored for its ease of use and high speed. This paper briefly proposes a USB interface and CAN bus connection scheme, and discusses the system's hardware composition, firmware development, and driver program. Keywords: Fieldbus; USB; CAN bus Introduction: With the rapid development of information technology, real-time acquisition and processing of various data have become an indispensable part of modern industrial control. This requires us to design interfaces that are simple, flexible, and have a high data transmission rate. Fieldbus is a digital, bidirectional, multi-branch communication network that connects intelligent field devices and automation systems. How to interconnect it securely, reliably, and cost-effectively with a PC is an urgent problem to be solved. Traditional peripheral and host communication interfaces are difficult to meet the above requirements. These interfaces generally use PCI bus or RS-232 serial bus. Although PCI bus has a high transmission rate (up to 132Mbps), its expansion slots are quite limited and its design is complex. RS-232 serial bus is easy to connect, but its bandwidth is very limited and its transmission speed is slow. USB technology was developed to meet this demand, offering a fast, bidirectional, synchronous, inexpensive, and hot-swappable universal serial bus. It also provides a built-in power supply, capable of providing 5 volts to low-voltage devices. These features have led to its widespread adoption. The CAN bus is an effective serial communication network for distributed or real-time control, possessing excellent characteristics and high reliability. It is particularly suitable for interconnecting industrial process monitoring equipment and is widely recognized as one of the most promising field device buses. The entire system design aims to create an adapter that can quickly transfer CAN bus data to a PC for processing and analysis via a USB interface, and can also transmit data or commands to CAN nodes to achieve communication between the computer and field devices. I. Hardware Circuit Design This system mainly consists of an AT89C52 control circuit, a USB interface circuit, a CAN bus circuit, a suspend/reset circuit, and an opto-isolation circuit. The main component structure diagram of this system is as follows: [align=center] Figure 1 Hardware Circuit Structure Diagram[/align] 1.1 AT89C52 Control Circuit The AT89C52 is a low-voltage, high-performance CMOS 8-bit microcontroller with an on-chip 8k bytes of rewritable Flash memory and 256 bytes of random access memory (RAM), offering powerful functionality. The 89C52 is the control core of this interface circuit, where ports P0 and P2 are used as 16-bit data I/O ports, and ports P1 and P3 are used for control. 1.2 CAN Bus In this system, the CAN controller uses the Philips SJA1000, which acts as a transmit/receive buffer to realize data transmission between the main controller and the bus. The CAN transceiver uses the TJA1050 chip, which is the interface between the CAN controller and the physical bus, mainly providing differential transmit capability to the bus and differential receive capability to the CAN controller. In the CAN bus structure, two 120Ω resistors are configured at both ends of the bus. Their function is to match the bus impedance, which can increase the stability and anti-interference capability of bus transmission and reduce the error rate in data transmission. To enhance the anti-interference capability of CAN bus nodes, the TXO and RX0 of the SJA1000 can be connected to the TJA1050 through a high-speed optocoupler 6N137, thus achieving good electrical isolation between nodes on the bus. The power supply of the optocoupler section and CAN_V must be isolated from each other using a low-power power supply isolation module, thereby improving the stability and safety of the nodes. 1.3 USB Interface The PDIUSBD12 in this design is a performance-optimized USB device, typically used in microcontroller-based systems to communicate with the microcontroller through a high-speed universal parallel interface. There are two ways to interface the PDIUSBD12 with the MCU: multiplexed address/data bus and single-address/data bus. In this system, we use the former: utilizing the AT89C52's INT0, ALE, WR, RD, and P0 ports, with pin A0 grounded. When the PDIUSBD12 receives valid information from the host, it generates an interrupt notification for the AT89C52 to process. If the microcontroller's output address is odd, it indicates sending instructions to the PDIUSBD12; if the output address is even, it indicates data transfer to the PDIUSBD12. The AT89C52 sends the data to the FIFO memory via the PDIUSBD12's parallel interface. To a microcontroller, the PDIUSBD12 appears as a memory device with an 8-bit data bus and a 1-bit address. The CLKOUT clock output provides the clock input for the SJA1000. Because instantaneous high-voltage noise can be introduced during USB signal transmission, this noise can be fatally damaging to the USB port's transceiver circuits. Therefore, it is necessary to suppress this noise voltage. The SN75240 is used in the USB interface circuit, which can effectively suppress abnormal voltages in the USB interface to ensure the safety of hardware devices. II. Microcontroller Firmware Programming 2.1 CAN Bus Software Design The CAN bus has a three-layer structure model: physical layer, data link layer, and application layer. The functions of the physical layer and data link layer are handled by the SJA1000. System development mainly focuses on the design of the application layer software, which consists of three subroutines: an initialization subroutine, a data transmission subroutine, and a data reception subroutine. It also includes handling of data overflow interrupts and frame errors. After the SJA1000 is powered on and hardware reset, it must be initialized in software before it can communicate with data. The initialization process mainly includes configuring the clock divider register CDR, bus timing registers BTR0 and BTR1, acceptance code register ACR, acceptance mask register AMR and output control register OCR in the reset mode, so as to define the bus speed, acceptance mask code, output pin drive mode, bus mode and clock divider. The following is the process of SJA1000 sending and receiving data. The basic process is that the master controller saves the data to the SJA1000 transmit buffer, and then sets the transmit request TR flag bit of the command register to start sending; the receiving process is that SJA1000 stores the data received from the bus into the receive buffer, and notifies the master controller to process the received information through its interrupt flag bit. After receiving, the buffer is cleared and it waits for the next reception [3]. [align=center]Figure 2 CAN Data Transmission Flow[/align] [align=center]Figure 3 CAN Data Reception Flow[/align] 2.2 USB Bus Software Design The PDIUSBD12 is a high-speed USB interface device with parallel bus and local DMA transfer capabilities. It supports all transmission modes of the USB 1.1 protocol. In this design, control transfer, interrupt transfer, and bulk transfer are used. Control transfer processes control information from the host to the USB device, using endpoint 0. Interrupt transfer is used to transmit small amounts of data that need to be processed promptly to achieve real-time performance, using endpoint 1. Bulk transfer is used to realize large data transmission between the CAN node and the host, using the master endpoint (endpoint 2), with a maximum of 64 bytes sent at a time. The PDIUSBD12 firmware is designed to be fully interrupt-driven, allowing USB transfers to occur in the background while the MCU handles foreground tasks. This ensures optimal transmission rates and a better software structure, while simplifying programming and debugging. Data exchange between the background ISR interrupt service routine and the foreground main program loop is achieved through event flags and data buffers. When PDIUSBD12 receives a data packet from USB, it generates an interrupt request to the MCU. The MCU responds to the interrupt immediately. In the ISR, the firmware moves the data packet from the PDIUSBD12 internal buffer to the data buffer and then clears the PDIUSBD12 internal buffer to enable the reception of new data packets. The MCU can continue its current foreground task and return to the main loop to check if there is new data in the loop buffer [1]. [align=center] Figure 4 PDIUSBD12 Firmware Programming Structure Diagram [/align] This part of the program structure may include [4]: ​​1) The request handler processes the standard device requests of USB and the manufacturer requests added by the user; after the USB device is connected to the host, it enters a complex device enumeration process and installs the correct driver. 2) The hardware extraction layer operates on the microcontroller's I/O ports, data bus, and other hardware interfaces. This layer contains the lowest-level functions, which need to be modified on different MCU platforms. Functions include `void outportb(unsigned char port, unsigned char val)` and `void inportb(unsigned char port)`. All I/O accesses to the PDIUSBD12 can be implemented using these functions. 3) The PDIUSBD12 command interface is a set of subroutines that operate the PDIUSBD12 device, simplifying device programming. 4) The interrupt service routine reads the data transmitted from the PDIUSBD12 interrupt when the PDIUSBD12 sends an interrupt request to the microcontroller, sets the event flag and Setup packet data buffer, and transmits it to the main loop. 5) The main loop sends USB requests, handles USB bus events, and processes user functions. Once the MCU powers on, it needs to initialize all its port memory areas and interrupt service routines. In the main loop program, the MCU polls for events. If data arrives from the CAN bus, it sends it to the PDIUSBD12 for transmission. If data arrives from the USB bus, the CAN transmission program is started. The receiving operations at each interface are handled by their respective interrupt routines. It's important to note that the maximum transmission length for the PDIUSBD12 port is 64 bytes at a time, while the SJA1000 can transmit a maximum of 8 bytes. If the transmission length from port 1 exceeds 8 bytes, the MCU must complete the CAN data transmission in multiple steps based on the data size. III. USB Driver Development On the Windows platform, any communication with USB peripherals must go through the USB device driver. This driver knows how to communicate with the system's USB driver interface and the application accessing the device. The device driver is a software component that ensures application access to hardware devices, allowing the application to access peripherals or port destinations only by their names, without needing to know the details of physical connections, signals, and protocols required to communicate with a device. The functions of a USB driver (USBD) can be summarized as: configuration management, bus management, data transfer management, and providing customer service. USBD divides I/O request packets (IRPs) into blocks of the size required by USB and the device, ensuring that each device can be allocated the USB resources it requires, so that it can support USB device configuration. USB uses the standard Windows system USB class driver to access the USB driver interface (USBDI). USBD.sys is the USB class driver, which uses the Universal Host Controller Driver (UHCD) or the Open Host Controller Interface (openHCI) to access the Universal Host Controller Interface device. USBHUB.sys is the USB driver for the root hub and external hub. After the PCI enumerator discovers the USB host controller, it will automatically load the relevant driver. [align=center] Figure 5 Block diagram of the USB WDM interface[/align] A large number of customer services are provided by the USB driver, which helps USB customers control and access their functional units. Writing USB device drivers requires the use of Visual C++, as well as the Windows 98 or 2000 Driver Device Development Kit (98DDK/NTDDK) [5]. IV. Application Design The design of the microprocessor firmware and USB device driver is the responsibility of the USB device developers. For most users, interaction with the system is achieved through the application, and the main data processing of the entire real-time acquisition system is completed here. Therefore, the design of an application with high operating efficiency, a user-friendly interface, and powerful data analysis and processing capabilities is a crucial factor that cannot be ignored in system design. The key is to read or send a specific amount of data from the USB peripheral. The main functions of the application include: starting/stopping the USB device, detecting the USB device, setting the USB data transmission pipe/port, displaying received data, and sending data. Here, we use Visual C++ 6.0 as the program development environment. The USB device can be treated as a file. Utilizing the Windows API: when the device is successfully plugged into the USB bus, USBD is automatically loaded, and CreateFile() returns a handle to the driver. DeviceIoControl() is used for control transmission, and ReadFile() and WriteFile() are used for data transmission. CloseHandle() is used to terminate communication with the USB device, at which point the handle of the device driver is released. Using the above API functions, host software for USB data transmission and reception can be developed [6]. V. Conclusion The design of the CAN fieldbus adapter system based on USB technology is feasible from both hardware and software perspectives. The experimental results have successfully completed the communication task, which provides a new solution for data reception and processing in industrial fields. With the development of fieldbus technology, exploring its interface technology with computers is of great practical significance. References [1] Philips Corp. PDIUSBD12 Users Manual [2] Philips Corp. SJA1000 Users Manual [3] Rao Yuntao et al. Fieldbus CAN Principles and Application Technology Beijing University of Aeronautics and Astronautics Press 2003.6 [4] Zhou Ligong PDIUSBD12 USB Firmware Programming and Driver Development Beijing University of Aeronautics and Astronautics Press 2003.2
Read next

CATDOLL 136CM Tami (Customer Photos)

Height: 136cm Weight: 23.3kg Shoulder Width: 31cm Bust/Waist/Hip: 60/54/68cm Oral Depth: 3-5cm Vaginal Depth: 3-15cm An...

Articles 2026-02-22
CATDOLL Oliva Soft Silicone Head

CATDOLL Oliva Soft Silicone Head

Articles
2026-02-22
CATDOLL 128CM Luisa

CATDOLL 128CM Luisa

Articles
2026-02-22
CATDOLL Maruko 88CM TPE Doll

CATDOLL Maruko 88CM TPE Doll

Articles
2026-02-22