Design of RS-232 to CAN Communication Adapter Card Based on PIC Microcontroller

Publisher:平和思绪Latest update time:2011-11-12 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

CAN bus - Controller Area Network (CAN) international standard (ISO11898). Compared with general communication buses, CAN bus has outstanding reliability, real-time and flexibility in data communication.

Its characteristics can be summarized as follows: CAN works in a multi-master mode. Based on the message identifier, each node is divided into different priorities. Non-destructive bus arbitration technology is used. Data can be transmitted and received in point-to-point or point-to-multipoint mode simply by filtering the message identifier. The direct communication distance can reach up to 10km (rate below 5kbps); the communication rate can reach up to 1Mbps (the maximum communication distance is 40m at this time). The number of nodes on CAN mainly depends on the bus drive circuit, which can reach 110 at present. The message adopts a short frame structure, with a short transmission time and a low probability of interference. Each frame of information has CRC check and other error detection measures, which has excellent error detection effect. The communication medium can be twisted pair, coaxial cable or optical fiber, which is flexible to choose. The node has the function of automatically shutting down the output in the case of serious errors, so that the operation of other nodes is not affected. It has a high performance-price ratio. The CAN protocol is based on the open system interconnection mode of the International Standards Organization. Since CAN has a simple data structure and is a small local area network, it does not require other intermediate layers. The application layer data is directly taken from the data link layer or directly written to the link layer. The fewer structural levels are conducive to the transmission of real-time control signals in the system.

The experimental communication network transmits data through the CAN bus. The designed CAN-RS-232 communication adapter card can be mounted at any node position of the communication network. For the PC connected to the CAN network through the RS-232 bus, the super terminal that comes with WINDOWS or the serial port operation program we wrote in VC can be used to receive and send data. Point-to-point and point-to-multipoint communication can be realized, and the transmission of data frames and remote request frames in the CAN protocol can be realized. The so-called remote request frame is that the receiving end starts its resource nodes to transmit their respective data by sending a remote frame. The board can also realize 10-bit precision A/D conversion and upload the obtained data to the host computer through the CAN bus. The CAN bus system structure is shown in Figure 1:

1 System Hardware Design

The board circuit mainly consists of four parts: PIC16F877A microcontroller, independent CAN communication controller SJA1000T, CAN bus driver TJA1050 and RS-232 bus driver MAX232. It mainly realizes data transmission based on CAN bus, and connects PC as a terminal to CAN bus through RS-232, collects analog signals and uploads them to the host computer through CAN bus. SJA1000T is an integrated independent CAN controller (fully compatible with PHILIPS's early CAN controller PCA82C200), which is responsible for completing the physical layer and data link layer functions of CAN bus communication protocol. For the microprocessor, it is equivalent to a memory I/O image device. Since the CAN controller and the microcontroller have their own clock generation circuits, in order to coordinate between the two, an external 10M crystal oscillator is added to the microcontroller as the clock of the entire system, and the clock output of the microcontroller is used as the clock input of the CAN controller; the interrupt output signal /INT of SJA1000T is connected to the INT interrupt of the microcontroller, but the communication between the microcontroller and the CAN controller can also be achieved through query mode; in order to enable the CAN controller and the microcontroller to be reset reliably at the same time, a manual reset circuit is added.

PIC16F877A microcontroller is a widely used mid-range microcontroller. In this system, it is mainly used to control the CAN controller SJA1000T, initialize and read/write the SJA1000T, and package the data from the CAN bus or RS-232 bus and then transfer it to the RS-232 bus or CAN bus, that is, to process and transmit the data streams on the two buses according to the protocol. The microcontroller of the lower computer mainly collects analog signals and realizes A/D conversion, and uploads them to the upper computer through the CAN bus.

1.1 CAN communication module

AD0~AD7 of SJA1000T is the address and data composite bus, which is connected to the PORTD port of PIC16F877A, that is, the microcontroller transmits data and addresses through the parallel port of SJA1000T; it should be noted that the 51 series microcontrollers have special read, write and address latch pins, while PIC16F877A does not, so we can only use other ports to simulate control. In this system, ALE, CS, RD and WR of SJA1000T are respectively connected to the RC0-RC3 pins of the microcontroller, that is, RC0-RC3 are used to control the address latch, chip select and read/write operations of SJA1000T.

TJA1050 is the interface between the CAN protocol controller and the physical bus, and is mainly used in baud rates ranging from 60kbps to 1Mbps. TJA1050 can provide differential transmission function for the bus and differential reception function for the CAN controller, and is fully compliant with the ISO11898 standard. The CAN controller SJA1000T is connected to the CAN bus driver TJA1050 through a serial data output line TxD and a serial data input line RxD, while TJA1050 is connected to the CAN bus through two bus terminals with differential reception and transmission capabilities. The bus terminal is a 120-ohm resistor, which matches the bus impedance and improves the anti-interference and reliability of data communication. The schematic diagram of the CAN communication module is shown in Figure 2.

1.2 RS-232 module

The RS-232 standard is a serial physical interface standard developed by the American Electronic Industry Association (EIA). It is widely used for proximal connections between computers and terminals or peripherals. A complete RS-232 interface has 25 wires. Generally, a simplified 9-pin plug socket is used in the computer serial port: When working, the level should be guaranteed to be between ±(5-15)V. Therefore, in order to be able to connect with the TTL device of the computer or controller, the level and logic relationship must be converted between the RS-232 and TTL circuits. The commonly used conversion chip is MAX232. MAX232 has a voltage multiplier circuit and a conversion circuit inside, and only a 5V power supply is required to realize the conversion between the TTL level and the RS-232 level. The schematic diagram of the RS-232 module is shown in Figure 3.

[page]

1.3 A/D conversion module

The PIC16F877A microcontroller has 8 analog channels with a conversion accuracy of 10 bits. This adapter card selects RA0 as the analog channel. The card uploads the A/D converted data to the host computer through the CAN bus.

1.4 Data Flow

The data flow diagram of CAN-RS-232 is shown in Figure 4.

2 System Software Design

The software design is divided into two parts: the underlying software design of the board and the serial port operation software design on the PC. The underlying software design of the board is mainly to program the PIC16F877A, which is used to package and process the data input from the PC to the microcontroller through the serial port according to the CAN protocol frame structure, and operate the CAN controller to send the data to the CAN bus; convert the data input from the CAN bus into serial data and output it to the PC: the serial port operation software design on the PC is mainly about the operation of the serial port and sending and receiving data through the serial port.

2.1 MCU Programming

The development tool used is MPLAB Integrated Development Environment (IDE), which is a comprehensive editor, project manager and design platform suitable for application development using Microchip's PICmicro series of microcontrollers for embedded design; the programming language used is C language, and HITECH's PICC compiler is directly connected to the MPLAB-IDE integrated development platform to achieve integrated compilation, connection and source code debugging.

The CAN controller SJA1000T has mode register MOD, command register CMR, status register SR, interrupt register IR, interrupt enable register IER, receive buffer RXB, transmit buffer TXB, arbitration loss capture register ALC, error code capture register ECC, error alarm limit register EWLR, error count register ERR, acceptance code register ACR, acceptance mask register AMR, bus timing register BTR, output control register OCR and clock division register CDR, etc. The microcontroller controls the SJA1000T by reading and writing these registers, thereby realizing the reception and transmission of CAN messages. The flowchart of the CAN controller SJA1000T initialization and receiving subroutines is given at the end of the article. The CAN initialization program is given below, in which WRCAN is a custom subroutine that writes data to SJAIO00T.

[page]

2.2 Serial port operation program design

Among various development tools, VC is powerful and flexible, so this experimental system uses VC as the development tool. There are three popular methods of serial communication: one is to use the CMSCOMM control provided by Microsoft for communication, which is realized by inserting ActiveX controls; the second is to use WIN API functions for programming, which is more difficult and requires mastering more API functions; the third is to use some serial communication controls provided on the Internet, such as CSerial class. This program uses WINAPI functions to develop serial ports, and the program is used on PCs to send and receive serial port data and runs correctly and stably.

3 Conclusion

This system uses a serial communication program on the PC to send and receive data on the RS-232 bus, and connects to the CAN network through a PIC microcontroller and a CAN communication module. The board can perform two-way communication: on the one hand, it can convert the data stream received from the CAN bus into a standard RS-232 format data stream and upload it to the PC; on the other hand, it can convert the received RS-232 format data stream into a message structure that complies with the CAN protocol and transmit it to the CAN bus. The advantages of CAN bus networking are fully utilized, and the bus-type communication network has the characteristics of high communication baud rate, long communication distance, large node capacity, and high reliability. The serial communication program programmed by VC is used to receive, display and send data, realizing data communication of multiple hosts based on the CAN network.

Reference address:Design of RS-232 to CAN Communication Adapter Card Based on PIC Microcontroller

Previous article:Application of PIC microcontroller in neural stimulator
Next article:The basic structure framework of PIC microcontroller programming

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号