Advanced PIC18 Projects—CAN Bus Projects:mikroC CAN Functions

mikroC CAN Functions

The mikroC language provides two libraries for CAN bus applications: the library for PIC microcontrollers with built-in CAN modules and the library based on using a SPI

bus for PIC microcontrollers having no built-in CAN modules. In this section we will discuss only the library functions available for PIC microcontrollers with built-in CAN modules. Similar functions are available for the PIC microcontrollers with no built-in CAN modules.

The mikroC CAN functions are supported only by PIC18XXX8 microcontrollers with MCP2551 or similar CAN transceivers. Both standard (11 identifier bits) and extended format (29 identifier bits) messages are supported.

The following mikroC functions are provided:

• CANSetOperationMode

• CANGetOperationMode

• CANInitialize

• CANSetBaudRAte

• CANSetMask

• CANSetFilter

• CANRead

• CANWrite

CANSetOperationMode

The CANSetOperationMode function sets the CAN operation mode. The function prototype is:

void CANSetOperationMode(char mode, char wait_flag)

The parameter wait_ flag is either 0 or 0 x FF. If it is set to 0 x FF, the function blocks and will not return until the requested mode is set. If it is set to 0, the function returns as a nonblocking call.

The mode can be one of the following:

• CAN_MODE_NORMAL Normal mode of operation

• CAN_MODE_SLEEP Sleep mode of operation

• CAN_MODE_LOOP Loop-back mode of operation

• CAN_MODE_LISTEN Listen-only mode of operation

• CAN_MODE_CONFIG Configuration mode of operation

CANGetOperationMode

The CANGetOperationMode function returns the current CAN operation mode. The function prototype is:

char CANGetOperationMode(void)

CANInitialize

The CANInitialize function initializes the CAN module. All mask registers are cleared to 0 to allow all messages. Upon execution of this function, the normal mode is set. The function prototype is:

Advanced PIC18 Projects—CAN Bus Projects -0210

id is the CAN message identifier. Only 11 or 29 bits may be used depending on message type (standard or extended). data is an array of bytes up to 8 where the received data is stored. datalen is the length of the received data (1 to 8).

CAN_RX_MSG_FLAGS can be one of the following:

Advanced PIC18 Projects—CAN Bus Projects -0211

Advanced PIC18 Projects—CAN Bus Projects -0212

Leave a comment

Your email address will not be published. Required fields are marked *