13 Configuring the UART Module for LIN
Local Interconnect Network (LIN) is a serial network protocol that is used primarily in automotive applications and consists of a LIN cluster that has one master and up to 16 slave modes on the bus. The UART module with protocol support has hardware built-in that handles the serial communication required by the LIN protocol and can be implemented using either the master process or slave process. With the combined hardware support and a LIN software library, users can get started with basic master and slave reception/transmission. In the LIN protocol, the master controls the bus activity, while the slave(s) send or receive data based on the scheduled tasks.
LIN Master mode has the capabilities of generating slave processes. Any data that is transmitted in Master/Slave mode is done as a slave process. Example 13-1 shows a basic initialization routine that can be used to configure the UART module with protocol support in LIN Master/Slave mode. Example 13-2 shows an initialization routine that can be used to configure the UART module with protocol support in LIN Slave mode.
LIN Master/Slave Mode Configuration
void UART_Initialize(){
UxCON0bits.MODE = 0b1100; //Select LIN Master / Slave mode
UxCON0bits.TXEN = 1; //Enable transmitter
UxCON0bits.RXEN = 1; //Enable receiver
UxCON2bits.TXPOL = 0; //Standard polarity, High Idle State
UxCON2bits.STP = 0b10; //Desired Stop Bits Selection
UxCON2bits.C0EN = 0; //Desired Checksum Mode, Legacy LIN checksum
// Baud rate generator settings
UxBRG = 0x3F; //Value to achieve desired baud rate
//PPS settings for TX functionality (May vary device to device)
ANSELxbits.ANSELxy = 0; //Make Rxy a digital I/O
TRISxbits.TRISxy = 0; //Make Rxy an output
RxyPPS = 0b010011; //Assign TX functionality to Rxy
UxON = 0x01; //Turn on UART module
}
LIN Slave-only Mode Configuration
void UART_Initialize(){
UxCON0bits.MODE = 0b1011; //Select LIN Slave mode
UxCON0bits.TXEN = 1; //Enable transmitter
UxCON0bits.RXEN = 1; //Enable receiver
UxCON2bits.TXPOL = 0; //Standard polarity, High Idle State
UxCON2bits.STP = 0b10; //Desired Stop Bits Selection
UxCON2bits.C0EN = 0; //Desired Checksum Mode, Legacy LIN checksum
//Data packet settings
UxP2 = 0x10; // Number of Data Bytes to Transmit
UxP3 = 0x10; // Number of Data Bytes to Recieve
// Baud rate generator settings
UxBRG = 0x3F; //Value to achieve desired baud rate
//PPS settings for TX functionality (May vary device to device)
ANSELxbits.ANSELxy = 0; //Make Rxy a digital I/O
TRISxbits.TRISxy = 0; //Make Rxy an output
RxyPPS = 0b010011; //Assign TX functionality to Rxy
UxON = 0x01; //Turn on UART module
}