CAN Library
mikroPascal 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 |
procedure CANxSetOperationMode(mode, WAIT : 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 |
function CANxGetOperationMode(): 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) { ... } |
Notes |
|
CANxInitialize
Prototype |
procedure CANxInitialize(SJW, BRP, PHSEG1, PHSEG2, PROPSEG, CAN_CONFIG_FLAGS : 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 var can_config_flags : word; ... 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 |
procedure CANxSetBaudRate(SJW, BRP, PHSEG1, PHSEG2, PROPSEG, CAN_CONFIG_FLAGS : 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 var can_config_flags : 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 & // 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 |
procedure CANxSetMask(CAN_MASK : word; val : longint; CAN_CONFIG_FLAGS : 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 & _CAN_CONFIG_XTD_MSG); |
Notes |
|
CANxSetFilter
Prototype |
procedure CANxSetFilter(CAN_FILTER : word; val : longint; CAN_CONFIG_FLAGS : 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 |
function CANxRead(var id : longint; var data : array[1] of byte; dataLen, CAN_RX_MSG_FLAGS : word) : 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. var msg_rcvd, rx_flags, data_len : word; data : array[8] of byte; msg_id : 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)) then begin ... end; |
Notes |
|
CANxWrite
Prototype |
function CANxWrite(id : longint; var data_ : array[1] of byte; dataLen, CAN_TX_MSG_FLAGS : word) : 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 var tx_flags: word; data: array[8] of byte; msg_id : 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 : word = $E0; // Use this to access opmode bits _CAN_MODE_NORMAL : word = 0x01; _CAN_MODE_SLEEP : word = 0x02; _CAN_MODE_LOOP : word = 0x03; _CAN_MODE_LISTEN : word = 0x04; _CAN_MODE_CONFIG : 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 : word = 0xFF; // 11111111 _CAN_CONFIG_PHSEG2_PRG_BIT : word = 0x01; _CAN_CONFIG_PHSEG2_PRG_ON : word = 0xFF; // XXXXXXX1 _CAN_CONFIG_PHSEG2_PRG_OFF : word = 0xFE; // XXXXXXX0 _CAN_CONFIG_LINE_FILTER_BIT : word = 0x02; _CAN_CONFIG_LINE_FILTER_ON : word = 0xFF; // XXXXXX1X _CAN_CONFIG_LINE_FILTER_OFF : word = 0xFD; // XXXXXX0X _CAN_CONFIG_SAMPLE_BIT : word = 0x04; _CAN_CONFIG_SAMPLE_ONCE : word = 0xFF; // XXXXX1XX _CAN_CONFIG_SAMPLE_THRICE : word = 0xFB; // XXXXX0XX _CAN_CONFIG_MSG_TYPE_BIT : word = 0x08; _CAN_CONFIG_STD_MSG : word = 0xFF; // XXXX1XXX _CAN_CONFIG_XTD_MSG : word = 0xF7; // XXXX0XXX _CAN_CONFIG_DBL_BUFFER_BIT : word = 0x10; _CAN_CONFIG_DBL_BUFFER_ON : word = 0xFF; // XXX1XXXX _CAN_CONFIG_DBL_BUFFER_OFF : word = 0xEF; // XXX0XXXX _CAN_CONFIG_MATCH_TYPE_BIT : word = 0x20; _CAN_CONFIG_ALL_VALID_MSG : word = 0xDF; // XX0XXXXX _CAN_CONFIG_MATCH_MSG_TYPE : 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 : word = 0x03; _CAN_TX_PRIORITY_0 : word = 0xFC; // XXXXXX00 _CAN_TX_PRIORITY_1 : word = 0xFD; // XXXXXX01 _CAN_TX_PRIORITY_2 : word = 0xFE; // XXXXXX10 _CAN_TX_PRIORITY_3 : word = 0xFF; // XXXXXX11 _CAN_TX_FRAME_BIT : word = 0x08; _CAN_TX_STD_FRAME : word = 0xFF; // XXXXX1XX _CAN_TX_XTD_FRAME : word = 0xF7; // XXXXX0XX _CAN_TX_RTR_BIT : word = 0x40; _CAN_TX_NO_RTR_FRAME : word = 0xFF; // X1XXXXXX _CAN_TX_RTR_FRAME : 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 : word = 0x07; // Use this to access filter bits _CAN_RX_FILTER_1 : word = 0x00; _CAN_RX_FILTER_2 : word = 0x01; _CAN_RX_FILTER_3 : word = 0x02; _CAN_RX_FILTER_4 : word = 0x03; _CAN_RX_FILTER_5 : word = 0x04; _CAN_RX_FILTER_6 : word = 0x05; _CAN_RX_OVERFLOW : word = 0x08; // Set if Overflowed else cleared _CAN_RX_INVALID_MSG : word = 0x10; // Set if invalid else cleared _CAN_RX_XTD_FRAME : word = 0x20; // Set if XTD message else cleared _CAN_RX_RTR_FRAME : word = 0x40; // Set if RTR message else cleared _CAN_RX_DBL_BUFFERED : 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 begin ... // Receiver overflow has occurred. // We have lost our previous message. end
CAN_MASK Constants
CAN_MASK
constants define mask codes. Function CANxSetMask expects one of these as its argument:
const _CAN_MASK_B1 : word = 0; _CAN_MASK_B2 : 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 : word = 0; _CAN_FILTER_B1_F2 : word = 1; _CAN_FILTER_B2_F1 : word = 2; _CAN_FILTER_B2_F2 : word = 3; _CAN_FILTER_B2_F3 : word = 4; _CAN_FILTER_B2_F4 : 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; var Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags, Rx_Data_Len : word; RxTx_Data : array[8] of byte; Rx_ID : longint; Msg_Rcvd : word; const ID_1st : longint = 12111; const ID_2nd : longint = 3; // node IDs begin 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 do begin Msg_Rcvd := CAN1Read(Rx_ID , RxTx_Data , Rx_Data_Len, Can_Rcv_Flags); if ((Rx_ID = ID_2nd) and (Msg_Rcvd <> 0)) <> 0 then begin 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; end; end.
Code for the second CAN node:
program Can_2nd; var Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags, Rx_Data_Len : word; RxTx_Data : array[8] of byte; Rx_ID : longint; Msg_Rcvd : word; const ID_1st : longint = 12111; const ID_2nd : longint = 3; // node IDs begin 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 do begin Msg_Rcvd := CAN1Read(Rx_ID , RxTx_Data , Rx_Data_Len, Can_Rcv_Flags); if ((Rx_ID = ID_1st) and (Msg_Rcvd <> 0)) <> 0 then begin 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; end; end.
HW Connection
Example of interfacing CAN transceiver with MCU and CAN bus
What do you think about this topic ? Send us feedback!