CAN Library

The mikroC 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:

  Important :

Library Routines

CANxSetOperationMode

Prototype

void CANxSetOperationMode(unsigned int mode, unsigned int WAIT);

Description

Sets the CAN module to requested mode.

Parameters
  • mode: CAN module operation mode. Valid values: CAN_OP_MODE constants. See CAN_OP_MODE constants.
  • WAIT: CAN mode switching verification request. If WAIT == 0, the call is non-blocking. The function does not verify if the CAN module is switched to requested mode or not. Caller must use CANxGetOperationMode to verify correct operation mode before performing mode specific operation. If WAIT != 0, the call is blocking – the function won’t “return” until the requested mode is set.
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
  • CAN library routine 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.

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
  • CAN library routine 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.

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 CAN module is set to :

  • Disable CAN capture
  • Continue CAN operation in Idle mode
  • Do not abort pending transmissions
  • Fcan clock : 4*Tcy (Fosc)
  • Baud rate is set according to given parameters
  • CAN mode is set to Normal
  • Filter and mask registers IDs are set to zero
  • Filter and mask message frame type is set according to CAN_CONFIG_FLAGS value

SAM, SEG2PHTS, WAKFIL and DBEN bits are set according to CAN_CONFIG_FLAGS value.

Parameters
  • SJW as defined in MCU's datasheet (CAN Module)
  • BRP as defined in MCU's datasheet (CAN Module)
  • PHSEG1 as defined in MCU's datasheet (CAN Module)
  • PHSEG2 as defined in MCU's datasheet (CAN Module)
  • PROPSEG as defined in MCU's datasheet (CAN Module)
  • CAN_CONFIG_FLAGS is formed from predefined constants. See CAN_CONFIG_FLAGS constants.
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_Init_Flags = _CAN_CONFIG_SAMPLE_THRICE &            // Form value to be used
                   _CAN_CONFIG_PHSEG2_PRG_ON &            // with CAN1Initialize
                   _CAN_CONFIG_XTD_MSG &
                   _CAN_CONFIG_MATCH_MSG_TYPE &
                   _CAN_CONFIG_LINE_FILTER_OFF;

CAN1Initialize(1,3,3,3,1,Can_Init_Flags);              // initialize the CAN1 module
Notes
  • CAN mode NORMAL will be set on exit.
  • CAN library routine 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.

CANxAssignBuffer

Prototype

void CANxAssignBuffer(char *buffer);

Description

Assigns FIFO buffer for the CAN module .

Parameters
  • buffer: pointer to a space in the RAM where the CAN message FIFO buffers will be stored.
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
char buffer[2*8*16] absolute 0xA0000000;
CAN1AssignBuffer(buffer);
Notes
  • CAN module can have up to 32 FIFO buffers, each having up to 32 messages. Transmit message size is 16 bytes long, while the receive message size may be 16 bytes (for full receive message mode) or 8 bytes for data-only message receive.
  • CAN library routine 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.

CANxConfigureFIFO

Prototype

void CANxConfigureFIFO(char num, char size, unsigned int flags);

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 & _CAN_FULL_MESSAGE); //RX buffer 8 messages deep
// configure TX FIFO buffer
CAN1ConfigureFIFO(_CAN_BUFFER_1, 8,_CAN_FIFO_TX & _CAN_TX_PRIORITY_3 & _CAN_TX_NO_RTR_FRAME); //TX buffer 8 messages deep
Notes
  • CAN mode NORMAL will be set on exit.
  • CAN library routine 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.

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.

SAM, SEG2PHTS and WAKFIL bits are set according to CAN_CONFIG_FLAGS value. Refer to datasheet for details.

Parameters
  • SJW as defined in MCU's datasheet (CAN Module)
  • BRP as defined in MCU's datasheet (CAN Module)
  • PHSEG1 as defined in MCU's datasheet (CAN Module)
  • PHSEG2 as defined in MCU's datasheet (CAN Module)
  • PROPSEG as defined in MCU's datasheet (CAN Module)
  • CAN_CONFIG_FLAGS is formed from predefined constants. See CAN_CONFIG_FLAGS constants.
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_MATCH_MSG_TYPE &
                   _CAN_CONFIG_LINE_FILTER_OFF;

CAN1SetBaudRate(1,3,3,3,1,can_config_flags);             // set the CAN1 module baud rate
Notes
  • CAN library routine 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.

CANxSetMask

Prototype

void CANxSetMask(unsigned int CAN_MASK, unsigned long val, unsigned int CAN_CONFIG_FLAGS);

Description

The function configures appropriate mask for advanced message filtering.

Parameters
  • CAN_MASK: CAN module mask number. Valid values: CAN_MASK constants. See CAN_MASK constants.
  • val: mask register value. This value is bit-adjusted to appropriate buffer mask registers.
  • CAN_CONFIG_FLAGS: selects type of message to filter. Valid values:
    • _CAN_CONFIG_ALL_VALID_MSG,
    • _CAN_CONFIG_MATCH_MSG_TYPE & _CAN_CONFIG_STD_MSG,
    • _CAN_CONFIG_MATCH_MSG_TYPE & _CAN_CONFIG_XTD_MSG.
    See CAN_CONFIG_FLAGS constants.
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 & _CAN_CONFIG_XTD_MSG);   // set all mask1 bits to ones
Notes
  • CAN library routine 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.

CANxFilterEnable

Prototype

void CANxFilterEnable(char CAN_FILTER);

Description

The function enables appropriate receive message filters.

Parameters
  • CAN_FILTER: filter enable selection parameter. Each bit corresponds to appropriate filter. By setting bit the corresponding filter will be enabled.
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
  • CAN library routine 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.

CANxFilterDisable

Prototype

void CANxFilterDisable(char CAN_FILTER);

Description

The function disables appropriate receive message filters.

Parameters
  • CAN_FILTER: filter disable selection parameter. Each bit corresponds to appropriate filter. By setting bit the corresponding filter will be disabled.
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
  • CAN library routine 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.

CANxSetFilter

Prototype

void CANxSetFilter(char CAN_FILTER, long val, unsigned int CAN_FILTER_MASK, unsigned int CAN_FILTER_RXBUFF, unsigned int CAN_CONFIG_FLAGS);

Description

Function sets message filter. Given value is bit adjusted to appropriate buffer mask registers.

Parameters
  • CAN_FILTER: CAN module filter number. Valid values: CAN_FILTER constants. See CAN_FILTER constants.
  • val: filter register value. This value is bit-adjusted to appropriate filter registers
  • CAN_FILTER_MASK: selects type of message to filter. See CAN_MASK constants.
  • CAN_FILTER_RXBUFF: selects type of message to filter. See CAN_BUFFER constants.
  • CAN_CONFIG_FLAGS: selects type of message to filter. Valid values: See CAN_CONFIG_FLAGS constants.
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
  • CAN library routine 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.

CANxReadBuffer

Prototype

unsigned int CANxReadBuffer(unsigned long *id, char *Data_, char buffer, unsigned int *dataLen, unsigned int *CAN_RX_MSG_FLAGS);

Description

If a full Receive Buffer is found, it will be processed in the following way :

  • Message ID is retrieved and stored to location pointed by id pointer.
  • Message data is retrieved and stored to array pointed by Data_ pointer.
  • Message receive buffer is determined by the buffer parameter.
  • Message length is retrieved and stored to location pointed by dataLen pointer.
  • Message flags are retrieved and stored to location pointed by CAN_RX_MSG_FLAGS pointer.
Parameters
  • id: message identifier address.
  • data_: an array of bytes up to 8 bytes in length.
  • buffer: receive buffer.
  • dataLen: data length address.
  • CAN_RX_MSG_FLAGS: message flags address. For message receive flags format refer to CAN_RX_MSG_FLAGS constants. See CAN_RX_MSG_FLAGS constants.
Returns
  • 0 if nothing is received
  • 0xFFFF if one of the Receive Buffers is full (message received)
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)) {
  ...
}
Notes
  • CAN library routine 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.

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 :

  • Message ID is retrieved and stored to location pointed by id pointer
  • Message data is retrieved and stored to array pointed by Data_ pointer
  • Message length is retrieved and stored to location pointed by dataLen pointer
  • Message flags are retrieved and stored to location pointed by CAN_RX_MSG_FLAGS pointer
Parameters
  • id: message identifier address
  • Data_: an array of bytes up to 8 bytes in length
  • dataLen: data length address
  • CAN_RX_MSG_FLAGS: message flags address. For message receive flags format refer to CAN_RX_MSG_FLAGS constants. See CAN_RX_MSG_FLAGS constants.
Returns
  • 0 if nothing is received
  • 0xFFFF if one of the Receive Buffers is full (message received)
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
  • CAN library routine 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.

CANxWriteBuffer

Prototype

unsigned int CANxWriteBuffer(unsigned long id, char *Data_, char buffer, unsigned int DataLen, unsigned int CAN_TX_MSG_FLAGS);

Description

If a empty Transmit Buffer is found, the function sends message in the queue for transmission.

Parameters
  • id: CAN message identifier. Valid values: 11 or 29 bit values, depending on message type (standard or extended)
  • Data_: data to be sent
  • Message transmit buffer is determined by the buffer parameter.
  • DataLen: data length. Valid values: 0..8
  • CAN_TX_MSG_FLAGS: message flags. Valid values: CAN_TX_MSG_FLAGS constants. See CAN_TX_MSG_FLAGS constants.
Returns
  • 0 if all Transmit Buffers are busy
  • 0xFFFF if at least one Transmit Buffer is available
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, _CAN_BUFFER_0, 1, tx_flags);
Notes
  • CAN library routine 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.

CANxWrite

Prototype

unsigned int CANxWrite(unsigned 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
  • id: CAN message identifier. Valid values: 11 or 29 bit values, depending on message type (standard or extended)
  • Data_: data to be sent
  • DataLen: data length. Valid values: 0..8
  • CAN_TX_MSG_FLAGS: message flags. Valid values: CAN_TX_MSG_FLAGS constants. See CAN_TX_MSG_FLAGS constants.
Returns
  • 0 if all Transmit Buffers are busy
  • 0xFFFF if at least one Transmit Buffer is available
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 library routine 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.

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:

Copy Code To ClipboardCopy Code To Clipboard
const unsigned int
    _CAN_MODE_BITS        = 0xE00000,   // Use this to access opmode  bits
    _CAN_MODE_NORMAL      = 0x00,
    _CAN_MODE_DISABLE     = 0x01,
    _CAN_MODE_LOOP        = 0x02,
    _CAN_MODE_LISTEN      = 0x03,
    _CAN_MODE_CONFIG      = 0x04,
    _CAN_MODE_LISTEN_ALL  = 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:

Copy Code To ClipboardCopy Code To Clipboard
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_MATCH_TYPE_BIT = 0x20,
    _CAN_CONFIG_ALL_VALID_MSG  = 0xDF,    // XX0XXXXX
    _CAN_CONFIG_MATCH_MSG_TYPE = 0xFF;    // XX1XXXXX

You may use bitwise AND (&) to form config byte out of these values. For example:

Copy Code To ClipboardCopy Code To Clipboard
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;
...
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:

Copy Code To ClipboardCopy Code To Clipboard
const unsigned int
    _CAN_TX_PRIORITY_BITS = 0x0003,
    _CAN_TX_PRIORITY_0    = 0xFFFC,   // XXXXXX00
    _CAN_TX_PRIORITY_1    = 0xFFFD,   // XXXXXX01
    _CAN_TX_PRIORITY_2    = 0xFFFE,   // XXXXXX10
    _CAN_TX_PRIORITY_3    = 0xFFFF,   // XXXXXX11

    _CAN_TX_FRAME_BIT     = 0x08,
    _CAN_TX_STD_FRAME     = 0xFF,     // XXXXX1XX
    _CAN_TX_XTD_FRAME     = 0xF7,     // XXXXX0XX

    _CAN_TX_RTR_BIT       = 0x0040,
    _CAN_TX_NO_RTR_FRAME  = 0xFFFF,   // X1XXXXXX
    _CAN_TX_RTR_FRAME     = 0xFFBF;   // X0XXXXXX

You may use bitwise AND (&) to adjust the appropriate flags. For example:

Copy Code To ClipboardCopy Code To Clipboard
// form value to be used with CANSendMessage:
send_config = _CAN_TX_PRIORITY_0 &
              _CAN_TX_XTD_FRAME  &
              _CAN_TX_NO_RTR_FRAME;
...
CAN1Write(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.

Copy Code To ClipboardCopy Code To Clipboard
const unsigned int
    _CAN_RX_FILTER_BITS = 0x001F,  // Use this to access filter bits
    _CAN_RX_OVERFLOW    = 0x0020,  // Set if Overflowed, else cleared
    _CAN_RX_INVALID_MSG = 0x0040,  // Set if invalid, else cleared
    _CAN_RX_XTD_FRAME   = 0x0080,  // Set if XTD message, else cleared
    _CAN_RX_RTR_FRAME   = 0x0100,  // Set if RTR message, else cleared
    _CAN_RX_DATA_ONLY   = 0x0200;  // Set if Data Only message

You may use bitwise AND (&) to adjust the appropriate flags. For example:

Copy Code To ClipboardCopy Code To Clipboard
if (MsgFlag & _CAN_RX_OVERFLOW != 0) {
  ...
  // Receiver overflow has occurred.
  // We have lost our previous message.
}

CAN_BUFFER Constants

CAN_BUFFER constants define CAN buffers. Function CANxAssignBuffer expects one of these as its argument:

Copy Code To ClipboardCopy Code To Clipboard
const unsigned int
  _CAN_BUFFER_0   =  0 ,
  _CAN_BUFFER_1   =  1 ,
  _CAN_BUFFER_2   =  2 ,
  _CAN_BUFFER_3   =  3 ,
  _CAN_BUFFER_4   =  4 ,
  _CAN_BUFFER_5   =  5 ,
  _CAN_BUFFER_6   =  6 ,
  _CAN_BUFFER_7   =  7 ,
  _CAN_BUFFER_8   =  8 ,
  _CAN_BUFFER_9   =  9 ,
  _CAN_BUFFER_10  =  10,
  _CAN_BUFFER_11  =  11,
  _CAN_BUFFER_12  =  12,
  _CAN_BUFFER_13  =  13,
  _CAN_BUFFER_14  =  14,
  _CAN_BUFFER_15  =  15,
  _CAN_BUFFER_16  =  16,
  _CAN_BUFFER_17  =  17,
  _CAN_BUFFER_18  =  18,
  _CAN_BUFFER_19  =  19,
  _CAN_BUFFER_20  =  20,
  _CAN_BUFFER_21  =  21,
  _CAN_BUFFER_22  =  22,
  _CAN_BUFFER_23  =  23,
  _CAN_BUFFER_24  =  24,
  _CAN_BUFFER_25  =  25,
  _CAN_BUFFER_26  =  26,
  _CAN_BUFFER_27  =  27,
  _CAN_BUFFER_28  =  28,
  _CAN_BUFFER_29  =  29,
  _CAN_BUFFER_30  =  30,
  _CAN_BUFFER_31  =  31;

CAN_FIFO Constants

CAN_FIFO constants define FIFO constants. Function CANxConfigureFIFO expects one of these as its argument:

Copy Code To ClipboardCopy Code To Clipboard
const unsigned int
  _CAN_FIFO_TXEN_BIT = 0x0080,
  _CAN_FIFO_TX = 0xFF7F,
  _CAN_FIFO_RX = 0xFFFF,
  
  _CAN_FIFO_DONLY_BIT = 0x1000,
  _CAN_DATA_ONLY = 0xEFFF,
  _CAN_FULL_MESSAGE = 0xFFFF;

CAN_MASK Constants

CAN_MASK constants define mask codes. Function CANxSetMask expects one of these as its argument:

Copy Code To ClipboardCopy Code To Clipboard
const unsigned int
    _CAN_MASK_0 = 0,
    _CAN_MASK_1 = 1,
    _CAN_MASK_2 = 2,
    _CAN_MASK_3 = 3;

CAN_FILTER Constants

CAN_FILTER constants define filter codes. Function CANxSetFilter expects one of these as its argument:

Copy Code To ClipboardCopy Code To Clipboard
const unsigned int
    _CAN_FILTER_0  =  0,
    _CAN_FILTER_1  =  1,
    _CAN_FILTER_2  =  2,
    _CAN_FILTER_3  =  3,
    _CAN_FILTER_4  =  4,
    _CAN_FILTER_5  =  5,
    _CAN_FILTER_6  =  6,
    _CAN_FILTER_7  =  7,
    _CAN_FILTER_8  =  8,
    _CAN_FILTER_9  =  9,
    _CAN_FILTER_10 = 10,
    _CAN_FILTER_11 = 11,
    _CAN_FILTER_12 = 12,
    _CAN_FILTER_13 = 13,
    _CAN_FILTER_14 = 14,
    _CAN_FILTER_15 = 15,
    _CAN_FILTER_16 = 16,
    _CAN_FILTER_17 = 17,
    _CAN_FILTER_18 = 18,
    _CAN_FILTER_19 = 19,
    _CAN_FILTER_20 = 20,
    _CAN_FILTER_21 = 21,
    _CAN_FILTER_22 = 22,
    _CAN_FILTER_23 = 23,
    _CAN_FILTER_24 = 24,
    _CAN_FILTER_25 = 25,
    _CAN_FILTER_26 = 26,
    _CAN_FILTER_27 = 27,
    _CAN_FILTER_28 = 28,
    _CAN_FILTER_29 = 29,
    _CAN_FILTER_30 = 30,
    _CAN_FILTER_31 = 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:

Copy Code To ClipboardCopy Code To Clipboard
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];   
char Msg_Rcvd;                                               // reception flag
const unsigned long ID_1st = 12111, ID_2nd = 3;              // node IDs
unsigned long Rx_ID;

// reserve space for 2 buffers with 8 messages (each message is 16 bytes)
// beggining of the buffer must be 32bit aligned
char FIFObuffers[2*8*16] absolute 0xA0000000;

char i;
void main() {
  AD1PCFG = 0;
  LATB = 0;
  TRISB = 0;

  Can_Init_Flags = 0;                             //
  Can_Send_Flags = 0;                             // clear flags
  Can_Rcv_Flags  = 0;                             //

  Can_Send_Flags =            // form value to be used
                   _CAN_TX_XTD_FRAME &            // with CAN1Write
                   _CAN_TX_NO_RTR_FRAME;

  Can_Init_Flags = _CAN_CONFIG_SAMPLE_THRICE &    // form value to be used
                   _CAN_CONFIG_PHSEG2_PRG_ON &    // with CAN1Initialize
                   _CAN_CONFIG_XTD_MSG &
                   _CAN_CONFIG_MATCH_MSG_TYPE &
                   _CAN_CONFIG_LINE_FILTER_OFF;

  CAN1Initialize(1,3,3,3,1,Can_Init_Flags);       // initialize CAN1

  CAN1SetOperationMode(_CAN_MODE_CONFIG,0xFF);    // set CONFIGURATION mode

  CAN1AssignBuffer(FIFObuffers); //assign the buffers
  //configure rx fifo
  CAN1ConfigureFIFO(0, 8,_CAN_FIFO_RX & _CAN_FULL_MESSAGE); //RX buffer 8 messages deep
  //configure tx fifo
  CAN1ConfigureFIFO(1, 8,_CAN_FIFO_TX & _CAN_TX_PRIORITY_3 & _CAN_TX_NO_RTR_FRAME); //TX buffer 8 messages deep

  //set mask 0
  CAN1SetMask(_CAN_MASK_0, -1, _CAN_CONFIG_MATCH_MSG_TYPE & _CAN_CONFIG_XTD_MSG);         // set all mask1 bits to ones
  //set filter 1
  CAN1SetFilter(_CAN_FILTER_31, ID_2nd, _CAN_MASK_0, _CAN_BUFFER_0, _CAN_CONFIG_XTD_MSG);  // set id of filter1 to 2nd node ID

  CAN1SetOperationMode(_CAN_MODE_NORMAL,0xFF);    // set NORMAL mode

  RxTx_Data[0] = 0xFE;
  CAN1Write(ID_1st, RxTx_Data, 1, Can_Send_Flags);
  
  while(1){
    Msg_Rcvd = CAN1Read(&Rx_ID, RxTx_Data, &Rx_Data_Len, &Can_Rcv_Flags);        // receive message
    if ((Rx_ID == ID_2nd) && Msg_Rcvd) {                                           // if message received check id
      LATB = RxTx_Data[0];                                                        // id correct, output data at PORTB
      RxTx_Data[0]++;                                                              // increment received data
      delay_ms(10);
      CAN1Write(ID_1st, RxTx_Data, 1, Can_Send_Flags);                             // send incremented data back
    }
  }



Code for the second CAN node:

Copy Code To ClipboardCopy Code To Clipboard
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
const unsigned long ID_1st = 12111, ID_2nd = 3;              // node IDs
unsigned long Rx_ID;

// reserve space for 2 buffers with 8 messages (each message is 16 bytes)
// beggining of the buffer must be 32bit aligned
char FIFIObuffers[2*8*16] absolute 0xA0000000;
char TX_Data[8];

void main() {
  AD1PCFG = 0;
  LATB = 0;
  TRISB = 0;

  Can_Init_Flags = 0;                             //
  Can_Send_Flags = 0;                             // clear flags
  Can_Rcv_Flags  = 0;                             //

  Can_Send_Flags =            // form value to be used
                   _CAN_TX_XTD_FRAME &            // with CAN1Write
                   _CAN_TX_NO_RTR_FRAME;

  Can_Init_Flags = _CAN_CONFIG_SAMPLE_THRICE &    // form value to be used
                   _CAN_CONFIG_PHSEG2_PRG_ON &    // with CAN1Initialize
                   _CAN_CONFIG_XTD_MSG &
                   _CAN_CONFIG_MATCH_MSG_TYPE &
                   _CAN_CONFIG_LINE_FILTER_OFF;

  CAN1Initialize(1,3,3,3,1,Can_Init_Flags);       // initialize CAN1

  CAN1SetOperationMode(_CAN_MODE_CONFIG,0xFF);    // set CONFIGURATION mode

  CAN1AssignBuffer(FIFIObuffers); //assign the buffers
  //configure rx fifo
  CAN1ConfigureFIFO(0, 8, _CAN_FIFO_RX & _CAN_FULL_MESSAGE); //RX buffer 8 messages deep

  //configure tx fifo
  CAN1ConfigureFIFO(1, 8, _CAN_FIFO_TX & _CAN_TX_PRIORITY_3 & _CAN_TX_NO_RTR_FRAME); //TX buffer 8 messages deep

  //set mask 1
  CAN1SetMask(_CAN_MASK_1, -1, _CAN_CONFIG_MATCH_MSG_TYPE & _CAN_CONFIG_XTD_MSG);         // set all mask1 bits to ones
  //set filter 5
  CAN1SetFilter(_CAN_FILTER_31, ID_1st, _CAN_MASK_1, _CAN_BUFFER_0, _CAN_CONFIG_XTD_MSG);

  CAN1SetOperationMode(_CAN_MODE_NORMAL,0xFF);    // set CONFIGURATION mode
  RxTx_Data[0] = 0;
  TRISD = 0;
  while(1) {
                                                           // endless loop
    Msg_Rcvd = CAN1Read(&Rx_ID , RxTx_Data , &Rx_Data_Len, &Can_Rcv_Flags);        // receive message
    latd = Can_Rcv_Flags;
    if ((Rx_ID == ID_1st) && Msg_Rcvd) {                                           // if message received check id
      LATB = RxTx_Data[0];                                                         // id correct, output data at PORTB
      RxTx_Data[0]++;                                                              // increment received data
      CAN1Write(ID_2nd, RxTx_Data, 1, Can_Send_Flags);                             // send incremented data back
    }
  }



HW Connection

Example of interfacing CAN transceiver with MCU and bus

Example of interfacing CAN transceiver with MCU and CAN bus

Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code