Implementation of LIN protocol based on single chip microcomputer

Publisher:书卷气息Latest update time:2011-08-26 Keywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1 Overview

LIN protocol is a new type of low-cost serial communication bus. Its full name is Local Interconnect Network. It first appeared in the automotive industry. It is a serial bus protocol proposed to solve the contradiction between the development requirements of automobile intelligence and networking and the reduction of automobile manufacturing costs. It is mainly used in occasions such as car doors and lights that require simple control but also require intelligent control. Its main features are: single master controller/multiple slave device communication mode; based on ordinary UART/SCI interface hardware implementation, the protocol is simple; the network transmission rate is not high, up to 20kb/s. Since the outstanding feature of the LIN protocol is that the protocol has a low degree of dependence on hardware, it can be implemented in software based on hardware resources such as the universal serial port of ordinary single-chip microcomputers, and the cost is low. Therefore, it can be widely used in other fields outside the automotive industry, such as data transmission and node control within smart home networks.

Based on the analysis of the LIN protocol, this paper studies and discusses the feasibility of the specific implementation of the protocol on ordinary single-chip microcomputers , that is, how to use the limited hardware resources of single-chip microcomputers to realize the master node and slave node of LIN. 2 Introduction to LIN Protocol The latest version of LIN protocol is LIN Specification Package Revision 2.O, which includes protocol specification, node diagnostic configuration specification, physical layer specification, API specification and other aspects, and detailed provisions are made from hardware configuration to node configuration language. The following is a brief introduction and analysis of its protocol specification. LIN data transmission is carried out using message frames. A complete message frame consists of a message header sent by a host node and a response sent by a host or slave node, as shown in Figure 1. The message header includes a break field, a synchronization byte field (synch) and a protection identifier byte field PID (Protected IDentifier). The break field consists of a dominant level that lasts for at least 13 bits and a recessive level that lasts for at least 1 bit; it is generated by the host node and marks the beginning of a data communication process message frame. The synchronization byte field contains clock synchronization information. The format of the synchronization byte field is 0x55, which is reflected in 5 falling edges in the 8-bit timing, that is, the edge of the transition from recessive to dominant. All slave nodes should be able to detect the existence of the interval field after the host node publishes the message header, and after correctly receiving the synchronization byte field, accurately calculate the baud rate of the data to be sent by the host node, and use this baud rate as the setting value of the baud rate to send or receive data in the next step. In this way, after receiving the interval field and the synchronization byte field, all slave nodes have achieved synchronization with the host node. In the next step, all slave nodes receive the protection identifier byte field of the message header at the calculated baud rate. The protection identifier byte field PID (Protected IDentifier) ​​is the same as the standard serial communication data frame format, consisting of 1 start bit, 1 stop bit and 8 data bits, and belongs to the data field, as shown in Figure 2. The protection identifier byte field consists of two parts: the identifier and the identifier parity check. ID0~ID5 are identifiers, which determine the content of the identifier and can take values ​​from 0 to 63. Among them, 0 to 59 (0x3b) are used for signal transmission frames, 60 (0x3c) and 61 (0x3d) are used to transmit diagnostic data, 62 (0x3e) is reserved for user-defined extensions, and 63 (0x3f) is reserved for future protocol upgrades. Parity bits P0 and P1 are odd and even checks for identifier bits ID0 to ID5, as a check measure for the received ID to ensure the correctness of the received ID. The response of the message frame consists of 3 to 9 byte fields (data field), including 2, 4 or 8 bytes of data field (data field) and 1 checksum field (checksum). They are composed of data and checksum fields sent by the node (host node or slave node) to send data, and all belong to the data field. There are two types of checksum fields: general checksum fields and enhanced checksum fields. The general checksum is the inverse code of the sum of all byte data in the data field. The sum is calculated in the ADDC mode, and the sum of the complement of the sum of all data bytes and the checksum byte must be "0xFF". The difference between the enhanced checksum and the general checksum is that the protection identifier byte field is added when calculating the data field sum. 3 Implementation of the LIN protocol on ordinary single-chip microcomputers Nowadays, there are many types of single-chip microcomputers, with different hardware resources and functions. Generally speaking, the methods of implementing the LIN protocol based on ordinary single-chip microcomputer software can be divided into two categories: one is the implementation method based on the general serial port of the single-chip microcomputer, and the other is the bit operation implementation method based on the two general port bits of the single-chip microcomputer. 3. 1 Implementation of the LIN protocol based on the general serial port of the single-chip microcomputer The implementation method of the LIN protocol based on the general serial port of the single-chip microcomputer is mainly for single-chip microcomputers with general serial ports. The representative of this type of single-chip microcomputer is the most commonly used 51 series single-chip microcomputer, such as Atmel's AT89C51/52. (1) Implementation of LIN master node based on microcontroller serial port From the analysis of LIN protocol, it can be seen that in a frame communication process, the master and slave nodes exchange data in the form of standard serial communication data frames for most of the time, which is why LIN protocol can be implemented based on the general serial port of microcontroller. The key to frame communication is to achieve synchronization between the master node and the slave node. In the synchronization process, the operations performed by the master and slave nodes are different: the task of the master node is to send the message header, and the task of the slave node is to receive and judge the message header to achieve synchronization with the master node. The interval field of the message header is an interval delimiter of more than 13 bit times and at least 1 bit time based on the clock frequency of the host node. For the master node, this part is the key to realize the function of the master node. The implementation of the interval field and the interval delimiter can be achieved by changing the serial port baud rate and using the serial port to output specific data. For example, in general, the microcontroller uses a baud rate of 19.2kb/s to transmit data. The baud rate of the serial port can be set to 9.6kb/s first. Then, by transmitting a data such as 0xc0, the bit-time length requirement of the synchronization interval and the synchronization interval delimiter can be calculated according to the baud rate of 19.2kb/s (because if the transmission rate of 19.2kb/s is used to transmit 00 data, only 10 bit-time synchronization intervals can be achieved, and the requirement of 13 bit-times cannot be met). The subsequent transmission of the PID field and the transmission or reception of the data field can be operated based on the universal serial port of the microcontroller at a normal baud rate of 19.2.kb/s. The hardware principle of the L1N master node based on the serial port is shown in Figure 3. During a frame communication process, the master node should set the serial port to a sending or receiving state of different baud rates according to different stages of the communication process to complete the transmission of the message header interval field, the synchronization byte field, the protection identifier byte field, and the next stage of receiving or sending data. (2) Implementation of LIN slave node based on microcontroller serial port The key to slave node implementation is to be able to correctly and in real time receive the message header, achieve synchronization with the master node, and prepare for the next step of data exchange. There are two implementation schemes for LIN slave nodes based on the microcontroller universal serial port: one is the query method and the other is the interrupt method. The difference between the two methods lies in the different methods of judging the message header reception. The hardware principle of the query method is shown in Figure 4. Similarly, the slave node also requires accurate baud rate and timing, and has high requirements for the clock. It is recommended to use a 22.1184MHz crystal oscillator.







LIN message frame www.elecfans.com




























Based on the serial port LlN master node hardware principle www.elecfans.com














In the hardware circuit of the query mode, in order to timely sense the starting stage of the master node message header, the serial port receiving data end, RXD end, can be connected to an external interrupt trigger port (INTl or INT0) of the single-chip computer. In this way, when the falling edge of the interval field sent by the master node arrives, the slave node can be triggered in real time to enter the receiving query program segment of the message header. In the receiving query process of the message header, the slave node accumulates the duration of the bus dominant level (low level) from the arrival of the falling edge of the bus level until the bus is found to be restored to the recessive level (high level). If the duration of this period is greater than 11 master node working bit times, the slave node determines that it is the beginning of a frame communication. Then the slave node prepares for the reception of the synchronization byte field. Starting from the first falling edge of the start bit of the synchronization byte field, the last 4 falling edges of the synchronization byte field are continuously timed and accumulated. Finally, the obtained timing time is divided by 8 to obtain the bit time of the master node sending data, that is, the baud rate of the master node to perform data communication in the next step. The slave node uses this as the serial port baud rate setting value and exchanges data with the master node through the serial port. The subsequent serial port sending or receiving data can be carried out by serial port query or interruption.

There are two methods for timing the interval field and the synchronization byte field: one method is to use software to simulate a bit time, and indirectly calculate the time by calculating the number of times the bit time program is called at each stage; the other method is to set the timer TO to interrupt after timing a bit time, query the number of timer T0 interrupts at each stage, and calculate the difference in the number of TO interrupts, and indirectly calculate the duration of each stage.

The query method has a simple hardware circuit, a small number of system interrupts, and a relatively stable program operation; but the disadvantage is that most of the system time is spent on waiting for the frame message header to query, and the system resource utilization is low.

The interrupt method is an improvement and improvement of the shortcomings of the query method, and its hardware principle is shown in Figure 5.





The interrupt method completely uses the interrupt method to receive the interval field and the synchronization byte field. Since the external interrupt trigger end of the ordinary single-chip microcomputer has only two trigger modes, the falling edge and the low level, the beginning stage of the message header interval field and the falling edge of the synchronization byte field can trigger the slave node, but the rising edge of the message signal cannot be perceived by the slave node. The improvement method is to let the received data stream pass through a three-state gate and a three-state NOT gate respectively before entering the serial port of the single-chip microcomputer. The two three-state gates are controlled by two ports of the single-chip microcomputer. Under normal circumstances, the three-state gate is turned on, the three-state NOT gate is turned off, and the data stream enters the single-chip microcomputer serial port normally. When the falling edge of the interval field triggers the single-chip microcomputer, the program controls the three-state gate to be turned off, the three-state NOT gate is turned on, and the data stream enters the single-chip microcomputer in reverse phase. The rising edge of the interval field becomes a falling edge after passing through the three-state NOT gate, which can also trigger the single-chip microcomputer interrupt. In the subsequent reception of the synchronization byte field, it can be carried out in the normal interrupt mode, that is, the five falling edges of the synchronization byte field trigger the single-chip microcomputer to interrupt five times. Since the interrupt mode is adopted, the timing of each stage can only be realized by querying the number of interrupts of timer T0.

The advantage of the interrupt method is that it has little impact on the operation of the main program and high resource utilization of the system. The disadvantage is that the peripheral circuit of the microcontroller is increased, and the hardware is slightly complicated.

3.2 Implementation of LIN protocol based on the common port bit of the microcontroller

For microcontrollers without a universal serial port, the port bit operation method must be used to implement the LIN protocol. The hardware resources of this type of microcontroller are generally very limited. Some have only one timer and do not have external interrupt capabilities, such as Microchip's PIC18F200 series. The outstanding feature of this type of microcontroller is its low price, and the LIN node made will have an incomparable price advantage.

(1) Implementation of LIN master node based on common port bit

The 1IN master node based on the common port bit of the microcontroller uses the bit operation method to implement the message header and data field. Its hardware structure schematic diagram is exactly the same as the hardware schematic diagram of the LIN master node based on the universal serial port of the microcontroller. The difference is that the data transceiver RXD and TXD ends of the former can be any common port bits of the microcontroller, while the latter must use the universal serial port of the microcontroller.
The realization of the message header interval field can set the timer TO to the working mode of timing one bit interrupt, set the LIN data transmission end TXD to the dominant level (low potential), start the timer T0 to count the duration of the dominant level, and when it reaches more than 13 bits, set the LIN data transmission end TXD to the recessive level (high potential), so that the transmission of the interval field is completed. The same method is also used in the subsequent realization of the interval field delimiter and the synchronization field. In the reception and transmission of the data field, the cooperation of the timer TO is also required. When sending data, take out one data from the data storage area to be sent one by one and convert it into 10 bit type bit data. Timer T0 also interrupts once for 1 bit time, and changes the timing variable value in the interrupt handler. The data transmission program sends 10 bit type bit data from the data transmission end TXD according to the difference of the timing variable in sequence according to the duration of 1 bit time; when receiving data, it is necessary to use timer T0 to count half a bit time to detect the start bit of 1 byte, and then restore the one bit time timing interrupt setting of timer TO. In this way, in the subsequent data bit detection, it can be ensured that the data bit is detected at the middle moment of the data bit, thereby ensuring the correctness of data bit reception. After receiving the 10 bit type bit data, it is also necessary to convert it into a byte type data and store it in the corresponding data buffer.

(2) Implementation of LIN slave node based on common port bit

The hardware circuit of LIN slave node based on common port bit of single-chip microcomputer and the hardware circuit of slave node based on universal serial port query mode of single-chip microcomputer are basically the same. The difference is that the universal serial port of single-chip microcomputer is not used.

Since there is no external interrupt function, the reception of the message header sent by the master node can only rely on the slave node to actively wait for query. Considering that the slave node program cannot always query and wait for synchronization with the master node, the slave node should query and wait for the message header of the master node from time to time. At the beginning, the timer is set to the indefinite long time interrupt mode. After the time is up, the slave node queries and waits for the message frame sent by the master node. When the synchronization information is detected, the timer is set to the standard bit time interrupt mode to define the bit time of the slave node receiving or sending data. The slave node receives the PID field according to the bit operation method mentioned above, and converts it into byte type data, determines the sending or receiving direction of the next data field, and then implements the sending or receiving of data according to the bit operation method. The two working modes of the timer are switched alternately during the query waiting and message communication process. After the message communication process is completed, the timer is reset to the indefinite long-time interrupt mode to wait for the next message communication process, and so on.

Since the slave node uses the irregular query waiting method to synchronize with the master node, the communication success rate is not high; but for occasions where the data communication rate and real-time requirements are not very high, it can still meet the requirements. If the microcontroller has external interrupt capability, the slave node can be changed from irregular query to the slave node using external interrupt to query the message sent by the master node, so that the communication success rate can be greatly improved.

Conclusion

Compared with the enhanced microcontroller, the function of the ordinary microcontroller is much simpler and the resources are also limited, but it has the advantage of low price. The characteristics of the LIN bus are simple protocols and can be implemented by software, which is particularly suitable for occasions where the data communication rate is not high and the control is simple. If the LIN communication protocol can be implemented with the limited resources of ordinary single-chip microcomputers, it will undoubtedly provide a good choice for the low-end serial communication field. LIN nodes based on ordinary single-chip microcomputers will also have great potential in related application fields of production and life.
Keywords:MCU Reference address:Implementation of LIN protocol based on single chip microcomputer

Previous article:Realize the sending and receiving of short messages by mobile phone based on single chip control
Next article:Design of Wireless PBX System Based on P89C669

Recommended ReadingLatest update time:2024-11-16 23:33

51 MCU C language learning notes 1: MCS-51 system structure
                51 MCU Pin Diagram 51 MCU System Structure   The traditional 51 MCU is DIP40 (dual in-line 40 pins), and there are also PLCC44, which are mostly used in places where the board requires compactness. If you want to occupy less space, you can use 2051, which has only 20 pins. Of course, the enha
[Microcontroller]
Portable test recorder designed with PIC microcontroller
Portable test recorder designed with PIC microcontroller This recorder, designed with the idea of ​​pursuing high reliability, low power consumption and small size, has the following functions: real-time acquisition, storage, processing and display of on-site physical quantities; automatic identific
[Microcontroller]
Portable test recorder designed with PIC microcontroller
Detailed introduction to the use of Keil C51 microcontroller variables
introduction The 8051 core microcontroller is a general-purpose microcontroller and occupies a large market share in China. Keil has been the most successful in using C language for research on 51-core microcontrollers. Due to the particularity of the storage structure of the 51-core microcontroller, the use of variab
[Microcontroller]
Detailed introduction to the use of Keil C51 microcontroller variables
STM series MCU project construction and application firmware development
STVD New Construction 1. Open ST Visual Develop software 2. File-NewWorkspace—Create workspace and project 3. Click "OK" to specify the workspace name and workspace path 4. Click OK, specify the project name, path, compilation tool chain and its path (COSMIC installation path) 5. Click OK, select the device, and cli
[Microcontroller]
STM series MCU project construction and application firmware development
#microcontroller# ------ stc89c52 pin description
STC89C52 function brief description: STC89C52 is a low-power, high-performance CMOS 8-bit microcontroller with 8K in-system programmable Flash memory. Manufactured using high-density non-volatile memory technology, it is fully instruction- and pin-compatible with industrial 80C51 products. On-chip Flash allows the pr
[Microcontroller]
#microcontroller# ------ stc89c52 pin description
MEGA16 MCU_UART Program
The most commonly used configuration method   UCSRB=0x00; UCSRA=0x00; //Clear control register UCSRC=(1 baud=MCLK/16/baud-1 //The maximum baud rate is 65K UBRRL=baud;         UBRRH=baud>>8; //Set baud rate UCSRB=(1 SREG=BIT(7); //Open global interrupt DDRD|=0X02; //Configure TX as output (very important)    is to prog
[Microcontroller]
In the automotive "chip" era, Yatli accelerates the layout of the automotive-grade MCU track
The automotive industry is undergoing a major transformation, and the MCU market has become the focus The automobile industry is entering the unprecedented era of the new four modernizations (CASE) of automobiles, including Connectivity, Autonomous, Sharing/Subscription and Electrification. As the four major trend
[Automotive Electronics]
In the automotive
Understanding the classification and performance requirements of automotive control chips (MCU) in one article
01 Introduction to control chips Control chips mainly refer to MCU (Microcontroller Unit), which is a microcontroller, also known as a single-chip microcomputer. It appropriately reduces the CPU's main frequency and specifications, and integrates multiple functional modules and interfaces such as memory, t
[Embedded]
Understanding the classification and performance requirements of automotive control chips (MCU) in one article
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号