Implementation scheme of LIN protocol based on ordinary microcontroller

Publisher:bin0990Latest update time:2006-06-12 Source: 单片机及嵌入式系统应用 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1 Overview
  The LIN protocol is a new low-cost serial communication bus that has emerged. Its full name is Local Interconnect Network, which is a local interconnect network. It first appeared in the automobile 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 for doors, lights, etc. that require simple control but Occasions that require intelligent control. Its main features are: using a single master controller/multiple slave device communication mode; based on ordinary UART/SCI interface hardware implementation, simple protocol; network transmission rate is not high, up to 20kb/s. Since the outstanding feature of the LIN protocol is that the protocol has low dependence on hardware, it can be implemented in software based on hardware resources such as common serial ports of ordinary microcontrollers, and the cost is low. Therefore, it can be widely used in other fields outside the automotive industry, such as within smart home networks. Data transmission, node control and other occasions.
  Based on the analysis of the LIN protocol, this article studies and discusses the feasibility of the specific implementation of the protocol on ordinary microcontrollers, that is, how to use the limited hardware resources of the microcontroller 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 specifications, node diagnostic configuration specifications, physical layer specifications, API specifications, etc., and is detailed from hardware configuration to node configuration language. Provisions. The following is a brief introduction and analysis of its protocol specifications.
  LIN data transmission is carried out in the form of message frames. A complete message frame consists of a 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 (break), a synchronization byte field (synch) and a protected identifier byte field PID (Protected IDentifier). The interval field is composed 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 message frame in a data communication process. The sync byte field contains clock synchronization information. The format of the sync byte field is 0x55, which is reflected in the fact that there are 5 falling edges in the 8 bit timings, that is, recessive transitions to dominant edges.


  All slave nodes should be able to detect the existence of the interval field after the master node publishes the message header, and after correctly receiving the synchronization byte field, accurately calculate the baud rate at which the master node will send data, and use this wave 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 reached synchronization with the master node.
  Next, all slave nodes receive the protection identifier byte field of the message header at the calculated baud rate.
  The protected identifier byte field PID (Protected IDentifier) ​​has the same format as the standard serial communication data frame. It consists of 1 start bit, 1 stop bit and 8 data bits. It belongs to the data field, as shown in Figure 2.
  The protected identifier byte field consists of two parts: the identifier and identifier parity. ID0~ID5 are identifiers, which determine the content of the identifier and can take values ​​from 0 to 63. Among them, 0~59 (0x3b) are used for signal transmission frames, 60 (0x3c) and 61 (0x3d) are used for transmitting diagnostic data, 62 (0x3e) is reserved for user-defined extensions, and 63 (0x3f) is reserved for future protocols. upgrade. The parity bits P0 and P1 are odd and even checks for the identifier bits ID0 to ID5, as a verification 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 the data sent by the node (master node or slave node) to send data and the verification place, and they 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 one's complement of the sum of all bytes in the data field. The sum is calculated according to the add-with-carry (ADDC) method. The sum of the complement of all data byte sums 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 LlN protocol on ordinary microcontrollers There are
  now many types of microcontrollers, with different hardware resources and widely varying functions. Generally speaking, the methods of implementing LIN protocol based on ordinary microcontroller software can be divided into two categories: one is based on the implementation of the general serial port of the microcontroller, and the other is the bit operation implementation method based on the two ordinary port bits of the microcontroller.
3. 1 Implementation of the LlN protocol based on the universal serial port of a single-chip computer
  The implementation method of the LIN protocol based on the universal serial port of a single-chip computer is mainly for single-chip computers with a universal serial port. The representative of this type of microcontroller is undoubtedly the most commonly used 51 series microcontroller, such as Atmel's AT89C51/52.
(1) Implementation of LlN master node based on microcontroller serial port
  From the analysis of LIN protocol, it can be seen that during a frame communication process, the master and slave nodes exchange data in the form of standard serial communication data frames most of the time. This is also The reason why the LIN protocol can be implemented based on the universal serial port of a microcontroller. The key to frame communication is to achieve synchronization between the master node and the slave node. During 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 in the packet header is an interval delimiter of more than 13 bit times and at least 1 bit time based on the host node clock frequency. For the master node, this part is the key to realizing the master node function. The implementation of interval fields and interval delimiters can be achieved by changing the serial port baud rate and using the serial port to output specific data. For example, under normal circumstances, the microcontroller uses a baud rate of 19.2kb/s to transmit data. You can first set the baud rate of the serial port to 9.6kb/s, and then transmit a data like 0xc0 to achieve the baud rate of 19.2kb/s. The baud rate is used to calculate the bit-time synchronization interval and the bit-time length requirements of the synchronization interval delimiter (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 13 cannot be achieved. position requirements). The subsequent transmission of the PID field and the transmission or reception of the data field can be operated at a normal baud rate of 19.2.kb/s based on the universal serial port of the microcontroller.
  The hardware principle of the master node based on the serial port LlN is shown in Figure 3.

  During a frame communication process, the master node must set the serial port to the sending or receiving state of different baud rates according to different stages in the communication process to complete the sending of the message header interval field and synchronization byte field to protect the identifier The sending of the byte field, and the next stage of receiving or sending data process.
(2) Implementation of LlN slave node based on microcontroller serial port
  The key to the slave node implementation is to be able to receive message headers correctly and in real time, achieve synchronization with the master node, and prepare for the next step of data exchange. There are two implementation solutions for LIN slave nodes based on the universal serial port of a microcontroller: one is the query method, and the other is the interrupt method. The difference between the two methods lies in the method of judging the reception of the message header. 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 higher clock requirements. It is recommended to use a 22.1184MHz crystal oscillator.
  In the query mode hardware circuit, in order to sense the initial stage of the master node message header in time, the serial port receiving data terminal and RXD terminal can be connected to an external interrupt trigger port (INTl or INT0) of the microcontroller. 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 reception query program segment for the message header. During the reception and query process of the message header, when the falling edge of the bus level arrives, the slave node performs a cumulative calculation on the duration of the dominant level (low level) of the bus until it is found that the bus returns to the recessive level. (high level). If the duration of this period is greater than 11 master node working bit times, then the slave node concludes that it is the beginning of a frame communication. Then the slave node is ready to receive the sync byte field. Starting from the first falling edge of the start bit of the sync byte field, it continuously accumulates the timing of the last four falling edges of the sync byte field. Finally, the obtained timing Divide the time by 8 to get the bit time when the master node sends data, that is, the baud rate at which the master node will perform data communication in the next step. The slave node uses this as the serial port baud rate setting value to exchange data with the master node through the serial port. Subsequent serial port transmission or reception of data can be carried out by serial port query or interrupt.
  There are two timing methods for the interval field and the synchronization byte field: one method is to use software to simulate a bit time, and calculate the time indirectly by counting the number of times the bit time program is called at each stage; the other method is to set the timer TO Set the timer to interrupt after a bit time. Query the number of timer T0 interrupts in each stage. By calculating the difference in the number of TO interrupts, you can also indirectly calculate the duration of each stage.
  The hardware circuit of the query method is simple, the types and times of system interruptions are few, and the program runs relatively stably. However, the disadvantage is that the system spends most of its time waiting for query of frame headers, and the system resource utilization is low.
  The interrupt mode improves and improves the shortcomings of the query mode. Its hardware principle is shown in Figure 5.

  Interrupt mode The reception of the interval field and the synchronization byte field is completely performed in the interrupt mode. Since the external interrupt triggering terminal of ordinary microcontrollers only has two triggering modes: falling edge and low level, the starting 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. Let the slave node sense. The improvement method is to let the received data stream pass through a tri-state gate and a tri-state NOT gate before entering the serial port of the microcontroller. The two tri-state gates are controlled by the two ports of the microcontroller. Under normal circumstances, the three-state gate is turned on, the three-state NOT gate is turned off, and the data flow enters the serial port of the microcontroller normally. When the falling edge of the interval field triggers the microcontroller, the program controls the three-state gate to turn off, the three-state NOT gate to conduct, and the data flow enters the microcontroller in reverse phase. The rising edge of the interval field passes through the three-state NOT gate and becomes a falling edge. The same can be done Trigger microcontroller interrupt. In the subsequent reception of the synchronization byte field, it can be carried out according to the normal interrupt method, that is, the five falling edges of the synchronization byte field trigger the microcontroller to interrupt the reception five times. Due to the interrupt method, the timing of each stage can only be achieved by querying the number of interrupts of the timer T0.
  The advantage of the interrupt method is that it has relatively little impact on the running of the main program and has high system resource utilization. The disadvantage is that the peripheral circuit of the microcontroller is added, and the hardware is slightly complicated.
3.2 Implementation of LIN protocol based on common port bits
  of microcontroller For microcontrollers that do not have 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 kind of microcontroller is its low price, and the LIN node produced will have an unparalleled price advantage.
(1) Implementation of LIN master node based on common port bits:
  The 1IN master node based on common port bits of single-chip microcomputer must adopt bit operation method in the realization of message header and data field. The hardware composition schematic diagram is exactly the same as the hardware schematic diagram of the LIN master node based on the universal serial port of a microcontroller. The difference is that the data transceiver and TXD terminals of the former can be any common port bits of the microcontroller, while the latter must use a universal serial port of the microcontroller.
  The implementation of the message header interval field can set the timer TO to interrupt when timing a bit, set the LIN data sending terminal TXD to the dominant level (low potential), and start the timer T0 to adjust the dominant level duration. Timing is carried out, and when more than 13 bits are reached, the rear LIN transmitting data terminal TXD is recessive level (high potential), thus completing the transmission of the interval field. The same method is also used in the subsequent implementation of interval field delimiters and synchronization fields. In the reception and transmission of the data field, the cooperation of the timer TO is also required to complete. When sending data, data are taken out one by one from the data storage area to be sent and converted into 10 bit type bit data. Timer T0 is also interrupted once when it is 1 bit, and the timing variable value is changed in the interrupt handler. The data sending program sends 10 pieces of bit type bit data from the data sending end TXD end in sequence according to the difference in timing variables and lasts for 1 bit time; when receiving data, you need to use timer T0 to time half a bit time. , to detect the start bit of 1 byte, and then resume the timing interrupt setting when one bit of timer TO is set. In this way, the subsequent data bit detection can ensure that the data bit is detected at the middle moment of the data bit, thereby ensuring the correctness of the data bit reception. After receiving the 10 bit type bit data, it must be converted into a byte type data and stored in the corresponding data buffer.

(2) Implementation of LIN slave node based on common port bits
  The hardware circuit of the LIN slave node based on the common port bits of the microcontroller is basically the same as the slave node hardware circuit based on the universal serial port query method of the microcontroller. The difference is that the universal serial port of the microcontroller 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 indefinite long-term interruption mode. After the time is up, the slave node will query the message frames waiting to be sent by the master node. When the synchronization information is detected, the timer is set to the standard bit time interrupt mode, and the process of receiving or sending data from the slave node is defined in bit time. 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 alternately switched during the query waiting and message communication processes. After the message communication process is completed, the timer is reset to the indefinite long interruption mode and waits for the next message communication process, and so on. .
  Since the slave node uses the irregular query and 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 query irregularly and use external interrupts to query the messages sent by the master node. In this way, the success rate of communication can be greatly improved.

Conclusion:
  Compared with enhanced microcontrollers, the functions of ordinary microcontrollers are much simpler and resources are very limited, but it has the advantage of low price. The LIN bus is characterized by a simple protocol and can be implemented in software. It is especially suitable for situations 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 microcontrollers, it will undoubtedly provide a good choice for the low-end serial communication field. LIN nodes based on ordinary microcontrollers will definitely make great achievements in related application fields of production and life.

Reference address:Implementation scheme of LIN protocol based on ordinary microcontroller

Previous article:
Next article:Design of electric bicycle speed and odometer based on microcontroller and photoelectric sensor

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号