Difference between revisions of "Component: CAN (Internal, MCP2515) (Comms: Interface)"
Line 20: | Line 20: | ||
==Detailed description== | ==Detailed description== | ||
+ | |||
+ | |||
Line 32: | Line 34: | ||
==Examples== | ==Examples== | ||
+ | |||
+ | |||
Line 546: | Line 550: | ||
|- | |- | ||
| width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-u8-icon.png]] - BYTE | | width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-u8-icon.png]] - BYTE | ||
+ | | width="90%" style="border-top: 2px solid #000;" | ''Return'' | ||
+ | |} | ||
+ | |||
+ | |||
+ | {| class="wikitable" style="width:60%; background-color:#FFFFFF;" | ||
+ | |- | ||
+ | | width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]] | ||
+ | | width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''ChangeRate''' | ||
+ | |- | ||
+ | | colspan="2" | Simple attempt to alter the bus rate of the CAN. External CAN channels only, | ||
+ | |- | ||
+ | |- | ||
+ | | width="10%" align="center" | [[File:Fc9-u8-icon.png]] - BYTE | ||
+ | | width="90%" | Rate | ||
+ | |- | ||
+ | | colspan="2" | Rate: 0-3 where 0=125, 1=250, 2=500, 3=1000 | ||
+ | |- | ||
+ | | width="10%" align="center" style="border-top: 2px solid #000;" | [[File:Fc9-void-icon.png]] - VOID | ||
| width="90%" style="border-top: 2px solid #000;" | ''Return'' | | width="90%" style="border-top: 2px solid #000;" | ''Return'' | ||
|} | |} | ||
Line 694: | Line 716: | ||
|- | |- | ||
| colspan="2" | Disabled. Messages will reattempt transmission Enabled. Message will only attempt to transmit once | | colspan="2" | Disabled. Messages will reattempt transmission Enabled. Message will only attempt to transmit once | ||
+ | |- | ||
+ | | width="10%" align="center" style="background-color:#EAE1EA;" | [[File:Fc9-conn-icon.png]] | ||
+ | | width="90%" style="background-color:#EAE1EA; color:#4B008D;" | Connections | ||
+ | |- | ||
+ | |- | ||
+ | | width="10%" align="center" style="background-color:#EAE1EA;" | [[File:Fc9-conn-icon.png]] | ||
+ | | width="90%" style="background-color:#EAE1EA; color:#4B008D;" | SPI | ||
+ | |- | ||
|- | |- | ||
| width="10%" align="center" | [[File:Fc9-type-16-icon.png]] | | width="10%" align="center" | [[File:Fc9-type-16-icon.png]] | ||
Line 735: | Line 765: | ||
| colspan="2" | | | colspan="2" | | ||
|- | |- | ||
− | | width="10%" align="center" | + | | width="10%" align="center" style="background-color:#EAE1EA;" | [[File:Fc9-conn-icon.png]] |
− | + | | width="90%" style="background-color:#EAE1EA; color:#4B008D;" | TX Buffer 0 | |
− | |||
− | |||
− | |||
− | |||
− | | width="90%" | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
− | |||
|- | |- | ||
| width="10%" align="center" | [[File:Fc9-type-14-icon.png]] | | width="10%" align="center" | [[File:Fc9-type-14-icon.png]] | ||
Line 804: | Line 818: | ||
|- | |- | ||
| colspan="2" | TX Buffer 0 Data Byte 7 | | colspan="2" | TX Buffer 0 Data Byte 7 | ||
+ | |- | ||
+ | | width="10%" align="center" style="background-color:#EAE1EA;" | [[File:Fc9-conn-icon.png]] | ||
+ | | width="90%" style="background-color:#EAE1EA; color:#4B008D;" | TX Buffer 1 | ||
+ | |- | ||
|- | |- | ||
| width="10%" align="center" | [[File:Fc9-type-14-icon.png]] | | width="10%" align="center" | [[File:Fc9-type-14-icon.png]] | ||
Line 854: | Line 872: | ||
|- | |- | ||
| colspan="2" | TX Buffer 1 Data Byte 7 | | colspan="2" | TX Buffer 1 Data Byte 7 | ||
+ | |- | ||
+ | | width="10%" align="center" style="background-color:#EAE1EA;" | [[File:Fc9-conn-icon.png]] | ||
+ | | width="90%" style="background-color:#EAE1EA; color:#4B008D;" | TX Buffer 2 | ||
+ | |- | ||
|- | |- | ||
| width="10%" align="center" | [[File:Fc9-type-14-icon.png]] | | width="10%" align="center" | [[File:Fc9-type-14-icon.png]] | ||
Line 905: | Line 927: | ||
| colspan="2" | TX Buffer 2 Data Byte 7 | | colspan="2" | TX Buffer 2 Data Byte 7 | ||
|- | |- | ||
− | | width="10%" align="center" | [[File:Fc9- | + | | width="10%" align="center" style="background-color:#EAE1EA;" | [[File:Fc9-conn-icon.png]] |
− | | width="90%" | | + | | width="90%" style="background-color:#EAE1EA; color:#4B008D;" | RX Buffer 0 |
|- | |- | ||
− | |||
|- | |- | ||
| width="10%" align="center" | [[File:Fc9-type-16-icon.png]] | | width="10%" align="center" | [[File:Fc9-type-16-icon.png]] | ||
| width="90%" | Settings | | width="90%" | Settings | ||
|- | |- | ||
− | | colspan="2" | RX Buffer | + | | colspan="2" | RX Buffer 0 Message Receive Mode |
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
− | | width="10%" align="center" | [[File:Fc9- | + | | width="10%" align="center" style="background-color:#EAE1EA;" | [[File:Fc9-conn-icon.png]] |
− | | width="90%" | | + | | width="90%" style="background-color:#EAE1EA; color:#4B008D;" | RX Buffer 1 |
|- | |- | ||
− | |||
|- | |- | ||
| width="10%" align="center" | [[File:Fc9-type-16-icon.png]] | | width="10%" align="center" | [[File:Fc9-type-16-icon.png]] | ||
− | | width="90%" | | + | | width="90%" | Settings |
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
+ | | colspan="2" | RX Buffer 1 Message Receive Mode | ||
|- | |- | ||
− | | width="10%" align="center" style="background-color:# | + | | width="10%" align="center" style="background-color:#EAE1EA;" | [[File:Fc9-conn-icon.png]] |
− | | width="90%" style="background-color:# | + | | width="90%" style="background-color:#EAE1EA; color:#4B008D;" | Simulation |
|- | |- | ||
|- | |- |
Latest revision as of 17:19, 9 November 2022
Author | Matrix Ltd |
Version | 2.10 |
Category | Comms: Interface |
Contents
CAN (Internal, MCP2515) component
Low level routines for controling the CAN interface either using an external MCP2515 IC and a SPI bus connection or using an internal CAN peripheral if available on your device. Both methods will require a CAN driver IC like the MCP2551 to drive the CAN signals on the bus.
Component Pack
COMMSA
Detailed description
No detailed description exists yet for this component
Examples
Internal CAN schematic
External CAN schematic
CAN Message Decoding
The CAN component works together with the injector component to allow you to decode a CAN message ID into a meaningful command.
Example File
CAN_Example1
When a key on the keypad is pressed the CAN components transmits a CAN packet.
The CAN packet is decoded by the injector component to give us a meaningful log on the console window.
High Level CAN Data Console
Low Level CAN Data Console
Reference from CAN Injector to ID decode file, $(srcdir) refers to the current project directory.
Demo CAN ID file File:CANID.csv
CAN Masks and Filters
How to work out which messages will be trapped by a particular mask/filter combination
Note that all values for message id's, masks and filters are numbers between 0x000 and 0x7FF.
Example 1
Mask0 = 0x0FF
Filter0 = 0x100
Filter1 = 0x050
In binary, this looks like:
Mask0 = 000 1111 1111
Filter0 = 001 0000 0000
Filter1 = 000 0101 0000
For the mask, a "1" signifies "check this bit" and a "0" means "ignore this bit"
So, these filters will accept the following messages ("x" = don't care)
Filter0 = xxx 0000 0000
Filter1 = xxx 0101 0000
i.e.
Filter0 = 0x000, 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x700
Filter1 = 0x050, 0x150, 0x250, 0x350, 0x450, 0x550, 0x650, 0x750
Example 2
Mask1 = 0x350
Filter2 = 0x200
Filter3 = 0x123
Filter4 = 0x3FF
Rewriting in binary:
Mask1 = 111 0101 0000
Filter2 = 010 0000 0000
Filter3 = 001 0010 0011
Filter4 = 111 1111 1111
Here, the mask will only check 4 bits and ignore the other 6. Here's what the filters will accept:
Filter2 = 010 x0x0 xxxx
Filter3 = 001 x0x0 xxxx
Filter4 = 111 x1x1 xxxx
They will actually trap a lot of messages (64 each!):
Filter2 = 0x200, 0x201, 0x202, ... 0x220, 0x221, ... 0x280, 0x281, ... 0x2A0, 0x2A1, ... 0x2AF
Filter3 = 0x100, 0x101, 0x102, ... 0x120, 0x121, ... 0x180, 0x181, ... 0x1A0, 0x1A1, ... 0x1AF
Filter4 = 0x750, 0x751, 0x752, ... 0x770, 0x771, ... 0x7D0, 0x7D1, ... 0x7F0, 0x7F1, ... 0x7FF
This second example is not very practical. In general, it is more logical to set the mask so that each filter accepts a consecutive range of messages.
As you can see, the mask determines which bits of the filters are actually looked at. Setting the mask to 0x000 will effectively mean that the filter will accept any incoming message. Also, the value of the mask directly relates to how many messages each filter will trap - i.e. 2^(number of '0' bits in the mask).
A useful way to use the mask would be to ignore the least significant bits. Lets say that you wanted the filters to accept 16 messages each - setting the Mask0 to 0x7F0 would achieve this. Then, setting the filters to the following:
Filter0 = 0x100
Filter1 = 0x110
would mean that the following messages are accepted:
Filter0 = 0x100, 0x101, 0x102, 0x103, 0x104, ... 0x10D, 0x10E, 0x10F
Filter1 = 0x110, 0x111, 0x112, 0x113, 0x114, ... 0x11D, 0x11E, 0x11F
Of course, for simple CAN applications you may wish to only accept one or two messages. Setting the mask to 0x7FF in this instance would mean that only the message ID specified by each filter would be accepted, e.g.
Mask1 = 0x7FF
Filter2 = 0x100
Filter5 = 0x200
This would mean that only messages 0x100 and 0x200 would be accepted into buffer 1.
Downloadable macro reference
![]() |
ReadSwitches |
Returns switch input states from the external MCP2515 device. 0 = No Switch Pressed, 1 = Switch 1 Pressed, 2 = Switch 2 Pressed, 3 = Both Pressed (External CAN only) | |
![]() |
Return |
![]() |
GetRxData |
Returns last received message data byte at position Index. Buffer parameter is currently ignored | |
![]() |
Buffer |
![]() |
Index |
![]() |
Return |
![]() |
GetRxIDHi |
Gets the Hi byte Rx ID in register format. Standard ID only 0-2047. Compatible with v5 component and previous, | |
![]() |
Buffer |
Receive Buffer. Range 0-1 | |
![]() |
Return |
![]() |
SetTxIDSimple |
Set the Tx CAN ID as a generic number. Standard IDs only 0-2047. | |
![]() |
Buffer |
Transmit buffer to load. Range: 0-2 | |
![]() |
ID |
The ID value you wish to use. Range: 0-2047 | |
![]() |
Return |
![]() |
SendBuffer |
Transmits one of the transmit buffers which should have already been populated with an ID and data. | |
![]() |
Buffer |
Specifies which transmit buffer to send | |
![]() |
Return |
![]() |
GetRxDataCount |
Returns last received message data length. Buffer parameter is currently ignored | |
![]() |
Buffer |
![]() |
Return |
![]() |
GetRxIDLo |
Gets the Lo byte Rx ID in register format. Standard ID only 0-2047. Compatible with v5 component and previous, | |
![]() |
Buffer |
![]() |
Return |
![]() |
CheckRx |
Checks to see if any messages are available for 'Buffer' specified | |
![]() |
Buffer |
Send buffer: 0 to 1 | |
![]() |
Return |
![]() |
ChangeRate |
Simple attempt to alter the bus rate of the CAN. External CAN channels only, | |
![]() |
Rate |
Rate: 0-3 where 0=125, 1=250, 2=500, 3=1000 | |
![]() |
Return |
![]() |
GetRxIDSimple |
Gets the Rx ID as a Integer Standard ID only 0-2047. | |
![]() |
Buffer |
Receive Buffer. Range 0-1 | |
![]() |
Return |
![]() |
ShowLEDs |
Set LED state (External CAN only) | |
![]() |
led1 |
Controls LED 1. Range: 0-1 | |
![]() |
led2 |
Controls LED 2. Range: 0-1 | |
![]() |
Return |
![]() |
GetRxIdent |
Gets the Rx ID as a Integer Standard and Extended IDs. | |
![]() |
Buffer |
![]() |
Return |
![]() |
Initialise |
Must be called before any other CAN component macros to enable and initialise the CAN peripheral. | |
![]() |
Return |