CAN Library
The mikroC PRO for FT90x 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 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.
Two CAN bus controllers are supported by this library. The controllers have the following features :
- Conforms to Bosch CAN 2.0B specification,
- Data rate up to 1Mbps,
- Hardware message filtering (dual/single filters),
- 64-byte receive FIFO,
- 16-byte transmit buffer,
- No overload frames are generated,
- Normal & Listen Only modes supported,
- Single Shot transmission,
- Ability to abort transmission,
- Readable error counters,
- Last Error Code.

- Consult the CAN standard about CAN bus termination resistance.
Library Routines
- CANSysInit
- canInit
- readCANReg
- writeCANReg
- canEnableInt
- canDisableInt
- canClearIntFlag
- canGetStatus
- canSetSpeed
- canSetFilter
- canPushMessage
- canPullMessage
- canRXQueueCount
CANSysInit
Prototype |
void CANSysInit(TCANStruct *CANx); |
||||||
---|---|---|---|---|---|---|---|
Description |
Initializes system registers for CAN module usage. |
||||||
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 system registers for CAN0 module usage CANSysInit(&CAN0); |
||||||
Notes |
None. |
canInit
Prototype |
CAN_STATUS canInit(TCANStruct *CANx, uint16_t bitratekB); |
||||||
---|---|---|---|---|---|---|---|
Description |
This routine initializes CAN device. This is basic initialization which sets mode of operation. |
||||||
Parameters |
|
||||||
Returns |
This routine returns typedef enum { CAN_OK, // CAN device initialized CAN_ERROR, // error occured, CAN device not initialized CAN_RX_OVERRUN } CAN_STATUS; |
||||||
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 |
|
||||||
Notes |
None. |
readCANReg
Prototype |
uint8_t readCANReg(TCANStruct *CANx, uint8_t reg); |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Description |
This routine reads any CAN register. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Parameters |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Returns |
This routine returns CAN register value. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Notes |
None. |
writeCANReg
Prototype |
void writeCANReg(TCANStruct *CANx, uint8_t val, uint8_t reg); |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Description |
This routine writes a value to any CAN register. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Notes |
None. |
canEnableInt
Prototype |
void canEnableInt(TCANStruct *CANx, uint8_t ints); |
||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Description |
This routine enables CAN interrupts. |
||||||||||||||||||||||
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 |
|
||||||||||||||||||||||
Notes |
None. |
canDisableInt
Prototype |
void canDisableInt(TCANStruct *CANx, uint8_t ints); |
||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Description |
This routine disables CAN interrupts. |
||||||||||||||||||||||
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 |
|
||||||||||||||||||||||
Notes |
None. |
canClearIntFlag
Prototype |
void canClearIntFlag(TCANStruct *CANx, uint8_t ints); |
||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Description |
This routine clears CAN interrupt status flags. |
||||||||||||||||||||||
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 |
|
||||||||||||||||||||||
Notes |
None. |
canGetStatus
Prototype |
uint8_t canGetStatus(TCANStruct *CANx); |
||||||
---|---|---|---|---|---|---|---|
Description |
This routine returns the global status variable value. |
||||||
Parameters |
|
||||||
Returns |
Returns an 8-bit status variable. The bits order and values in the status variable are :
|
||||||
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 |
|
||||||
Notes |
None. |
canSetSpeed
Prototype |
void canSetSpeed(TCANStruct *CANx, uint16_t bitrate); |
||||||
---|---|---|---|---|---|---|---|
Description |
This routine sets the CAN bus speed. |
||||||
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 |
|
||||||
Notes |
None. |
canSetFilter
Prototype |
CAN_STATUS canSetFilter(TCANStruct *CANx, uint32_t canID, uint8_t options); |
||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Description |
This routine sets the CAN filter. |
||||||||||||||||||||||
Parameters |
|
||||||||||||||||||||||
Returns |
This routine returns typedef enum { CAN_OK, // filter was set CAN_ERROR, // could not set filter CAN_RX_OVERRUN } CAN_STATUS; |
||||||||||||||||||||||
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 |
|
||||||||||||||||||||||
Notes |
None. |
canPushMessage
Prototype |
CAN_STATUS canPushMessage(TCANStruct *CANx, TCANMsg *pTransmitBuf); |
||||||
---|---|---|---|---|---|---|---|
Description |
Function implements a CAN transmit queue. With each function call a message is added to the queue. |
||||||
Parameters |
|
||||||
Returns |
This routine returns typedef enum { CAN_OK, // message was pushed to the transmit queue CAN_ERROR, // queue is full, message was not added, bit TXOR in canGetStatus() set CAN_RX_OVERRUN } CAN_STATUS; |
||||||
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 |
|
||||||
Notes |
None. |
canPullMessage
Prototype |
CAN_STATUS canPullMessage(TCANStruct *CANx, TCANMsg *pReceiveBuf); |
||||||
---|---|---|---|---|---|---|---|
Description |
Function implements a CAN receive queue. With each function call a message is pulled from the queue. |
||||||
Parameters |
|
||||||
Returns |
This routine returns typedef enum { CAN_OK, // message was pulled from receive queue CAN_ERROR, // queue empty, no message received CAN_RX_OVERRUN // RX queue overrun occured } CAN_STATUS; |
||||||
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 |
|
||||||
Notes |
None. |
canRXQueueCount
Prototype |
uint8_t canRXQueueCount(TCANStruct *CANx); |
||||||
---|---|---|---|---|---|---|---|
Description |
Function gets the number of messages currently in the RX queue. |
||||||
Parameters |
|
||||||
Returns |
The number of messages in the RX queue. |
||||||
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 |
|
||||||
Notes |
None. |
What do you think about this topic ? Send us feedback!