Advanced PIC18 Projects—CAN Bus Projects:CAN Bus Programming

CAN Bus Programming

To operate the PIC18F258 microcontroller on the CAN bus, perform the following steps:

• Configure the CAN bus I/O port directions (RB2 and RB3)

• Initialize the CAN module (CANInitialize)

• Set the CAN module to CONFIG mode (CANSetOperationMode)

• Set the mask registers (CANSetMask)

• Set the filter registers (CANSetFilter)

• Set the CAN module to normal mode (CANSetOperationMode)

• Write/read data (CANWrite/CANRead)

PROJECT 9.1—Temperature Sensor CAN Bus Project

The following is a simple two-node CAN bus–based project. The block diagram of the project is shown in Figure 9.15. The system is made up of two CAN nodes. One node (called DISPLAY node) requests the temperature every second and displays it on an LCD. This process is repeated continuously. The other node (called COLLECTOR node) reads the temperature from an external semiconductor temperature sensor.

Advanced PIC18 Projects—CAN Bus Projects -0213

The project’s circuit diagram is given in Figure 9.16. Two CAN nodes are connected together using a two-meter twisted pair cable, terminated with a 120-ohm resistor at each end.

Advanced PIC18 Projects—CAN Bus Projects -0214

The DISPLAY Processor

Like the COLLECTOR processor, the DISPLAY processor consists of a PIC18F258 microcontroller with a built-in CAN module and an MCP2551 transceiver chip. The microcontroller is operated from an 8MHz crystal. The MCLR input is connected to an external reset button. The CAN outputs (RB2/CANTX and RB3/CANRX) of the microcontroller are connected to the TXD and RXD inputs of the MCP2551. Pins CANH and CANL of the transceiver chip are connected to the CAN bus. An HD44780-type LCD is connected to PORTC of the microcontroller to display the temperature values.

The COLLECTOR Processor

The COLLECTOR processor consists of a PIC18F258 microcontroller with a built-in CAN module and an MCP2551 transceiver chip. The microcontroller is

operated from an 8MHz crystal. The MCLR input is connected to an external reset button. Analog input AN0 of the microcontroller is connected to a LM35DZ-type semiconductor temperature sensor. The sensor can measure temperature in the range of 0oC to 100oC and generates an analog voltage directly proportional to the measured temperature (i.e., the output is 10mV/oC). For example, at 20oC the output voltage is 200mV.

The CAN outputs (RB2/CANTX and RB3/CANRX) of the microcontroller are connected to the TXD and RXD inputs of an MCP2551-type CAN transceiver chip. The CANH and CANL outputs of this chip are connected directly to a twisted cable terminating at the CAN bus. The MCP2551 is an 8-pin chip that supports data rates up to 1Mb/s. The chip can drive up to 112 nodes. An external resistor connected to pin 8 of the chip controls the rise and fall times of CANH and CANL so that EMI can be reduced. For high-speed operation this pin should be connected to ground. A reference voltage equal to VDD/2 is output from pin 5 of the chip.

The program listing is in two parts: the DISPLAY program and the COLLECTOR program. The operation of the system is as follows:

• The DISPLAY processor requests the current temperature from the COLLECTOR processor over the CAN bus

• The COLLECTOR processor reads the temperature, formats it, and sends to the DISPLAY processor over the CAN bus

• The DISPLAY processor reads the temperature from the CAN bus and then displays it on the LCD

• This process is repeated every second

DISPLAY Program

Figure 9.17 shows the program listing of the DISPLAY program, called DISPLAY.C. At the beginning of the program PORTC pins are configured as outputs, RB3 is configured as input (CANRX), and RB2 is configured as output (CANTX). In this project the CAN bus bit rate is selected as 100Kb/s. With a microcontroller clock frequency of 8MHz, the Baud Rate Calculator program (see Figure 9.14) is used to calculate the timing parameters as:

The mikroC CAN bus function CANInitialize is used to initialize the CAN module. The timing parameters and the initialization flag are specified as arguments in this function. The initialization flag is made up from the bitwise AND of:

Advanced PIC18 Projects—CAN Bus Projects -0215

Where sampling the bus three times is specified, the standard identifier is specified, double buffering is turned on, and the line filter is turned off.

Then the operation mode is set to CONFIG and the filter masks and filter values are specified. Both mask1 and mask2 are set to all 1’s (-1 is a shorthand way of writing hexadecimal FFFFFFFF, i.e., setting all mask bits to 1’s) so that all filter bits match up with incoming data.

Advanced PIC18 Projects—CAN Bus Projects -0216

Advanced PIC18 Projects—CAN Bus Projects -0217

Advanced PIC18 Projects—CAN Bus Projects -0218

Filter 3 for buffer 2 is set to value 3 so that identifiers having values 3 are accepted by the receive buffer.

The operation mode is then set to NORMAL. The program then configures the LCD and displays the message “CAN BUS” for one second on the LCD.

The main program loop executes continuously and starts with a for statement. Inside this loop the LCD is cleared and text “TEMP ¼” is displayed on the LCD. Then character “T” is sent over the bus with the identifier equal to 500 (the COLLECTOR

Advanced PIC18 Projects—CAN Bus Projects -0219

Advanced PIC18 Projects—CAN Bus Projects -0220

Advanced PIC18 Projects—CAN Bus Projects -0221

node filter is set to accept identifier 500). This is a request to the COLLECTOR node to send the temperature reading. The program then reads the temperature from the CAN bus, converts it to a string in array txt, and displays it on the LCD. This process repeats after a one-second delay.

COLLECTOR Program

Figure 9.18 shows the program listing of the COLLECTOR program, called COLLECTOR.C. The initial part of this program is the same as the DISPLAY program. The receive filter is set to 500 so that messages with identifier 500 are accepted by the program.

Inside the program loop, the program waits until it receives a request to send the temperature. Here the request is identified by the reception of character “T”. Once a valid request is received, the temperature is read and converted into oC (stored in variable temperature) and then sent to the CAN bus as a byte with an identifier value equal to 3. This process repeats forever.

Leave a comment

Your email address will not be published. Required fields are marked *