• CAN_CONFIG_VALID_STD_MSG Accept only valid standard identifier messages
• CAN_CONFIG_ALL_VALID_MSG Accept all valid messages
These configuration values can be bitwise AND’ed to form complex configuration values.
9.10.4 CANSetBaudRate
The CANSetBaudRate function is used to set the CAN bus baud rate. The function prototype is:
void CANSetBaudRate(char SJW, char BRP, char PHSEG1, char PHSEG2,
char PROPSEG, char CAN_CONFIG_FLAGS)
The arguments of the function are as in function CANInitialize.
9.10.5 CANSetMask
The CANSetMask function sets the mask for filtering messages. The function prototype is:
void CANSetMask(char CAN_MASK, long value,
char CAN_CONFIG_FLAGS)
CAN_MASK can be one of the following:
• CAN_MASK_B1 Receive buffer 1 mask value
• CAN_MASK_B2 Receive buffer 2 mask value
value is the mask register value. CAN_CONFIG_FLAGS can be either CAN_CONFIG_XTD (extended message), or CAN_CONFIG_STD (standard message).
9.10.6 CANSetFilter
The CANSetFilter function sets filter values. The function prototype is:
void CANSetFilter(char CAN_FILTER, long value,
char CAN_CONFIG_FLAGS)
• CAN_FILTER can be one of the following:
• CAN_FILTER_B1_F1 Filter 1 for buffer 1
• CAN_FILTER_B1_F2 Filter 2 for buffer 1
• CAN_FILTER_B2_F1 Filter 1 for buffer 2
• CAN_FILTER_B2_F2 Filter 2 for buffer 2
• CAN_FILTER_B2_F3 Filter 3 for buffer 2
• CAN_FILTER_B2_F4 Filter 4 for buffer 2
CAN_CONFIG_FLAGS can be either CAN_CONFIG_XTD (extended message) or CAN_CONFIG_STD (standard message).
9.10.7 CANRead
The CANRead function is used to read messages from the CAN bus. If no message is available, 0 is returned. The function prototype is:
char CANRead(long *id, char *data, char *datalen,
char *CAN_RX_MSG_FLAGS)
id is the CAN message identifier. Only 11 or 29 bits may be used depending on message type (standard or extended). data is an array of bytes up to 8 where the received data is stored. datalen is the length of the received data (1 to 8).
CAN_RX_MSG_FLAGS can be one of the following:
• CAN_RX_FILTER_1 Receive buffer filter 1 accepted this message
• CAN_RX_FILTER_2 Receive buffer filter 2 accepted this message
• CAN_RX_FILTER_3 Receive buffer filter 3 accepted this message
• CAN_RX_FILTER_4 Receive buffer filter 4 accepted this message
• CAN_RX_FILTER_5 Receive buffer filter 5 accepted this message
• CAN_RX_FILTER_6 Receive buffer filter 6 accepted this message
• CAN_RX_OVERFLOW Receive buffer overflow occurred
• CAN_RX_INVALID_MSG Invalid message received
• CAN_RX_XTD_FRAME Extended identifier message received
• CAN_RX_RTR_FRAME RTR frame message received
• CAN_RX_DBL_BUFFERED This message was double buffered
These flags can be bitwise AND’ed if desired.
9.10.8 CANWrite
The CANWrite function is used to send a message to the CAN bus. A zero is returned if message can not be queued (buffer full). The function prototype is:
char CANWrite(long id, char *data, char datalen,
char CAN_TX_MSG_FLAGS)
id is the CAN message identifier. Only 11 or 29 bits may be used depending on message type (standard or extended). data is an array of bytes up to 8 where the data to be sent is stored. datalen is the length of the data (1 to 8).
CAN_TX_MSG_FLAGS can be one of the following:
• CAN_TX_PRIORITY_0 Transmit priority 0
• CAN_TX_PRIORITY_1 Transmit priority 1
• CAN_TX_PRIORITY_2 Transmit priority 2
• CAN_TX_PRIORITY_3 Transmit priority 3
• CAN_TX_STD_FRAME Standard identifier message
• CAN_TX_XTD_FRAME Extended identifier message
• CAN_TX_NO_RTR_FRAME Non RTR message
• CAN_TX_RTR_FRAME RTR message
These flags can be bitwise AND’ed if desired.
9.11 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.
Figure 9.15: Block diagram of the project
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.
Figure 9.16: Circuit diagram of the project
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 0°C to 100°C and generates an analog voltage directly proportional to the measured temperature (i.e., the output is 10mV/°C). For example, at 20°C 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.