CAN Library
The mikroPascal 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 |
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 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_Init_Flags : 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 |
procedure CANxAssignBuffer(buffer : ^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 |
// reserve space for 2 buffers with 8 messages (each message is 16 bytes) // beggining of the buffer must be 32bit aligned var Buffers : array[2*8*16] of byte; absolute 0xA0000000; CAN1AssignBuffer(@Buffers); |
Notes |
|
CANxConfigureFIFO
Prototype |
procedure CANxConfigureFIFO(num : byte; size : byte; flags : 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); //RX buffer 8 messages deep // configure TX FIFO buffer CAN1ConfigureFIFO(_CAN_BUFFER_1, 8, _CAN_FIFO_TX and _CAN_TX_NO_RTR_FRAME and _CAN_TX_PRIORITY_3); //TX buffer 8 messages deep |
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_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 |
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 |
procedure CANxFilterEnable(CAN_FILTER : 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 |
procedure CANxFilterDisable(CAN_FILTER : 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 |
procedure CANxSetFilter(CAN_FILTER : word; val : dword; CAN_FILTER_MASK, CAN_FILTER_RXBUFF, 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) 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 |
function CAN1ReadBuffer(var id : dword; var Data_ : array[8] of byte; buffer : byte; var dataLen: word; var CAN_RX_MSG_FLAGS : word) : 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)) then begin ... end; |
Notes |
|
CANxRead
Prototype |
function CANxRead(var id : dword; var Data_ : array[8] of byte; var dataLen : word; var 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 |
|
CANxWriteBuffer
Prototype |
function CANxWriteBuffer(id : dword; var Data_ : array[8] of byte; buffer : byte; DataLen, CAN_TX_MSG_FLAGS : word) : 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 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 CAN1WriteBuffer(msg_id, data, _CAN_BUFFER_0, 1, tx_flags); |
Notes |
|
CANxWrite
Prototype |
function CANxWrite(id : dword; var Data_ : array[8] 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 = 0xE0; // Use this to access opmode bits
_CAN_MODE_NORMAL : word = 0x00;
_CAN_MODE_DISABLE : word = 0x01;
_CAN_MODE_LOOP : word = 0x02;
_CAN_MODE_LISTEN : word = 0x03;
_CAN_MODE_CONFIG : word = 0x04;
_CAN_MODE_LISTEN_ALL : 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_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 = 0x0003;
_CAN_TX_PRIORITY_0 : word = 0xFFFC; // XXXXXX00
_CAN_TX_PRIORITY_1 : word = 0xFFFD; // XXXXXX01
_CAN_TX_PRIORITY_2 : word = 0xFFFE; // XXXXXX10
_CAN_TX_PRIORITY_3 : word = 0xFFFF; // XXXXXX11
_CAN_TX_FRAME_BIT : word = 0x0008;
_CAN_TX_STD_FRAME : word = 0xFFFF; // XXXXX1XX
_CAN_TX_XTD_FRAME : word = 0xFFF7; // XXXXX0XX
_CAN_TX_RTR_BIT : word = 0x0040;
_CAN_TX_NO_RTR_FRAME : word = 0xFFFF; // X1XXXXXX
_CAN_TX_RTR_FRAME : 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 : word = 0x001F; // Use this to access filter bits
_CAN_RX_OVERFLOW : word = 0x0020; // Set if Overflowed else cleared
_CAN_RX_INVALID_MSG : word = 0x0040; // Set if invalid else cleared
_CAN_RX_XTD_FRAME : word = 0x0080; // Set if XTD message else cleared
_CAN_RX_RTR_FRAME : word = 0x0100; // Set if RTR message else cleared
_CAN_RX_DATA_ONLY : word = 0x0200; // Set if Data Only message
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_BUFFER Constants
CAN_BUFFER
constants define CAN buffers. Function CANxAssignBuffer expects one of these as its argument:
const
_CAN_BUFFER_0 : word = 0 ;
_CAN_BUFFER_1 : word = 1 ;
_CAN_BUFFER_2 : word = 2 ;
_CAN_BUFFER_3 : word = 3 ;
_CAN_BUFFER_4 : word = 4 ;
_CAN_BUFFER_5 : word = 5 ;
_CAN_BUFFER_6 : word = 6 ;
_CAN_BUFFER_7 : word = 7 ;
_CAN_BUFFER_8 : word = 8 ;
_CAN_BUFFER_9 : word = 9 ;
_CAN_BUFFER_10 : word = 10;
_CAN_BUFFER_11 : word = 11;
_CAN_BUFFER_12 : word = 12;
_CAN_BUFFER_13 : word = 13;
_CAN_BUFFER_14 : word = 14;
_CAN_BUFFER_15 : word = 15;
_CAN_BUFFER_16 : word = 16;
_CAN_BUFFER_17 : word = 17;
_CAN_BUFFER_18 : word = 18;
_CAN_BUFFER_19 : word = 19;
_CAN_BUFFER_20 : word = 20;
_CAN_BUFFER_21 : word = 21;
_CAN_BUFFER_22 : word = 22;
_CAN_BUFFER_23 : word = 23;
_CAN_BUFFER_24 : word = 24;
_CAN_BUFFER_25 : word = 25;
_CAN_BUFFER_26 : word = 26;
_CAN_BUFFER_27 : word = 27;
_CAN_BUFFER_28 : word = 28;
_CAN_BUFFER_29 : word = 29;
_CAN_BUFFER_30 : word = 30;
_CAN_BUFFER_31 : 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 : word = 0x0080;
_CAN_FIFO_TX : word = 0xFF7F;
_CAN_FIFO_RX : word = 0xFFFF;
_CAN_FIFO_DONLY_BIT : word = 0x1000;
_CAN_DATA_ONLY : word = 0xEFFF;
_CAN_FULL_MESSAGE : word = 0xFFFF;
CAN_MASK Constants
CAN_MASK
constants define mask codes. Function CANxSetMask expects one of these as its argument:
const
_CAN_MASK_0 : word = 0;
_CAN_MASK_1 : word = 1;
_CAN_MASK_2 : word = 2;
_CAN_MASK_3 : word = 3;
CAN_FILTER Constants
CAN_FILTER
constants define filter codes. Function CANxSetFilter expects one of these as its argument:
const
_CAN_FILTER_0 : word = 0;
_CAN_FILTER_1 : word = 1;
_CAN_FILTER_2 : word = 2;
_CAN_FILTER_3 : word = 3;
_CAN_FILTER_4 : word = 4;
_CAN_FILTER_5 : word = 5;
_CAN_FILTER_6 : word = 6;
_CAN_FILTER_7 : word = 7;
_CAN_FILTER_8 : word = 8;
_CAN_FILTER_9 : word = 9;
_CAN_FILTER_10 : word = 10;
_CAN_FILTER_11 : word = 11;
_CAN_FILTER_12 : word = 12;
_CAN_FILTER_13 : word = 13;
_CAN_FILTER_14 : word = 14;
_CAN_FILTER_15 : word = 15;
_CAN_FILTER_16 : word = 16;
_CAN_FILTER_17 : word = 17;
_CAN_FILTER_18 : word = 18;
_CAN_FILTER_19 : word = 19;
_CAN_FILTER_20 : word = 20;
_CAN_FILTER_21 : word = 21;
_CAN_FILTER_22 : word = 22;
_CAN_FILTER_23 : word = 23;
_CAN_FILTER_24 : word = 24;
_CAN_FILTER_25 : word = 25;
_CAN_FILTER_26 : word = 26;
_CAN_FILTER_27 : word = 27;
_CAN_FILTER_28 : word = 28;
_CAN_FILTER_29 : word = 29;
_CAN_FILTER_30 : word = 30;
_CAN_FILTER_31 : 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;
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;
var Buffers : array[2*8*16] of byte; absolute 0xA0000000;
const ID_1st : longint = 12111;
const ID_2nd : longint = 3; // node IDs
begin
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 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
var Buffers : array[2*8*16] of byte; absolute 0xA0000000;
begin
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 do
begin
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
begin
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;
end;
end.
HW Connection
Example of interfacing CAN transceiver with MCU and CAN bus
What do you think about this topic ? Send us feedback!