CAN Library
The mikroPascal 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 |
sub procedure CANSysInit(dim CANx as ^TCANStruct) |
||||||
---|---|---|---|---|---|---|---|
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(dim CANx as ^TCANStruct, dim bitratekB as word) as byte |
||||||
---|---|---|---|---|---|---|---|
Description |
This routine initializes CAN device. This is basic initialization which sets mode of operation. |
||||||
Parameters |
|
||||||
Returns |
This routine 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. |
||||||
Example |
|
||||||
Notes |
None. |
readCANReg
Prototype |
sub function readCANReg(dim CANx as ^ TCANStruct, dim reg as byte) as byte |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
sub procedure writeCANReg(dim CANx as ^TCANStruct, dim val as byte, dim reg as byte) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
sub procedure canEnableInt(dim CANx as ^TCANStruct, dim ints as byte) |
||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
sub procedure canDisableInt(dim CANx as ^TCANStruct, dim ints as byte) |
||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
sub procedure canClearIntFlag(dim CAN as ^TCANStruct, dim ints as byte) |
||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
sub function canGetStatus(dim CANx as ^TCANStruct) as byte |
||||||
---|---|---|---|---|---|---|---|
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 |
sub procedure canSetSpeed(dim CANx as ^TCANStruct, dim bitrate as word) |
||||||
---|---|---|---|---|---|---|---|
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 |
sub function canSetFilter(dim CANx as ^TCANStruct, dim canID as longword, dim options as byte) as byte |
||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Description |
This routine sets the CAN filter. |
||||||||||||||||||||||
Parameters |
|
||||||||||||||||||||||
Returns |
This routine 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. |
||||||||||||||||||||||
Example |
|
||||||||||||||||||||||
Notes |
None. |
canPushMessage
Prototype |
CAN_STATUS canPushMessage(dim CANx as ^TCANStruct, dim TCANMsg as ^pTransmitBuf) |
||||||
---|---|---|---|---|---|---|---|
Description |
Function implements a CAN transmit queue. With each function call a message is added to the queue. |
||||||
Parameters |
|
||||||
Returns |
This routine 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. |
||||||
Example |
|
||||||
Notes |
For many applications a queue with length "1" will be sufficient. Only applications with a high busload or very slow bus speed might need a queue of length "3" or more. |
canPullMessage
Prototype |
sub function canPullMessage(dim CANx as ^TCANStruct, dim TCANMsg as ^pReceiveBuf) as byte |
||||||
---|---|---|---|---|---|---|---|
Description |
Function implements a CAN receive queue. With each function call a message is pulled from the queue. |
||||||
Parameters |
|
||||||
Returns |
This routine 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. |
||||||
Example |
|
||||||
Notes |
None. |
canRXQueueCount
Prototype |
sub function canRXQueueCount(dim CANx as ^TCANStruct) as byte |
||||||
---|---|---|---|---|---|---|---|
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!