The mikroC PRO for PIC provides a library (driver) for working with the 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 depends on the 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
Important :
- Consult the CAN standard about CAN bus termination resistance.
Library Routines
- CANSetOperationMode
- CANGetOperationMode
- CANInitialize
- CANSetBaudRate
- CANSetMask
- CANSetFilter
- CANRead
- CANWrite
- CANSetTxIdleLevel
CANSetOperationMode
Prototype | void CANSetOperationMode(unsigned short mode, unsigned short wait_flag); |
---|---|
Returns | Nothing. |
Description | Sets CAN to requested mode, i.e. copies mode to CANSTAT. Parameter mode needs to be one of CAN_OP_MODE constants .Parameter wait_flag needs to be either 0 or 0xFF:
|
Requires | Microcontroller must be connected to CAN transceiver (MCP2551 or similar) which is connected to CAN bus. |
Example |
CANSetOperationMode(_CAN_MODE_CONFIG, 0xFF); |
CANGetOperationMode
Prototype | unsigned short CANGetOperationMode(); |
---|---|
Returns | Current opmode. |
Description | Function returns current operational mode of CAN module. |
Requires | Microcontroller must be connected to CAN transceiver (MCP2551 or similar) which is connected to CAN bus. |
Example |
if (CANGetOperationMode() == _CAN_MODE_NORMAL) { ... }; |
CANInitialize
Prototype | void CANInitialize(char SJW, char BRP, char PHSEG1, char PHSEG2, char PROPSEG, char CAN_CONFIG_FLAGS); |
---|---|
Returns | Nothing. |
Description | Initializes CAN. All pending transmissions are aborted. Sets all mask registers to 0 to allow all messages. Filter registers are set according to flag value: if (CAN_CONFIG_FLAGS & _CAN_CONFIG_VALID_XTD_MSG != 0) // Set all filters to XTD_MSG else if (config & _CAN_CONFIG_VALID_STD_MSG != 0) // Set all filters to STD_MSG else // Set half of the filters to STD, and the rest to XTD_MSG. Parameters:
|
Requires | CAN must be in Config mode; otherwise the function will be ignored. Microcontroller must be connected to CAN transceiver (MCP2551 or similar) which is connected to CAN bus. |
Example |
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; ... CANInitialize(1, 1, 3, 3, 1, init); // initialize CAN |
CANSetBaudRate
Prototype | void CANSetBaudRate(char SJW, char BRP, char PHSEG1, char PHSEG2, char PROPSEG, char CAN_CONFIG_FLAGS); |
---|---|
Returns | Nothing. |
Description | Sets CAN baud rate. Due to complexity of CAN protocol, you cannot simply force a bps value. Instead, use this function when CAN is in Config mode. Refer to datasheet for details. Parameters:
|
Requires | CAN must be in Config mode; otherwise the function will be ignored. Microcontroller must be connected to CAN transceiver (MCP2551 or similar) which is connected to CAN bus. |
Example |
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; ... CANSetBaudRate(1, 1, 3, 3, 1, init); |
CANSetMask
Prototype | void CANSetMask(char CAN_MASK, long value, char CAN_CONFIG_FLAGS); |
---|---|
Returns | Nothing. |
Description | Function sets mask for advanced filtering of messages. Given value is bit adjusted to appropriate buffer mask registers.Parameters:
|
Requires | CAN must be in Config mode; otherwise the function will be ignored. Microcontroller must be connected to CAN transceiver (MCP2551 or similar) which is connected to CAN bus. |
Example |
// Set all mask bits to 1, i.e. all filtered bits are relevant: CANSetMask(_CAN_MASK_B1, -1, _CAN_CONFIG_XTD_MSG); // Note that -1 is just a cheaper way to write 0xFFFFFFFF. Complement will do the trick and fill it up with ones. |
CANSetFilter
Prototype | void CANSetFilter(char CAN_FILTER, long value, char CAN_CONFIG_FLAGS); |
---|---|
Returns | Nothing. |
Description | Function sets message filter. Given value is bit adjusted to appropriate buffer mask registers.Parameters:
|
Requires | CAN must be in Config mode; otherwise the function will be ignored. Microcontroller must be connected to CAN transceiver (MCP2551 or similar) which is connected to CAN bus. |
Example |
// Set id of filter B1_F1 to 3: CANSetFilter(_CAN_FILTER_B1_F1, 3, _CAN_CONFIG_XTD_MSG); |
CANRead
Prototype | char CANRead(long *id, char *data, char *datalen, char *CAN_RX_MSG_FLAGS); |
---|---|
Returns | Message from receive buffer or zero if no message found. |
Description | Function reads message from receive buffer. If at least one full receive buffer is found, it is extracted and returned. If none found, function returns zero. Parameters:
|
Requires | CAN must be in mode in which receiving is possible. Microcontroller must be connected to CAN transceiver (MCP2551 or similar) which is connected to CAN bus. |
Example |
char rcv, rx, len, data[8]; long id; // ... rx = 0; // ... rcv = CANRead(id, data, len, rx); |
CANWrite
Prototype | unsigned short CANWrite(long id, char *data, char datalen, char CAN_TX_MSG_FLAGS); |
---|---|
Returns | Returns zero if message cannot be queued (buffer full). |
Description | If at least one empty transmit buffer is found, function sends message on queue for transmission. If buffer is full, function returns 0. Parameters:
|
Requires | CAN must be in Normal mode. Microcontroller must be connected to CAN transceiver (MCP2551 or similar) which is connected to CAN bus. |
Example |
char tx, data; long id; // ... tx = _CAN_TX_PRIORITY_0 & _CAN_TX_XTD_FRAME; // ... CANWrite(id, data, 2, tx); |
CANSetTxIdleLevel
Prototype | void CANSetTxIdleLevel(char driveHighState); | ||||||
---|---|---|---|---|---|---|---|
Returns | Nothing. | ||||||
Description | This function sets the state of CANTX pin when recessive. Parameters:
|
||||||
Requires | Microcontroller must be connected to CAN transceiver (MCP2551 or similar) which is connected to CAN bus. | ||||||
Example |
CANSetTxIdleLevel(_CAN_DRIVE_HIGH_STATE_ENABLE); |
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
CAN_OP_MODE
constants define CAN operation mode. Function CANSetOperationMode
expects one of these as its argument:const char
_CAN_MODE_BITS = 0xE0, // Use this to access opmode bits
_CAN_MODE_NORMAL = 0x00,
_CAN_MODE_SLEEP = 0x20,
_CAN_MODE_LISTEN = 0x60,
_CAN_MODE_LOOP = 0x40,
CAN_CONFIG_FLAGS
CAN_CONFIG_FLAGS
constants define flags related to CAN module configuration. Functions CANInitialize
and CANSetBaudRate
expect one of these (or a bitwise combination) as their argument:const char
_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_DBL_BUFFER_BIT = 0x10,
_CAN_CONFIG_DBL_BUFFER_ON = 0xFF, // XXX1XXXX
_CAN_CONFIG_DBL_BUFFER_OFF = 0xEF, // XXX0XXXX
_CAN_CONFIG_MSG_BITS = 0x60,
_CAN_CONFIG_ALL_MSG = 0xFF, // X11XXXXX
_CAN_CONFIG_VALID_XTD_MSG = 0xDF, // X10XXXXX
_CAN_CONFIG_VALID_STD_MSG = 0xBF, // X01XXXXX
_CAN_CONFIG_ALL_VALID_MSG = 0x9F; // X00XXXXX
You may use bitwise AND (
&
) to form config byte out of these values. For example:init = _CAN_CONFIG_SAMPLE_THRICE &
_CAN_CONFIG_PHSEG2_PRG_ON &
_CAN_CONFIG_DBL_BUFFER_ON &
_CAN_CONFIG_STD_MSG & _CAN_CONFIG_VALID_XTD_MSG &
For more detail: Connect CAN Protocol with PIC