CAN Library
mikroBasic 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 |
sub procedure CANxSetOperationMode(dim mode, WAIT as word) |
---|---|
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 |
sub function CANxGetOperationMode() as word |
---|---|
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) then ... end if |
Notes |
|
CANxInitialize
Prototype |
sub procedure CANxInitialize(dim SJW, BRP, PHSEG1, PHSEG2, PROPSEG, CAN_CONFIG_FLAGS as word) |
---|---|
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 dim can_config_flags as word ... can_config_flags = _CAN_CONFIG_SAMPLE_THRICE and ' Form value to be used _CAN_CONFIG_PHSEG2_PRG_ON and ' with CAN1Initialize _CAN_CONFIG_STD_MSG and _CAN_CONFIG_DBL_BUFFER_ON and _CAN_CONFIG_MATCH_MSG_TYPE and _CAN_CONFIG_LINE_FILTER_OFF CAN1Initialize(1,3,3,3,1,can_config_flags) ' initialize the CAN1 module |
Notes |
|
CANxSetBaudRate
Prototype |
sub procedure CANxSetBaudRate(dim SJW, BRP, PHSEG1, PHSEG2, PROPSEG, CAN_CONFIG_FLAGS as word) |
---|---|
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 dim can_config_flags as word ... 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 and ' Form value to be used _CAN_CONFIG_PHSEG2_PRG_ON and ' with CAN1Initialize _CAN_CONFIG_STD_MSG and _CAN_CONFIG_DBL_BUFFER_ON and _CAN_CONFIG_MATCH_MSG_TYPE and _CAN_CONFIG_LINE_FILTER_OFF CAN1SetBaudRate(1,3,3,3,1,can_config_flags) ' set the CAN1 module baud rate |
Notes |
|
CANxSetMask
Prototype |
sub procedure CANxSetMask(dim CAN_MASK as word, dim val as longint, dim CAN_CONFIG_FLAGS as word) |
---|---|
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 and _CAN_CONFIG_XTD_MSG) |
Notes |
|
CANxSetFilter
Prototype |
sub procedure CANxSetFilter(dim CAN_FILTER as word, dim val as longint, dim CAN_CONFIG_FLAGS as word) |
---|---|
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 |
sub function CANxRead(dim byref id as longint, dim byref data as byte[1], dim dataLen, CAN_RX_MSG_FLAGS as word) as word |
---|---|
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. dim msg_rcvd, rx_flags, data_len as word data as byte[8] msg_id as longint ... 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)<>0) then ... end if |
Notes |
|
CANxWrite
Prototype |
sub function CANxWrite(dim id as longint, dim byref data_ as byte[1], dim dataLen, CAN_TX_MSG_FLAGS as word) as word |
---|---|
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 dim tx_flags as word data as byte[8] msg_id as longint ... CAN1SetOperationMode(_CAN_MODE_NORMAL,0xFF) ' set NORMAL mode (CAN1 must be in mode in which transmission is possible) tx_flags = _CAN_TX_PRIORITY_0 and _CAN_TX_XTD_FRAME and _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 _CAN_MODE_BITS as word = $E0 ' Use this to access opmode bits _CAN_MODE_NORMAL as word = 0x00 _CAN_MODE_DISABLE as word = 0x01 _CAN_MODE_LOOP as word = 0x02 _CAN_MODE_LISTEN as word = 0x03 _CAN_MODE_CONFIG as word = 0x04 _CAN_MODE_LISTEN_ALL as word = 0x07
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 _CAN_CONFIG_DEFAULT as word = 0xFF ' 11111111 _CAN_CONFIG_PHSEG2_PRG_BIT as word = 0x01 _CAN_CONFIG_PHSEG2_PRG_ON as word = 0xFF ' XXXXXXX1 _CAN_CONFIG_PHSEG2_PRG_OFF as word = 0xFE ' XXXXXXX0 _CAN_CONFIG_LINE_FILTER_BIT as word = 0x02 _CAN_CONFIG_LINE_FILTER_ON as word = 0xFF ' XXXXXX1X _CAN_CONFIG_LINE_FILTER_OFF as word = 0xFD ' XXXXXX0X _CAN_CONFIG_SAMPLE_BIT as word = 0x04 _CAN_CONFIG_SAMPLE_ONCE as word = 0xFF ' XXXXX1XX _CAN_CONFIG_SAMPLE_THRICE as word = 0xFB ' XXXXX0XX _CAN_CONFIG_MSG_TYPE_BIT as word = 0x08 _CAN_CONFIG_STD_MSG as word = 0xFF ' XXXX1XXX _CAN_CONFIG_XTD_MSG as word = 0xF7 ' XXXX0XXX _CAN_CONFIG_DBL_BUFFER_BIT as word = 0x10 _CAN_CONFIG_DBL_BUFFER_ON as word = 0xFF ' XXX1XXXX _CAN_CONFIG_DBL_BUFFER_OFF as word = 0xEF ' XXX0XXXX _CAN_CONFIG_MATCH_TYPE_BIT as word = 0x20 _CAN_CONFIG_ALL_VALID_MSG as word = 0xDF ' XX0XXXXX _CAN_CONFIG_MATCH_MSG_TYPE as word = 0xFF ' XX1XXXXX
You may use bitwise and
to form config byte out of these values. For example:
init = _CAN_CONFIG_SAMPLE_THRICE and _CAN_CONFIG_PHSEG2_PRG_ON and _CAN_CONFIG_STD_MSG and _CAN_CONFIG_DBL_BUFFER_ON and _CAN_CONFIG_VALID_XTD_MSG and _CAN_CONFIG_LINE_FILTER_OFF ... CAN1Initialize(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 _CAN_TX_PRIORITY_BITS as word = 0x03 _CAN_TX_PRIORITY_0 as word = 0xFC ' XXXXXX00 _CAN_TX_PRIORITY_1 as word = 0xFD ' XXXXXX01 _CAN_TX_PRIORITY_2 as word = 0xFE ' XXXXXX10 _CAN_TX_PRIORITY_3 as word = 0xFF ' XXXXXX11 _CAN_TX_FRAME_BIT as word = 0x08 _CAN_TX_STD_FRAME as word = 0xFF ' XXXXX1XX _CAN_TX_XTD_FRAME as word = 0xF7 ' XXXXX0XX _CAN_TX_RTR_BIT as word = 0x40 _CAN_TX_NO_RTR_FRAME as word = 0xFF ' X1XXXXXX _CAN_TX_RTR_FRAME as word = 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 and _CAN_TX_XTD_FRAME and _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 _CAN_RX_FILTER_BITS as word = 0x0007 ' Use this to access filter bits _CAN_RX_FILTER_1 as word = 0x00 _CAN_RX_FILTER_2 as word = 0x01 _CAN_RX_FILTER_3 as word = 0x02 _CAN_RX_FILTER_4 as word = 0x03 _CAN_RX_FILTER_5 as word = 0x04 _CAN_RX_FILTER_6 as word = 0x05 _CAN_RX_OVERFLOW as word = 0x08 ' Set if Overflowed else cleared _CAN_RX_INVALID_MSG as word = 0x10 ' Set if invalid else cleared _CAN_RX_XTD_FRAME as word = 0x20 ' Set if XTD message else cleared _CAN_RX_RTR_FRAME as word = 0x40 ' Set if RTR message else cleared _CAN_RX_DBL_BUFFERED as word = 0x80 ' Set if this message was hardware double-buffered
You may use bitwise and
to adjust the appropriate flags. For example:
if (MsgFlag and _CAN_RX_OVERFLOW) <> 0 then ... ' Receiver overflow has occurred. ' We have lost our previous message. end if
CAN_MASK Constants
CAN_MASK
constants define mask codes. Function CANxSetMask expects one of these as its argument:
const _CAN_MASK_B1 as word = 0 _CAN_MASK_B2 as word = 1
CAN_FILTER Constants
CAN_FILTER
constants define filter codes. Function CANxSetFilter expects one of these as its argument:
const _CAN_FILTER_B1_F1 as word = 0 _CAN_FILTER_B1_F2 as word = 1 _CAN_FILTER_B2_F1 as word = 2 _CAN_FILTER_B2_F2 as word = 3 _CAN_FILTER_B2_F3 as word = 4 _CAN_FILTER_B2_F4 as word = 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:
program CAN_1st dim Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags, Rx_Data_Len as word RxTx_Data as byte[8] Rx_ID as longint Msg_Rcvd as word const ID_1st as longint = 12111 const ID_2nd as longint = 3 ' node IDs main: ADPCFG = 0xFFFF PORTB = 0 TRISB = 0 Can_Init_Flags = 0 Can_Send_Flags = 0 Can_Rcv_Flags = 0 Can_Send_Flags = _CAN_TX_PRIORITY_0 and ' form value to be used _CAN_TX_XTD_FRAME and ' with CANSendMessage _CAN_TX_NO_RTR_FRAME Can_Init_Flags = _CAN_CONFIG_SAMPLE_THRICE and ' form value to be used _CAN_CONFIG_PHSEG2_PRG_ON and ' with CANInitialize _CAN_CONFIG_XTD_MSG and _CAN_CONFIG_DBL_BUFFER_ON and _CAN_CONFIG_MATCH_MSG_TYPE and _CAN_CONFIG_LINE_FILTER_OFF RxTx_Data[0] = 9 CAN1Initialize(1,3,3,3,1,Can_Init_Flags) ' initialize CAN CAN1SetOperationMode(_CAN_MODE_CONFIG,0xFF) ' set CONFIGURATION mode CAN1SetMask(_CAN_MASK_B1, -1, _CAN_CONFIG_MATCH_MSG_TYPE and _CAN_CONFIG_XTD_MSG) ' set all mask1 bits to ones CAN1SetMask(_CAN_MASK_B2, -1, _CAN_CONFIG_MATCH_MSG_TYPE and _CAN_CONFIG_XTD_MSG) ' set all mask2 bits to ones CAN1SetFilter(_CAN_FILTER_B2_F3,ID_2nd,_CAN_CONFIG_XTD_MSG) ' set id of filter B2_F3 to 2nd node ID CAN1SetOperationMode(_CAN_MODE_NORMAL,0xFF) ' set NORMAL mode CAN1Write(ID_1st, RxTx_Data, 1, Can_Send_Flags) while TRUE Msg_Rcvd = CAN1Read(Rx_ID , RxTx_Data , Rx_Data_Len, Can_Rcv_Flags) if ((Rx_ID = ID_2nd) and (Msg_Rcvd <> 0)) <> 0 then PORTB = RxTx_Data[0] ' output data at PORTB RxTx_Data[0] = RxTx_Data[0] + 1 Delay_ms(10) CAN1Write(ID_1st, RxTx_Data, 1, Can_Send_Flags) ' send incremented data back end if wend end.
Code for the second CAN node:
program Can_2nd dim Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags, Rx_Data_Len as word RxTx_Data as byte[8] Rx_ID as longint Msg_Rcvd as word const ID_1st as longint = 12111 const ID_2nd as longint = 3 ' node IDs main: ADPCFG = 0xFFFF PORTB = 0 TRISB = 0 Can_Init_Flags = 0 Can_Send_Flags = 0 Can_Rcv_Flags = 0 Can_Send_Flags = _CAN_TX_PRIORITY_0 and ' form value to be used _CAN_TX_XTD_FRAME and ' with CANSendMessage _CAN_TX_NO_RTR_FRAME Can_Init_Flags = _CAN_CONFIG_SAMPLE_THRICE and ' form value to be used _CAN_CONFIG_PHSEG2_PRG_ON and ' with CANInitialize _CAN_CONFIG_XTD_MSG and _CAN_CONFIG_DBL_BUFFER_ON and _CAN_CONFIG_MATCH_MSG_TYPE and _CAN_CONFIG_LINE_FILTER_OFF CAN1Initialize(1,3,3,3,1,Can_Init_Flags) ' initialize CAN CAN1SetOperationMode(_CAN_MODE_CONFIG,0xFF) ' set CONFIGURATION mode CAN1SetMask(_CAN_MASK_B1, -1, _CAN_CONFIG_MATCH_MSG_TYPE and _CAN_CONFIG_XTD_MSG) ' set all mask1 bits to ones CAN1SetMask(_CAN_MASK_B2, -1, _CAN_CONFIG_MATCH_MSG_TYPE and _CAN_CONFIG_XTD_MSG) ' set all mask2 bits to ones CAN1SetFilter(_CAN_FILTER_B1_F1,ID_1st,_CAN_CONFIG_XTD_MSG) ' set id of filter_B1_F1 to 1st node ID CAN1SetOperationMode(_CAN_MODE_NORMAL,0xFF) ' set NORMAL mode while TRUE Msg_Rcvd = CAN1Read(Rx_ID , RxTx_Data , Rx_Data_Len, Can_Rcv_Flags) if ((Rx_ID = ID_1st) and (Msg_Rcvd <> 0)) <> 0 then PORTB = RxTx_Data[0] ' output data at portB RxTx_Data[0] = RxTx_Data[0] + 1 CAN1Write(ID_2nd, RxTx_Data, 1, Can_Send_Flags) ' send incremented data back end if wend end.
HW Connection
Example of interfacing CAN transceiver with MCU and CAN bus
What do you think about this topic ? Send us feedback!