CAN Library
The mikroC PRO for dsPIC30/33 and PIC24 provides a library (driver) for working with the dsPIC30F CAN module.
The CAN is a very robust protocol that has error detection and signalization, self–checking and fault confinement. Faulty CAN data and remote frames are re-transmitted automatically, similar to the Ethernet.
Data transfer rates depend on distance. For example, 1 Mbit/s can be achieved at network lengths below 40m while 250 Kbit/s can be achieved at network lengths below 250m. The greater distance the lower maximum bitrate that can be achieved. The lowest bitrate defined by the standard is 200Kbit/s. Cables used are shielded twisted pairs.
CAN supports two message formats:
- Standard format, with 11 identifier bits and
- Extended format, with 29 identifier bits

- Consult the CAN standard about CAN bus termination resistance.
- CAN library routines require you to specify the module you want to use. To use the desired CAN module, simply change the letter x in the routine prototype for a number from 1 to 2.
- Number of CAN modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
Library Routines
- CANxSetOperationMode
- CANxGetOperationMode
- CANxInitialize
- CANxSetBaudRate
- CANxSetMask
- CANxSetFilter
- CANxRead
- CANxWrite
CANxSetOperationMode
Prototype |
void CANxSetOperationMode(unsigned int mode, unsigned int WAIT); |
---|---|
Description |
Sets the CAN module to requested mode. |
Parameters |
|
Returns |
Nothing. |
Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
Example |
// set the CAN1 module into configuration mode (wait inside CAN1SetOperationMode until this mode is set) CAN1SetOperationMode(_CAN_MODE_CONFIG, 0xFF); |
Notes |
|
CANxGetOperationMode
Prototype |
unsigned int CANxGetOperationMode(); |
---|---|
Description |
The function returns current operation mode of the CAN module. See CAN_OP_MODE constants or device datasheet for operation mode codes. |
Parameters |
None. |
Returns |
Current operation mode. |
Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
Example |
// check whether the CAN1 module is in Normal mode and if it is then do something. if (CAN1GetOperationMode() == _CAN_MODE_NORMAL) { ... } |
Notes |
|
CANxInitialize
Prototype |
void CANxInitialize(unsigned int SJW, unsigned int BRP, unsigned int PHSEG1, unsigned int PHSEG2, unsigned int PROPSEG, unsigned int CAN_CONFIG_FLAGS); |
---|---|
Description |
Initializes the CAN module. The internal dsPIC30F CAN module is set to :
|
Parameters |
|
Returns |
Nothing. |
Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. |
Example |
// initialize the CAN1 module with appropriate baud rate and message acceptance flags along with the sampling rules unsigned int can_config_flags; ... can_config_flags = _CAN_CONFIG_SAMPLE_THRICE & // Form value to be used _CAN_CONFIG_PHSEG2_PRG_ON & // with CAN1Initialize _CAN_CONFIG_STD_MSG & _CAN_CONFIG_DBL_BUFFER_ON & _CAN_CONFIG_MATCH_MSG_TYPE & _CAN_CONFIG_LINE_FILTER_OFF; CAN1Initialize(1,3,3,3,1,can_config_flags); // initialize the CAN1 module |
Notes |
|
CANxSetBaudRate
Prototype |
void CANxSetBaudRate(unsigned int SJW, unsigned int BRP, unsigned int PHSEG1, unsigned int PHSEG2, unsigned int PROPSEG, unsigned int CAN_CONFIG_FLAGS); |
---|---|
Description |
Sets CAN baud rate. Due to complexity of the CAN protocol, you can not simply force a bps value. Instead, use this function when CAN is in Config mode. Refer to datasheet for details.
|
Parameters |
|
Returns |
Nothing. |
Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. CAN must be in Config mode, otherwise the function will be ignored. See CANxSetOperationMode. |
Example |
// set required baud rate and sampling rules unsigned int can_config_flags; ... CAN1SetOperationMode(_CAN_MODE_CONFIG,0xFF); // set CONFIGURATION mode (CAN1 module must be in config mode for baud rate settings) can_config_flags = _CAN_CONFIG_SAMPLE_THRICE & // Form value to be used _CAN_CONFIG_PHSEG2_PRG_ON & // with CAN1SetBaudRate _CAN_CONFIG_STD_MSG & _CAN_CONFIG_DBL_BUFFER_ON & _CAN_CONFIG_MATCH_MSG_TYPE & _CAN_CONFIG_LINE_FILTER_OFF; CAN1SetBaudRate(1,3,3,3,1,can_config_flags); // set the CAN1 module baud rate |
Notes |
|
CANxSetMask
Prototype |
void CANxSetMask(unsigned int CAN_MASK, long val, unsigned int CAN_CONFIG_FLAGS); |
---|---|
Description |
Function sets mask for advanced filtering of messages. Given |
Parameters |
|
Returns |
Nothing. |
Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. CAN must be in Config mode, otherwise the function will be ignored. See CANxSetOperationMode. |
Example |
// set appropriate filter mask and message type value CAN1SetOperationMode(_CAN_MODE_CONFIG,0xFF); // set CONFIGURATION mode (CAN1 module must be in config mode for mask settings) // Set all B1 mask bits to 1 (all filtered bits are relevant): // Note that -1 is just a cheaper way to write 0xFFFFFFFF. // Complement will do the trick and fill it up with ones. CAN1SetMask(_CAN_MASK_B1, -1, _CAN_CONFIG_MATCH_MSG_TYPE & _CAN_CONFIG_XTD_MSG); |
Notes |
|
CANxSetFilter
Prototype |
void CANxSetFilter(unsigned int CAN_FILTER, long val, unsigned int CAN_CONFIG_FLAGS); |
---|---|
Description |
Function sets message filter. Given |
Parameters |
|
Returns |
Nothing. |
Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. CAN must be in Config mode, otherwise the function will be ignored. See CANxSetOperationMode. |
Example |
// set appropriate filter value and message type CAN1SetOperationMode(_CAN_MODE_CONFIG,0xFF); // set CONFIGURATION mode (CAN1 module must be in config mode for filter settings) // Set id of filter B1_F1 to 3 CAN1SetFilter(_CAN_FILTER_B1_F1, 3, _CAN_CONFIG_XTD_MSG); |
Notes |
|
CANxRead
Prototype |
unsigned int CANxRead(unsigned long *id, char *data_, unsigned int *dataLen, unsigned int *CAN_RX_MSG_FLAGS); |
---|---|
Description |
If at least one full Receive Buffer is found, it will be processed in the following way :
|
Parameters |
|
Returns |
|
Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. The CAN module must be in a mode in which receiving is possible. See CANxSetOperationMode. |
Example |
// check the CAN1 module for received messages. If any was received do something. unsigned int msg_rcvd, rx_flags, data_len; char data[8]; unsigned long msg_id; ... CAN1SetOperationMode(_CAN_MODE_NORMAL,0xFF); // set NORMAL mode (CAN1 module must be in mode in which receive is possible) ... rx_flags = 0; // clear message flags if (msg_rcvd = CAN1Read(&msg_id, data, &data_len, &rx_flags)) { ... } |
Notes |
|
CANxWrite
Prototype |
unsigned int CANxWrite(long id, char *data_, unsigned int DataLen, unsigned int CAN_TX_MSG_FLAGS); |
---|---|
Description |
If at least one empty Transmit Buffer is found, the function sends message in the queue for transmission. |
Parameters |
|
Returns |
|
Requires |
MCU with the CAN module. MCU must be connected to the CAN transceiver (MCP2551 or similar) which is connected to the CAN bus. The CAN module must be in mode in which transmission is possible. See CANxSetOperationMode. |
Example |
// send message extended CAN message with appropriate ID and data unsigned int tx_flags; char data[8]; unsigned long msg_id; ... CAN1SetOperationMode(_CAN_MODE_NORMAL,0xFF); // set NORMAL mode (CAN1 must be in mode in which transmission is possible) tx_flags = _CAN_TX_PRIORITY_0 & _CAN_TX_XTD_FRAME & _CAN_TX_NO_RTR_FRAME; // set message flags CAN1Write(msg_id, data, 1, tx_flags); |
Notes |
|
CAN Constants
There is a number of constants predefined in CAN library. To be able to use the library effectively, you need to be familiar with these. You might want to check the example at the end of the chapter.
CAN_OP_MODE Constants
CAN_OP_MODE
constants define CAN operation mode. Function CANxSetOperationMode expects one of these as its argument:
const unsigned int _CAN_MODE_BITS = 0xE0, // Use this to access opmode bits _CAN_MODE_NORMAL = 0x00, _CAN_MODE_SLEEP = 0x20, _CAN_MODE_LOOP = 0x40, _CAN_MODE_LISTEN = 0x60, _CAN_MODE_CONFIG = 0x80;
CAN_CONFIG_FLAGS Constants
CAN_CONFIG_FLAGS
constants define flags related to CAN module configuration. Functions CANxInitialize and CANxSetBaudRate expect one of these (or a bitwise combination) as their argument:
const unsigned int _CAN_CONFIG_DEFAULT = 0xFF, // 11111111 _CAN_CONFIG_PHSEG2_PRG_BIT = 0x01, _CAN_CONFIG_PHSEG2_PRG_ON = 0xFF, // XXXXXXX1 _CAN_CONFIG_PHSEG2_PRG_OFF = 0xFE, // XXXXXXX0 _CAN_CONFIG_LINE_FILTER_BIT = 0x02, _CAN_CONFIG_LINE_FILTER_ON = 0xFF, // XXXXXX1X _CAN_CONFIG_LINE_FILTER_OFF = 0xFD, // XXXXXX0X _CAN_CONFIG_SAMPLE_BIT = 0x04, _CAN_CONFIG_SAMPLE_ONCE = 0xFF, // XXXXX1XX _CAN_CONFIG_SAMPLE_THRICE = 0xFB, // XXXXX0XX _CAN_CONFIG_MSG_TYPE_BIT = 0x08, _CAN_CONFIG_STD_MSG = 0xFF, // XXXX1XXX _CAN_CONFIG_XTD_MSG = 0xF7, // XXXX0XXX _CAN_CONFIG_DBL_BUFFER_BIT = 0x10, _CAN_CONFIG_DBL_BUFFER_ON = 0xFF, // XXX1XXXX _CAN_CONFIG_DBL_BUFFER_OFF = 0xEF, // XXX0XXXX _CAN_CONFIG_MSG_BITS = 0x60, _CAN_CONFIG_ALL_MSG = 0xFF, // X11XXXXX _CAN_CONFIG_VALID_XTD_MSG = 0xDF, // X10XXXXX _CAN_CONFIG_VALID_STD_MSG = 0xBF, // X01XXXXX _CAN_CONFIG_ALL_VALID_MSG = 0x9F; // X00XXXXX
You may use bitwise AND (&
) to form config byte out of these values. For example:
init = _CAN_CONFIG_SAMPLE_THRICE & _CAN_CONFIG_PHSEG2_PRG_ON & _CAN_CONFIG_STD_MSG & _CAN_CONFIG_DBL_BUFFER_ON & _CAN_CONFIG_VALID_XTD_MSG & _CAN_CONFIG_LINE_FILTER_OFF; ... CANInitialize(1, 1, 3, 3, 1, init); // initialize CAN
CAN_TX_MSG_FLAGS Constants
CAN_TX_MSG_FLAGS
are flags related to transmission of a CAN message:
const unsigned int _CAN_TX_PRIORITY_BITS = 0x03, _CAN_TX_PRIORITY_0 = 0xFC, // XXXXXX00 _CAN_TX_PRIORITY_1 = 0xFD, // XXXXXX01 _CAN_TX_PRIORITY_2 = 0xFE, // XXXXXX10 _CAN_TX_PRIORITY_3 = 0xFF, // XXXXXX11 _CAN_TX_FRAME_BIT = 0x08, _CAN_TX_STD_FRAME = 0xFF, // XXXXX1XX _CAN_TX_XTD_FRAME = 0xF7, // XXXXX0XX _CAN_TX_RTR_BIT = 0x40, _CAN_TX_NO_RTR_FRAME = 0xFF, // X1XXXXXX _CAN_TX_RTR_FRAME = 0xBF; // X0XXXXXX
You may use bitwise AND (&
) to adjust the appropriate flags. For example:
// form value to be used with CANSendMessage: send_config = _CAN_TX_PRIORITY_0 & _CAN_TX_XTD_FRAME & _CAN_TX_NO_RTR_FRAME; ... CANSendMessage(id, data, 1, send_config);
CAN_RX_MSG_FLAGS Constants
CAN_RX_MSG_FLAGS
are flags related to reception of CAN message. If a particular bit is set; corresponding meaning is TRUE or else it will be FALSE.
const unsigned int _CAN_RX_FILTER_BITS = 0x07, // Use this to access filter bits _CAN_RX_FILTER_1 = 0x00, _CAN_RX_FILTER_2 = 0x01, _CAN_RX_FILTER_3 = 0x02, _CAN_RX_FILTER_4 = 0x03, _CAN_RX_FILTER_5 = 0x04, _CAN_RX_FILTER_6 = 0x05, _CAN_RX_OVERFLOW = 0x08, // Set if Overflowed else cleared _CAN_RX_INVALID_MSG = 0x10, // Set if invalid else cleared _CAN_RX_XTD_FRAME = 0x20, // Set if XTD message else cleared _CAN_RX_RTR_FRAME = 0x40, // Set if RTR message else cleared _CAN_RX_DBL_BUFFERED = 0x80; // Set if this message was hardware double-buffered
You may use bitwise AND (&
) to adjust the appropriate flags. For example:
if (MsgFlag & _CAN_RX_OVERFLOW != 0) { ... // Receiver overflow has occurred. // We have lost our previous message. }
CAN_MASK Constants
CAN_MASK
constants define mask codes. Function CANxSetMask expects one of these as its argument:
const unsigned int _CAN_MASK_B1 = 0, _CAN_MASK_B2 = 1;
CAN_FILTER Constants
CAN_FILTER
constants define filter codes. Function CANxSetFilter expects one of these as its argument:
const unsigned int _CAN_FILTER_B1_F1 = 0, _CAN_FILTER_B1_F2 = 1, _CAN_FILTER_B2_F1 = 2, _CAN_FILTER_B2_F2 = 3, _CAN_FILTER_B2_F3 = 4, _CAN_FILTER_B2_F4 = 5;
Library Example
The example demonstrates CAN protocol. The 1st node initiates the communication with the 2nd node by sending some data to its address. The 2nd node responds by sending back the data incremented by 1. The 1st node then does the same and sends incremented data back to the 2nd node, etc.
Code for the first CAN node:
unsigned int Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags; // can flags unsigned int Rx_Data_Len; // received data length in bytes char RxTx_Data[8]; // can rx/tx data buffer char Msg_Rcvd; // reception flag unsigned long Tx_ID, Rx_ID; // can rx and tx ID void main() { ADPCFG = 0xFFFF; PORTB = 0; TRISB = 0; Can_Init_Flags = 0; // Can_Send_Flags = 0; // clear flags Can_Rcv_Flags = 0; // Can_Send_Flags = _CAN_TX_PRIORITY_0 & // Form value to be used _CAN_TX_XTD_FRAME & // with CAN2Write _CAN_TX_NO_RTR_FRAME; Can_Init_Flags = _CAN_CONFIG_SAMPLE_THRICE & // Form value to be used _CAN_CONFIG_PHSEG2_PRG_ON & // with CAN2Initialize _CAN_CONFIG_XTD_MSG & _CAN_CONFIG_DBL_BUFFER_ON & _CAN_CONFIG_MATCH_MSG_TYPE & _CAN_CONFIG_LINE_FILTER_OFF; RxTx_Data[0] = 9; // set initial data to be sent CAN2Initialize(1,3,3,3,1,Can_Init_Flags); // initialize CAN2 CAN2SetOperationMode(_CAN_MODE_CONFIG,0xFF); // set CONFIGURATION mode CAN2SetMask(_CAN_MASK_B1,-1,_CAN_CONFIG_MATCH_MSG_TYPE & _CAN_CONFIG_XTD_MSG); // set all mask1 bits to ones CAN2SetMask(_CAN_MASK_B2,-1,_CAN_CONFIG_MATCH_MSG_TYPE & _CAN_CONFIG_XTD_MSG); // set all mask2 bits to ones CAN2SetFilter(_CAN_FILTER_B2_F3,3,_CAN_CONFIG_XTD_MSG); // set id of filter B1_F1 to 3 CAN2SetOperationMode(_CAN_MODE_NORMAL,0xFF); // set NORMAL mode Tx_ID = 12111; // set transmit ID CAN2Write(Tx_ID, RxTx_Data, 1, Can_Send_Flags); // send initial message while(1) { // endless loop Msg_Rcvd = CAN2Read(&Rx_ID , RxTx_Data , &Rx_Data_Len, &Can_Rcv_Flags); // receive message if ((Rx_ID == 3u) && Msg_Rcvd) { // if message received check id PORTB = RxTx_Data[0]; // id correct, output data at PORTB RxTx_Data[0]++; // increment received data Delay_ms(10); CAN2Write(Tx_ID, RxTx_Data, 1, Can_Send_Flags); // send incremented data back } } }
Code for the second CAN node:
unsigned int Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags; // can flags unsigned int Rx_Data_Len; // received data length in bytes char RxTx_Data[8]; // can rx/tx data buffer char Msg_Rcvd; // reception flag unsigned long Tx_ID, Rx_ID; // can rx and tx ID void main() { ADPCFG = 0xFFFF; PORTB = 0; TRISB = 0; Can_Init_Flags = 0; // Can_Send_Flags = 0; // clear flags Can_Rcv_Flags = 0; // Can_Send_Flags = _CAN_TX_PRIORITY_0 & // Form value to be used _CAN_TX_XTD_FRAME & // with CAN2Write _CAN_TX_NO_RTR_FRAME; Can_Init_Flags = _CAN_CONFIG_SAMPLE_THRICE & // Form value to be used _CAN_CONFIG_PHSEG2_PRG_ON & // with CAN2Initialize _CAN_CONFIG_XTD_MSG & _CAN_CONFIG_DBL_BUFFER_ON & _CAN_CONFIG_MATCH_MSG_TYPE & _CAN_CONFIG_LINE_FILTER_OFF; CAN2Initialize(1,3,3,3,1,Can_Init_Flags); // initialize CAN2 CAN2SetOperationMode(_CAN_MODE_CONFIG,0xFF); // set CONFIGURATION mode CAN2SetMask(_CAN_MASK_B1,-1,_CAN_CONFIG_MATCH_MSG_TYPE & _CAN_CONFIG_XTD_MSG); // set all mask1 bits to ones CAN2SetMask(_CAN_MASK_B2,-1,_CAN_CONFIG_MATCH_MSG_TYPE & _CAN_CONFIG_XTD_MSG); // set all mask2 bits to ones CAN2SetFilter(_CAN_FILTER_B1_F1,12111,_CAN_CONFIG_XTD_MSG); // set id of filter B1_F1 to 12111 CAN2SetOperationMode(_CAN_MODE_NORMAL,0xFF); // set NORMAL mode Tx_ID = 3; // set tx ID while(1) { // endless loop Msg_Rcvd = CAN2Read(&Rx_ID , RxTx_Data , &Rx_Data_Len, &Can_Rcv_Flags); // receive message if ((Rx_ID == 12111u) && Msg_Rcvd) { // if message received check id PORTB = RxTx_Data[0]; // id correct, output data at PORTB RxTx_Data[0]++; // increment received data CAN2Write(Tx_ID, RxTx_Data, 1, Can_Send_Flags); // send incremented data back } } }
HW Connection
Example of interfacing CAN transceiver with MCU and CAN bus
What do you think about this topic ? Send us feedback!