PICDEM LIN User's Guide

PICDEM™ LIN
User’s Guide
 2004 Microchip Technology Inc.
DS41185C
Note the following details of the code protection feature on Microchip devices:
•
Microchip products meet the specification contained in their particular Microchip Data Sheet.
•
Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the
intended manner and under normal conditions.
•
There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our
knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchip’s Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual property.
•
Microchip is willing to work with the customer who is concerned about the integrity of their code.
•
Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not
mean that we are guaranteeing the product as “unbreakable.”
Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our
products. Attempts to break Microchip’s code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts
allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.
Information contained in this publication regarding device
applications and the like is intended through suggestion only
and may be superseded by updates. It is your responsibility to
ensure that your application meets with your specifications.
No representation or warranty is given and no liability is
assumed by Microchip Technology Incorporated with respect
to the accuracy or use of such information, or infringement of
patents or other intellectual property rights arising from such
use or otherwise. Use of Microchip’s products as critical
components in life support systems is not authorized except
with express written approval by Microchip. No licenses are
conveyed, implicitly or otherwise, under any intellectual
property rights.
Trademarks
The Microchip name and logo, the Microchip logo, Accuron,
dsPIC, KEELOQ, microID, MPLAB, PIC, PICmicro, PICSTART,
PRO MATE, PowerSmart, rfPIC, and SmartShunt are
registered trademarks of Microchip Technology Incorporated
in the U.S.A. and other countries.
AmpLab, FilterLab, MXDEV, MXLAB, PICMASTER, SEEVAL,
SmartSensor and The Embedded Control Solutions Company
are registered trademarks of Microchip Technology
Incorporated in the U.S.A.
Analog-for-the-Digital Age, Application Maestro, dsPICDEM,
dsPICDEM.net, dsPICworks, ECAN, ECONOMONITOR,
FanSense, FlexROM, fuzzyLAB, In-Circuit Serial
Programming, ICSP, ICEPIC, Migratable Memory, MPASM,
MPLIB, MPLINK, MPSIM, PICkit, PICDEM, PICDEM.net,
PICLAB, PICtail, PowerCal, PowerInfo, PowerMate,
PowerTool, rfLAB, rfPICDEM, Select Mode, Smart Serial,
SmartTel and Total Endurance are trademarks of Microchip
Technology Incorporated in the U.S.A. and other countries.
SQTP is a service mark of Microchip Technology Incorporated
in the U.S.A.
All other trademarks mentioned herein are property of their
respective companies.
© 2004, Microchip Technology Incorporated, Printed in the
U.S.A., All Rights Reserved.
Printed on recycled paper.
Microchip received ISO/TS-16949:2002 quality system certification for
its worldwide headquarters, design and wafer fabrication facilities in
Chandler and Tempe, Arizona and Mountain View, California in
October 2003. The Company’s quality system processes and
procedures are for its PICmicro® 8-bit MCUs, KEELOQ® code hopping
devices, Serial EEPROMs, microperipherals, nonvolatile memory and
analog products. In addition, Microchip’s quality system for the design
and manufacture of development systems is ISO 9001:2000 certified.
DS41185C-page ii
 2004 Microchip Technology Inc.
PICDEM™ LIN User’s Guide
Table of Contents
Chapter 1. PICDEM™ LIN Introduction
1.1
Welcome ......................................................................................... 1
1.2
PICDEM LIN Demonstration Board ................................................ 2
1.3
Sample Devices .............................................................................. 2
1.4
Sample Programs ........................................................................... 2
1.5
PICDEM LIN User’s Guide ............................................................. 2
1.6
Reference Documents .................................................................... 3
Chapter 2. Getting Started
2.1
PICDEM LIN as a Stand-Alone Board ............................................ 5
2.2
PICDEM LIN as a Stand-Alone Board – Sample Programs ........... 6
2.3
Hardware Description ..................................................................... 7
Chapter 3. Tutorial
3.1
Tutorials ........................................................................................ 13
Appendix A. Hardware Detail ................................................................19
A.1
Board Layout and Schematics ...................................................... 19
Appendix B. LIN bus Master..................................................................29
B.1
LIN bus Master Code .................................................................... 29
Appendix C. LIN bus Slave for the PIC16C432 ....................................41
C.1
LIN bus Slave for the PIC16C432 Code ....................................... 41
Appendix D. LIN bus Slave for the PIC16C432 (slave2.asm)..............53
D.1
LIN bus Slave for the PIC16C432 (slave2.asm) ........................... 53
Index ..........................................................................................................65
Worldwide Sales and Service .................................................................68
 2004 Microchip Technology Inc.
DS41185C-page iii
PICDEM™ LIN User’s Guide
DS41185C-page iv
 2004 Microchip Technology Inc.
PICDEM™ LIN USER’S GUIDE
Chapter 1. PICDEM™ LIN Introduction
1.1
WELCOME
Thank you for purchasing the PICDEM LIN demonstration board from Microchip
Technology Incorporated. The PICDEM LIN is a simple board, which demonstrates the
capabilities of several Microchip microcontrollers, specifically from the PIC16C432,
PIC16C433, PIC16C7XX, PIC16X8X, PIC18FXX8 families and the stand-alone LIN
transceiver with built-in voltage regulator MCP201, using the LIN bus protocol.
The PICDEM LIN can be used stand-alone with a programmed part, or with an emulator
system, such as MPLAB® ICE. Sample programs are provided to demonstrate the
unique features of the supported devices.
The PICDEM LIN Kit comes with the following:
1.
2.
3.
4.
PICDEM LIN Demonstration Board
Sample devices
Sample programs
PICDEM LIN Demonstration Board User’s Guide (this document)
Note:
Please contact your nearest Microchip sales office for any
missing parts from the kit.
FIGURE 1-1:
 2004 Microchip Technology Inc.
PICDEM LIN KIT
DS41185C-page 1
PICDEM™ LIN User’s Guide
1.2
PICDEM LIN DEMONSTRATION BOARD
The PICDEM LIN demonstration board has the following hardware features:
1. 18-pin, 28-pin and 40-pin DIP sockets (although 3 sockets are provided, only one
device may be used at a time).
2. On-board +5V regulator for direct input from 12V.
3. RS-232C socket and associated hardware for direct connection to RS-232C
interface.
4. CAN bus interface.
5. Control panel interface for LIN bus master.
6. RF stage for keyless entry function.
7. Seat memory unit.
8. Motor control slave node.
9. Jumper to disconnect on-board RC oscillator (approximately 2 MHz).
10. Prototype area for user hardware.
1.3
SAMPLE DEVICES
Several UV erasable and Flash devices are included. The device types may change.
These devices are programmed with firmware to provide LIN bus communication. The
supplied devices are typically one of the following:
• PIC16F874
• PIC16C432
• PIC16C433
1.4
SAMPLE PROGRAMS
The PICDEM LIN Kit includes a CD-ROM with sample demonstration programs on
them. These programs may be used with the included sample devices or with an
emulator system. The programs are:
• demo432.asm – PIC16C432 LIN bus Demo
• demo433.asm – PIC16C433 LIN bus Demo
• demo874.asm – PIC16F874 LIN bus Demo
1.5
PICDEM LIN USER’S GUIDE
This document describes the PICDEM LIN demonstration board, tutorial and demonstration software, to give the user a brief overview of the PICmicro® MCUs supported.
Detailed information on individual microcontrollers may be found in the device’s
respective data sheet.
Chapter 1: Introduction – This chapter introduces the PICDEM LIN and provides a brief
description of the hardware.
Chapter 2: Getting Started – This chapter goes through a basic step-by-step process
for getting your PICDEM LIN up and running as a stand-alone board.
Chapter 3: Tutorials – This chapter provides a detailed description of the tutorial
programs.
Appendix A: Hardware Detail - This appendix describes in detail the hardware of the
PICDEM LIN board.
DS41185C-page 2
 2004 Microchip Technology Inc.
PICDEM™ LIN Introduction
1.6
REFERENCE DOCUMENTS
Reference Documents may be obtained by contacting your nearest Microchip sales
office, or by downloading via the Microchip web site www.microchip.com.
•
•
•
•
•
•
•
Individual data sheets
MPLAB® IDE, Simulator and Editor User’s Guide (DS51025)
MPASM™ User’s Guide with MPLINK™ and MPLIB™ (DS33014)
PRO MATE® II User’s Guide (DS30082)
MPLAB® ICE Emulator User’s Guide (DS51159)
PICmicro® Mid-Range Reference Manual (DS33023)
LIN bus Specification Ver. 1.2 (can be obtained from www.lin-subbus.de)
 2004 Microchip Technology Inc.
DS41185C-page 3
PICDEM™ LIN User’s Guide
NOTES:
DS41185C-page 4
 2004 Microchip Technology Inc.
PICDEM™ LIN USER’S GUIDE
Chapter 2. Getting Started
The PICDEM LIN may be used as a stand-alone board or with an emulator. The
emulator discussed in this chapter is the PICMASTER® emulator. However, other
emulators may be used. For a list of PICmicro® MCU compatible emulators, please
refer to the Microchip Development Systems Ordering Guide, (DS30177).
2.1
PICDEM LIN AS A STAND-ALONE BOARD
The PICDEM LIN may be demonstrated immediately by following the steps listed
below:
• Make sure the preprogrammed sample devices are in the appropriate socket on
the PICDEM LIN board.
• Apply power to the PICDEM LIN (see Figure 2-1).
• Connect the two panels with a flat ribbon cable (see Figure 2-2).
• Press buttons S8 or S10 on the console panel to see ports on the slave nodes
increment or decrement.
These steps are illustrated in Figure 2-1.
CONNECTING THE LIN BUS DEMO BOARD
Panel 1
Master Board
 2004 Microchip Technology Inc.
Power Start
432 Motor Driver
High Current Node
Prototype
Board
Slave Node 1
Console Panel
RKE Node
12V Supply
Panel 2
Slave Node 2
FIGURE 2-1:
Flat Ribbon Cable
DS41185C-page 5
PICDEM™ LIN User’s Guide
FIGURE 2-2:
FLAT RIBBON CABLE CONNECTION
Prototyping
Board
Slave Node 1
VBAT
LIN
GND
VBAT
LIN
GND
JP5
JP4
Panel 2
Panel 1
FLAT RIBBON CABLE
2.2
PICDEM LIN AS A STAND-ALONE BOARD – SAMPLE PROGRAMS
To demonstrate PICDEM LIN operation with one of the sample programs, the sample
device will have to be erased and reprogrammed. Once the device has been
reprogrammed:
• Make sure the sample device is in the appropriate socket on the PICDEM LIN
board.
• Apply power to the PICDEM LIN.
• Consult the appropriate chapter in this document for information on the execution
of each demo.
2.2.1
Erasing the Sample Device
To erase an EPROM device:
• Remove any labels covering the device window. If you do not have a windowed
device (Figure 2-3), you cannot reprogram it. A windowed version of all EPROM
devices may be ordered by requesting the JW package.
• Place the device in an Ultraviolet (UV) EPROM Eraser. The amount of time
required to completely erase a UV erasable device depends on: the wavelength of
the light, its intensity, distance from UV source, and the process technology of the
device (how small are the memory cells).
• Verify that the device is blank (e.g., perform a blank check) before attempting to
program it.
FIGURE 2-3:
DS41185C-page 6
WINDOWED DEVICE
 2004 Microchip Technology Inc.
Getting Started
To erase an EEPROM/Flash device:
• Enable your programmer in MPLAB IDE. If you change programmers, you will
have to restart MPLAB IDE.
• Place the device in the programmer.
• Select Erase Program Memory or the equivalent erase command from the
Programmer menu.
Note:
2.2.2
You do not have to erase an EEPROM/Flash device before
reprogramming it.
Reprogramming the Sample Device
To reprogram the erased sample device, the following will be necessary:
1. Sample programs installed on the hard drive.
The PICDEM LIN package includes a 3.5-inch disk, which contains sample
programs for all the processor types supported. Instructions on how to install the
programs can be found in the “README” file also on the disk.
2. An assembler, such as MPASMTM Assembler available with MPLAB IDE.
Sample programs may be used to program the sample device once they have
been assembled. Microchip Technology’s MPLAB Integrated Development
Environment (IDE) includes an assembler (MPASM). However, other assemblers
may be used.
3. A device programmer, such as PRO MATE® II.
4. Once the sample program is in hex file format, a programmer may be used to
program a blank device. Microchip Technology’s PRO MATE II or PICSTART®
Plus programmers may be used. Both are compatible with MPLAB IDE. However, other programmers may be used.
If the code protection bit(s) have not been programmed, the on-chip program memory
can be read out for verification purposes.
Note:
2.3
Microchip does not recommend code protecting windowed
devices.
HARDWARE DESCRIPTION
2.3.1
Circuits in Common
Much of the circuitry on each of the subsystem boards is the same. These building
blocks will be referred to later in each of the node descriptions without further detail.
2.3.1.1
POWER SUPPLY
The power supplies on each board are built around an automotive grade voltage
regulator. The voltage regulators are protected by a reverse polarity blocking diode and
a 45V Zener diode, filtered on both the input and output. An LED is connected to the
output for ’power-on’ indication.
2.3.1.2
LIN BUS TRANSCEIVER
The Local Interface Network transceiver is socketed to accept an industry standard LIN
or K/L-Line transceiver. The Microchip Technology MCP201 can be accommodated by
removing the jumper connecting pin 3, VDD, to the board VCC power plane.
 2004 Microchip Technology Inc.
DS41185C-page 7
PICDEM™ LIN User’s Guide
2.3.1.3
ICD/ICSP™ INTERFACE
The LIN bus master node, as well as the seat memory control node, are equipped with
an ICSP interface connector. The ICSP interface allows programming the PICmicro
devices in-circuit, without removing them from the board. When the ICSP interface is
used, RB3, RB6 and RB7 are no longer available to the user on 28- and 40-pin devices.
2.3.2
The LIN bus Master Node
The LIN bus master node is designed to perform several tasks:
• Stand-alone LIN bus Master
• LIN-to-CAN Gateway
• RS-232-to-LIN Gateway
The CPU may be any of several PICmicro devices, in either 28- or 40-pin dual in-line
packages. As a master node, the buttons and output devices on the console panel can
be scanned and controlled, and LIN messages transmitted over the network. The
schematic of the LIN bus master node is shown in Figure A-2.
The master node also has both a CAN controller and transceiver, as well as RS-232
buffers connected to the internal USART. These facilities allow the node to function as
a CAN- or RS-232-to-LIN gateway.
2.3.2.1
PROCESSOR SOCKETS
Two sockets are provided, only one device may be used at a time.
• 28-pin socket
• 40-pin socket
2.3.2.2
RS-232 SERIAL PORT
An RS-232 level shifting IC has been provided with all the necessary hardware to support connection of a RS-232 host through the DB9 connector. The port is configured as
DCE, and can be connected to a PC using a straight through cable.
28/40-pin devices have their RX and TX pins tied to the RX and TX lines of the MAX
RS-232 via the Jumper E7.
The jumper E7 routes the RX and TX lines of the USART to either the LIN bus transceiver chip or the MAX RS-232 chip. Figure 2-4 shows the configuration if the USART
is connected to the LIN bus transceiver. In this case, no pins will be connected to the
MAX RS-232 chip.
Figure 2-5 shows the configuration where the USART is connected to the LIN bus
transceiver chip, RB5 to the Transmit line, and RB4 to the Receive line of the RS-232
chip.
FIGURE 2-4:
JUMPER E7 CONNECTS USART TO LIN BUS TRANSCEIVER
Jumper E7
1
DS41185C-page 8
 2004 Microchip Technology Inc.
Getting Started
FIGURE 2-5:
USART CONNECTED TO LIN BUS TRANSCEIVER, RB4 AND
RB5 TO MAX RS-232
Jumper E7
1
FIGURE 2-6:
JUMPER E7 CONNECTS USART TO MAX RS-232
Jumper E7
1
FIGURE 2-7:
USART CONNECTED TO MAX RS-232 AND RB4 AND RB5 TO
LIN BUS TRANSCEIVER
Jumper E7
1
2.3.2.3
SWITCHES
One switch provides the following function:
• S2 – MCLR to hard reset the processor
Switch S2 has a debouncing capacitor. When pressed, the switch is grounded. When
IDLE, it is pulled high (+5V).
2.3.2.4
OSCILLATOR OPTIONS
The LIN bus master runs off a 20 MHz crystal, but the oscillator can be changed from
crystal to a resonator or RC oscillator. This is done by unsoldering the crystal and C20,
and installing a 10K resistor on R36. This will allow the PICmicro MCU to run off an RC
oscillator.
 2004 Microchip Technology Inc.
DS41185C-page 9
PICDEM™ LIN User’s Guide
2.3.2.5
CAN BUS CONNECTIONS
There are two ways to connect to the CAN bus. One way is to use the MCP2510
stand-alone CAN controller, and the other way is to use a PICmicro MCU with an
internal CAN interface (e.g., PIC18F258 or PIC18F458).
Jumper E4 and E5 will select whether the CAN transceiver chip will be connected to
the MCP2510 stand-alone CAN controller, or pins RB2 and RB3 of the 40/28-pin
socket.
RB3 of the 40/28-pin socket.
2.3.2.6
CONSOLE PANEL
The console panel board is connected to PORTD and PORTE of the 40-pin socket.
The purpose of the control panel is to provide input and output to the master board,
which could, for example, be transformed via LIN bus firmware to a LIN bus message.
If the console board is separated from the master board, then the console board can
be connected with a flat ribbon cable via Jumper J6.
2.3.3
The Console Panel
The console panel provides input and output to the LIN bus Master. The schematic of
the console panel is shown in Figure A-1.
The inputs from the console panel can be used to generate the LIN bus message. The
LED, audio transducer and switches on the console panel interface to PORTD and
PORTE of the master board. If the boards are separated, the console panel can be
connected via a flat cable from J7 to the master board.
2.3.3.1
CONSOLE INPUTS
There are ten switches and one Jumper input. All the push button switches are pulled
to VDD and closed to Ground. The IGNITION switch output is VDD when closed, and
open circuit in the OFF position. The PARK/NEUTRAL switch is pulled up to VDD when
OFF and closes to Ground. The jumper E3 is meant to simulate the diagnostic port
jumper in a typical automotive diagnostic link. This jumper could, for example, cause
the master to send data and error codes to a diagnostic unit.
2.3.3.2
CONSOLE OUTPUTS
The malfunction indicator lamp (MIL) is connected to VDD and turned on by taking the
input to ground. The Chime audio beeper has a self-contained sound generator and
only needs to be grounded to activate.
2.3.4
The Remote Keyless Entry Panel
The purpose of the Remote Keyless Entry Panel is to provide an interface from a key
fob to the LIN bus. The schematic of the remote keyless entry node is shown in
Figure A-4. The node can be used as either LIN bus master or LIN bus slave.The
decision of which configuration is chosen depends on the firmware design.
The Remote Keyless Entry panel is equipped with a 28-pin socket for a PICmicro MCU,
an RF board, a LEARN button and LED, and a LIN bus transceiver.
In order to program the PICmicro MCU to identify encoder transmitters, a LEARN
switch input is routed to RB5 and a LED is routed to RB2. A high voltage tolerant input
is routed to RB1.
2.3.4.1
RF RECEIVER
The RF receiver is a standard Telecontrolli module, as supplied in the KEELOQ®
development kits. The module installed is for 433.9 MHz.
DS41185C-page 10
 2004 Microchip Technology Inc.
Getting Started
2.3.5
General Prototype Board
This circuit is designed for general purpose use. The schematic of the board is shown
in Figure A-3. The general prototype area is assembled with four sockets. A 28-pin and
40-pin socket are for dual in-line packages. In the prototype area itself, are two areas
where a 16-pin SOIC package can be installed. This area could be used, for example,
for drivers.
Pins of RC6 and RC7 are routed to the single chip LIN bus transceiver.
2.3.6
Power Seat Panel
This circuit can be used to demonstrate a typical seat position memory module. With
proper firmware, it can be commanded from either the on-board button inputs or via the
LIN bus. The schematic of this panel is given in Figure A-5.
2.3.6.1
BUTTON AND SENSOR INPUTS
Three button inputs are connected to RA4, RA5 and RC3. These are labeled MEM#1,
MEM#2 and SET, respectively. J12 is provided to enable external switch input connections. J12 also includes a signal to drive an external LED “Memory” function indicator.
Headers J9, J11, J15 and J16 connect to the external (user supplied) motors and have
connections for analog feedback of motor position. These four analog inputs are routed
through protective RC networks to RA0:3. These analog voltages may not exceed the
range of 0V to 5.0V. If the potentiometers are connected ratio metrically between Vcc
and ground, R17, R19, R27 and R31 may be omitted.
Header J8 is for external, manual motor control push button switches. These switches
are connected directly to the relay driver circuit and operate the position motors
independently from the microcontroller. These buttons can also be read through
PORTB of the PIC® microcontroller.
A high voltage tolerant input is routed to RC5. This is labeled as IGNITION.
2.3.6.2
RELAY DRIVER
Power relays for the seat motors are provided on-board. These are driven by either a
high- or low-side, octal 500 mA driver. Configuration of the board to accept a particular
driver is done by jumpers J10, J13 and J14. Refer to the schematic to determine the
proper driver device.
Jumper
Position
J10
A-B
J13
J14
Description
Relay common to VBAT, U8 is a low-side driver
B-C
Relay common to GND, U8 is a high-side driver
A-B
Pin 9, U8 = VBAT
B-C
Pin 9, U8 = GND
A-B
Pin 10, U8 = GND
B-C
Pin 10, U8 = VBAT
U13 and U14 provide high-voltage, open-collector isolation to the PIC microcontroller.
The drivers are controlled by PORTB of the PICmicro MCU.
2.3.6.3
ICD/ICSP INTERFACE
The seat memory control node is equipped with an ICSP interface connector. The ICSP
interface allows programming of the PICmicro MCU in-circuit without removing it from
the board. When the ICSP interface is used for In-Circuit-Debug (ICD), RB3, RB6 and
RB7 are no longer available to the user on 28- and 40-pin devices. Motor control
headers J9 and J15 should not be used.
 2004 Microchip Technology Inc.
DS41185C-page 11
PICDEM™ LIN User’s Guide
2.3.7
LIN bus Slave Node H-Bridge Drivers
2.3.7.1
SLAVE NODE 1
Slave node 1 is designed for the PIC16C433 and Allegro A3976KLB dual H-bridge
driver. The I/O pins of the PIC16C433 can be connected to the driver device or any of
the input circuits, by wire-wrapping the appropriate connections on the Jumper Field
J20. Alternatively, the micro pins can be individually connected to an LED for indication
by E18, E20-24. Two analog inputs are routed from Connector JP9 to the Jumper Field
J20.
The schematic of this board is displayed in Figure A-6.
2.3.7.2
SLAVE NODE 3 HIGH VOLTAGE, HIGH CURRENT SLAVE NODE
This node is designed to demonstrate the PIC16C432 with a high voltage, high current
motor driver. A high voltage and high current driver, such as a A3952SW, can be used.
The motor driver is controlled by pins RB4 through RB7. The output of the driver is
routed to J24 and J25. RB0 to RB3 can be connected to LEDs via the Jumpers E25 to
E28.
The schematic of this board is given in Figure A-8.
2.3.7.3
SLAVE NODE 4 HIGH VOLTAGE, HIGH CURRENT SLAVE NODE WITH
ANALOG INPUTS
This slave node functions the same as the previous slave node, with the following
additions:
• RB4 through RB7 can also be routed to LEDs
• RA0 to RA3 can be used as analog inputs. The analog inputs are routed to the
Connector JP1.
The schematic of this board is shown in Figure A-9.
2.3.8
LIN bus Slave Node High-Side Driver
2.3.8.1
SLAVE NODE 2
Slave node 2 is designed for the PIC16C432 and a MC33143DW high-side driver. The
I/O pins of the PIC16C432 can be connected to either the high-side driver and/or the
LEDs.
In order to connect I/O pins from PORTB to the LEDs, Jumpers E10 to E17 have to be
installed. If some of the I/O pins from PORTB are to be connected to the high-side
driver, the respective Jumpers E10 to E17 should be removed and a connection has to
be established on the Jumper Field J19.
Analog inputs to PORTA pins are provided through Connector JP11.
The schematic of this board is given in Figure A-7.
DS41185C-page 12
 2004 Microchip Technology Inc.
PICDEM™ LIN USER’S GUIDE
Chapter 3. Tutorial
3.1
TUTORIALS
The LIN bus master and slave programs are preprogrammed into the sample devices.
These programs are listed on the included CD-ROM for user reference. For example,
if the sample device has been reprogrammed with another sample program, the tutorial
may be reassembled and reprogrammed into the device.
The tutorial program functions as follows.
For detailed information on the LIN bus hardware, please refer to Appendix A.
3.1.1
The Master Software
The source code of the master software is shown in Appendix B. The flow chart of the
main routine is shown in Figure 3-1. In this code example, the LIN bus master is
realized with the USART of the PIC16F874.
After initialization, the master routine reads in a key from the console panel. After a key
is pressed and debounced, the parity bits, according to the LIN bus specification, are
generated. The parity bits are inserted automatically into the identifier byte. The identifier
byte includes the code for the key, which was pressed, and the parity bits. After the parity
bits are generated, the checksum is computed. The checksum is appended after the last
data byte. After the checksum computation, all data is transmitted via the LIN bus. Once
the transmission is done, the routine waits until another key is pressed.
Following, the single subroutines are described.
Note:
3.1.1.1
The LIN firmware is only LIN 1.2 compliant.
THE KEYBOARD READ ROUTINE
This routine reads in one key from the console panel and debounces it. Depending on
the key, an 8-bit value is returned to the main routine. This 8-bit value is stored in the
ID register (ID_TEMP).
3.1.1.2
PARITY BIT GENERATION
This routine computes the parity bits for the identifier byte. The identifier byte, which
contains only the code for the key, is used for this calculation. The calculation is done
according to the LIN bus specification, Ver. 1.2. The calculate parity bits are stored in
bit 6 and bit 7 of the identifier byte.
 2004 Microchip Technology Inc.
DS41185C-page 13
PICDEM™ LIN User’s Guide
3.1.1.3
CHECKSUM CALCULATION
The checksum in this routine is calculated according to LIN bus specification, Ver. 1.2.
The identifier byte contains a field that is used to determine the amount of data bytes
calculated into the checksum. The number of data bytes to transmit is coded in the
identifier byte. The FSR register points to the master routine in the data byte field. Once
the checksum routine is called, the FSR register points to the first data byte to be transmitted. The data byte array is initialized during start-up. After the checksum is calculated, it is checked to see whether the CRC has to be appended to the data bytes
(Master Transmission mode), or if the CRC has to be checked (Master Reception
mode). The information, whether the CRC has to be checked or appended to the data
bytes, is given by the identifier byte (ID_TEMP).
3.1.1.4
THE LIN BUS TRANSMISSION ROUTINE
In this routine, the ID data bytes and the CRC bytes are transmitted via the USART to
the LIN bus.
First, the Synchronization byte is transmitted. This signal is 13 bits wide. Since the
USART can only transmit 8 bits at a time, the baud rate is slowed to achieve a 13-bit
signal at the nominal bit rate. The nominal bit rate in this example program is 19.2
Kbaud.
After the synchronization byte is transmitted to the USART, switched back to the
nominal baud rate, and the synchronization field is transmitted, the synchronization
field is followed by the identifier byte. After the identifier byte is transmitted, it is checked
to see whether data has to be received from a slave node or data has to be transmitted
to a slave node.
In this example code, the Master is a transmitter only.
In Transmit mode, the master will transmit his data and CRC value. After everything is
transmitted, the routine returns to the main routine.
In Receive mode, all data, including CRC, is received. The CRC value is checked and
the routine returns to the main routine.
3.1.1.5
ERROR HANDLING
In the example, no error handling is implemented. The LIN bus specification gives the
user the flexibility to handle errors.
DS41185C-page 14
 2004 Microchip Technology Inc.
Tutorial
FIGURE 3-1:
MAIN ROUTINE OF LIN BUS MASTER
Start-up
Main Routine
Read Keys
Generate Parity
Bit
Generate
Checksum
Transmit Data via
LIN bus
 2004 Microchip Technology Inc.
DS41185C-page 15
PICDEM™ LIN User’s Guide
3.1.2
The PIC16C432 LIN bus Slave Code
The source code of the PIC16C432 LIN bus slave software is shown in Appendix C.
The flowchart of the main routine is shown in Figure 3-2. In this code example, the LIN
bus slave is realized entirely in software without hardware support.
After Reset, the PIC16C432 initializes its registers. After the initialization phase, the
MCU waits for the synchronization break signal from the master. Once the signal is
detected, the slave branches into the LIN handler subroutine. After receiving the
transmitting data, the main routine checks what actions have to be taken upon the
received identifier. Two actions are implemented:
• Pushing the WindowDown button on the console panel will decrement the LEDs
connected to PORTB
• Pushing the WindowUp button on the console panel will increment the LEDs
connected to PORTB
After PORTB is either incremented or decremented, the routine waits for the next
synchronization break signal.
3.1.2.1
LIN BUS HANDLER
After the synchronization break signal was received, the PIC16C432 waits for the Start
bit of the synchronization byte. Once the Start bit is detected, a software counter is
incremented until all bits for the synchronization byte are received. After measuring the
time, the baud rate is calculated by dividing the measured time by eight.
Following the calculation of the baud rate, the identifier byte is received. This identifier
byte is used to determine whether the slave has to transmit data or receive data. Prior
to decoding the identifier byte, the parity bits are checked. Error handling is not
implemented, since the error handling is left to the user by the LIN bus specification.
In this example implementation, only the Receive mode is used.
Transmit mode
Prior to transmitting all data, the CRC value for the data to be transmitted is generated.
After all data is transmitted, the LIN handler routine returns to the main routine.
Receive mode
After receiving all data received, the CRC value is checked. After the CRC check, the
LIN bus handler routine returns to the main routine.
DS41185C-page 16
 2004 Microchip Technology Inc.
Tutorial
FIGURE 3-2:
PROGRAM FLOW OF THE PIC16C432 LIN SLAVE CODE
Start-up
Main Program
No
Synch Field
detected?
Yes
LIN Handler
Receive Message
ID=TEST?
Yes
Turn on LED
connected to RB0
No
ID=WindowUp?
ID=WindowDown?
Yes
Yes
Increment PORTB
Decrement PORTB
No
 2004 Microchip Technology Inc.
DS41185C-page 17
PICDEM™ LIN User’s Guide
3.1.3
The PIC16C433 LIN bus Slave Code
The source code of the PIC16C433 LIN bus slave software is shown in Appendix D.
The Program flow is almost identical to that described in 3.1.2 “The PIC16C432 LIN
bus Slave Code”, with the following exceptions:
• Pushing the WindowDown button on the console panel will turn on GPIO0
• Pushing the WindowUp button on the console panel will turn off GPIO0
DS41185C-page 18
 2004 Microchip Technology Inc.
 2004 Microchip Technology Inc.
1
2
3
4
R48
R50
4.7K
1N5819
D46
OUT
C21 +
9
10
6
7
8
Size
A
03-01596
SlaveNode1
Sheet
2 of
5
1K
R30
VCC
"ON"
LED2 Green LED
9.0
Drawn by
Chuck Simmers
Rev
GP5
GP4
GP3
VCC
PWROUT2
PWREN2
PWROUT1
VCC
1
3
5
7
9
11
13
15
17
J20
2
4
6
8
10
12
14
16
18
STAT2
STAT1
PWREN1
GND
BACT
GP2
GP1
GP0
PIC16C433
13
GP5/OSC1/CLKIN
GP0/AN0
12
GP4/OSC2/AN3/CLKOUT
GP1/AN1/VREF
11
GP2/T0CKI/AN2/INT
GP3/MCLR/VPP
18
NC
VBAT
1
NC
LIN BUS
17
BACT
U10
22 uF
3
PICDEM™ LIN PIC16C433 and H-bridge Driver
Microchip Technology Inc.
AMAD
2355 W. Chandler Blvd.
Chandler, Arizona 85224
15K
R59
1N5819
15K
1N5819
D50
1N5819
25K
470
OSCOUT
OSCIN
10K
R40
25K
D45
D51
REG3
LM2937IMP-5.0
IN
VCC
1
R64
VCC
R65
R68
VCC
VCC
VCC
C35
Y3
C28 4 MHz
30 pF
Friday, February 14, 2003
APG
HEADER 4
JP9
0.1 uF
S6 Reset
+
30 pF C26
1N4755 43V
C19
D23 10 uF
VBB
23
22
2
3
11
14
A3976KLB
ENABLE2
PHASE2
ENABLE1
PHASE1
FAULT1
FAULT2
U14
VBB
VBB
OUT2B
OUT2A
OUT1B
OUT1A
LIN
.01 uF
C18
13
16
12
9
1
.005 uF
C17
1
1
1
1
1
1
2
2
2
2
2
2
1
2
3
JP5
470
470
R46
470
R44
470
R41
470
R37
470
R35
R33
Dual
H-Bridge
Output
4 HEADER
1
2
3
4
JP7
LIN
J14
LIN bus
Red LED
Red LED
LED8
Red LED
LED7
Red LED
LED6
Red LED
LED5
Red LED
LED4
LED3
1N4750 27V
D24
E24
E23
E22
E21
E20
E18
FIGURE A-1:
ChassisGND
J13
1N4004
1N4004
D25
D22
GND
A.1
2
Vbat-12V
J15
PICDEM™ LIN USER’S GUIDE
Appendix A. Hardware Detail
BOARD LAYOUT AND SCHEMATICS
H-BRIDGE DRIVER
DS41185C-page 19
1N4755 43V
VCC
R61
R70
VCC
1N5819
D43
15K
R63
1N5819
D44
18
2
3
4
5
16
17
RA0/AN0
RA2/AN2
RA3/AN3
RA3/AN4/T0CKI
MCLR/VPP
Drawn by
Chuck Simmers
5
Size
A
9.0
Rev
VCC
VCC
1
1
1
1
7
8
9
10
11
12
13
14
19
20
1
VCC
PIC16C432
RB0/INT
RB1
RB2
RB3
RB4
RB5
RB6
RB7
BACT
VBAT
LIN BUS
S7 Reset
VBB
C38
0.1 uF
OSC2/CLKOUT
03-01596
SlaveNode2
Sheet
3 of
R73
4.7K
VCC
OSC1/CLKIN
U13
PICDEM™ LIN PIC16C432 and High-side Driver
Microchip Technology Inc.
AMAD
2355 W. Chandler Blvd.
Chandler, Arizona 85224
25K
R72
1N5819
15K
R62
D37
25K
VCC
1N5819
D42
1N5819
15K
25K
R71
D36
D35
1N5819
D41
1N5819
15K
R60
VCC
APG
VCC
Friday, February 14, 2003
1
2
3
4
5
6
7
8
JP11
1N5819
25K
D34
R69
VCC
C32
470
R47
10K
Y5
4 MHz
30 pF
C29
VCC
C34 +
22 uF
R58
+
3
30 pF
C33
D29 10 uF
Note: For Analog inputs
replace series resistors with 240
ohms and grounded resistors
with .005uF caps
ChassisGND
J23
1N4004
REG5
1N4004 LM2937IMP-5.0
1
IN
OUT
GND
2
D31
3
VCC
1
Vbat-12V
J21
2
E19
GND
PWROUT2
PWREN
PWROUT1
D40
2
1
2
1
2
1
2
1
2
4
6
8
10
12
14
16
18
20
22
24
DS41185C-page 20
15
1
2
12
9
16
4
21
14
1
3
5
7
9
11
13
15
17
19
21
23
20
19
18
17
16
15
14
13
12
11
U12
INT
OUT2
STAT2
STAT1
OUT1
23
13
10
3
24
LIN
.01 uF
C36
HDSP-48XX
DP3
PWRINT
STAT2
STAT1
MC33143DW
GTST
IN1
CEN
IN2
Vbat
Vbat
Vbat
Vbat
SFPD
VBB
J19
VBB
1
2
3
4
5
6
7
8
9
10
VBAT
E10
2 E11
E12
2 E13
E14
2 E15
E16
2 E17
VCC
"ON"
C37
HCOut2c
470
I
H
G
F
E
D
C
B
A
COM
RP3
1
2
3
LIN
JP6
J22
LIN bus
1
2
3
JP8
High-Side
Output
1N4750 27V
D49
HCOut1c
.005 uF
10
9
8
7
6
5
4
3
2
1
R32 470
FIGURE A-2:
HCVbat
HCVbat
PICDEM™ LIN User’s Guide
HIGH-SIDE DRIVER
 2004 Microchip Technology Inc.
OSCIN
8
9
10
RE0
RE1
RE2
+
tbd
R5
1
RB0/INT
RB1
RB2
RB3/PGM
RB4
RB5
RB6/PGC
RB7/PGD
RD0/PSP0
RD1/PSP1
RD2/PSP2
RD3/PSP3
RD4/PSP4
RD5/PSP5
RD6/PSP6
RD7/PSP7
RC0/T1OSO/T1CKI
RC1/T1OSI/CCP2
RC2/CCP1
RC3/SCK/SCL
RC4/SDI/SDA
RC5/SDO
RC6/TX/CK
RC7/RX/DT
IN
LM2937ET-5.0
Rev
Drawn by
Chuck Simmers
Size
B
9
RD0
RD1
RD2
RD3
RD4
RD5
RD6
RD7
19
20
21
22
27
28
29
30
9.0
XCVRen
RC1
RC2
RC3
RC4
RC5
TX
RX
15
16
17
18
23
24
25
26
tbd
R14
03-01596
ProtoNode
Sheet
3 of
WAKEup
IGNition
FAULTn
RB3
RB4
RB5
RB6
RB7
33
34
35
36
37
38
39
40
REG1
3
OUT
PICDEM™ LIN Prototyping Board
Microchip Technology Inc.
AMAD
2355 W.Chandler Blvd.
Chandler, Arizona 85224
PICmicro® MCU 40-pin
RE0/RD/AN5
RE1/WR/AN6
RE2/CS/AN7
RA0/AN0
RA1/AN1
RA2/AN2/VREFRA3/AN3/VREF+
RA4/T0CLI
RA5/AN4/SS
OSC2/CLKOUT
OSC1/CLKIN
MCLR/VPP/THV
U8
Friday, February 14, 2003
APG
2
3
4
5
6
7
RA0
RA1
RA2
RA3
RA4
RA5
OSCOUT 14
1
13
RESETn
ChassisGND
J3
CHASSIS
D1
C6
10 uF
D8
1N4004
1N4004
HCVbat
1N4755 43V
VBAT
Vbat-12V
J5
D2
GND
2
RA5
RA4
RA3
C5
tbd
Q1
PNPtbd
1K
17
6
7
8
5
PIC16C432
RA0/AN0
RA2/AN2
RA3/AN3
RA3/AN4/T0CKI
MCLR/Vpp
OSC2/CLKOUT
VBB1
Green LED
OSC1/CLKIN
U4
"ON"
LED1
RB0/INT
RB1
RB2
RB3
RB4
RB5
RB6
RB7
BACT
VBAT
LIN BUS
7
8
9
10
11
12
13
14
19
20
1
0.1 uF C15
NC
NC
VCC
OSCIN
OSCOUT
XCVRen
RC1
RC2
RC3
RD0
RD1
1
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
35
37
39
J12
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
RD7
RD6
RD5
RD4
RX
TX
RC5
RC4
RD3
RD2
RB7
RB6
RB5
RB4
RB3
FAULTn
IGNition
WAKEup
VCC
PIC16C433
1
2
3
4
5
6
7
8
9
10
JP4
VBAT
LIN BUS
BACT
RA0
RA1
RA2
VBB
LINbus
WAKEup
IGNition
FAULTn
RB3
RB4
RB5
RB6
RB7
R24
R22
4.7K
18
VBB
1 LINbus
17 WAKEup
13
GP5/OSC1/CLKIN
GP0/AN0 12
GP1/AN1/VREF 11
GP4/OSC2/AN3/CLKOUT
GP3/MCLR/VPP
GP2/T0CKI/AN2/INT
U5
18
2
3
4
RESETn
RA0
RA1
RA2
RA3
RA4
RA5
RE0
RE1
RE2
9
10
RA0
RA1
RA2
RA3
RESETn
OSCOUT 16
OSCIN
22 uF
+ C3
R27
VCC
VCC
VCC
1
2
3
4
5
6
7
8
9
10
30 pF
OSCIN
9
1
20
19
18
17
16
15
14
13
12
11
RA0
RA1
RA2
RA3
RA4
RA5
10
9
8
7
6
5
4
3
2
1
470
I
H
G
F
E
D
C
B
A
COM
RP2
RC0/T1OSO/T1CKI
RC1/T1OSI/CCP2
RC2/CCP1
RC3/SCK/SCL
RC4/SDI/SDA
RC5/SDO
RC6/TX/CK
RC7/RX/DT
RB0/INT
RB1
RB2
RB3/PGM
RB4
RB5
RB6/PGC
RB7/PGD
PICmicro® MCU 28-pin
RA0/AN0
RA1/AN1
RA2/AN2/VREFRA3/AN3/VREF+
RA4/T0CLI
RA5/AN4/SS
OSC2/CLKOUT
OSC1/CLKIN
MCLR/VPP/THV
U7
RESETn
R28 470
2
3
4
5
6
7
OSCOUT10
R18
10K
HDSP-48XX
DP2
Y1
C9 4 MHz
30 pF C12
470
11
12
13
14
15
16
17
18
21
22
23
24
25
26
27
28
R16
100K
FAULTn
TX
XCVRen
RB3
RX
VCC
RC1
RC2
RC3
RC4
RC5
RB4
RB5
RB6
RB7
WAKEup
E1
IGNition
2
1
 2004 Microchip Technology Inc.
2
4
1
8
U1
VCC
15K
25K
D5
MCP201
CS/WAKE
TX
LIN
RX
FAULT/SLPS
GND
J9
VCC
D9
1N5819
3
S2 Reset
2
VD D
7
VBB
1
VBAT
5
6
C1
IGNition
J2
R8
1K
1N4750 27V
D4
VBB1
LIN
LIN bus J4
LIN bus
LIN bus
1
2
3
JP2
.01 uF
C2
RJ11-6pinCON
RB3
RB6
RB7
GND
VCC
MCLR
JP3
.005 uF
6
5
4
3
2
1
VCC
R15
R6
1N5819
FIGURE A-3:
HCVbat
HCVbat
Hardware Detail
GENERAL PROTOTYPING BOARD
DS41185C-page 21
VCC
1
2
3
4
JP10
LIN Bus
J17
VCC
C30
R45
Y4
4.0 MHz
R42
10K
VCC
VBB
IN
PIC16C432
RA0/AN0
RA2/AN2
RA3/AN3
RA3/AN4/T0CKI
MCLR/VPP
OSC2/CLKOUT
OSC1/CLKIN
15K
R56
1N5819
D38
1N5819
15K
D39
1N5819
25K
1N5819
18
2
3
4
5
16
17
U11
VCC
REG4
LM2937IMP-5.0
Reset
S5
25K
D32
D33
+
1
R66
VCC
R57
R67
VCC
1N4750 27V
D28
30K
R31
30 pF
10K
C25 30 pF
Chassis Ground
BLK
J16
1N4004
+12VDC Vbat
C20
10 uF
1N4004
D27
J18
ORN
GND
2
R51
OUT
0
R29
3
R43
R38
7
8
9
10
11
12
13
14
19
20
1
D30
C23
22 uF
RB0/INT
RB1
RB2
RB3
RB4
RB5
RB6
RB7
BACT
VBAT
LIN BUS
1K
+
VBAT0.2A
S4
10K
S3
10K
VCC
VCC
VBB
VCC
R36
tbd
TP1
Green LED
.01 uF
C31
VCC
VBB
R34
tbd
5
10
8
9
4
A3952SW
REF
MODE
PHASE
ENABLE
BRAKE
U9
VCC
7
VCC
DS41185C-page 22
GND
R39
6
30K
Friday, February 14, 2003
5W
0.5
1
1N5819
D47
RED
BLU
Motor Term B
J24
Motor Term A
J25
470
470
R55
470
R54
470
R53
R52
Drawn by
Chuck Simmers
5
Size
A
Sheet
4 of
Rev
03-01596
SlaveNode3
9.0
PICDEM™ LIN, PIC® Microcontroller and H-Bridge
1N5819
D48
1N5819
1N5819
LED
LED
LED12
LED
LED11
LED
LED10
LED9
D53
2
2
2
2
D52
E28
E27
E26
E25
Microchip Technology Inc.
AMAD
2355 W. Chandler Blvd.
Chandler, Arizona 85224
C27
.001 uF
R49
HCOut2b
2
12
HCOut1b
11
3
APG
RC
SENSE
OUTB
OUTA
VBB
C22 47 uF
.01 uF
C24
HCVbat
1
1
1
FIGURE A-4:
1
D26
PICDEM™ LIN User’s Guide
H-BRIDGE DRIVER BOARD 1
 2004 Microchip Technology Inc.
JP1
1
2
3
4
5
6
7
8
4.0 MHz
30 pF
C11
VCC
R12
1N5819
D12
25K
15K
R1
R10
1N5819
D10
1N5819
1N5819
D11
1N5819
15K
25K
25K
D14
1N5819
D13
1N5819
15K
25K
OUT
PIC16C432
RA0/AN0
RA2/AN2
RA3/AN3
RA3/AN4/T0CKI
MCLR/VPP
OSC2/CLKOUT
OSC1/CLKIN
1N5819
18
2
3
4
5
16
15K
VCC
IN
REG2
LM2937IMP-5.0
RESET
S1
1
17
R11
D15
D16
D17
+
R2
VCC
VCC
R3
VCC
R13
R4
VCC
1N4750 27V
D3
30K
R9
Y2
R17
10K
10K
VCC
C4
10 uF
1N4004
1N4004
D7
C8 30 pF
VCC R19
VBB
LIN Bus
J7
1
2
3
Note: For Analog
inputs replace
series resistors
with 240 ohms
and grounded
resistors with
.005uF caps
J1
Chassis Ground
BLK
J6
+12VDC Vbat
ORN
J8
GND
2
3
7
8
9
10
11
12
13
14
19
20
1
C7
22 uF
RB0/INT
RB1
RB2
RB3
RB4
RB5
RB6
RB7
BACT
VBAT
LIN BUS
U3
+
0
R7
RB4
RB5
RB6
RB7
VCC
R21
tbd
VBB
RB0
RB1
RB2
RB3
RB4
RB5
RB6
RB7
.01 uF
C10
5
10
8
9
4
R20
tbd
VCC
A3952SW
REF
MODE
PHASE
ENABLE
BRAKE
U6
VCC
VBB
RB[0..7]
HCVbat
VBAT0.2A
7
VC C
 2004 Microchip Technology Inc.
GND
6
12
R25
30K
Friday, February 14, 2003
5W
0.5
20
19
18
17
16
15
14
13
12
11
1N5819
D18
470
I
H
G
F
E
D
C
B
A
COM
RP1
RED
BLU
Motor Term B
J11
Motor Term A
J10
10
9
8
7
6
5
4
3
2
1
Size
A
03-01596
SlaveNode4
Sheet
5 of
5
9.0
Drawn by
Chuck Simmers
Rev
PICDEM™ LIN, PIC® Microcontroller and H-Bridge
1N5819
D20
D21
1N5819
D19
1N5819
HDSP-48XX
DP1
Microchip Technology Inc.
AMAD
2355 W. Chandler Blvd.
Chandler, Arizona 85224
C16
.001 uF
R26
HCOut2a
2
E2
2 E3
E4
2 E5
E6
2 E7
E8
2 E9
HCOut1a
C13 47 uF
.01 uF
C14
2
1
2
1
2
1
2
1
1
2
3
4
5
6
7
8
9
10
11
3
1
1
1
1
APG
RC
SENSE
OUTB
OUTA
VBB
RB0
RB1
RB2
RB3
RB4
RB5
RB6
RB7
VCC
R23 470
FIGURE A-5:
1
D6
Hardware Detail
H-BRIDGE DRIVER BOARD 2
DS41185C-page 23
Friday, February 14, 2003
APG
'D1'
'D3'
S9
'D6'
'D7'
4
Drawn by
Chuck Simmers
Size
A
9.0
Rev
03-01595
Console
Sheet
1 of
PICDEM™ LIN Instrument Control Console
Microchip Technology Inc.
AMAD
2355 W. Chandler Blvd.
Chandler, Arizona 85224
DOWN
S8
Window Control
UP
LOCK
Door Lock Control
'D5'
S7
UNLOCK
'D4'
S6
'D2'
S3
S5
S1
Mirror Control Quadrant
VCC
VCC
30K
VCC
RP1
Diagnostic
Trouble
Codes
Jumper
E3
VCC
Park/Neutral 'E2'
S10
'E1'
Ignition
S11
2
1
'D0'
COM
A
B
C
D
E
F
G
H
I
DS41185C-page 24
1
2
3
4
5
6
7
8
9
10
10K
R9
C6
VCC
100 uF
10K
R14
R11
D8
D9
2
4
6
8
10
12
14
16
J7
1
3
5
7
9
11
13
15
VCC
'E0'
VCC
Yellow LED
'E3'
1N4148
BUZZER
BZ1
Chime
470
Malfunction Indicator Lamp
VCC
PortD0
PortD1
PortD2
PortD3
PortD4
PortD5
PortD6
PortD7
PortE0
PortE1
PortE2
PortE3
GND
FIGURE A-6:
+
S4
PICDEM™ LIN User’s Guide
INSTRUMENT CONTROL CONSOLE
 2004 Microchip Technology Inc.
VCC
PortD0
PortD1
PortD2
PortD3
PortD4
PortD5
PortD6
PortD7
PortE0
PortE1
PortE2
PortE3
GND
VCC
1
3
5
7
9
11
13
15
J6
VCC
2
4
6
8
10
12
14
16
CHASSIS
ChassisGND
J2
VBAT
VBAT-12V
J4
RD0
RD1
RD2
RD3
RD4
RD5
RD6
RD7
RE0
RE1
RE2
RC1
J5
RESETn
1
RA0
3
RA1
5
RA2
7
RA3
9
RA4
11
RA5
13
RE0
15
RE1
17
RE2
19
VCC
21
23
OSCIN
OSCOUT 25
27
XCVRen
29
RC1
31
SPICSn
33
SPICLK
35
RD0
37
RD1
39
1N4755 43V
+
1N4004
1N4004
C3
D1 10 uF
D3
D6
1
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
30 pF
C5
RD7
RD6
RD5
RD4
RX
TX
SPISO
SPISI
RD3
RD2
RB7
RB6
RB5
RB4
RB3
FAULTn
IGNition
WAKEup
VCC
0.1 uF
30 pF C4
C2
9
20.0 MHz
1
OSCIN
2
3
4
5
6
7
8
9
10
RA0
RA1
RA2
RA3
RA4
RA5
RE0
RE1
RE2
OSCOUT 14
OSCIN
1
13
RA0
RA1
RA2
RA3
RA4
RA5
RD0/PSP0
RD1/PSP1
RD2/PSP2
RD3/PSP3
RD4/PSP4
RD5/PSP5
RD6/PSP6
RD7/PSP7
RC0/T1OSO/T1CKI
RC1/T1OSI/CCP2
RC2/CCP1
RC3/SCK/SCL
RC4/SDI/SDA
RC5/SDO
RC6/TX/CK
RC7/RX/DT
PIC16F877
RE0/RD/AN5
RE1/WR/AN6
RE2/CS/AN7
RA0/AN0
RA1/AN1
RA2/AN2/VREFRA3/AN3/VREF+
RA4/T0CLI
RA5/AN4/SS
OSC2/CLKOUT
OSC1/CLKIN
RB0/INT
RB1
RB2
RB3/PGM
RB4
RB5
RB6/PGC
RB7/PGD
R4
4.7K
RB0/INT
RB1
RB2
RB3/PGM
RB4
RB5
RB6/PGC
RB7/PGD
PIC16F873
RA0/AN0
RA1/AN1
RA2/AN2/VREFRA3/AN3/VREF+
RA4/T0CLI
RA5/AN4/SS
OSC2/CLKOUT
VCC
19
20
21
22
27
28
29
30
RD0
RD1
RD2
RD3
RD4
RD5
RD6
RD7
XCVRen
RC1
SPICSn
SPICLK
SPISI
SPISO
TX
RX
15
16
17
18
23
24
25
26
TX
RX
RC1
WAKEup
IGNition
FAULTn
RB3
RB4
RB5
RB6
RB7
11
12
13
14
15
16
17
18
21
22
23
24
25
26
27
28
33
34
35
36
37
38
39
40
RC0/T1OSO/T1CKI
RC1/T1OSI/CCP2
RC2/CCP1
RC3/SCK/SCL
RC4/SDI/SDA
RC5/SDO
RC6/TX/CK
RC7/RX/DT
1N4148
"ON"
OSC1/CLKIN
MCLR/VPP/THV
U2
2
3
4
5
6
7
D5
VCC
VBB
LED1 Green LED
MCLR/VPP/THV
U3
RESETn
1K
R3
R8
10K
+
0
R10
OSCOUT10
VCC
470
22 uF
3
RESETn
Y1
R5
S2
C1
R1
4.7K
VCC
OUT
Reset
IN
REG1
LM2937ET - 5.0
GND
2
1
3
5
7
+
+
+
+
2
4
6
8
E9
2
SPICSn
SPICLK
SPISI
SPISO
XCVRen
FAULTn
WAKEup
CON8AP
+
+
+
+
TP5
TP6
1
1
VCC
1
18
17
16
15
14
13
12
11
10
8
13
12
17
14
IGNition
2
E8
1
MCP2510
3
VCC
3
R12
100K
1
1
1
1
E5
R7 10K
TP1
TP2
TP3
TP4
4 MHz
C10
Y2
C7
1 3
E4
U1 MAX 233ACPP
T1IN
T1OUT 5
T2OUT 18
T2IN
R1IN 4
R1OUT
19 1
R2OUT
R2IN
11
C2+
C1+
C1C2+ 15
C2- 10
VC2- 16
VV+
VCC
TXCAN 1
RESET
RXCAN 2
CS
CLKOUT 3
4
SO
TX0RTS
SI
TX1RTS 5
SCK
TX2RTS 6
INT
OSC2 7
RX0BF
OSC1 8
RX1BF
GND 9
U5
RB6
RB7
RB3
E1
2
1
3
20
2
RB4
RB5
1
3
30 pF
30 pF
VCC
2
E7
2
E2
1
REF
RS
U6
MCP2551
2
R6
R2
5
8
Friday, December 20, 2002
MCP201
6
5
4
3
2
1
IGNition
J1
1
6
2
7
3
8
4
9
5
R13
1K
P3
1
6
2
7
3
8
4
9
5
bus
J3
CAN
PC
Host
Size
B
03-01595
MASTERnode
Sheet
2 of
4
8.0
Drawn by
Chuck Simmers
Rev
PICDEM™ LIN Master Node and CAN Interface
Microchip Technology Inc.
AMAD
2355 W. Chandler Blvd.
Chandler, Arizona 85224
LIN BUS LIN
LIN
.01 uF
JP2
1
2
3
P4
ICD
C8
1N4750 27V
D7
VBB
RB3
RB6
RB7
GND
VCC
MCLR
JP1 RJ11-6 pin CON
.005 uF C9
5
6
VCC
1N5819
6
CANL
CANH 7
VCC
15K
25K
D2
CS/WAKE
TX
LIN
RX
FAULT/SLPS
GND
APG
2
4
1
8
E6
GND
VCC
RXD
TXD
U4
VCC
2
3
4
1
1
D4
1N5819
2
3
VDD
7
 2004 Microchip Technology Inc.
VBAT
FIGURE A-7:
HCVBAT
HCVBAT
Hardware Detail
MASTER NODE AND CAN INTERFACE
DS41185C-page 25
Friday, February 14, 2003
15K
R37
D15
1N5819
IN
30 pF
Y3
C12 4 MHz
30 pF C11
R20
1K
1K
4
Drawn by
Chuck Simmers
Size
A
9.0
Rev
2
3
4
5
6
7
03-01595
RKEnode
Sheet
3 of
9
1
10
R15
500
R39
PICDEM™ LIN Remote Keyless Entry Decoder
R28
10K
VCC
C18 +
22 uF
3
1N5819
VCC
OUT
Microchip Technology Inc.
AMAD
2355 W. Chandler Blvd.
Chandler, Arizona 85224
25K
D11
+
1
R41
VCC
APG
IGNition
J20
C21
D21 10 uF
1N4755 43V
ChassisGND
CHASSIS
J19
D13
1N4004
1N4004
REG2
LM2937ET-5.0
GND
2
D20
PIC16F873 VCC
RA0/AN0
RA1/AN1
RA2/AN2/VREFRA3/AN3/VREF+
RA4/T0CLI
RA5/AN4/SS
OSC2/CLKOUT
OSC1/CLKIN
MCLR/VPP/THV
U9
Ignition
"ON"
RR3-xxx
ANT1
ANTENNA
RB0/INT
RB1
RB2
RB3/PGM
RB4
RB5
RB6/PGC
RB7/PGD
1
3
5
7
9
11
13
15
JP4
2
4
6
8
10
12
14
16
VCC
RC0/T1OSO/T1CKI
RC1/T1OSI/CCP2
RC2/CCP1
RC3/SCK/SCL
RC4/SDI/SDA
RC5/SDO
RC6/TX/CK
RC7/RX/DT
VCC
LED2 Green LED
VCC
VBB
TX
RX
HCVbat
11
12
13
14
15
16
17
18
21
22
23
24
25
26
27
28
VCC
XCVRen
LEARN
LED
WAKEup
2
U7
R21
10K
2
4
1
8
U11
E12
C22
.01 uF
VCC
Yellow LED
LED3
"LEARN"
S12
R24 1K
MCP201
CS/WAKE
6
TX
LIN .005 uF
RX
C23
FAULT/SLPS
5
GND
VCC
VCC
E10
R32
100K
1
HCVbat
VCC
GND
ANT
NC
NC
NC
GND
NC
NC
VCC
GND
VCC
TEST
OUT
VCC
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Vbat-12V
J17
3
VDD
DS41185C-page 26
7
D16
JP3
ICD
LIN
1
2
3
LIN BUS
J18
LIN
JP6
RJ11-6pinCON
RB3
RB6
RB7
GND
VCC
MCLR
1N4750 27V
R38
30K
VBB
6
5
4
3
2
1
FIGURE A-8:
VBAT
VBAT
PICDEM™ LIN User’s Guide
REMOTE KEYLESS ENTRY DECODER
 2004 Microchip Technology Inc.
VCC
270
270
R25
R29
VCC
1
C15
R34
R35
4.7K
IN
Set
Mem#2
VCC
CW0
CCW0
CW1
CCW1
CW2
CCW2
CW3
CCW3
22 uF
C17
30 pF
1
2
3
4
5
6
7
8
9
10
100K
R26
100K
VCC
VCC
VCC
U14F
13
U14D
9
VCC
VCC
8
7407
RB0/INT
RB1
RB2
RB3/PGM
RB4
RB5
RB6/PGC
RB7/PGD
7407
7407
10
2
100K
R22
RC0/T1OSO/T1CKI
RC1/T1OSI/CCP2
RC2/CCP1
RC3/SCK/SCL
RC4/SDI/SDA
RC5/SDO
RC6/TX/CK
RC7/RX/DT
12
U14E
11
7407
U14A
1
PIC16F873
RA0/AN0
RA1/AN1
RA2/AN2/VREFRA3/AN3/VREF+
RA4/T0CLI
RA5/AN4/SS
VCC
2
3
4
5
6
7
OSC2/CLKOUT
OSC1/CLKIN
MCLR/VPP/THV
U10
"ON"
LED4 Green LED
VCC
VBB
VCC
11
12
13
14
15
16
17
18
21
22
23
24
25
26
27
28
VCC
R40
100K
CW0
CCW0
CW1
CCW1
CW2
CCW2
CW3
CCW3
VCC
2
4
1
8
2
6
10
2
6
Yellow LED
"MEM"
LED5
3
7407
13
7407
9
7407
3
7407
MCP201
CS/WAKE
TX
LIN
RX
FAULT/SLPS
GND
E13
U14C
5
U13E
11
U12
1
1K
R30
E11
CW0
CCW0
CW1
CCW1
CW2
CCW2
CW3
CCW3
U13A
1
U13C
5
HCVbat
5
6
C26
.005 uF
U14B
7407
4
U13F
7407
12
U13D
7407
8
U13B
7407
4
D19
.01 uF
C27
1N4750 27V
R42
30K
VBB
1
2
3
4
5
6
7
8
3
1
2
3
JP7
R17
tbd
J14
LIN BUS
3
R19
tbd
1
R27
tbd
VCC
U8
UDN2585A
18
OUT0 17
OUT1 16
OUT2 15
OUT3 14
OUT4 13
OUT5 12
OUT6 11
OUT7
LIN bus
IN0
IN1
IN2
IN3
IN4
IN5
IN6
IN7
1
J13
1N4004
D12
3
J10
APG
Friday, February 14, 2003
R31
tbd
1
2
50K
COM
A
B
C
D
E
F
G
H
I
RN1
9
OSCIN
OSCOUT10
1
1K
R33
500
R45
Ignition
1N5819
R36
10K
R23
VCC
C24 +
D14
1N5819
Y4
C20 4 MHz
.005 uF .005 uF
C16
470
OUT
3
15K
25K
30 pF C19
REG3
LM2937IMP-5.0
.005 uF .005 uF
Mem#1
CW0
CCW0
CW1
CCW1
CW2
CCW2
CW3
CCW3
S13
S14
270
R18
C14
270
R16
0.1 uF C13
S15
1
2
3
4
5
6
7
8
9
10
+
S16 Reset
1N4755 43V
C25
D18 10 uF
1N4004
1
2
3
4
5
6
J8
D22
1N4004
D17
J12
CHASSIS
VBAT
GND
2
CON4
D10
R43
R44
2
1
1
2
3
4
7
2
VCC
3
VDD
2
VBAT
9
Vs
 2004 Microchip Technology Inc.
GND
VCC
VCC
VCC
JP5
1
3
5
1
3
5
1
3
5
1
3
5
+
+
+
J16
+
+
+
J15
+
+
+
J11
+
+
+
J9
+
+
+
+
+
+
+
+
+
+
+
+
Size
B
03-01595
SeatMemnode
Sheet
4 of
4
Rev
9.0
VCC
Drawn by
Chuck Simmers
2
4
6
2
4
6
2
4
6
2
4
6
RJ11-6pin CON
RB3
RB6
RB7
GND
VCC
MCLR
PICDEM™ LIN Power Seat Memory Controller
Microchip Technology Inc.
AMAD
2355 W. Chandler Blvd.
Chandler, Arizona 85224
K8
K7
K6
K5
K4
K3
K2
K1
VCC
6
5
4
3
2
1
FIGURE A-9:
10
J25
Hardware Detail
POWER SEAT MEMORY CONTROLLER
DS41185C-page 27
PICDEM™ LIN User’s Guide
NOTES:
DS41185C-page 28
 2004 Microchip Technology Inc.
PICDEM™ LIN USER’S GUIDE
Appendix B. LIN bus Master
B.1
LIN bus MASTER CODE
Software License Agreement
The software supplied herewith by Microchip Technology Incorporated (the “Company”) is intended and supplied to you, the
Company’s customer, for use solely and exclusively with products manufactured by the Company.
The software is owned by the Company and/or its supplier, and is protected under applicable copyright laws. All rights are reserved.
Any use in violation of the foregoing restrictions may subject the user to criminal sanctions under applicable laws, as well as to civil
liability for the breach of the terms and conditions of this license.
THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR
SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
**************************************************************************************
; * Title
: LIN Bus Master
*
; * Author
: Thomas Schmidt
*
; * Date
: 06.06.2000
*
; * Revision
: 0.1
*
; * Last Modified
: 12.06.2000
*
; * Description
: This program implements the LIN Bus master based on USART
*
;
**************************************************************************************
LIST p=16f874, r=hex
#include <p16f874.inc>
errorlevel
-302
; suppress message 302 from list file
;************************************************************************
;* Fuse configuration
*
;************************************************************************
__CONFIG
_CP_OFF&_WDT_OFF&_HS_OSC&_BODEN_OFF&_PWRTE_ON&_CPD_OFF&_DEBUG_OFF&_LVP_OFF&_WRT_ENABLE
_OFF
#define
#define
ABT_ERROR
TRANSMIT_OK
0x01
0x02
; Definitions for Commands for Window.
; into the first data byte
#define RearViewLeft
0x14
;
#define RearViewRight
0x24
;
#define RearViewDown
0x34
;
#define RearViewUp
0x04
;
#define RearViewOff
0x00
;
; Abritation error code
; Transmission was successful
This definitions will be loaded
Command
Command
Command
Command
Command
for
for
for
for
for
Rear
Rear
Rear
Rear
Rear
view
view
view
view
view
mirror
mirror
mirror
mirror
mirror
left
right
down
up
off
; Definition of function IDs. These values are the ID values
#define LockDoors
0x10
; ID Value for LockDoors
#define UnlockDoors
0x11
; ID Value for Unlock Doors
 2004 Microchip Technology Inc.
DS41185C-page 29
PICDEM™ LIN User’s Guide
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
0x12
0x13
0x18
0x19
0x1a
0x1b
0x1c
0x1d
0x1e
0x1f
;
;
;
;
;
;
;
;
;
;
#define LeftMirror
0x13
; This ID selects the left mirror
; Other
#define
#define
#define
#define
#define
#define
0
1
0x30
1
0
0x00
cblock
WindowsUp
WindowsDown
TiltSeatBack
TiltSeatForward
SlideSeatForward
SlideSeatBackward
FrontEdgeSeatUp
FrontEdgeSeatDown
BackEdgeSeatUp
BackEdgeSeatDown
Definitions
RXMODE
TXMODE
DATAPOINTER
CRC_ERROR
CRC_OK
NoAction
0x20
LINCOUNTER
ID_TEMP
TEMP_COUNTER
MESSAGE_COUNTER
TEMP
TEMP1
TEMP2
ID
ID
ID
ID
ID
ID
ID
ID
ID
ID
Value
Value
Value
Value
Value
Value
Value
Value
Value
Value
for
for
for
for
for
for
for
for
for
for
Windows up
Windows down
Tilt seat back
Tilt seat forward
Slide seat forward
Slide seat backward
Front Edge seat up
Front Edge Seat down
Back edge seat up
Back edge seat down
;
;
;
;
;
;
;
Receive mode
Transmit mode
Address of data bytes
CRC_ERROR occured
No CRC_ERROR occured
No action required, because no key was
pressed
;
;
;
;
;
;
;
counter for LIN Bus bytes
temporary register for ID byte
temporary counter register
number of bytes to receive or transmit
temporary register
temporary register
temporary register
endc
RESETVECTOR
ORG 0x00
goto
StartUp
; goto StartUp routine
INTVECTOR
ORG 0x04
goto
StartUp
; no interrupts, therefore goto StartUp routine
DS41185C-page 30
 2004 Microchip Technology Inc.
LIN bus Master
;
;
;
;
;
;
;
;
;
;
;
StartUp
******************************************************************************
* ROUTINE
: StartUp
*
* AUTHOR
: Thomas Schmidt
*
* DATE LAST MODIFIED : 08/14/2001
*
* REVISION
: 1.0
*
* CHANGES
: none
*
* INPUT PARAMETER
: none
*
* OUTPUT PARAMETER
: none
*
* DESCRIPTION
: This routine is called after power-up. PORTS and
*
*
peripherals are initialized.
*
******************************************************************************
call
StartUpInit
; Initialize PORTS and peripherals
call
l_sys_init
; initialize USART for LIN Bus communication
call
StartUpRAMInit
; Initialize RAM after power-up
; toggle CS line for transceiver
bsf
PORTC, 0
; set RC0 to 1
bsf
STATUS, RP0
; select bank1
bcf
TRISC, 0
; make RC0 an output
bcf
STATUS, RP0
; select bank0
bcf
PORTC, 0
; enable LIN Bus transceiver
bsf
PORTC, 0
;
; ******************************************************************************
; * ROUTINE
: Main Routine
*
; * AUTHOR
: Thomas Schmidt
*
; * DATE LAST MODIFIED : 08/14/2001
*
; * REVISION
: 1.0
*
; * CHANGES
: none
*
; * INPUT PARAMETER
: none
*
; * OUTPUT PARAMETER
: none
*
; * DESCRIPTION
: This routine calls routines to Scan PORTD. After the *
; *
port is scan the LIN Bus data frame is transmitted.
*
; *
*
; *******************************************************************************
MainRoutine call
ReadKeysPortD
; read keys from PORTD
movwf TEMP
; copy result from Scan into TEMP
movwf ID_TEMP
; Load ID_TEMP with code from Key
; check if no key was pressed
xorlw 0x00
; check if no action is required,
; which means no key was pressed
btfsc STATUS, Z
; Check zero Flag
goto
MainRoutine
; no key was pressed
; move command into first data byte
movlw DATAPOINTER
movwf FSR
; point to frist data byte
; with FSR register
call
call
; generated parity bits
; generated checksum
l_id_gen
l_checksum
; Reset Data pointer to Start Postion
movlw DATAPOINTER
; point data bytes
movwf FSR
; Transmit LIN Bus Data
call
l_u8wr_sss
EndMainRoutine
goto
 2004 Microchip Technology Inc.
MainRoutine
; transmit data
; do forever
DS41185C-page 31
PICDEM™ LIN User’s Guide
; ******************************************************************************
; * ROUTINE
: LIN Bus Transmission/Reception Routine
*
; * AUTHOR
: Thomas Schmidt
*
; * DATE LAST MODIFIED : 08/14/2001
*
; * REVISION
: 1.0
*
; * CHANGES
: none
*
; * INPUT PARAMETER
: none
*
; * OUTPUT PARAMETER
: none
*
; * DESCRIPTION
: This routine sends out a LIN Bus data frame and/or
*
; *
receives or transmits data. The reception or trans.
*
; *
of data is defined in the ID itself. After the ID-Byte*
; *
is sent it is decoded in a look up table if data
*
; *
should be transmitted or received. Depending on
*
; *
the result either a transmit or receive mode is
*
; *
activated.
*
; *
ID_TEMP = holds the ID to send (parity is already
*
; *
generated).
*
; *
DATAPOINTER = points to first data byte. Last byte
*
; *
is CRC byte for transmission otherwise
*
; *
last byte is data byte
*
; *
*
; ******************************************************************************
l_u8_wr_sss swapf
ID_TEMP, w
; get ID4 and ID5 into lower bits
andlw
b'00000011'
; mask two bits
call
table_tx
; get data length code
addlw
0x01
; add one for CRC checksum
movwf
MESSAGE_COUNTER
; copy data length into TEMP register
; change baud rate for
bcf
RCSTA, SPEN ;
bsf
STATUS, RP0 ;
;movlw 0x13
;
;
;movlw 0x28
;
;
;
test_synchb
transmission
turn USART off
select Page 1
Initialize USART for 12.5KBaud communication @4MHz
Nominal Baudrate = 19.2KBaud @ 4MHz
Initialize USART for 6KBaud
communcation @ 4MHz
Nominal Baudrate = 9.6KBaud @ 4MHz
bcf
movlw
TXSTA, BRGH
0x81
; select slow baud rate for 20MHz communciation
; Initialize USART for 2.4Kbaud @ 20MHz (BRGH=0)
movwf
bcf
bsf
SPBRG
STATUS, RP0
RCSTA, SPEN
; initialize SPBRG register
; select Page 0
; turn USART on
; transmit synchronization break
movlw
0x00
; Send Synchbreak Signal
movwf
TXREG
; Send 0x00
btfss
PIR1, RCIF
; check if data was send
goto
test_synchb ; data not fully received
movf
xorlw
RCREG, w
0x00
btfss
retlw
STATUS, Z
ABT_ERROR
;
;
;
;
;
;
copy data receive into w-register
check if data transmited is the same like
data received.
check zero flag
result is not the same, therefore an abritration
error occured
; transmit synchronization field
bcf
RCSTA, SPEN ; turn USART off
DS41185C-page 32
 2004 Microchip Technology Inc.
LIN bus Master
bsf
;movlw
;movlw
STATUS, RP0
0x19
0x0c
; select Page 1 in order to change baudrate to 19.2Kbaud
; change baudrate to 9.6KBaud @ 4MHz
; change baudrate to 19.2Kbaud @ 4MHz
bcf
movlw
TXSTA, BRGH
0x1f
; select slow rate for 20MHz communication
; change baudrate to 9.6KBaud @ 20MHz (BRGH=0)
movwf
bcf
SPBRG
STATUS, RP0
; change baudrate
; select Page 0
bsf
RCSTA,
movlw
0x55
movwf
TXREG
test_synchfield btfss
goto
movf
xorlw
SPEN
; turn USART on
; synch. field
; send synch field
PIR1, RCIF
; check if data was send
test_synchfield
; data not fully received
RCREG, w
; copy data receive into w-register
0x55
; check if data transmited is the same like
; data received.
btfss STATUS, Z
; check zero flag
retlw ABT_ERROR
; result is not the same, therefore an
; abritration error occured
; send identifier
movf ID_TEMP, w
movwf TXREG
test_ID btfss PIR1, RCIF
goto
test_ID
movf
RCREG, w
xorwf RCREG, w
btfss
retlw
STATUS, Z
ABT_ERROR
byte
;
;
;
;
;
;
;
;
; copy ID_TEMP into
; transmit register
check if data was send
data not fully received
copy data receive into w-register
check if data transmited is the same like
data received. (w-reg = transmited data)
check zero flag
result is not the same, therefore an abritration
error occured
; check if data has to be received or transmitted
movf
ID_TEMP, w
; copy ID into W register
andlw 0x0f
; Delete upper four bits of Identifier byte (these
; bit include parity bits and data lenght code).
; Lower bit are the indifier bits.
call
bcf
addwf
goto
goto
DecodeIDTable
PCLATH, 1
PCL, f
ReceiveMode
TransmitMode
;
;
;
;
;
Decode Identifier bits
Reset PCLATH register after Look-up table call
add mode to low byte of PC
receive data
transmit data
; receive data
ReceiveMode movlw
DATAPOINTER
movwf
FSR
TestRXData
; Point to data
; point to location where received information
; is going to be stored
btfss
PIR1, RCIF
; check if data was received
goto
TestRXData
; no data was not received yes
movf
RCREG, w
; copy received data into w-register
movwf
INDF
; copy into RAM
incf
FSR, f
; point to next location
decfsz MESSAGE_COUNTER, f
; decrement number of bytes to receive
goto
TestRXData
; read next data
; check CRC value
call
l_checksum
 2004 Microchip Technology Inc.
; check CRC value
DS41185C-page 33
PICDEM™ LIN User’s Guide
return
; return to main
; transmit data bytes
TransmitMode movf
INDF, w
movwf
TXREG
test_databyte btfss
goto
movf
xorwf
; copy data byte transmit register
; and transmit data
; check if data was send
; data not fully received
; copy data receive into w-register
; check if data transmited is the same like
; data received. (w-reg = transmitted data)
btfss STATUS, Z
; check zero flag
retlw ABT_ERROR
; result is not the same, therefore an
; abritration error occured
incf
FSR, f
; point to next data byte
decfsz MESSAGE_COUNTER, f ;decrement number of transmitted data bytes by
; one
goto
TransmitMode
; get ready for the next data byte
retlw TRANSMIT_OK
; the transmission was successful
table_tx
addwf
retlw
retlw
retlw
retlw
PIR1, RCIF
test_databyte
RCREG, w
RCREG, w
PCL, f
0x02
0x02
0x04
0x08
;
;
;
;
;
add value to low byte of program counter
data length = 2 bytes
data length = 2 bytes
data length = 4 bytes
data length = 8 bytes
; ******************************************************************************
; * ROUTINE
: StartUp initialization
*
; * AUTHOR
: Thomas Schmidt
*
; * DATE LAST MODIFIED
: 08/14/2001
*
; * REVISION
: 1.0
*
; * CHANGES
: none
*
; * INPUT PARAMETER
: none
*
; * OUTPUT PARAMETER
: none
*
; * DESCRIPTION
: This routine initializes PORTs and peripherals after*
; *
power-up.
*
; *
*
; ******************************************************************************
StartUpInit
clrf
PORTA
; reset PORTA
clrf
PORTB
; reset PORTB
clrf
PORTC
; reset PORTC
clrf
PORTD
; reset PORTD
clrf
PORTE
; reset PORTD
bsf
bcf
movlw
movwf
clrf
movlw
movwf
bcf
bcf
DS41185C-page 34
STATUS, RP0
TRISE, 0
0xff
TRISD
TRISA
0xff
ADCON1
STATUS, RP0
PORTE, 0
;
;
;
;
;
;
;
;
;
select page 1
make RE0 an output
make PORTD all inputs
init TRISD register
make all pins on portA outputs
make all pin digital IOs
select page 0
turn LED on
 2004 Microchip Technology Inc.
LIN bus Master
; ******************************************************************************
; * ROUTINE
: RAM Initialization Routine
*
; * AUTHOR
: Thomas Schmidt
*
; * DATE LAST MODIFIED
: 08/14/2001
*
; * REVISION
: 1.0
*
; * CHANGES
: none
*
; * INPUT PARAMETER
: none
*
; * OUTPUT PARAMETER
: none
*
; * DESCRIPTION
: This routine initializes RAM (general purpose RAM) *
; *
*
; ******************************************************************************
StartUpRAMInit movlw
DATAPOINTER
; first address for data
movwf
FSR
; load into FSR register
movlw
0x0d
; initialize nine register
movwf
TEMP
; TEMP is counter register
LoopInit
movlw
0x0d
; initialization value
movwf
INDF
; use indirect addressing
incf
FSR, f
; point to next RAM location
decfsz
TEMP, f
; decrement temp register
goto
LoopInit
; do until done
; ******************************************************************************
; * ROUTINE
: System Initialization Routine for USART
*
; * AUTHOR
: Thomas Schmidt
*
; * DATE LAST MODIFIED
: 08/14/2001
*
; * REVISION
: 1.0
*
; * CHANGES
: none
*
; * INPUT PARAMETER
: none
*
; * OUTPUT PARAMETER
: none
*
; * DESCRIPTION
: This routine initializes the USART for the LIN Bus *
; *
communication. BaudRate = 19.2KBaud
*
; *
*
; ******************************************************************************
l_sys_init
bcf
RCSTA, SPEN
; turn USART off
bsf
PORTC, 6
; set TX line to high
bsf
STATUS, RP0
; select Page 1
movlw
b'00100100'
; mask for TXSTA register
movwf
TXSTA
; initialize TXSTA register
movlw
b'10111111'
; initialize TRISC register
movwf
TRISC
; TRISC register
bcf
STATUS, RP0
; select Page 0
movlw
b'00010000'
; mask for RCSTA register
movwf
RCSTA
; initialize RCSTA register
bsf
RCSTA, SPEN
; turn USART on
retlw
0x00
; initialization sucessful => return 0x00
 2004 Microchip Technology Inc.
DS41185C-page 35
PICDEM™ LIN User’s Guide
;
;
;
;
;
;
;
;
;
;
;
;
;
l_id_gen
******************************************************************************
* ROUTINE: ID Partiy Bit Generation
*
* AUTHOR: Thomas Schmidt
*
* DATE LAST MODIFIED: 08/14/2001
*
* REVISION: 1.0
*
* CHANGES: none
*
* INPUT PARAMETER: none
*
* OUTPUT PARAMETER: Updated ID with Parity bits in ID_TEMP
*
* DESCRIPTION: This routine generated the parity bits P1 and P0
*
*
for the identifier byte according to the LIN Bus
*
*
specification 1.2.
*
*
*
******************************************************************************
movf
ID_TEMP, w
; Temporary ID location
; calculate P0
movwf
TEMP1
movwf
TEMP2
rrf
TEMP1, f
movf
TEMP1, w
xorwf
TEMP2, f
rrf
TEMP1, f
movf
TEMP1, w
xorwf
TEMP2, f
bcf
STATUS, C
rrf
TEMP1, f
rrf
TEMP1, w
xorwf
TEMP2, f
;
;
;
;
;
;
;
;
;
;
;
;
move ID value into TEMP1
move ID into TEMP2
rotate ID_TEMP one to the right (get ID1)
copy TEMP1 to w
TEMP2=ID0 XOR ID1
get ID2
copy ID2 into w-register
TEMP2= TEMP2 XOR ID2
clear carry flag
get ID3 into bit 0
ID4 into bit 0 and store result in w-register
TEMP2 = TEMP2 XOR ID4
;
;
;
;
;
test if bit is zero or one
P0=1
P0=0
; calculate P1
movf
ID_TEMP, w
movwf
TEMP1
movwf
TEMP2
rrf
TEMP2, f
rrf
TEMP1, f
rrf
TEMP1, f
rrf
TEMP1, f
movf
TEMP1, w
xorwf
TEMP2, f
rrf
TEMP1, f
movf
TEMP1, w
xorwf
TEMP2, f
rrf
TEMP1, w
xorwf
TEMP2, f
comf
TEMP2, f
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
copy ID_TEMP into w-register
copy ID_TEMP into TEMP1
and TEMP2
ID1 into bit0
ID1 into bit0
ID2 into bit0
ID3 into bit0
copy TEMP1 into w-register
TEMP2 = ID1 XOR ID3
ID4 into bit0
TEMP1 into w-register
TEMP2 = TEMP2 XOR ID4
ID5 into bit0
TEMP2 = TEMP2 XOR ID5
negate TEMP2
set_p1
btfsc
goto
bcf
goto
bsf
;
;
;
;
;
check if P1=1
P1=1
P1=0
return
set P1
testb
return
btfsc
goto
bcf
goto
bsf
set_p0
cal_p1
DS41185C-page 36
TEMP2, 0
set_p0
ID_TEMP, 6
cal_p1
ID_TEMP, 6
TEMP2, 0
set_p1
ID_TEMP, 7
testb
ID_TEMP, 7
set P0 to 1
; return to main
 2004 Microchip Technology Inc.
LIN bus Master
; ******************************************************************************
; * ROUTINE
: CRC Check and Generation
*
; * AUTHOR
: Thomas Schmidt
*
; * DATE LAST MODIFIED : 08/14/2001
*
; * REVISION
: 1.0
*
; * CHANGES
: none
*
; * INPUT PARAMETER
: none
*
; * OUTPUT PARAMETER
: CRC Check :CRC check is appended after last Data Byte *
; *
CRC Gen
:CRC is check and CRC_OK or CRC_ERROR is
*
; *
into the w-register
*
; * DESCRIPTION
: This routine generates or check CRC based on the
*
; *
Modulo-256 checksum defined in the LIN Bus spec. 1.2 *
; *
*
; ******************************************************************************
l_checksum movlw DATAPOINTER
; point to first data byte location
movwf FSR
; initialize FSR register
swapf ID_TEMP, w
; get ID4 and ID5 into bit0 and bit 1
andlw 0x03
; get rid of all other bits
call
table_tx
; get number of data bytes to be transmitted
next_calc
movwf
TEMP_COUNTER
; copy number of data bytes into Temp-Counter
decf
movf
movwf
incf
movf
addwf
btfsc
incf
decfsz
goto
TEMP_COUNTER, f ; Number of data bytes - 1 = number of loop counts
INDF, w
; copy first data byte into w-register
TEMP
; copy first data byte into temp register
FSR, f
; point to next data memory location
INDF, w
; move data into w-register
TEMP, f
; add data byte to temp and store in temp
STATUS, C
; add with carry?
TEMP, f
; yes, increment TEMP
TEMP_COUNTER, f ; decrement bit counter
next_calc
; calculate next
; check if data has to be received or transmited
movf
ID_TEMP, w
; copy ID into W register
andlw 0x0f
; Delete upper four bits of Identifier byte (these
; bit include parity bits and data lenght code).
; Lower bit are the indifier bits.
call
bcf
DecodeIDTable
PCLATH, 1
; Decode Identifier bits
; Reset PCLATH register
addwf
goto
goto
PCL, f
CRCCheck
CRCAppend
;
;
;
;
; generate CRC value
CRCAppend comf
TEMP, f
movf
TEMP, w
incf
FSR, f
movwf
INDF
return
CRCCheck
incf
movf
addwf
xorlw
btfss
FSR, f
INDF, w
TEMP, w
0xff
STATUS, Z
 2004 Microchip Technology Inc.
add mode to low byte of PC
data was received therefore check CRC
data is going to be transmited, therefore
append CRC
;
;
;
;
;
complement CRC value
copy checksum into w-register
point to location for checksum
copy checksum behind
return to main routine
;
;
;
;
;
point to CRC data
copy received CRC value into w-register
add received CRC to calculated CRC
Result should be 0xFF after XOR result is zero
is result zero?
DS41185C-page 37
PICDEM™ LIN User’s Guide
retlw CRC_ERROR
retlw
CRC_OK
; return with CRC_ERROR in w-register
; return with CRC_OK in w-register
; ******************************************************************************
; * ROUTINE
: PORTD Scan Routine
*
; * AUTHOR
: Thomas Schmidt
*
; * DATE LAST MODIFIED
: 08/14/2001
*
; * REVISION
: 1.0
*
; * CHANGES
: none
*
; * INPUT PARAMETER
: ID_TEMP
*
; * OUTPUT PARAMETER
: LIN Bus ID based on what key is pressed
*
; * DESCRIPTION
: This Routine scans PORTD for what key is pressed. *
; *
After the scan, the routine check what key is
*
; *
pressed and return the ID for the key into the
*
; *
w-register.
*
; *
*
; ******************************************************************************
ReadKeysPortD movf
PORTD, w
; copy content of PORTD into W-register
movwf TEMP
; store in temp register
xorlw 0xff
; see if anything changed
btfsc STATUS, Z
; result is zero therefore no key was pressed
goto
ReadKeysPortD
; keep on reading
call
Delay25ms
; debounce key (delay for 25ms)
; read key again to see if it still pressed
DS41185C-page 38
movf
xorwf
btfsc
TEMP, w
PORTD, w
STATUS, Z
ReadKeysPortD
;
;
;
;
;
copy content of PORTD into W-register
see if anything changed
result is zero therefore key is still
pressed
keep on reading
goto
rrf
btfss
retlw
rrf
btfss
retlw
rrf
btfss
retlw
rrf
btfss
retlw
rrf
btfss
retlw
rrf
btfss
retlw
rrf
btfss
retlw
rrf
btfss
retlw
retlw
TEMP, f
STATUS, C
RearViewUp
TEMP, f
STATUS, C
RearViewRight
TEMP, f
STATUS, C
RearViewDown
TEMP, f
STATUS, C
RearViewLeft
TEMP, f
STATUS, C
UnlockDoors
TEMP, f
STATUS, C
LockDoors
TEMP, f
STATUS, C
WindowsUp
TEMP, f
STATUS, C
WindowsDown
NoAction
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
rotate key register
was MirrorUp key pressed?
yes, return wiht RearViewUp value
check next key
was MirrorRight key pressed?
yes, return with RearViewRight value
check MirrorLeft key
was MirrorLeft key pressed?
yes, return with RearViewLeft value
check MirrorKeyDown
was MirrorKeyDown pressed?
yes, return with RearViewDown value
check UNLOCK key
was UNLOCK key pressed?
yes, return with UnlockDoors value
was LOCK doors key pressed
check if LOCK doors key was pressed
yes, return with LockDoors value
check if WindowUp key was pressed
was WindowUp key pressed?
yes, return with WindowsUp value
check if WindowsDown key was pressed
was WindowsDown key pressed?
yes, return with WindowsDownValue
No Key was pressed
 2004 Microchip Technology Inc.
LIN bus Master
; ******************************************************************************
; * ROUTINE
: Delay Routine for 25ms @ 4MHz
*
; * AUTHOR
: Thomas Schmidt
*
; * DATE LAST MODIFIED : 08/14/2001
*
; * REVISION
: 1.0
*
; * CHANGES
: none
*
; * INPUT PARAMETER
: none
*
; * OUTPUT PARAMETER
: none
*
; * DESCRIPTION
: This routine generates a delay for 25ms @ 4MHz
*
; *
*
; ******************************************************************************
Delay25ms movlw
0xff
; intialize TEMP1
movwf
TEMP1
; with 0xff
movlw
0x14
; initialize TEMP2
movwf
TEMP2
; with 0x14
Loop1
decfsz
TEMP1, f
; decrement TEMP1
goto
ExLoop1
; extent loop 1
decfsz
TEMP2, f
; Decrement TEMP2
goto
ReloadTemp
; Reload TEMP1
return
; return to main routine
ReloadTemp movlw
movwf
goto
0xff
TEMP1
Loop1
; load TEM1 with
; 0xff and go back
;
ExLoop1
Loop1
; add two insturction cycles to it
goto
 2004 Microchip Technology Inc.
DS41185C-page 39
PICDEM™ LIN User’s Guide
;
;
;
;
;
;
;
;
;
;
;
;
;
;
******************************************************************************
* ROUTINE
: Decode ID Table
*
* AUTHOR
: Thomas Schmidt
*
* DATE LAST MODIFIED
: 08/14/2001
*
* REVISION
: 1.0
*
* CHANGES
: Table offset value in w-register
*
* INPUT PARAMETER
: RXMODE (switch to receive mode) or TXMODE (switch
*
*
transmit mode)
*
* OUTPUT PARAMETER
: Updated ID with Parity bits in ID_TEMP
*
* DESCRIPTION
: This routine generated the parity bits P1 and P0
*
*
for the identifier byte according to the LIN Bus
*
*
specification 1.2.
*
*
*
******************************************************************************
org
0x200
DecodeIDTable
bsf
PCLATH, 1
addwf
PCL, f
; add to PC
retlw
TXMODE
; Receive data from bus
retlw
TXMODE
; Receive data from bus
retlw
TXMODE
; Receive Data from bus
retlw
TXMODE
; Receive Data from bus
retlw
TXMODE
; Receive data from bus
retlw
TXMODE
; Receive data from bus
retlw
TXMODE
; Receive Data from bus
retlw
TXMODE
; Receive Data from bus
retlw
TXMODE
; Receive data from bus
retlw
TXMODE
; Receive data from bus
retlw
TXMODE
; Receive Data from bus
retlw
TXMODE
; Receive Data from bus
retlw
TXMODE
; Receive data from bus
retlw
TXMODE
; Receive data from bus
retlw
TXMODE
; Receive Data from bus
retlw
TXMODE
; Receive Data from bus
END
DS41185C-page 40
 2004 Microchip Technology Inc.
PICDEM™ LIN USER’S GUIDE
Appendix C. LIN bus Slave for the PIC16C432
C.1
LIN bus SLAVE FOR THE PIC16C432 CODE
Software License Agreement
The software supplied herewith by Microchip Technology Incorporated (the “Company”) is intended and supplied to you, the
Company’s customer, for use solely and exclusively with products manufactured by the Company.
The software is owned by the Company and/or its supplier, and is protected under applicable copyright laws. All rights are reserved.
Any use in violation of the foregoing restrictions may subject the user to criminal sanctions under applicable laws, as well as to civil
liability for the breach of the terms and conditions of this license.
THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR
SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
;*******************************************************************************
; * Title
: LIN bus Slave for the PIC16C432
; * Author
: Thomas Schmidt
; * Date
: 05.12.2000
; * Revision
: 0.4
; * Last Modified:
; * Description :
; ******************************************************************************
LIST P=16C432, r=hex
; ***********************************************************************
; * Include files
*
; ***********************************************************************
#include <P16C432.INC>
; ***********************************************************************
; * Register definitions
*
; ***********************************************************************
cblock
0x20
AUTOBAUD_LOW
; low byte of bit-time coutner
AUTOBAUD_HIGH
; high byte of bit-time counter
AUTOHALF_LOW
; low byte of half the bit time
AUTOHALF_HIGH
; high byte for half the bit time
TEMP1, TEMP2
; temporary registers
RXTX_REG
; receive register
MESSAGE_COUNTER
; Countes number of bytes to receive or transmit
COUNTER
; receive & Transmit counter register
ID_TEMP
; Register for ID byte
endc
 2004 Microchip Technology Inc.
DS41185C-page 41
PICDEM™ LIN User’s Guide
; ***********************************************************************
; * Other definitions
; ***********************************************************************
#define BITS
0x08
; number of bits to receive
#define RXMODE
0x00
; Receive mode
#define TXMODE
0x01
; Transmit mode
#define LISTENMODE
0x02
; Listen only mode
#define CRC_ERROR
0x01
; CRC_ERROR occured
#define CRC_OK
0x00
; No CRC_ERROR occured
#define PARITY_OK
0x00
; No Parity error occcured
#define PARITY_ERROR_P0 0x01
; Parity error on P0 occured
#define PARITY_ERROR_P1 0x02
; Parity error on P1 occured
#define DATAPOINTER
0x35
; address of first data byte
#define IGNORE
0x00
; ignorê received command
#define INCREMENT
0x01
; increment PORTB
#define DECREMENT
0x02
; decrement PORTB
; ***********************************************************************
; * Fuse configuration
*
; ***********************************************************************
__CONFIG _CP_OFF&_WDT_OFF&_XT_OSC&_PWRTE_ON&_BODEN_OFF
; ***********************************************************************
; * Reset vector
*
; ***********************************************************************
ORG 0x00
goto
MainRoutine
MainRoutine
TestSynchByte
TestEndSynch
;*******************************************************************************
; * ROUTINE
: MainRoutine*
; * AUTHOR
: Thomas Schmidt*
; * DATE LAST MODIFIED
: 08/14/2001*
; * REVISION
: 1.0*
; * CHANGES
: none*
; * INPUT PARAMETER
: none*
; * OUTPUT PARAMETER
: none*
; * DESCRIPTION
: Main routine. The main routine detects
; *
first the transmission time of the incomming calibration character. After that the
; *
routine receives and transmits incomming
; *
characters.
;*******************************************************************************
ORG 0x06
call
StartUp
; Call Startup Routine
btfsc
goto
btfss
goto
PORTA, LINRX
TestSynchByte
PORTA, LINRX
TestEndSynch
;
;
;
;
Check for synch byte
Synch byte not received
check for end of synchbyte
end of synchronization byte not received
; Receive message on LIN bus
call
LinHandler
; call LIN bus handler
DS41185C-page 42
 2004 Microchip Technology Inc.
LIN bus Slave for the PIC16C432
; Decode
movf
andlw
call
clrf
addwf
goto
goto
goto
message and take action upon receive message Identifier
ID_TEMP, w
; copy ID_TEMP into w-register
0x0f
; decode only lower four nibbles
DecodeAction
; see if LED has to be turn on
PCLATH
; reset PCLATH register
PCL, f
; add to PC
TestSynchByte
; Ignore all other IDs
WindowUp
; Window up function
WindowDown
; Window down function
WindowUp
; WindowUp command was receive from Master
incf
PORTB, f
; increment PORTB
goto
TestSynchByte
; Go back
WindowDown
; WindowDown command was receive from Master
decf
PORTB, f
; decrement PORTB
goto
TestSynchByte
; go back
StartUp
InitData
Count
;*******************************************************************************
; * ROUTINE
: StartUp
; * AUTHOR
: Thomas Schmidt
; * DATE LAST MODIFIED
: 08/14/2001
; * REVISION
: 1.0
; * CHANGES
: none
; * INPUT PARAMETER
: none
; * OUTPUT PARAMETER
: none
; * DESCRIPTION
: This routine is called after power-up. PORTS and
; *
peripherals are initialized.
; *
;*******************************************************************************
clrf
PORTA
; set all latches of PORTA to zero
clrf
PORTB
; set all latches of PORTB to zero
movlw
0x07
; Make all pins on PORTA
movwf
CMCON
; digitial I/Os.
bsf
STATUS,RP0
; select page 1
movlw
b'00000010
; make RA1 an input
movwf
PORTA
; make all other pins on PORTA outputs
clrf
PORTB
; make all PORTB pins outputs
bsf
LININTF, LINTX
; set Transmit line high
bsf
LININTF, LINVDD
; turn LIN Vdd on.
bcf
STATUS,RP0
; select page 0
; Initialize Data RAM for LIN bus Communication
movlw
DATAPOINTER
; point to first data location
movwf
FSR
; and initialize FSR register
movlw
0x0f
; Initialize 16 register
movwf
TEMP1
; temporary counter register
movlw
0xaa
; init value is 0xaa
movwf
INDF
; copy into location where FSR points to
incf
FSR, f
; point to next location
decfsz
TEMP1, f
; decrement temporary counter
goto
Count
; counter is not zero, therefore keep on
; initializing
retlw
0x00
; return to Main Routine
 2004 Microchip Technology Inc.
DS41185C-page 43
PICDEM™ LIN User’s Guide
LinHandler
WaitStartBit
TestClear
;*******************************************************************************
; * ROUTINE
: LIN bus Transmission/Reception Routine
; * AUTHOR
: Thomas Schmidt
; * DATE LAST MODIFIED
: 08/14/2001
; * REVISION
: 1.0
; * CHANGES
: none
; * INPUT PARAMETER
: none
; * OUTPUT PARAMETER
: none
; * DESCRIPTION
: This routine sends out a LIN bus data frame and/or
; *
receives or transmits data. The reception or trans.
; *
of data is defined in the ID itself. After the ID
; *
-Byte is sent it is decoded in a look up table if
: *
data should be transmitted or received. Depending
; *
on the result either a transmit or receive mode is
; *
activated.
; *
ID_TEMP = holds the ID to send (parity
; *
is already generated).
; *
DATAPOINTER = points to first data byte. Last byte
; *
is CRC byte for transmission otherwise
; *
last byte is data byte
; *
;*******************************************************************************
clrf
AUTOBAUD_LOW
; reset register
clrf
AUTOBAUD_HIGH
; reset register
clrf
AUTOHALF_LOW
; reset register
clrf
AUTOHALF_HIGH
; reset register
clrf
COUNTER
; reset counter
clrf
MESSAGE_COUNTER
; reset message counter register
btfsc
goto
PORTA, LINRX
WaitStartBit
; was start bit there
; no, keep on looking
; yes, measure first low bit time
btfss
PORTA, LINRX
; was there a transition from low to high?
goto
AutoMeasureSet
; no, increment Autobaud counter registers
incf
COUNTER,f
; increment counter register
goto
TestSet
; yes, therefore increment bit counter and measure
high time
AutoMeasureSet
incfsz
goto
incf
goto
; Autobaud counter. The signal is sampled every 6 Instruction cycles
; This means the number of countes equals 8*Tbit/6Tcy. Example:
; Transmission rate 19.2Kbaud, Fosc=4MHz => Tbit=51us, 8*51us/6*1us = 68
; counts. Counting the Transitions takes 9Tcy. This value has to be
; added to the number of counts. Example from above: 68 Couts + 5*9Tcy
; (5 because five transitions are counted.
AUTOBAUD_LOW, f
; increment Autobaud low register
TestClear
; keep on testing
AUTOBAUD_HIGH,f
; increment high byte of autobaud register
TestClear
; keep on testing
AutoMeasureClr
incfsz
goto
incf
goto
AUTOBAUD_LOW, f
TestSet
AUTOBAUD_HIGH,f
TestSet
DS41185C-page 44
;
;
;
;
increment Autobaud low register
keep on testing
increment high byte of autobaud register
keep on testing
 2004 Microchip Technology Inc.
LIN bus Slave for the PIC16C432
TestSet
btfsc
goto
PORTA, LINRX
AutoMeasureClr
; check if pin is still high
; no, there was not, measure time
incf
movf
xorlw
btfss
goto
COUNTER, f
COUNTER, w
0x08
STATUS, Z
TestClear
;
;
;
;
;
;
;
;
;
;
;
;
;
CalcHalfBit
AdjustLowByte
is result zero?
No, therefore keep on measuring
It takes 6Tcy to process the counter register. This is executed
eight times => 8*6Tcy=48Tcy where the low byte is not update according
to 6 countes. Therefore 48Tcy/6countes= 8. Eight countes have
to be added to the low byte of the auto_baud_low register
add one count to low byte, because auf LIN bus propagation time
4us to drive LIN bus high and 2V/us raise time. Therefore we have
10us delay before a slave sees a high value. 10us is two counts
Therefore the total counts to be added are 10 counts (=0x0a)
movlw
addwf
Divide
increment counter register
check if all 8 bits were received
0x09
AUTOBAUD_LOW, f
; adjust low byte
; Calculation of transmission time for one bit
bcf
STATUS, C
; clear carry bit
rrf
AUTOBAUD_HIGH,f
; rotate autobaud
rrf
AUTOBAUD_LOW, f
; rotate autobaud
bcf
STATUS, C
; clear carry bit
rrf
AUTOBAUD_HIGH,f
; rotate autobaud
rrf
AUTOBAUD_LOW, f
; rotate autobaud
bcf
STATUS, C
; clear carry bit
rrf
AUTOBAUD_HIGH,f
; rotate autobaud
rrf
AUTOBAUD_LOW, f
; rotate autobaud
high register
low register
high register
low register
high register
low register
; Calculate the transmission time for half the bit time (means
; divide transmission time of one bit by two).
bcf
STATUS, C
; clear carry bit
rrf
AUTOBAUD_HIGH,w
; rotate autobaud high register
movwf
AUTOHALF_HIGH
; copy result into AUTOHALF_HIGH register
rrf
AUTOBAUD_LOW, w
; rotate autobaud high register
movwf
AUTOHALF_LOW
; copy result into AUTOHALF_LOW register
; Adjust 16-bit counter for receive and transmit routine. This means
; that the overhead of instruction cycles in of the receive/transmit
; routine has to be substracted from the transmission time of one bit
; and half a bit.
movlw
0x02
; 18-19 instruction cycles overhead from
; transmit/receive routine. This overhead
; must be substracted from iterations
subwf
AUTOBAUD_LOW, f
; adjust low byte from Autobaud counter
movlw
0x02
; substract 2 from low byte of half the bit time
subwf
AUTOHALF_LOW, f
; substract from low byte of half the bit time
; wait until Stop-bit comes in. Otherwise the reception of the next
; bit will start to early.
EndStopBit
btfss
goto
PORTA, LINRX
EndStopBit
; Wait for stop bit to be finished
; Stop bit not finished yet
; receive Identifier byte
call
Receive
; Receive Identifier byte
; store Identifier byte
movf
RXTX_REG, w
movwf
ID_TEMP
; copy Identifier byte into w-register
; copy Identifier into ID_TEMP register
 2004 Microchip Technology Inc.
DS41185C-page 45
PICDEM™ LIN User’s Guide
call
CheckParityBits
; check if parity bits are correct
; Decode ID4 and ID5. These two bits indicate how many bytes of
; data have to be transmitted or received.
swapf
RXTX_REG, w
; change ID4 and ID5 to lower nibble
andlw
0x03
; delete rest
call
DecDataLength
; decode data length
clrf
PCLATH
; reset PCLATH register
movwf
MESSAGE_COUNTER
; store length of message into MESSAGE_LENGTH
; register
incf
MESSAGE_COUNTER,f ; increment message counter by one for
; receiving or transmitting CRC byte
; Decode Identifier byte.
movf
RXTX_REG, w
andlw
0x0f
;
;
;
;
load RXTX_REG into w-register (Identifierbyte into
w-register)
Delete upper four bits of Identifier byte (these
bit include parity bits and data lenght code).
Lower
; bit are the indifier bits.
call
clrf
DecodeIDTable
PCLATH
; Decode Identifier bits
; reset PCLATH register
addwf
goto
goto
goto
PCL, f
ReceiveMode
TransmitMode
ReceiveMode
;
;
;
;
TransmitMode
call
movlw
movwf
CheckCRC
DATAPOINTER
FSR
; generated CRC
; point to first data byte
; initialize FSR register
TransmitNextD
movf
movwf
INDF, w
RXTX_REG
; copy data byte into w-register
; copy data in RXTX_REG
call
incf
decfsz
goto
retlw
Transmit
; transmit data
FSR, f
; point to next location
MESSAGE_COUNTER, f ; decrement Message Counter by one
TransmitNextD
; transmit next data
0x00
; return to main
ReceiveMode
ReceiveNextData
; Receive Mode in this sequence data is received
movlw
DATAPOINTER
; point to data location
movwf
FSR
; where data should be stored
call
Receive
; receive next data
movf
RXTX_REG, w
; copy data into w-register
movwf
INDF
; copy data into data area
incf
FSR, f
; point to next location
decfsz
MESSAGE_COUNTER, f ; decrement number of bytes to receive by one
goto
ReceiveNextData
; receive next data byte
call
call
retlw
DS41185C-page 46
add mode to low byte of PC
receive data
transmit data
receive data
CheckParityBits
CheckCRC
0x00
; check if parity bits are correct
; check if checksum is correct
; return to main
 2004 Microchip Technology Inc.
LIN bus Slave for the PIC16C432
Receive
;*******************************************************************************
; * ROUTINE
: LIN bus Receive Routine
; * AUTHOR
: Thomas Schmidt
; * DATE LAST MODIFIED : 08/14/2001
; * REVISION
: 1.0
; * CHANGES
: none
; * INPUT PARAMETER
: none
; * OUTPUT PARAMETER
: none
; * DESCRIPTION
: This routine receives an 8-bit value via the LIN
Bus
;*******************************************************************************
clrf
RXTX_REG
; clear receive register
movlw
BITS
; number of bits to receive
movwf
COUNTER
; load number of bits into counter register
ReceiveStartBit
btfsc
goto
call
call
PORTA, LINRX
ReceiveStartBit
DelayHalfBit
DelayFullBit
ReceiveNext
btfsc
bsf
btfss
bcf
rrf
call
decfsz
goto
retlw
PORTA, LINRX
STATUS,C
PORTA, LINRX
STATUS,C
RXTX_REG, f
DelayFullBit
COUNTER, f
ReceiveNext
0x00
Transmit
TransmitNext
;
;
;
;
;
;
;
;
;
;
;
;
;
;
test for falling edge
start-bit not found
wait until middle of start-bit
ignore start-bit and sample first
data bit in the middle of the bit
is LINRX zero or a one?
bit is a one => set carry bit
is LINRX one or a zero?
LINRX is zero => clear carry bit
rotate value into receive register
call Delay routine
decrement receive count register by one
receive next bit
return
;*******************************************************************************
; * ROUTINE
: LIN bus Transmit Routine
; * AUTHOR
: Thomas Schmidt
; * DATE LAST MODIFIED
: 08/14/2001
; * REVISION
: 1.0
; * CHANGES
: none
; * INPUT PARAMETER
: none
; * OUTPUT PARAMETER
: none
; * DESCRIPTION
: This routine transmits an 8-bit value via the LIN bus
; ******************************************************************************
movlw
BITS
; number of bit's to transmit
movwf
COUNTER
; initialize count register
bsf
STATUS, RP0
; select page 1
bcf
LININTF, LINTX
; generate start-bit
bcf
STATUS, RP0
; select page 0
call
DelayFullBit
; generate Delay for one bit-time
rrf
RXTX_REG, f
; rotate receive register
bsf
STATUS, RP0
; select page 1
btfss
STATUS, C
; test bit to be transmited
bcf
LININTF, LINTX
; Send a zero
btfsc
STATUS, C
; Check if a high has to be transmited
bsf
LININTF, LINTX
; send a one
bcf
STATUS, RP0
; select page 0
call
DelayFullBit
; call Delay routine
decfsz
COUNTER, f
; decrement counter register
goto
TransmitNext
; transmit next bit
bsf
STATUS, RP0
; select page 1
bsf
LININTF, LINTX
; generate Stop bit
bcf
STATUS, RP0
; select page 0
call
DelayFullBit
; delay for Stop bit
retlw
0x00
; return to main routine
 2004 Microchip Technology Inc.
DS41185C-page 47
PICDEM™ LIN User’s Guide
DelayFullBit
LoadHighByte
DecLowByte1
DecLowByteOnly
DecLowByte2
DecLowByte11
DecLowByte22
DelayHalfBit
LoadHighByteH
DecLowByteH1
DecLowByteOnlyH
DecLowByteH2
DecLowByteH11
DecLowByteH22
DS41185C-page 48
;*******************************************************************************
; * ROUTINE
: LIN bus Delay Routinefor Full bit time
; * AUTHOR
: Thomas Schmidt
; * DATE LAST MODIFIED : 08/14/2001
; * REVISION
: 1.0
; * CHANGES
: none
; * INPUT PARAMETER
: none
; * OUTPUT PARAMETER
: none
; * DESCRIPTION
: This routine generates a delay for a full bit time. The
routine is called from the transmit or receive routine.
;*******************************************************************************
movf
AUTOBAUD_HIGH,w
; copy content of Autobaud high register into
btfss
STATUS, Z
; is high byte = 0?
goto
LoadHighByte
; no, high byte is not zero
goto
DecLowByteOnly
; decrement only low byte
movwf
clrf
decfsz
goto
decfsz
goto
TEMP2
TEMP1
TEMP1, f
DecLowByte11
TEMP2, f
DecLowByte1
;
;
;
;
;
;
load TEMP2 with content of AUTOBAUD_HIGH
reset TEMP1 register
decrement low byte
do until result is zero
decrement low byte
decrement low byte again
movf
movwf
decfsz
goto
retlw
nop
goto
nop
goto
AUTOBAUD_LOW, w
TEMP1
TEMP1, f
DecLowByte22
0x00
DecLowByte1
;
;
;
;
;
;
;
copy low byte from autobaud register
into TEMP1
decrement low byte until zero
extra two cycle delay
return from subroutine
stretch time
additional two cycle delay
DecLowByte2
; additional two cycle delay
;*******************************************************************************
; * ROUTINE
: LIN bus Delay Routine Half Bit time
; * AUTHOR
: Thomas Schmidt
; * DATE LAST MODIFIED : 08/14/2001
; * REVISION
: 1.0
; * CHANGES
: none
; * INPUT PARAMETER
: none
; * OUTPUT PARAMETER
: none
; * DESCRIPTION
: This routine generates a delay for a half a bit time.
The routine is called from the receive routine.
;*******************************************************************************
movf
AUTOHALF_HIGH,w
; copy content of Autobaud high register into
btfss
STATUS, Z
; is high byte = 0?
goto
LoadHighByteH
; no, high byte is not zero
goto
DecLowByteOnlyH
; decrement only low byte
movwf
clrf
decfsz
goto
decfsz
goto
TEMP2
TEMP1
TEMP1, f
DecLowByteH11
TEMP2, f
DecLowByteH1
;
;
;
;
;
;
load TEMP2 with content of AUTOHALF_HIGH
reset TEMP1 register
decrement low byte
do until result is zero
decrement low byte
decrement low byte again
movf
movwf
decfsz
goto
retlw
nop
goto
nop
goto
AUTOHALF_LOW, w
TEMP1
TEMP1, f
DecLowByteH22
0x00
;
;
;
;
;
copy low byte from autobaud register
into TEMP1
decrement low byte until zero
extra two cycle delay
return from subroutine
DecLowByteH1
; additional two cycle delay
DecLowByteH2
; additional two cycle delay
 2004 Microchip Technology Inc.
LIN bus Slave for the PIC16C432
CheckCRC
;*******************************************************************************
; * ROUTINE
: CRC Check and Generation *
; * AUTHOR
: Thomas Schmidt*
; * DATE LAST MODIFIED
: 08/14/2001*
; * REVISION
: 1.0*
; * CHANGES
: none*
; * INPUT PARAMETER
: none*
; * OUTPUT PARAMETER
: CRC Check: CRC check is appended after last Data Byte
CRC Gen : CRC is check and CRC_OK or CRC_ERROR is into
the w-register
; * DESCRIPTION
: This routine generates or check CRC based on the Modulo-256 checksum defined in the LIN bus spec. 1.2
;*******************************************************************************
movlw
DATAPOINTER
; point to first data byte
movwf
FSR
;
swapf
ID_TEMP, w
; get ID4 and ID5 into bit0 and bit 1
andlw
0x03
; get rid of all other bits
call
DecDataLength
; get number of data bytes to be transmitted
clrf
PCLATH
; reset PCLATH register
movwf
decf
TEMP1
TEMP1, f
; copy number of data bytes into Temp-Counter
; decrement number of data bytes by one, because
; TEMP2 register is preloaded
movf
movwf
incf
movf
addwf
btfsc
incf
decfsz
goto
INDF, w
TEMP2
FSR, f
INDF, w
TEMP2, f
STATUS, C
TEMP2, f
TEMP1, f
NextCalc
;
;
;
;
;
;
;
;
;
call
clrf
DecodeIDTable
PCLATH
; Decode Identifier bits
; Reset PCLATH register
addwf
goto
goto
PCL, f
CRCCheck
CRCAppend
;
;
;
;
add mode to low byte of PC
data was received therefore check CRC
data is going to be transmited, therefore
append CRC
CRCAppend
comf
movf
incf
movwf
retlw
TEMP2, f
TEMP2, w
FSR, f
INDF
0x00
;
;
;
;
;
complement CRC value
copy checksum into w-register
point to location for checksum
copy checksum behind
return from subroutine
CRCCheck
incf
movf
addwf
xorlw
btfss
retlw
retlw
FSR, f
INDF, w
TEMP2, w
0xff
STATUS, Z
CRC_ERROR
CRC_OK
;
;
;
;
;
;
point to CRC byte
copy received CRC value into w-register
add received CRC to calculated CRC
Result should be 0xFF after XOR result is zero
is result zero?
return with CRC_ERROR
NextCalc
 2004 Microchip Technology Inc.
copy first data byte into
copy first data byte into
point to next data memory
move data into w-register
add data byte to temp and
add with carry?
yes, increment TEMP
decrement bit counter
calculate next
w-register
temp register
location
store in temp
DS41185C-page 49
PICDEM™ LIN User’s Guide
CheckParityBits
CheckP0
CalcP1
CheckP1
DS41185C-page 50
;*******************************************************************************
; * ROUTINE
: ID Partiy Bit Generation
; * AUTHOR
: Thomas Schmidt
; * DATE LAST MODIFIED : 08/14/2001
; * REVISION
: 1.0
; * CHANGES
: none
; * INPUT PARAMETER
: none
; * OUTPUT PARAMETER
: Updated ID with Parity bits in ID_TEMP
; * DESCRIPTION
: This routine generated the parity bits P1 and P0 for
the identifier byte according to the LIN bus specification 1.2.
;*******************************************************************************
movf
ID_TEMP, w
; copy ID value into w-register
; calculate P0
movwf
movwf
rrf
movf
xorwf
rrf
movf
xorwf
bcf
rrf
rrf
xorwf
TEMP1
TEMP2
TEMP1, f
TEMP1, w
TEMP2, f
TEMP1, f
TEMP1, w
TEMP2, f
STATUS, C
TEMP1, f
TEMP1, w
TEMP2, f
;
;
;
;
;
;
;
;
;
;
;
;
move ID value into TEMP1
move ID into TEMP2
rotate ID_TEMP one to the right (get ID1)
copy TEMP1 to w
TEMP2=ID0 XOR ID1
get ID2
copy ID2 into w-register
TEMP2= TEMP2 XOR ID2
clear carry flag
get ID3 into bit 0
ID4 into bit 0 and store result in w-register
TEMP2 = TEMP2 XOR ID4
btfsc
goto
btfss
goto
retlw
TEMP2, 0
CheckP0
ID_TEMP, 6
CalcP1
PARITY_ERROR_P0
;
;
;
;
;
test if bit is zero or one
check if received P0=1
Check if received P0=0
btfss
retlw
ID_TEMP, 6
PARITY_ERROR_P0
; check if P0 to 1
; P1=0 therefore parity error occrured
parity error occured
; calculate P1
movf
ID_TEMP, w
movwf
TEMP1
movwf
TEMP2
rrf
TEMP2, f
rrf
TEMP1, f
rrf
TEMP1, f
rrf
TEMP1, f
movf
TEMP1, w
xorwf
TEMP2, f
rrf
TEMP1, f
movf
TEMP1, w
xorwf
TEMP2, f
rrf
TEMP1, w
xorwf
TEMP2, f
comf
TEMP2, f
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
copy ID_TEMP into w-register
copy ID_TEMP into TEMP1
and TEMP2
ID1 into bit0
ID1 into bit0
ID2 into bit0
ID3 into bit0
copy TEMP1 into w-register
TEMP2 = ID1 XOR ID3
ID4 into bit0
TEMP1 into w-register
TEMP2 = TEMP2 XOR ID4
ID5 into bit0
TEMP2 = TEMP2 XOR ID5
negate TEMP2
btfsc
goto
btfsc
retlw
retlw
TEMP2, 0
CheckP1
ID_TEMP, 7
PARITY_ERROR_P1
PARITY_OK
;
;
;
;
;
check if
check if
check if
received
received
btfss
retlw
retlw
ID_TEMP, 7
PARITY_ERROR_P1
PARITY_OK
; set P1
; parity error occured
; no parity error occured
P1=1
received P1=1
P1=0
P1 is not 0 therefore parity error
P1 is 0 therefore no parity error
 2004 Microchip Technology Inc.
LIN bus Slave for the PIC16C432
DecodeIDTable
DecodeAction
DecDataLength
;***********************************************************************
; Data ID Table. This table is called from the receive routine after
; the Identifier byte is received
; ***********************************************************************
org
0x200
bsf
PCLATH, 1
; set PCLATH register
addwf
PCL, f
; add to PC
retlw
RXMODE
; Receive data from bus
retlw
RXMODE
; Receive data from bus
retlw
RXMODE
; Receive Data from bus
retlw
RXMODE
; Receive Data from bus
retlw
RXMODE
; Receive data from bus
retlw
RXMODE
; Receive data from bus
retlw
RXMODE
; Receive Data from bus
retlw
RXMODE
; Receive Data from bus
retlw
RXMODE
; Receive data from bus
retlw
RXMODE
; Receive data from bus
retlw
RXMODE
; Receive Data from bus
retlw
RXMODE
; Receive Data from bus
retlw
RXMODE
; Receive data from bus
retlw
RXMODE
; Receive data from bus
retlw
RXMODE
; Receive Data from bus
retlw
RXMODE
; Receive Data from bus
;*******************************************************************************
; * ROUTINE
: Decode ID Table
; * AUTHOR
: Thomas Schmidt
; * DATE LAST MODIFIED : 08/14/2001
; * REVISION
: 1.0
; * CHANGES
: Table offset value in w-register
; * INPUT PARAMETER
: RXMODE (switch to receive mode) or TXMODE (switch
transmit mode)
; * OUTPUT PARAMETER
: Updated ID with Parity bits in ID_TEMP
; * DESCRIPTION
: This routine generated the parity bits P1 and P0 for
the identifier byte according to the LIN bus specification 1.2.
;*******************************************************************************
bsf
PCLATH, 1
;
addwf
PCL, f
; add to PC
retlw
IGNORE
; Lock Doors
retlw
IGNORE
; Unlock Doors
retlw
INCREMENT
; Windows Up
retlw
DECREMENT
; Windows Down
retlw
IGNORE
; Mirror Left
retlw
IGNORE
; Mirror Right
retlw
IGNORE
; Mirror Down
retlw
IGNORE
; Mirror Up
retlw
IGNORE
; Tilt Seat Back
retlw
IGNORE
; Tilt Seat Forward
retlw
IGNORE
; Slide Seat Forward
retlw
IGNORE
; Slide Seat Backward
retlw
IGNORE
; Front Edge Set Up
retlw
IGNORE
; Front Edge Set Down
retlw
IGNORE
; Back Edge Seat Up
retlw
IGNORE
; Back Edge Seat Down
; ***********************************************************************
; Data length Table. This table is called from the receive routine after
; the Identifier byte is received
; ***********************************************************************
bsf
PCLATH, 1
; Set PCLATH register
addwf
PCL, f
; add to PC
retlw
0x02
; data length is 2
retlw
0x02
; data length is 2
retlw
0x04
; data length is 4
retlw
0x08
; data length is 8
END
 2004 Microchip Technology Inc.
DS41185C-page 51
PICDEM™ LIN User’s Guide
NOTES:
DS41185C-page 52
 2004 Microchip Technology Inc.
PICDEM™ LIN USER’S GUIDE
Appendix D. LIN bus Slave for the PIC16C432 (slave2.asm)
D.1
LIN bus SLAVE FOR THE PIC16C432 (SLAVE2.ASM)
Software License Agreement
The software supplied herewith by Microchip Technology Incorporated (the “Company”) is intended and supplied to you, the
Company’s customer, for use solely and exclusively with products manufactured by the Company.
The software is owned by the Company and/or its supplier, and is protected under applicable copyright laws. All rights are reserved.
Any use in violation of the foregoing restrictions may subject the user to criminal sanctions under applicable laws, as well as to civil
liability for the breach of the terms and conditions of this license.
THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR
SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
;
;
;
;
;
;
;
***********************************************************************************************
* Title
: LIN bus Slave for the PIC16C433 (slave2.asm)
* Author
: Thomas Schmidt
* Date
: 6/26/2001
* Revision
: 1.0
* Description :
***********************************************************************************************
LIST P=16C433, r=hex
; ***********************************************************************
; * Include files
; ***********************************************************************
#include <P16C433.INC>
; ***********************************************************************
; * Pin definitions
; ***********************************************************************
; ***********************************************************************
; * Register definitions
; ***********************************************************************
cblock 0x20
AUTOBAUD_LOW
; low byte of bit-time coutner
AUTOBAUD_HIGH
; high byte of bit-time counter
AUTOHALF_LOW
; low byte of half the bit time
AUTOHALF_HIGH
; high byte for half the bit time
TEMP1, TEMP2
; temporary registers
RXTX_REG
; receive register
MESSAGE_COUNTER
; Counts number of bytes to receive or transmit
COUNTER
; receive & Transmit counter register
ID_TEMP
; Register for ID byte
endc
 2004 Microchip Technology Inc.
DS41185C-page 53
PICDEM™ LIN User’s Guide
; ***********************************************************************
; * Other definitions
; ***********************************************************************
#define BITS
0x08
; number of bits to receive
#define RXMODE
0x00
; Receive mode
#define TXMODE
0x01
; Transmit mode
#define LISTENMODE
0x02
; Listen only mode
#define CRC_ERROR
0x01
; CRC_ERROR occurred
#define CRC_OK
0x00
; No CRC_ERROR occured
#define PARITY_OK
0x00
; No Parity error occrured
#define PARITY_ERROR_P00x01
; Parity error on P0 occured
#define PARITY_ERROR_P10x02
; Parity error on P1 occured
#define DATAPOINTER 0x35
; address of first data byte
#define IGNORE
0x00
; ignore received command
#define INCREMENT
0x01
; increment PORTB
#define DECREMENT
0x02
; decrement PORTB
; ***********************************************************************
; * Fuse configuration
; ***********************************************************************
__CONFIG _CP_OFF&_XT_OSC&_PWRTE_ON&_MCLRE_ON&_WDT_OFF
; ******************************************************************************
; * Reset vector
; ******************************************************************************
ORG 0x00
goto MainRoutine
;
;
;
;
;
;
;
;
;
MainRoutine
TestSynchByte
TestEndSynch
*******************************************************************************
* ROUTINE
: MainRoutine
* AUTHOR
: Thomas Schmidt
* DATE LAST MODIFIED : 08/14/2001
* REVISION
: 1.0
* CHANGES
: none
* INPUT PARAMETER
: none
* OUTPUT PARAMETER
: none
* DESCRIPTION
: Main routine. The main routine detects first the transmission time of the incomming calibration character.
After that the routine receives and transmits incomming
characters.
;*******************************************************************************
ORG 0x06
call
StartUp
; Initialize Registers
btfsc
goto
btfss
goto
GPIO, LINRX
TestSynchByte
GPIO, LINRX
TestEndSynch
;
;
;
;
Check for synch byte
Synch byte not received
check for end of synchbyte
end of synchronization byte not received
; Receive message on LIN bus
call
LinHandler
; call LIN bus handler
; Decode message and take action upon receive message Identifier
; Decode message and take action upon receive message Identifier
movf
ID_TEMP, w
; copy ID_TEMP into w-register
andlw
0x0f
; decode only lower four nibbles
call
DecodeAction
; see if LED has to be turn on
clrf
PCLATH
; reset PCLATH register
addwf
PCL, f
; add to PC
goto
TestSynchByte
; Ignore all other IDs
goto
WindowUp
; Window up function
goto
WindowDown
; Window down function
DS41185C-page 54
 2004 Microchip Technology Inc.
LIN bus Slave for the PIC16C432 (slave2.asm)
WindowUp
; WindowUp command was receive from Master
incf GPIO, f
; increment GPIO register
goto TestSynchByte
; Go back
WindowDown
; WindowDown command was receive from Master
decf GPIO, f
; decrement GPIO register
goto TestSynchByte
; go back
StartUp
InitData
Count
;****************************************************************************
; * ROUTINE
: StartUp
; * AUTHOR
: Thomas Schmidt
; * DATE LAST MODIFIED : 08/14/2001
; * REVISION
: 1.0
; * CHANGES
: none
; * INPUT PARAMETER
: none
; * OUTPUT PARAMETER
: none
; * DESCRIPTION
: This routine is called after power-up. PORTS and
peripherals are initialized.
;****************************************************************************
movlw
0xC0
; set all latches of GPIO except
movwf
GPIO
; LINTX & LINRX to zero
bsf
STATUS,RP0
; select page 1
movlw
0x07
; Make all pins on GPIO
movwf
ADCON1
; digitial I/Os.
movlw
0xf8
; make GP0, GP1 and GP2 outputs
movwf
TRISIO
; initialize TRISIO register
bcf
STATUS,RP0
; select page 0
; Initialize Data RAM for LIN bus Communication
movlw
DATAPOINTER
; point to first data location
movwf
FSR
; and initialize FSR register
movlw
0x0f
; Initialize 16 register
movwf
TEMP1
; temporary counter register
movlw
0xaa
; init value is 0xaa
movwf
INDF
; copy into location where FSR points to
incf
FSR, f
; point to next location
decfsz TEMP1, f
; decrement temporary counter
goto
Count
; counter is not zero, therefore keep on
; initializing
retlw
0x00
; return to main routine
 2004 Microchip Technology Inc.
DS41185C-page 55
PICDEM™ LIN User’s Guide
LinHandler
WaitStartBit
TestClear
;****************************************************************************
; * ROUTINE
: LIN bus Transmission/Reception Routine
; * AUTHOR
: Thomas Schmidt
; * DATE LAST MODIFIED : 08/14/2001
; * REVISION
: 1.0
; * CHANGES
: none
; * INPUT PARAMETER
: none
; * OUTPUT PARAMETER
: none
; * DESCRIPTION
: This routine sends out a LIN bus data frame and/or
receives or transmits data. The reception or trans. of
data is defined in the ID itself. After the ID-Byte is
sent it is decoded in a look up table if data should be
transmitted or received. Depending on the result either
a transmit or receive mode is activated.
ID_TEMP =holds the ID to send (parity is already generated
DATAPOINTER =points to first data byte. Last bytes CRC
byte for transmission otherwise last byte
is data byte
; *******************************************************************************
clrf
AUTOBAUD_LOW
; reset register
clrf
AUTOBAUD_HIGH
; reset register
clrf
AUTOHALF_LOW
; reset register
clrf
AUTOHALF_HIGH
; reset register
clrf
COUNTER
; reset counter
clrf
MESSAGE_COUNTER
; reset message counter register
btfsc
goto
; was start bit there
; no, keep on looking
; yes, measure first low bit time
btfss
GPIO, LINRX
; was there a transition from low to high?
goto
AutoMeasureSet
; no, increment Autobaud counter registers
incf
goto
AutoMeasureSet
GPIO, LINRX
WaitStartBit
COUNTER,f
TestSet
; increment counter register
; yes, therefore increment bit counter and measure
; high time
; Autobaud counter. The signal is sampled every 6 Instruction cycles
; This means the number of countes equals 8*Tbit/6Tcy. Example:
; Transmission rate 19.2Kbaud, Fosc=4MHz => Tbit=51us, 8*51us/6*1us = 68 counts.
; Counting the Transitions takes 9Tcy. This value has to be added to the
; number of counts. Example from above: 68 Couts + 5*9Tcy (5 because five
; transitions are counted.
incfsz
AUTOBAUD_LOW, f
; increment Autobaud low register
goto
TestClear
; keep on testing
incf
AUTOBAUD_HIGH,f
; increment high byte of autobaud register
goto
TestClear
; keep on testing
AutoMeasureClr
incfsz
goto
incf
goto
AUTOBAUD_LOW, f
TestSet
AUTOBAUD_HIGH,f
TestSet
;
;
;
;
increment Autobaud low register
keep on testing
increment high byte of autobaud register
keep on testing
TestSet
btfsc
goto
incf
movf
xorlw
btfss
goto
GPIO, LINRX
AutoMeasureClr
COUNTER, f
COUNTER, w
0x08
STATUS, Z
TestClear
;
;
;
;
;
;
;
check if pin is still high
no, there was not, measure time
increment counter register
check if all 8 bits were received
DS41185C-page 56
is result zero?
No, therefore keep on measuring
 2004 Microchip Technology Inc.
LIN bus Slave for the PIC16C432 (slave2.asm)
; It takes 6Tcy to process the counter register. This is executed
; eight times => 8*6Tcy=48Tcy where the low byte is not update according
; to 6 countes. Therefore 48Tcy/6countes= 8. Eight countes have
; to be added to the low byte of the auto_baud_low register
; add one count to low byte, because auf LIN bus propagation time
; 4us to drive LIN bus high and 2V/us raise time. Therefore we have
; 10us delay before a slave sees a high value. 10us is two counts
; Therefore the total counts to be added are 10 counts (=0x0a)
movlw
0x09
addwf
AUTOBAUD_LOW, f
; adjust low byte
Divide
CalcHalfBit
AdjustLowByte
; Calculation of transmission time for one bit
bcf
STATUS, C
; clear carry bit
rrf
AUTOBAUD_HIGH,f
; rotate autobaud
rrf
AUTOBAUD_LOW, f
; rotate autobaud
bcf
STATUS, C
; clear carry bit
rrf
AUTOBAUD_HIGH,f
; rotate autobaud
rrf
AUTOBAUD_LOW, f
; rotate autobaud
bcf
STATUS, C
; clear carry bit
rrf
AUTOBAUD_HIGH,f
; rotate autobaud
rrf
AUTOBAUD_LOW, f
; rotate autobaud
high register
low register
high register
low register
high register
low register
; Calculate the transmission time for half the bit time (means
; divide transmission time of one bit by two).
bcf
STATUS, C
; clear carry bit
rrf
AUTOBAUD_HIGH,w
; rotate autobaud high register
movwf
AUTOHALF_HIGH
; copy result into AUTOHALF_HIGH register
rrf
AUTOBAUD_LOW, w
; rotate autobaud high register
movwf
AUTOHALF_LOW
; copy result into AUTOHALF_LOW register
; Adjust 16-bit counter for receive and transmit routine. This means
; that the overhead of instruction cycles in of the receive/transmit
; routine has to be substracted from the transmission time of one bit
; and half a bit.
movlw
0x2
; 18-19 instruction cycles overhead from
; transmit/receive routine. This overhead
; must be substracted from iterations
subwf
AUTOBAUD_LOW, f
; adjust low byte from Autobaud counter
movlw
0x02
; substract 2 from low byte of half the bit time
subwf
AUTOHALF_LOW, f
; substract from low byte of half the bit time
; wait until Stop-bit comes in. Otherwise the reception of the next
; bit will start to early.
Wait4Stop
btfss
goto
GPIO, LINRX
Wait4Stop
; receive Identifier byte
call
Receive
movf
movwf
call
RXTX_REG, w
ID_TEMP
CheckParityBits
;
;
;
;
;
Receive Identifier byte
store Identifier byte
copy Identifier byte into w-register
copy Identifier into ID_TEMP register
check if parity bits are correct
; Decode ID4 and ID5. These two bits indicate how many bytes of
; data have to be transmitted or received.
swapf
RXTX_REG, w
; change ID4 and ID5 to lower nibble
andlw
0x03
; delete rest
call
DecDataLength
; decode data length
clrf
PCLATH
; reset PCLATH register
movwf
MESSAGE_COUNTER
; store length of message into MESSAGE_LENGTH
; register
incf
MESSAGE_COUNTER,f
; increment message counter by one for
; receiving or transmitting CRC byte
 2004 Microchip Technology Inc.
DS41185C-page 57
PICDEM™ LIN User’s Guide
; Decode Identifier byte.
movf
RXTX_REG, w
andlw
0x0f
;
;
;
;
;
;
;
call
clrf
DecodeIDTable
PCLATH
; Decode Identifier bits
; reset PCLATH register
addwf
goto
goto
goto
PCL, f
ReceiveMode
TransmitMode
ReceiveMode
;
;
;
;
TransmitMode
call
movlw
movwf
CheckCRC
DATAPOINTER
FSR
; generated CRC
; point to first data byte
; initialize FSR register
TransmitNextD
movf
movwf
INDF, w
RXTX_REG
; copy data byte into w-register
; copy data in RXTX_REG
call
incf
decfsz
goto
retlw
Transmit
FSR, f
MESSAGE_COUNTER, f
TransmitNextD
0x00
;
;
;
;
;
; Receive
movlw
movwf
ReceiveNextData call
movf
movwf
incf
decfsz
goto
ReceiveMode
call
call
retlw
DS41185C-page 58
load RXTX_REG into w-register (Identifierbyte
into
w-register)
Delete upper four bits of Identifier byte (these
bit include parity bits and data lenght code
Lower
bit are the indifier bits.
add mode to low byte of PC
receive data
transmit data
Listen to the bus
transmit data
point to next location
decrement Message Counter by one
transmit next data
return to main
Mode in this sequence data is received
DATAPOINTER
; point to data location
FSR
; where data should be stored
Receive
; receive next data
RXTX_REG, w
; copy data into w-register
INDF
; copy data into data area
FSR, f
; point to next location
MESSAGE_COUNTER, f
; decrement number of bytes to receive by one
ReceiveNextData
; receive next data byte
CheckParityBits
CheckCRC
0x00
; check if parity bits are correct
; check if checksum is correct
; return to main
 2004 Microchip Technology Inc.
LIN bus Slave for the PIC16C432 (slave2.asm)
;
;
;
;
;
;
;
;
;
Receive
*******************************************************************************
* ROUTINE
: LIN bus Receive Routine
* AUTHOR
: Thomas Schmidt
* DATE LAST MODIFIED : 08/14/2001
* REVISION
: 1.0
* CHANGES
: none
* INPUT PARAMETER
: none
* OUTPUT PARAMETER
: none
* DESCRIPTION
: This routine receives an 8-bit value via the LIN bus
;****************************************************************************
clrf
RXTX_REG
; clear receive register
movlw
BITS
; number of bits to receive
movwf
COUNTER
; load number of bits into counter register
ReceiveStartBit
btfsc
goto
call
call
GPIO, LINRX
ReceiveStartBit
DelayHalfBit
DelayFullBit
;
;
;
;
;
test for falling edge
start-bit not found
wait until middle of start-bit
ignore start-bit and sample first
data bit in the middle of the bit
ReceiveNext
btfsc
bsf
btfss
bcf
rrf
call
decfsz
goto
GPIO, LINRX
STATUS,C
GPIO, LINRX
STATUS,C
RXTX_REG, f
DelayFullBit
COUNTER, f
ReceiveNext
;
;
;
;
;
;
;
;
is LINRX zero or a one?
bit is a one => set carry bit
is LINRX one or a zero?
LINRX is zero => clear carry bit
rotate value into receive register
call Delay routine
decrement receive count register by one
receive next bit
retlw
0x00
; return
;****************************************************************************
; * ROUTINE
: LIN bus Transmit Routine
; * AUTHOR
: Thomas Schmidt
; * DATE LAST MODIFIED
: 08/14/2001
; * REVISION
: 1.0
; * CHANGES
: none
; * INPUT PARAMETER
: none
; * OUTPUT PARAMETER
: none
; * DESCRIPTION
: This routine transmits an 8-bit value via the LIN
bus
Transmit
TransmitNext
;****************************************************************************
movlw
BITS
; number of bit's to transmit
movwf
COUNTER
; initialize count register
bcf
GPIO, LINTX
; generate start-bit
call
DelayFullBit
; generate Delay for one bit-time
rrf
RXTX_REG, f
; rotate receive register
btfss
STATUS, C
; test bit to be transmited
bcf
GPIO, LINTX
; Send a zero
btfsc
STATUS, C
; Check if a high has to be transmited
bsf
GPIO, LINTX
; send a one
call
DelayFullBit
; call Delay routine
decfsz COUNTER, f
; decrement counter register
goto
TransmitNext
; transmit next bit
bsf
GPIO, LINTX
; generate Stop bit
call
DelayFullBit
; delay for Stop bit
retlw
0x00
; return to main routine
 2004 Microchip Technology Inc.
DS41185C-page 59
PICDEM™ LIN User’s Guide
DelayFullBit
LoadHighByte
DecLowByte1
DecLowByteOnly
DecLowByte2
DecLowByte11
DecLowByte22
;****************************************************************************
; * ROUTINE
: LIN bus Delay Routinefor Full bit time
; * AUTHOR
: Thomas Schmidt
; * DATE LAST MODIFIED
: 08/14/2001
; * REVISION
: 1.0
; * CHANGES
: none
; * INPUT PARAMETER
: none
; * OUTPUT PARAMETER
: none
; * DESCRIPTION
: This routine generates a delay for a full bit time.
; *
The routine is called from the transmit or receive
; *
routine.
; *******************************************************************************
movf
AUTOBAUD_HIGH,w
; copy content of Autobaud high register into
btfss
STATUS, Z
; is high byte = 0?
goto
LoadHighByte
; no, high byte is not zero
goto
DecLowByteOnly
; decrement only low byte
movwf
clrf
decfsz
goto
decfsz
goto
TEMP2
TEMP1
TEMP1, f
DecLowByte11
TEMP2, f
DecLowByte1
;
;
;
;
;
;
load TEMP2 with content of AUTOBAUD_HIGH
reset TEMP1 register
decrement low byte
do until result is zero
decrement low byte
decrement low byte again
movf
movwf
decfsz
goto
retlw
nop
goto
nop
goto
AUTOBAUD_LOW, w
TEMP1
TEMP1, f
DecLowByte22
0x00
DecLowByte1
;
;
;
;
;
;
;
copy low byte from autobaud register
into TEMP1
decrement low byte until zero
extra two cycle delay
return from subroutine
stretch time
additional two cycle delay
DecLowByte2
; additional two cycle delay
;
;
;
;
;
;
;
;
;
DelayHalfBit
LoadHighByteH
DecLowByteH1
*******************************************************************************
* ROUTINE
: LIN bus Delay RoutineHalf Bit time
* AUTHOR
: Thomas Schmidt
* DATE LAST MODIFIED
: 08/14/2001
* REVISION
: 1.0
* CHANGES
: none
* INPUT PARAMETER
: none
* OUTPUT PARAMETER
: none
* DESCRIPTION
: This routine generates a delay for a half a bit time.
The routine is called from the receive routine.
; *******************************************************************************
movf
AUTOHALF_HIGH,w
; copy content of Autobaud high register into
btfss
STATUS, Z
; is high byte = 0?
goto
LoadHighByteH
; no, high byte is not zero
goto
DecLowByteOnlyH
; decrement only low byte
movwf
clrf
decfsz
goto
decfsz
goto
DecLowByteOnlyH movf
movwf
DecLowByteH2
decfsz
goto
retlw
DecLowByteH11
nop
goto
DecLowByteH22
nop
goto
DS41185C-page 60
TEMP2
TEMP1
TEMP1, f
DecLowByteH11
TEMP2, f
DecLowByteH1
;
;
;
;
;
;
load TEMP2 with content of AUTOHALF_HIGH
reset TEMP1 register
decrement low byte
do until result is zero
decrement low byte
decrement low byte again
AUTOHALF_LOW, w
TEMP1
TEMP1, f
DecLowByteH22
0x00
;
;
;
;
;
copy low byte from autobaud register
into TEMP1
decrement low byte until zero
extra two cycle delay
return from subroutine
DecLowByteH1
; additional two cycle delay
DecLowByteH2
; additional two cycle delay
 2004 Microchip Technology Inc.
LIN bus Slave for the PIC16C432 (slave2.asm)
CheckCRC
;****************************************************************************
; * ROUTINE
: CRC Check and Generation
; * AUTHOR
: Thomas Schmidt
; * DATE LAST MODIFIED : 08/14/2001
; * REVISION
: 1.0
; * CHANGES
: none
; * INPUT PARAMETER
: none
; * OUTPUT PARAMETER
: CRC Check: CRC check is appended after last Data Byte
CRC Gen : CRC is check and CRC_OK or CRC_ERROR is
; *
into the w-register
; * DESCRIPTION
: This routine generates or check CRC based on the
; *
Modulo-256 checksum defined in the LIN bus spec. 1.2
; *
; ***************************************************************************
movlw
DATAPOINTER
; point to first data byte
movwf
FSR
;
swapf
ID_TEMP, w
; get ID4 and ID5 into bit0 and bit 1
andlw
0x03
; get rid of all other bits
call
DecDataLength
; get number of data bytes to be transmitted
clrf
PCLATH
; reset PCLATH register
movwf
decf
TEMP1
TEMP1, f
; copy number of data bytes into Temp-Counter
; decrement number of data bytes by one,
; TEMP2 register is preloaded
movf
movwf
incf
movf
addwf
btfsc
incf
decfsz
goto
INDF, w
TEMP2
FSR, f
INDF, w
TEMP2, f
STATUS, C
TEMP2, f
TEMP1, f
NextCalc
;
;
;
;
;
;
;
;
;
call
clrf
DecodeIDTable
PCLATH
; Decode Identifier bits
; reset PCLATH register
addwf
goto
goto
PCL, f
CRCCheck
CRCAppend
;
;
;
;
add mode to low byte of PC
data was received therefore check CRC
data is going to be transmited, therefore
append CRC
CRCAppend
comf
movf
incf
movwf
return
TEMP2, f
TEMP2, w
FSR, f
INDF
;
;
;
;
;
complement CRC value
copy checksum into w-register
point to location for checksum
copy checksum behind
return from subroutine
CRCCheck
incf
movf
addwf
xorlw
FSR, f
INDF, w
TEMP2, w
0xff
;
;
;
;
point to CRC byte
copy received CRC value into w-register
add received CRC to calculated CRC
Result should be 0xFF after XOR result is
btfss
retlw
retlw
STATUS, Z
CRC_ERROR
CRC_OK
; is result zero?
; return with CRC_ERROR
because
NextCalc
copy first data byte into
copy first data byte into
point to next data memory
move data into w-register
add data byte to temp and
add with carry?
yes, increment TEMP
decrement bit counter
calculate next
w-register
temp register
location
store in temp
zero
 2004 Microchip Technology Inc.
DS41185C-page 61
PICDEM™ LIN User’s Guide
; *******************************************************************************
; * ROUTINE
: ID Partiy Bit Generation
; * AUTHOR
: Thomas Schmidt
; * DATE LAST MODIFIED
: 08/14/2001
; * REVISION
: 1.0
; * CHANGES
: none
; * INPUT PARAMETER
: none
; * OUTPUT PARAMETER
: Updated ID with Parity bits in ID_TEMP
; * DESCRIPTION
: This routine generated the parity bits P1 and P0
; *
for the identifier byte according to the LIN bus
; *
specification 1.2.
; *
; *******************************************************************************
CheckParityBits movf
ID_TEMP, w
; copy ID value into w-register
; calculate P0
CheckP0
CalcP1
movwf
movwf
rrf
movf
xorwf
rrf
movf
xorwf
bcf
rrf
rrf
xorwf
TEMP1
TEMP2
TEMP1, f
TEMP1, w
TEMP2, f
TEMP1, f
TEMP1, w
TEMP2, f
STATUS, C
TEMP1, f
TEMP1, w
TEMP2, f
;
;
;
;
;
;
;
;
;
;
;
;
move ID value into TEMP1
move ID into TEMP2
rotate ID_TEMP one to the right (get ID1)
copy TEMP1 to w
TEMP2=ID0 XOR ID1
get ID2
copy ID2 into w-register
TEMP2= TEMP2 XOR ID2
clear carry flag
get ID3 into bit 0
ID4 into bit 0 and store result in w-register
TEMP2 = TEMP2 XOR ID4
btfsc
goto
btfss
goto
retlw
TEMP2, 0
CheckP0
ID_TEMP, 6
CalcP1
PARITY_ERROR_P0
;
;
;
;
;
test if bit is zero or one
check if received P0=1
Check if received P0=0
btfss
retlw
ID_TEMP, 6
PARITY_ERROR_P0
; check if P0 to 1
; P1=0 therefore parity error occrured
; calculate P1
movf
ID_TEMP, w
movwf
TEMP1
movwf
TEMP2
rrf
TEMP2, f
rrf
TEMP1, f
rrf
TEMP1, f
rrf
TEMP1, f
movf
TEMP1, w
xorwf
TEMP2, f
rrf
TEMP1, f
movf
TEMP1, w
xorwf
TEMP2, f
rrf
TEMP1, w
xorwf
TEMP2, f
comf
TEMP2, f
btfsc
TEMP2, 0
goto
CheckP1
btfsc
ID_TEMP, 7
retlw
PARITY_ERROR_P1
retlw
PARITY_OK
CheckP1
DS41185C-page 62
btfss
retlw
retlw
ID_TEMP, 7
PARITY_ERROR_P1
PARITY_OK
parity error occured
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
copy ID_TEMP into w-register
copy ID_TEMP into TEMP1
and TEMP2
ID1 into bit0
ID1 into bit0
ID2 into bit0
ID3 into bit0
copy TEMP1 into w-register
TEMP2 = ID1 XOR ID3
ID4 into bit0
TEMP1 into w-register
TEMP2 = TEMP2 XOR ID4
ID5 into bit0
TEMP2 = TEMP2 XOR ID5
negate TEMP2
;
;
;
;
check if P1=1
check if received P1=1
check if P1=0
received P1 is not 0 therefore parity error
; received P1 is 0 therefore no parity error
; set P1
; parity error occured
; no parity error occured
 2004 Microchip Technology Inc.
LIN bus Slave for the PIC16C432 (slave2.asm)
DecDataLength
; ***********************************************************************
; Data length Table. This table is called from the receive routine after
; the Identifier byte is received
; ***********************************************************************
org
0x200
bsf
PCLATH, 1
; Set PCLATH register
addwf
pcl, f
; add to pc
retlw
0x02
; data length is 2
retlw
0x02
; data length is 2
retlw
0x04
; data length is 4
retlw
0x08
; data length is 8
;
;
;
;
DecodeIDTable
***********************************************************************
Data ID Table. This table is called from the receive routine after
the Identifier byte is received
***********************************************************************
bsf
addwf
retlw
retlw
retlw
retlw
retlw
retlw
retlw
retlw
retlw
retlw
retlw
retlw
retlw
retlw
retlw
retlw
 2004 Microchip Technology Inc.
PCLATH, 1
PCL, f
RXMODE
RXMODE
RXMODE
RXMODE
RXMODE
RXMODE
RXMODE
RXMODE
RXMODE
RXMODE
RXMODE
RXMODE
RXMODE
RXMODE
RXMODE
RXMODE
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
set PCLATH register
add to PC
Receive data from bus
Receive data from bus
Receive Data from bus
Receive Data from bus
Receive data from bus
Receive data from bus
Receive Data from bus
Receive Data from bus
Receive data from bus
Receive data from bus
Receive Data from bus
Receive Data from bus
Receive data from bus
Receive data from bus
Receive Data from bus
Receive Data from bus
DS41185C-page 63
PICDEM™ LIN User’s Guide
DecodeAction
;****************************************************************************
; * ROUTINE
: Decode ID Table
; * AUTHOR
: Thomas Schmidt
; * DATE LAST MODIFIED : 08/14/2001
; * REVISION
: 1.0
; * CHANGES
: Table offset value in w-register
; * INPUT PARAMETER
: RXMODE (switch to receive mode) or TXMODE (switch
; *
transmit mode)
; * OUTPUT PARAMETER
: Updated ID with Parity bits in ID_TEMP
; * DESCRIPTION
: This routine generated the parity bits P1 and P0
; *
for the identifier byte according to the LIN bus
; *
specification 1.2.
; ****************************************************************************
bsf
PCLATH, 1
; Set PCLATH register
addwf
PCL, f
; add to PC
retlw
IGNORE
; Lock Doors
retlw
IGNORE
; Unlock Doors
retlw
INCREMENT
; Windows Up
retlw
DECREMENT
; Windows Down
retlw
IGNORE
; Mirror Left
retlw
IGNORE
; Mirror Right
retlw
IGNORE
; Mirror Down
retlw
IGNORE
; Mirror Up
retlw
IGNORE
; Tilt Seat Back
retlw
IGNORE
; Tilt Seat Forward
retlw
IGNORE
; Slide Seat Forward
retlw
IGNORE
; Slide Seat Backward
retlw
IGNORE
; Front Edge Set Up
retlw
IGNORE
; Front Edge Set Down
retlw
IGNORE
; Back Edge Seat Up
retlw
IGNORE
; Back Edge Seat Down
END
DS41185C-page 64
 2004 Microchip Technology Inc.
PICDEM™ LIN User’s Guide
Index
B
Board .................................................................................... 1
Power Supply ............................................................... 5
Stand-Alone ................................................................. 5
Board Layout and Schematics ........................................... 19
C
CAN bus Connections ........................................................ 10
CD-ROM
Sample Programs ........................................................ 2
Checksum Calculation ....................................................... 14
Console Panel .................................................................... 10
Inputs ......................................................................... 10
Outputs ...................................................................... 10
D
Demonstration Board
Hardware Features ...................................................... 2
Demonstration Board. See Board
Demonstration Programs. See Sample Programs
Device Erasing ..................................................................... 6
EEPROM/Flash ............................................................ 7
EPROM ........................................................................ 6
E
Error Handling .................................................................... 14
G
General Prototype Board ................................................... 11
H
Hardware
CAN bus Interface ........................................................ 2
Control Panel Interface ................................................ 2
DIP Sockets ................................................................. 2
Jumper ......................................................................... 2
Motor Control Slave Node ............................................ 2
On-board +12V Regulator ............................................ 2
Prototype Area ............................................................. 2
RF Stage ...................................................................... 2
RS-232 Socket ............................................................. 2
Seat Memory Unit ........................................................ 2
I
Transmit Mode ........................................................... 16
LIN bus Master Code ......................................................... 29
LIN bus Slave for the PIC16C432 (slave2.asm) ................ 53
LIN bus Slave for the PIC16C432 Code ............................ 41
LIN bus Slave Node High-Side Drivers .............................. 12
Slave Node 1 ............................................................. 12
Slave Node 2 ............................................................. 12
LIN bus Specification Ver. 1.2 ................................. 3, 13, 14
LIN bus Transmission Routine ........................................... 14
M
Master Node ........................................................................ 8
LIN-to-CAN Gateway ................................................... 8
RS-232-to-LIN Gateway .............................................. 8
Stand-Alone ................................................................. 8
Master Software ................................................................. 13
Microchip
WWW Address ............................................................ 3
MPASM Assembler .............................................................. 7
MPASM User’s Guide .......................................................... 3
MPLAB ICE .......................................................................... 3
MPLAB IDE ...................................................................... 3, 7
MPLIB Object Librarian ........................................................ 3
MPLINK Object Linker ......................................................... 3
O
Oscillator Options ................................................................ 9
P
Parity Bit Generation .......................................................... 13
PIC16C432 LIN bus Slave Code ....................................... 16
PIC16C433 LIN bus Slave Code ....................................... 18
PICDEM LIN Board. See Board
PICDEM LIN Kit ................................................................... 1
User’s Guide ................................................................ 1
PICDEM LIN User’s Guide .................................................. 2
PICmicro® Mid-Range Reference Manual .......................... 3
Power Seat Panel .............................................................. 11
Button and Sensor Inputs .......................................... 11
ICD/ICSP Interface .................................................... 11
Relay Driver ............................................................... 11
Power Supply ....................................................................... 7
PRO MATE II ................................................................... 3, 7
Processor Sockets ............................................................... 8
ICD/ICSP Interface ............................................................... 8
R
K
Reference Documents ......................................................... 3
Remote Keyless Entry Panel ............................................. 10
Reprogramming Sample Device .......................................... 7
RF Receiver ....................................................................... 10
RS-232 Serial Port ............................................................... 8
Keyboard Read Routine ..................................................... 13
L
LEDs .................................................................................. 12
LIN bus
WWW Address ............................................................. 3
LIN bus Handler ................................................................. 16
Receive Mode ............................................................ 16
 2004 Microchip Technology Inc.
DS41185C-page 65
PICDEM™ LIN User’s Guide
S
Sample Devices ............................................................... 1, 2
Sample Programs ................................................................ 1
Stand-Alone Board
Sample Devices ........................................................... 6
Sample Programs ........................................................ 6
Switches ............................................................................... 9
T
Tutorials ............................................................................. 13
W
Windowed Device ................................................................ 6
DS41185C-page 66
 2004 Microchip Technology Inc.
PICDEM™ LIN User’s Guide
NOTES:
 2004 Microchip Technology Inc.
DS41185C-page 67
WORLDWIDE SALES AND SERVICE
AMERICAS
ASIA/PACIFIC
ASIA/PACIFIC
EUROPE
Corporate Office
2355 West Chandler Blvd.
Chandler, AZ 85224-6199
Tel: 480-792-7200
Fax: 480-792-7277
Technical Support:
480-792-7627
Web Address:
www.microchip.com
Australia - Sydney
Tel: 61-2-9868-6733
Fax: 61-2-9868-6755
India - Bangalore
Tel: 91-80-2229-0061
Fax: 91-80-2229-0062
China - Beijing
Tel: 86-10-8528-2100
Fax: 86-10-8528-2104
India - New Delhi
Tel: 91-11-5160-8632
Fax: 91-11-5160-8632
Austria - Weis
Tel: 43-7242-2244-399
Fax: 43-7242-2244-393
Denmark - Ballerup
Tel: 45-4420-9895
Fax: 45-4420-9910
China - Chengdu
Tel: 86-28-8676-6200
Fax: 86-28-8676-6599
Japan - Kanagawa
Tel: 81-45-471- 6166
Fax: 81-45-471-6122
France - Massy
Tel: 33-1-69-53-63-20
Fax: 33-1-69-30-90-79
China - Fuzhou
Tel: 86-591-750-3506
Fax: 86-591-750-3521
Korea - Seoul
Tel: 82-2-554-7200
Fax: 82-2-558-5932 or
82-2-558-5934
Germany - Ismaning
Tel: 49-89-627-144-0
Fax: 49-89-627-144-44
Atlanta
Alpharetta, GA
Tel: 770-640-0034
Fax: 770-640-0307
Boston
Westford, MA
Tel: 978-692-3848
Fax: 978-692-3821
Chicago
Itasca, IL
Tel: 630-285-0071
Fax: 630-285-0075
Dallas
Addison, TX
Tel: 972-818-7423
Fax: 972-818-2924
Detroit
Farmington Hills, MI
Tel: 248-538-2250
Fax: 248-538-2260
China - Hong Kong SAR
Tel: 852-2401-1200
Fax: 852-2401-3431
China - Shanghai
Tel: 86-21-6275-5700
Fax: 86-21-6275-5060
China - Shenzhen
Tel: 86-755-8290-1380
Fax: 86-755-8295-1393
China - Shunde
Tel: 86-757-2839-5507
Fax: 86-757-2839-5571
China - Qingdao
Tel: 86-532-502-7355
Fax: 86-532-502-7205
Singapore
Tel: 65-6334-8870
Fax: 65-6334-8850
Taiwan - Kaohsiung
Tel: 886-7-536-4816
Fax: 886-7-536-4817
Taiwan - Taipei
Tel: 886-2-2500-6610
Fax: 886-2-2508-0102
Italy - Milan
Tel: 39-0331-742611
Fax: 39-0331-466781
Netherlands - Drunen
Tel: 31-416-690399
Fax: 31-416-690340
England - Berkshire
Tel: 44-118-921-5869
Fax: 44-118-921-5820
Taiwan - Hsinchu
Tel: 886-3-572-9526
Fax: 886-3-572-6459
Kokomo
Kokomo, IN
Tel: 765-864-8360
Fax: 765-864-8387
Los Angeles
Mission Viejo, CA
Tel: 949-462-9523
Fax: 949-462-9608
San Jose
Mountain View, CA
Tel: 650-215-1444
Fax: 650-961-0286
Toronto
Mississauga, Ontario,
Canada
Tel: 905-673-0699
Fax: 905-673-6509
08/24/04
DS41185C-page 68
 2004 Microchip Technology Inc.