CAN Library
mikroBasic PRO for PIC32 provides a library (driver) for working with the PIC32 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
- CANxAssignBuffer
- CANxConfigureFIFO
- CANxSetBaudRate
- CANxSetMask
- CANxSetFilter
- CANxFilterEnable
- CANxFilterDisable
- CANxReadBuffer
- CANxRead
- CANxWriteBuffer
- 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 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_Init_Flags as word ... Can_Init_Flags = _CAN_CONFIG_SAMPLE_THRICE and ' Form value to be used _CAN_CONFIG_PHSEG2_PRG_ON and ' with CAN1Initialize _CAN_CONFIG_XTD_MSG and _CAN_CONFIG_MATCH_MSG_TYPE and _CAN_CONFIG_LINE_FILTER_OFF CAN1Initialize(1,3,3,3,1,Can_Init_Flags) ' initialize the CAN1 module |
Notes |
|
CANxAssignBuffer
Prototype |
sub procedure CANxAssignBuffer(dim buffer as ^byte) |
---|---|
Description |
Assigns FIFO buffer for the CAN module . |
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 |
dim Buffers as byte[2*8*16] absolute 0xA0000000 CAN1AssignBuffer(@Buffers) |
Notes |
|
CANxConfigureFIFO
Prototype |
sub procedure CANxConfigureFIFO(dim num as byte, dim size as byte, dim flags as word) |
---|---|
Description |
Configures CAN FIFO buffers. |
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 |
' configure RX FIFO buffer CAN1ConfigureFIFO(_CAN_BUFFER_0, 8, _CAN_FIFO_RX and _CAN_FULL_MESSAGE) ' configure TX FIFO buffer CAN1ConfigureFIFO(_CAN_BUFFER_1, 8, _CAN_FIFO_TX and _CAN_TX_NO_RTR_FRAME and _CAN_TX_PRIORITY_3) |
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 CAN1SetBaudRate _CAN_CONFIG_STD_MSG 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 |
The function configures appropriate mask for advanced message filtering. |
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) CAN1SetMask(_CAN_MASK_0, -1, _CAN_CONFIG_MATCH_MSG_TYPE and _CAN_CONFIG_XTD_MSG) ' set all mask1 bits to ones |
Notes |
|
CANxFilterEnable
Prototype |
sub procedure CANxFilterEnable(dim CAN_FILTER as byte) |
---|---|
Description |
The function enables appropriate receive message filters. |
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 |
' Filters 0, 4, 8, 12 are to be enabled: CAN1FilterEnable(0x1111) |
Notes |
|
CANxFilterDisable
Prototype |
sub procedure CANxFilterDisable(dim CAN_FILTER as byte) |
---|---|
Description |
The function disables appropriate receive message filters. |
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 |
' Filters 0, 4, 8, 12 are to be disabled: CAN1FilterDisable(0x1111) |
Notes |
|
CANxSetFilter
Prototype |
sub procedure CANxSetFilter(dim CAN_FILTER as word, dim val as longword, dim CAN_FILTER_MASK, CAN_FILTER_RXBUFF, 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) CAN1SetFilter(_CAN_FILTER_31, ID_1st, _CAN_MASK_3, _CAN_BUFFER_0, _CAN_CONFIG_XTD_MSG); ' set id of filter_B1_F1 to 1st node ID |
Notes |
|
CANxReadBuffer
Prototype |
sub function CANxReadBuffer(dim byref id as longword, dim byref data_ as byte[8], dim buffer as byte, dim byref dataLen as word, dim byref CAN_RX_MSG_FLAGS as word) as word |
---|---|
Description |
If a 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 |
if (msg_rcvd = CAN1ReadBuffer(msg_id, data, _CAN_BUFFER_0, data_len, rx_flags)<>0) then ... end if |
Notes |
|
CANxRead
Prototype |
sub function CANxRead(dim byref id as longword, dim byref Data_ as byte[8], 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 |
|
CANxWriteBuffer
Prototype |
sub function CANxWriteBuffer(dim id as longword, dim byref Data_ as byte[8], dim buffer as byte, dim DataLen, CAN_TX_MSG_FLAGS as word) as word |
---|---|
Description |
If a 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 CAN1WriteBuffer(msg_id, data, _CAN_BUFFER_0, 1, tx_flags) |
Notes |
|
CANxWrite
Prototype |
sub function CANxWrite(dim id as longword, dim byref Data_ as byte[8], 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_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_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 = 0x0003 _CAN_TX_PRIORITY_0 as word = 0xFFFC ' XXXXXX00 _CAN_TX_PRIORITY_1 as word = 0xFFFD ' XXXXXX01 _CAN_TX_PRIORITY_2 as word = 0xFFFE ' XXXXXX10 _CAN_TX_PRIORITY_3 as word = 0xFFFF ' XXXXXX11 _CAN_TX_FRAME_BIT as word = 0x0008 _CAN_TX_STD_FRAME as word = 0xFFFF ' XXXXX1XX _CAN_TX_XTD_FRAME as word = 0xFFF7 ' XXXXX0XX _CAN_TX_RTR_BIT as word = 0x0040 _CAN_TX_NO_RTR_FRAME as word = 0xFFFF ' X1XXXXXX _CAN_TX_RTR_FRAME as word = 0xFFBF ' 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 = 0x001F ' Use this to access filter bits _CAN_RX_OVERFLOW as word = 0x0020 ' Set if Overflowed else cleared _CAN_RX_INVALID_MSG as word = 0x0040 ' Set if invalid else cleared _CAN_RX_XTD_FRAME as word = 0x0080 ' Set if XTD message else cleared _CAN_RX_RTR_FRAME as word = 0x0100 ' Set if RTR message else cleared _CAN_RX_DATA_ONLY as word = 0x0200 ' 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_BUFFER Constants
CAN_BUFFER
constants define CAN buffers. Function CANxAssignBuffer expects one of these as its argument:
const _CAN_BUFFER_0 as word = 0 _CAN_BUFFER_1 as word = 1 _CAN_BUFFER_2 as word = 2 _CAN_BUFFER_3 as word = 3 _CAN_BUFFER_4 as word = 4 _CAN_BUFFER_5 as word = 5 _CAN_BUFFER_6 as word = 5 _CAN_BUFFER_7 as word = 7 _CAN_BUFFER_8 as word = 8 _CAN_BUFFER_9 as word = 9 _CAN_BUFFER_10 as word = 10 _CAN_BUFFER_11 as word = 11 _CAN_BUFFER_12 as word = 12 _CAN_BUFFER_13 as word = 13 _CAN_BUFFER_14 as word = 14 _CAN_BUFFER_15 as word = 15 _CAN_BUFFER_16 as word = 16 _CAN_BUFFER_17 as word = 17 _CAN_BUFFER_18 as word = 18 _CAN_BUFFER_19 as word = 19 _CAN_BUFFER_20 as word = 20 _CAN_BUFFER_21 as word = 21 _CAN_BUFFER_22 as word = 22 _CAN_BUFFER_23 as word = 23 _CAN_BUFFER_24 as word = 24 _CAN_BUFFER_25 as word = 25 _CAN_BUFFER_26 as word = 26 _CAN_BUFFER_27 as word = 27 _CAN_BUFFER_28 as word = 28 _CAN_BUFFER_29 as word = 29 _CAN_BUFFER_30 as word = 30 _CAN_BUFFER_31 as word = 31
CAN_FIFO Constants
CAN_FIFO
constants define FIFO constants. Function CANxConfigureFIFO expects one of these as its argument:
const _CAN_FIFO_TXEN_BIT as word = 0x0080 _CAN_FIFO_TX as word = 0xFF7F _CAN_FIFO_RX as word = 0xFFFF _CAN_FIFO_DONLY_BIT as word = 0x1000 _CAN_DATA_ONLY as word = 0xEFFF _CAN_FULL_MESSAGE as word = 0xFFFF
CAN_MASK Constants
CAN_MASK
constants define mask codes. Function CANxSetMask expects one of these as its argument:
const _CAN_MASK_B0 as word = 0 _CAN_MASK_B1 as word = 1 _CAN_MASK_B2 as word = 2 _CAN_MASK_B3 as word = 3
CAN_FILTER Constants
CAN_FILTER
constants define filter codes. Function CANxSetFilter expects one of these as its argument:
const _CAN_FILTER_0 as word = 0 _CAN_FILTER_1 as word = 1 _CAN_FILTER_2 as word = 2 _CAN_FILTER_3 as word = 3 _CAN_FILTER_4 as word = 4 _CAN_FILTER_5 as word = 5 _CAN_FILTER_6 as word = 5 _CAN_FILTER_7 as word = 7 _CAN_FILTER_8 as word = 8 _CAN_FILTER_9 as word = 9 _CAN_FILTER_10 as word = 10 _CAN_FILTER_11 as word = 11 _CAN_FILTER_12 as word = 12 _CAN_FILTER_13 as word = 13 _CAN_FILTER_14 as word = 14 _CAN_FILTER_15 as word = 15 _CAN_FILTER_16 as word = 16 _CAN_FILTER_17 as word = 17 _CAN_FILTER_18 as word = 18 _CAN_FILTER_19 as word = 19 _CAN_FILTER_20 as word = 20 _CAN_FILTER_21 as word = 21 _CAN_FILTER_22 as word = 22 _CAN_FILTER_23 as word = 23 _CAN_FILTER_24 as word = 24 _CAN_FILTER_25 as word = 25 _CAN_FILTER_26 as word = 26 _CAN_FILTER_27 as word = 27 _CAN_FILTER_28 as word = 28 _CAN_FILTER_29 as word = 29 _CAN_FILTER_30 as word = 30 _CAN_FILTER_31 as word = 31
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
dim Buffers as byte[2*8*16] absolute 0xA0000000
const ID_1st as longint = 12111
const ID_2nd as longint = 3 ' node IDs
main:
AD1PCFG = 0xFFFF
PORTB = 0
TRISB = 0
Can_Init_Flags = 0
Can_Send_Flags = 0
Can_Rcv_Flags = 0
Can_Send_Flags =
_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_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
CAN1AssignBuffer(@Buffers)
CAN1ConfigureFIFO(_CAN_BUFFER_0, 8, _CAN_FIFO_RX and _CAN_FULL_MESSAGE)
CAN1ConfigureFIFO(_CAN_BUFFER_1, 8, _CAN_FIFO_TX and _CAN_TX_NO_RTR_FRAME and _CAN_TX_PRIORITY_3)
CAN1SetMask(_CAN_MASK_0, -1, _CAN_CONFIG_MATCH_MSG_TYPE and _CAN_CONFIG_XTD_MSG) ' set all mask1 bits to ones
CAN1SetFilter(_CAN_FILTER_0, ID_2nd, _CAN_MASK_0, _CAN_BUFFER_0, _CAN_CONFIG_XTD_MSG) ' set id of filter_B1_F1 to 1st node ID
CAN1SetOperationMode(_CAN_MODE_NORMAL,0xFF) ' set NORMAL mode
RxTx_Data[0] = 9
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
LATB = 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
dim Buffers as byte[2*8*16] absolute 0xA0000000
const ID_1st as longint = 12111
const ID_2nd as longint = 3 ' node IDs
main:
AD1PCFG = 0xFFFF
PORTB = 0
TRISB = 0
Can_Init_Flags = 0
Can_Send_Flags = 0
Can_Rcv_Flags = 0
Can_Send_Flags =
_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_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
CAN1AssignBuffer(@Buffers)
CAN1ConfigureFIFO(_CAN_BUFFER_0, 8, _CAN_FIFO_RX and _CAN_FULL_MESSAGE)
CAN1ConfigureFIFO(_CAN_BUFFER_1, 8, _CAN_FIFO_TX and _CAN_TX_NO_RTR_FRAME and _CAN_TX_PRIORITY_3)
CAN1SetMask(_CAN_MASK_3, -1, _CAN_CONFIG_MATCH_MSG_TYPE and _CAN_CONFIG_XTD_MSG) ' set all mask1 bits to ones
CAN1SetFilter(_CAN_FILTER_31, ID_1st, _CAN_MASK_3, _CAN_BUFFER_0, _CAN_CONFIG_XTD_MSG) ' set id of filter_B1_F1 to 1st node ID
CAN1SetOperationMode(_CAN_MODE_NORMAL,0xFF) ' set NORMAL mode
TRISD = 0
while TRUE
Msg_Rcvd = CAN1Read(Rx_ID , RxTx_Data , Rx_Data_Len, Can_Rcv_Flags)
LATD = Can_Rcv_Flags
if ((Rx_ID = ID_1st) and (Msg_Rcvd <> 0)) <> 0 then
LATB = 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!