Example

Top  Previous 

 

Examples


 

Example1:

 

Our embedded platform is dsPIC33EP256GP502 . Uart2 connects RS485 Transceiver, and TX_transmit Enable is controlled by RB11.  Logic High will enable RS485 transmit and disable RS485 receiver.   This Uart2 connects our "Modbus RTU/ASCII Dual Masters adaptor" as Monitor/Debugger.

 

Please see screenshot of model below:

 

example9_emWait

 

 

In "Setup Target" block, we choose "PIC24/Dspic30/Dspic33"  platform.   Double click "emModbusServerDebugSetup", we will see the Modbus Server/Debug parameters settings below:

 

example9_emWait_setup_param

 

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:

 

DebugTool_Setting

 

 

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.

Double click "emWait", we will see the "emWait" parameters settings below:

 

example9_emWait_param

 

It means this "emWait" block will has 0 hour, 1 minute, 12 seconds and 2 ms delay.

 

Before you build this simulink model,  you must create the firmware project by your IDE.  and remember the firmware project directory name. In our example, it is microchip MPLAX IDE software, you must use MCC to configure timer1 as 1ms timer and interrupt enabled.   You must use MCC to enable Uart2 with interrupt enabled and both "software Transmit Buffer Size" and  "software Receive Buffer Size" =255 or 254.   Timer1 interrupt priority is below UART (you can choose equal too).  We put our MCC configuration file BasePrj.mc3 into example folder for your reference.  You must modify according to your hardware.  You'd better compile your firmware which is created by MCC to identify any error by MCC.

 

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"

 

 

embedded_build

 

 

 

It will start build, and popup dialog window to input base sampling period:

 

embedded_sample

 

 

 

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.

 

embedded_projectDir

 

 

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, please add 3 extra "Display" blocks to watch output ports "Hr/Min/Sec".  And by select stop time= inf, and click "Run" button,  you will see result below:

 

example9_Simulink_result1

 

 

We saw "Display4leftSec" data counting down, and "Display4On" is 0 which means time is not on.  After around 1 min 12 secs and 2 ms, time is on, we will  see result below:

 

example9_Simulink_result2

 

Output ports "Hr"/"Min"/"Sec" are 0, and Output port "On" is true. So delay time has done.

 

 

 

Please open "Your embedded creator library folder"/examples/example9_emWait.slx or example9_emWait_1.slx  (You must change "PC Com Port for Debug or Monitor" in emModbusServerDebugSetup block according to your physical USB port number)