|
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. .
Parameter "Sync Rx CAN ID" is no use for this example because we disable "Sync".
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):
Double click block "emCANTxShort", we will see the parameter settings below:
It means that this block will transmit standard short data frame with CAN ID=0x67. Because parameter "Transmitted data type"=[int8*2 bool*4 int32]. So DLC=2+1+4=7, and 3 data input ports. The 1st Data input is int8 vectors with 2 elements. The 2nd Data input is bool vectors with 4 elements. The 3rd Data input is int32 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. The parameter "Need response to this transmitted sequence" is true. So after transmitting, controller will wait for received special CAN bus frames which is extended CAN ID =12340112 and this response CAN data frame is long data frame. Response Data type=[ int8*2 bool*6 uint32*2]. It means it has 3 Data output ports, the 1st data output port "Data1" is int8 vector with 2 elements, the 2nd data output port "Data2" is bool vector with 6 elements, the 3rd data output port "Data3" is uint32 vector with 2 elements. Please see response CAN bus data long frame structure below:
Please see data field of this response long 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 vector area.
Double click block "emCANTxlong", we will see the parameter settings below:
It means that this block will transmit extended long data frame with CAN ID=0x88. Because parameter "Transmitted data type"=[int8*2 bool*6 int16*3]. So DLC=2+1+6=9, and 3 data input ports. The 1st Data input is int8 vectors with 2 elements. The 2nd Data input is bool vectors with 6 elements. The 3rd Data input is int16 vectors with 3 elements. It is Big-Endian. Please see response CAN bus data long frame structure below:
Please see data field of this long frame below:
The yellow color is int8 vector data area. The white color is bool vector area (Bit7 is the 1st element of bool vector). The blue color is int16 vector area.
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:
We see the Target transmit CAN ID =0x67 standard frame and CAN ID=0x88 extended long data frames.
From screenshot above, we see our Target transmitted CAN bus frames. We saw CAN ID=0x88x (extended frame) is transmitted once after 5.001 Sec (Red color: around 5 sec) from transmission of CAN ID=0x67. This is correct because CAN ID=0x67 need response and its timeout is 5 sec. We didn't send out response. So timeout 5 Sec. However, we saw the time from CAN ID=0x88x second frame to CAN ID=0x67 is 0.995 sec (Red color: around 1 sec). Our sample time is 0.5 Sec. Why it is 0.5x2= 1 Sec? The reason is block "emCAN Channel all Tx Done?" and "Set CAN Tx Idle" must run before block "Is CAN Tx Channel Idle?".
How to make "emCAN Channel all Tx Done?" and "Set CAN Tx Idle" run before block "Is CAN Tx Channel Idle?" ? Please click "Debug/Information Overlays/Blocks/Execution Order" like screenshot below:
You will see model as below:
Right click on block "Is CAN Tx Channel Idle?" ,"Set CAN Tx Idle", and "emCAN Channel all Tx Done?" to display context menu. Click "Properties..." to make Priority of block "emCAN Channel all Tx Done?" and "Set CAN Tx Idle" smaller than Priority of block "Is CAN Tx Channel Idle?" ( Small number has higher priority).
You must re-build model and re-program target. After programming again, we open CAN bus Analyzer again. Please see screenshot below:
You will see red color 0.498 second (around 0.5 Sec). That's what we expect.
We run Simulink in PC, the result is shown below:
We saw the block all data output ports of "emCANTxShort" are 0. This is because we didn't send out response frames from CAN bus Analyzer. We transmit 2 CAN bus data frames by CAN Bus analyzer as below:
Now we can see Simulink running result below:
Now we see result in red color. It is data we respond to.
Please open "Your embedded creator library folder"/examples/example11_emCANTransmit.slx (You must change "PC Com Port for Debug or Monitor" in emModbusServerDebugSetup block according to your physical USB port number)
|