LIN, short for Local Interconnect Network, is a network protocol used in serial communication between different components inside of vehicles. "Wait, isn't CAN used in vehicles?" yes, somewhere, but implementing CAN protocol between all the components of a car turned out to be too expensive for manufacturers, so an agreement had to be made. Most cars today have some components talking via CAN, and others via LIN. Simple sensors are connected through LIN to create simple networks, these types of networks can then be connected with other networks in the vehicle through CAN. Let's look at how can actually works.
The Protocol
The LIN protocol is designed to have one Master and a maximum of 15 Slaves. The Master initiates communication by sending the synchronization byte, and after that the ID byte, each Slave responds to one particular ID byte. This way, only one Slave (the one with the corresponding ID byte) will respond to the initialization. The Master and the Slaves communicate with LIN protocol frames. Each frame consists of a break signal, a synchronization byte, data bytes (maximum 8) and a checksum byte.
Each slave will receive the initial break and synch bytes, as well as the ID byte. But only one slave which recognizes the ID byte will respond or receive the incoming Master messages.
LIN Frames
Initial Signal
At the start, the Master sends out a BREAK signal, which consists of 13 zero bits including the start bit. This signal grabs the attention of all the Slaves that the Master will send something.
Synch byte
After the initial BREAK signal, the Master sends the Synchronization byte. This byte determines the transmission rate which will be used. The bit pattern is 0x55 (01010101, max number of edges).
ID Byte
After the break signal, the master sends an ID byte. As we said earlier, only one slave responds to this byte. The ID byte consists of two parity bits, and 6 ID bits.
The slave uses the ID bits to determine if it should respond. If so, depending on the ID byte, the slave will respond with 2,4 or 8 bytes of data. Or, the slave will receive that amount of data from the Master.

Checksum
After response, the slave will send a checksum to complete the safe communication. The checksum is also sent by the Master if it is sending data after the ID byte.
Implementing LIN with MCP2003B Click
Firmware engineers at MikroElektronika have made a simple LIN protocol library which can be used to implement this communication protocol.
Let's take a quick look at the code, written for Microchip's PIC32
#include#include "lin.h" sbit tx_pin at LATF4_BIT; void main() { uint8_t send_buffer[10] = {0}; uint8_t recv_buffer[10] = {0}; uint8_t response_buffer[10] = {0}; UART2_Init( 9600 ); delay_ms(300); send_buffer[0] = 'H'; send_buffer[1] = 'E'; send_buffer[2] = 'L'; send_buffer[3] = 'L'; send_buffer[4] = 'O'; response_buffer[0] = 'H'; response_buffer[1] = 'E'; response_buffer[2] = 'L'; response_buffer[3] = 'L'; response_buffer[4] = 'O'; response_buffer[5] = 'B'; response_buffer[6] = 'C'; response_buffer[7] = 'K'; lin_begin(9600); lin_hal_init(); lin_send(50,send_buffer,5,2); lin_slave_receive(50,recv_buffer,2,0); lin_send(50,send_buffer,5,2); lin_slave_receive(50,response_buffer,2,1); lin_slave_receive(50,recv_buffer,2,0); while(1); }
First, we need to include the library, also, we need to define our TX pin, which will be used to send messages as well as the BREAK signal.
#include#include "lin.h" sbit tx_pin at LATF4_BIT;
In our main function, we will declare 3 buffers, one for sending bytes, one for receiving bytes from the slave, and one which will be used by the slave to respond to the received ID byte form the master.
Of course, since LIN is using serial communication, we need to initialize UART, also we will initialize the library.
uint8_t send_buffer[10] = {0}; uint8_t recv_buffer[10] = {0}; uint8_t response_buffer[10] = {0}; UART2_Init( 9600 ); delay_ms(300); lin_begin(9600); lin_hal_init();
We will add our desired values to our buffers. Master will send HELLO, the slave will receive it once. The second time master sends the same ID byte, the slave will respond with HELLOBCK (hello back), which is 8 bytes of data.
lin_send(50,send_buffer,5,2); lin_slave_receive(50,recv_buffer,2,0); lin_send(50,send_buffer,5,2); lin_slave_receive(50,response_buffer,2,1); lin_slave_receive(50,recv_buffer,2,0); while(1);
The master's message (HELLO) will be received to the slave's recv_buffer, after that, the next time master sends something, the slave will respond from the response buffer, which will also end up in the recv_buffer.
Be cautious though - LIN is highly dependent on time. It is best to implement a FIFO buffer along with a timer, which will schedule when master will send a frame, and when the slaves will be sniffing the line for it. Due to the large differences between architectures, we have left this implementation for the user to choose.
The LIN library with the example can be found on our Libstock Page.
The MCP2003 Click can be found here.