Matlab-Simulink device driver Blockset for Microchip dsPIC / PIC24 / PIC32 Microcontrollers.
Importing Simulink Generated Code into Mplab
This example explain how to import simulink generated C files (using Real Time Workshop Embedded Coder and the dsPIC blockset) into a MPLAB project. This method should not be used in normal operation because the Blockset compile the generated files so as to obtain the binary file (.hex and .elf). However, compiling the C files within MPLAB allows to debug the software using the MPLAB deugging capabilities.
For this demonstration, I use the simulink model described in the example for Explorer 16 Development Board (with the dsPIC 33FJ256GP710 or PIC 24JF128GA010 microcontroller).
The model file is in the toolbox directory : examples\dsPIC_33f_Explorer16.mdl
This method do not use the MPLAB function to import C code from simulink that may be difficult to use.
- Compile the model
The model must be compiled in simulink (the dsPIC blockset must be installed) to get the matlab lirarys compiled so as to import them easily in MPLAB.
Real Time Workshop creates the subdirectory dsPIC_33f_Explorer16_dspic\Source. It contains the .C and .h source files generated.
Step 2 : MPLAB
from Version 3.3 of the blockset, a MPLAB project file is created. Thus, you just have to compile once the project in MATLAB and then open the created MPLAB project. The following steps are no more required !
- Create a new project
- Select the target device (The same as defined in the simulink model : 33fJ256GP710)
- Select Language Toolsuite : Use the C30 Compiler ( free student version available on the microchip website)
- Import C sources files (from *Toolbox* \ examples \ dsPIC_33f_Explorer16_dspic \ Source )
- Import h source files (from *Toolbox* \ examples \ dsPIC_33f_Explorer16_dspic \ Source )
- Import Matlab library and PIC/dsPIC library : (all .a files in the *Toolbox*\examples\dsPIC_33f_Explorer16_dspic\Source directory ; The .a library corresponding to the PIC in the Microchip directory C:\Program Files\Microchip\MPLAB C30\lib for 24f pic, use the 24Fxxx file ( I use the coff version)
- Import Linker script file for the chosen PIC/dsPIC ; from the microchip directory : C:\Program Files\Microchip\MPLAB C30\support\gld
Illustrations were realized with MPLAB 7.52.
with MPLAB 7.60 and get the following message during the compilation process:
"A language-plugin exception occurred and was logged"
It seems to be an MPLAB error. Refere post about this error on the microchip forum. My advice would be to use MPLAB 7.52.
Why you should avoid to use MPLAB
I would not recommend to import C code files generated with simulink into MPLAB for the following reasons :
- I is easier to maintain and improve code using simulink
- Adding personnal C code can be easily done in simulink using the dsPIC block "call C function"
- There should be no low level bug
- High level debugging is easier in real-time, using MPLAB debugger usually add strange behaviour and make the debugging very hard.
MPLAB should only be used if the generated C code contains bug, or to debug peripheral ( If you find bugs in peripheral C code, please let me know)