CAN Communication

From OpenMBD
Jump to: navigation, search


Description

This example model shows how to use the CAN receive, CAN send, CAN config and XCP on CAN config blocks. The model sends two CAN messages, one message with a standard identifier of 0x12 sending four uint16's and sending the Most Significant Byte first, the other message sends the same data but with an extended identifier of 0x000012 and the Least Significant Byte first. Also two CAN messages are received, one with standard and one with extended identifier. The data that is being received in these messages is of type uint8. The 'CAN config' block is used to enable the CAN peripheral of the micro controller and the 'XCP on CAN config' block is used to enable communication with HANtune over the CANbus.

Model

CANcommunicationModel.png

CAN config

CANcommunicationConfigMaskSettings.png

In the top left of the model the 'CAN config' block is placed. Double click it to open the settings window as shown above. The baudrate is chosen to be 500kbis/s, this is a commonly used baudrate in cars. The 'Recepetion filter mode' is set to receive both the standard, 11-bit, as the extended, 29-bit, messages. No filtering based on identifier is needed so those options are left at 0. The transmit buffer is set to 32 messages, with this setting it is possible to send 32 CAN messages at once in total. Note that when CAN is used for connecting HANtune the messages for HANtune also end up in this buffer. This means that when 2 CAN send blocks are used in the model, there is room left for 30 HANtune signals. The 'Event buffer size' can be left unchanged.

CAN send

CANcommunicationMaskSettingsCANsend.pngCANcommunicationMaskSettingsCANsendExt.png

Note the difference between the settings of the two CAN send blocks while in the model the blocks look identical and have identical input. One block is using the standard identifier and the other is using the extended identifier. Also one block is using a different byte order than the other block while both blocks use the same data type (uint16) and have the same data input. The resulting different messages are made visible in PCAN-View. In the receive part the two messages can be seen, having identifiers 00000012h and 012h. The difference in the byte order is clearly visible in the data:

CANcommunicationPCANview.png

The messages with identifier 665h and 666h are the messages sent between HANtune and the microcontroller.

CAN Receive

In the bottom of the PCAN-View window two messages with identifiers 011h and 00000011h are send. These messages are received. Note that the setting for receiving extended identifiers is not in this window.

CANcommunicationMaskSettingsCANreceive.pngCANcommunicationMaskSettingsCANreceiveExt.png

Instead it is indicated in the number of the identifier, as can be seen in the model, hex2dec('80000011'). In order to receive a message with an extended identifier the first bit of the identifier has to be set high. There is no difference in how the data is received because the data is sent 1 byte at a time, the byte order has no influence in this case, as can be seen from the screenshot in HANtune:

CANcommunicationHANtuneReceive.png

HANtune configuration

CANcommunicationXCPonCANmaskSettings.png

The XCP on CAN config block needs to be present if communication with HANtune over the CANbus is desired. In this block it is possible to change the CAN bus which should be used for the configuration and the identifiers used for the communication. If the identifiers are adapted here they should also be adapted in HANtune.


See Also

CAN Receive |CAN Send |CAN Config |XCP on CAN Config