|
Example |
Top Previous |
|
Example1:
Our embedded platform is dsPIC33EP256GP502 . CAN bus peripheral 1 is used with baud rate= 250000 bit/sec. In MPLABX IDE MCC, we set F0= 0x120 std frame used Mask0, F1= 0x31 std frame used Mask0, F1= 0x12340112 ext frame used Mask1, F2= 0x50124 ext frame used Mask1, F1= 0x12340218 ext frame used Mask1. Number of Transmit Buffer is 3 from Buffer0 to Buffer2.
Please see MCC configuration for can1 below:
Please be careful, For dspic/Pic24 MCU, CAN bus use DMA to transfer data. We show you the DMA configuration in MPLABX IDE MCC for can1 below:
Uart2 connects RS485 Transceiver, and TX_transmit Enable is controlled by RB11. Logic High will enable RS485 transmit and disable RS485 receiver. This RS485 is used for Simulink Debug. This example code will read CAN frame every 0.5 seconds. The following operations will be doing during sample time:
Please see screenshot of model below:
In "Setup Target" block, we choose "PIC24/Dspic30/Dspic33" platform. Please double click on "emModbusServerDebugSetup" block.
We only use this block for "Probe". So the QTY of modbus server registers is set to 0.
Please double click on "emCANSetup" block. We will see parameters for CAN bus controller below:
Let's explain settings. Mask0= 7F0, it has no suffix "X" or "x". So Mask0 will be standard 11 bits. In previous MCC configuration, Filter0=0x120 and use Mask0. We can know this CAN bus controller can receive standard frame with CAN ID= 0x120 to 0x12F because MASK0 the most right-handed 4 bits are 0 (means we don't care CAN ID's most right-handed 4 bits). In previous MCC configuration, Filter1=0x31 and use Mask0. We can know this CAN bus controller can receive standard frame with CAN ID= 0x30 to 0x3F because MASK0 the most right-handed 4 bits are 0 (means we don't care CAN ID's most right-handed 4 bits). Mask1=1FFF00FFX, it has suffix "X". So Mask1 will be extended 29 bits. In previous MCC configuration, Filter3=0x12340112 and use Mask1. We can know this CAN bus controller can receive extended frame with CAN ID= 0x12340012 to 0x1234FF12 because MASK1 Bit8 to Bit15 are 0 (means we don't care CAN ID's Bit8 to Bit15). In previous MCC configuration, Filter4=0x50124 and use Mask1. We can know this CAN bus controller can receive extended frame with CAN ID= 0x50024 to 0x5FF24 because MASK1 Bit8 to Bit15 are 0 (means we don't care CAN ID's Bit8 to Bit15). In previous MCC configuration, Filter5=0x12340128 and use Mask1. We can know this CAN bus controller can receive extended frame with CAN ID= 0x12340028 to 0x1234FF28 because MASK1 Bit8 to Bit15 are 0 (means we don't care CAN ID's Bit8 to Bit15). As for Mask2, No filter uses Mask2 in MCC configuration. So we don't care Mask2 value.
Parameter "Rx CAN ID for short frame or RTR" is [120, 31, 50124X]. It means CAN Bus controller for Simulink will receive short standard data/Remote frame with CAN ID=0x120 and 0x31, and short extended data/Remote frame with CAN ID=0x50124. These CAN IDs are covered by MCC configuration,
Parameter "Frame Number" is "1-based frame No". It means that the 1st frame's frame number (the 1st byte of data field) is 1 for long data frame. Of cause, the 2nd frame's frame number (the 1st byte of data field) is 2 for long data frame.
Parameter "Max frame Qty for long frame Rx/Tx" is 2. It means that the longest data byte QTY is 14 (7 bytes each frame, Total 2 frames will have total 14 bytes).
Parameter "Sync Enable " is false. It means that transmitting occurs at any time. It is no use for this example because we do not transmit any CAN bus frame.
Parameter "Sync Rx CAN ID" is no use for this example because we do not transmit any CAN bus frame.
Parameter "Max Sequence Qty for CAN BUS transmit A Channel" is 31, So we can use Seq0 to Seq30 of Channel A to transmit. But this parameter is no use for this example because we do not transmit any CAN bus frame. Parameter "Max Sequence Qty for CAN BUS transmit B Channel" is 0, So we cannot use Channel B to transmit. But this parameter is no use for this example because we do not transmit any CAN bus frame Parameter "Max Sequence Qty for CAN BUS transmit C Channel" is 0, So we cannot use Channel C to transmit. But this parameter is no use for this example because we do not transmit any CAN bus frame. "Sample time in Sec" is -1 which means sample time inherit. (use base sample time 0.5 sec)
Block "Read CANID:0x31" will do operation 1 in previous operations introduce. Please double click "Read CANID:0x31". You will see parameter settings below:
It will receive standard short data frame with CAN ID=0x31, and 8 bytes's data field is int8 vector with 3 elements, bool vector with 4 elements, uint32 scalar. It is little-endian. Please see data field of this CAN bus frame below:
The yellow color is int8 vector data area. The white color is bool vector area (Bit0 is the 1st element of bool vector). The blue color is uint32 scalar area.
Block "Read CANID:0x120" will do operation 2 in previous operations introduce. Please double click "Read CANID:0x120". You will see parameter settings below:
It will receive standard remote frame with CAN ID=0x120.
Block "Read CANID:0x12340112x" will do operation 3 in previous operations introduce. Please double click "Read CANID:0x12340112x". You will see parameter settings below:
It will receive extended long data frame with CAN ID=0x12340112, and 14 bytes's data field is int8 vector with 5 elements, bool vector with 2 elements, uint32 vector with 2 elements. It is big-endian. Please see CAN bus multiple frames below:
And its data structure for big-endian is shown below:
In our hardware environment, we use "Modbus RTU/ASCII Dual Masters adaptor" as debugger/monitor. Please plug into "Modbus RTU/ASCII Dual Masters adaptor" to your PC USB Port. And if you are the first time to use this adaptor, run software "ConfigTool.exe" to config debug/monitor tool:
In above configuration, The first Uart setting must match Target including baud rate and physical interface (RS485/RS422/RS232). The second part setting can be different, but you must make sure parameter "PC Side USB/Bluetooth Serial Port Baud Rate" in "emModbusServerDebugSetup" block matches it.
After your "Modbus RTU/ASCII Dual Masters adaptor" connects to Target (dspic33) and PC USB, right click on any empty space of simulink model, pop up context menu, click on menu item "Build Embedded Target and open its IDE"
It will start build, and popup dialog window to input base sampling period:
Input your base sample time and click OK. If any error occurs, it will stop building, and display error information. The error block will display in Yellow color. If build successfully, it will popup window to ask you firmware directory for your IDE project.
If you give out the correct IDE project directory, Simulink will open IDE software automatically. And you can compile firmware in your IDE, and program into Target by emulator IDE supported.
If your firmware program into target successfully, you can use Microchip CAN bus Analyzer or Dafulai Electronic CAN bus Analyzer Hardware/software to transmit CAN BUS frames. Microchip CAN bus Analyzer cannot transmit remote frame, but Dafulai Electronic CAN bus Analyzer can transmit remote frame.
We used Microchip CAN bus Analyzer to test. Run PC Software "CAN BUS Analyzer". Please see screenshot below:
It means that PC transmit CAN bus standard data frame with CAN ID=0x31 and DLC=8, and D0=0x11,D1=0x12, ... , D7=0x00.
We will receive this CAN bus data. We run simulink in PC. The result is shown below:
From the result above, it matches what we transmit from PC.
We send out long data frames by CAN Bus analyzer as below:
We saw the result by running simulink, the result is shown below:
From the result above, it matches what we transmit from PC. But we saw "Fault" output port of "emCANSetup" is true. This is correct because we only send once long data frame , we didn't send any CAN bus frames periodically. So we detect "CAN Bus communication fault".
Please open "Your embedded creator library folder"/examples/example10_emCANRead.slx (You must change "PC Com Port for Debug or Monitor" in emModbusServerDebugSetup block according to your physical USB port number) |