void LIN_Transmit(void) { // send break followed by 0x55 'autobaud' byte while (U1STAbits.TRMT == 0); // wait for transmitter empty while (U1STAbits.UTXBRK == 1); // wait for HW to clear the previous BREAK U1STAbits.UTXEN = 1; // Enable UART TX U1STAbits.UTXBRK = 1; // set the BREAK bit U1TXREG = 0; // dummy write to trigger UART transmit Nop(); // must wait 1 instruction cycle U1TXREG = 0x55; // AUTO-BAUD sync character per J2602 spec
// send the LIN_MESSAGE_ID byte, is arbitrary but must be in the range 0x00 to 0x3B // there are also 2 parity bits sent LIN_ID = 0x23; p0 = (LIN_ID & 0x01) ^ ((LIN_ID & 0x02) >> 1) ^ ((LIN_ID & 0x04) >> 2) ^ ((LIN_ID & 0x10) >> 4); p0 = p0 & 0x01; // get bit value p1 = ~(((LIN_ID & 0x02) >> 1) ^ ((LIN_ID & 0x08) >> 3) ^ ((LIN_ID & 0x10) >> 4) ^ ((LIN_ID & 0x20) >> 5)); p1 = p1 & 0x01; // get the bit value id_byte = (p1 << 7) | (p0 << 6) | LIN_ID; // stuff parity bits into proper places
// form protected ID byte and transmit it // the bit stuffing is optional, used here to test LIN receiver hardware checksum = id_byte; // initial checksum value (limited to a byte value) while (U1STAbits.TRMT == 0); // wait for transmitter empty Nop(); U1TXREG = id_byte; // transmit the protected ID byte