Example | Top Previous

Examples Example1: Our embedded platform is dsPIC33EP256GP502 . This MCU uses dual words writing for flash. Please see screenshot of model below: As you see above, in order to watch result, we used "probe" blocks. So we used "Modbus RTU/ASCII Dual Masters adaptor" as debugger hardware. This hardware connects Target Uart2, and Uart2 controls RS485, TX_transmit Enable is controlled by RB11. Logic High will enable RS485 transmit and disable RS485 receiver. Please see our "emModbusServerDebugSetup" block settings below (only for debugging): 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. Double click block "setup Simulated EEPROM", we will see parameters settings for "Simulated EEPROM" below: It means Our MCU only supports dual words flash writing. And our Simulated EEPROM size is 256 bytes. The initial EEPROM values for empty EEPROM is [ 0 0 1 24]. which means 0, 0, 1, 24, 0, 0 ,0. 0, 0, 0, ..., 0. Double click block "simEEPROMRead", we will see the block "simEEPROMRead" parameters settings below: This block will read 9 bytes' data from address 0 (Input port "StartAddr") to address 8. For block "simEEPROMWrite", it will write 3 bytes (input port "Data"): 11 12 and 13 (from constant block "data4write" which has uint8 type of constant vector output). For block "simEEPROMWriteFlash", it will write flash if input port "En" = true and if never update new value to flash. "En" is from "PowerDown" block. When "PowerDown" Digital Pin is logic Low, it leads to "En" Logic high and enable "simEEPROMWriteFlash". If you have no power-down detection pin, you can enable "En" periodically when system is in stop state. Double click on block "PowerDown", you will see parameters for this digital input: It means that Power down pin is Port B's bit 6, and we enable filter, initial Power down pin value is 1 which means "power on". "Probe" blocks are just for watching "OK" state of "read/write" and watching read result data. For any block output except "Constant block", if its name does not contain "Right Arrow" (à), you must use "Probe" block to watch its value. 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). And you must config Flash. 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" 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. We can use Simulink directly view results. By select stop time= inf, and click "Run" button, you will see result below: From above "Display4readOK" block, its value is 1 (true), so "Read EEPROM success". "Display4WriteOK" block displays "1" (true), so "Write EEPROM success". "Display4WriteFlashOK" block displays "1" (true), so "Write Flash success". "Display4WriteFlashOK" block displays " 0, 0, 1, 24, 0, 0 , 11, 12,13", it is what to expect. " 0, 0, 1, 24, 0, 0" is from parameter "Empty EEPROM initial byte Data" of "set up Simultated EEPROM" block. "11, 12,13" is from block "data4write". Please open "Your embedded creator library folder"/examples/example8_simEEPROM.slx (You must change "PC Com Port for Debug or Monitor" in emModbusServerDebugSetup block according to your physical USB port number)