Example | Top Previous

Examples Example: 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. Our embedded Modbus Server ID=1. This example function is "Sequence control". Seq0: When power on, Digital out "RB12", Digital out "RB13", and Digital out "RA4" keep logic 0. Target MCU waits for Digital Input RB7. If RB7 becomes logic 1, MCU will enter next Sequence called "Seq1". Seq1: Digital out "RB12" keeps logic 1. Timer start to count time elapsed. If 12 seconds elapse, Target MCU will enter next Sequence called "Seq2". Seq2: Digital out "RB12" keeps logic 0. Digital out "RB13" keeps logic 1. Timer start to count time elapsed. If 12 seconds elapse, Target MCU will enter next Sequence called "Seq3". Seq3: Digital out "RB13" keeps logic 0. Digital out "RA4" keeps logic 1. Timer start to count time elapsed. If 12 seconds elapse, Target MCU will return to initial Sequence called "Seq0". Please see screenshot of model below: Notes: Input port u1 of "Switch case" block is "State Variable", You must use output of "Probe" to connect it in order to monitor "State Variable" value of "Finite State Machine" in PC side. We set up Tag of all "case action subsystem" by right clicking "Properties" on each "Action Subsystem" such as the following picture: The Tag value must be the same value such as below: "Case 0 Action Subsystem" , which called "Seq0", implements Function of "Seq0". Double Click "Case 0 Action Subsystem", you will see the implementation below: "Case 1 Action Subsystem" , which called "Seq1", implements Function of "Seq1". Double Click "Case 1 Action Subsystem", you will see the implementation below: The block "ColorDisplay" is the same as counterpart of Seq0. You just use Copy/Paste. "Case 2 Action Subsystem" , which called "Seq2", implements Function of "Seq2". Double Click "Case 2 Action Subsystem", you will see the implementation below: The block "ColorDisplay" is the same as counterpart of Seq0. You just use Copy/Paste. "default Action Subsystem" , which called "Seq3", implements Function of "Seq3". Double Click "default Action Subsystem", you will see the implementation below: The block "ColorDisplay" is the same as counterpart of Seq0. You just use Copy/Paste. 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 you build this simulink model by right clicking "Build Embedded Target and open its IDE" on any empty space of model, you program the firmware to target by your emulator. You run simulink model, and you will see picture in PC below: It means that target MCU is in "Seq0" state. Digital Out RB12, RB13, and RA4 all are Logic 0. When momentary button is pressed once which causes digital input RB7 logical 1, Target MCU enters next state "Seq1". Please see picture below: In state of "Seq1", Digital out RB12 changes to Logic 1. And after 12 seconds, Target MCU enters next state "Seq2". Please see picture below: In state of "Seq2", Digital out RB12 returns to Logic 0, Digital out RB13 changes to Logic 1. And after 12 seconds, Target MCU enters next state "Seq3". Please see picture below: In state of "Seq3", Digital out RB13 returns to Logic 0, Digital out RA4 changes to Logic 1. And after 12 seconds, Target MCU enters initial state "Seq0" to wait for momentary button pressed again. Please open "Your embedded creator library folder"/examples/example15_StateMachine.slx (You must change "PC Com Port for Debug or Monitor" in emModbusServerDebugSetup block according to your physical USB port number)