Execute Open-Source Code in a PIC Microcontroller Using the MPLAB IDE
The PIC32 single-board computer is a de facto standard tool for developing microcomputer applications within the hobbyist and educational communities. It provides an open-source hardware (OSH) environment based on a simple microcontroller board, as well as an open-source (OS) development environment for writing software for the board.
Here’s an approach that enables PIC32 code to be configured for execution with the Microchip Technology PIC32MX250F128B small-outline 32-bit microcontroller. It uses the Microchip Technology MPLAB X IDE and MPLAB XC32 C Compiler and the Microchip Technology Microstick II programmer/debugger.
Your own reasons for using this approach will depend on your personal needs and background. Perhaps as a long-term PIC32 user, you want to explore a new processor performance option with your existing PIC32 code base. Or, you want to take advantage of or gain experience with the Microchip advanced IDE development tools and debug with your existing PIC32 code. All of these goals are easily achieved using the approach and the beta library covered in this article.
Several fundamental open-source PIC32 code examples are described using the beta core library of PIC32 functions I developed. The beta version is available, for evaluation purposes only, as a free download from the “PIC32 Library Code for PIC32” link on my KibaCorp company website, kibacorp.com. From there, you can also download a short description of the Microstick II hardware configuration used for the library.
To illustrate the capabilities in their simplest form, here is a simple Blink LED example from my book Beginner’s Guide to Programming the PIC32. The example shows how this custom library makes it easy to convert PIC32 code to a PIC32 binary file.
PIC32 BLINK EXAMPLE 1
The PIC32 code example is as follows: Wire an LED through a 1-K resistor to pin 13 (D7) of the PIC32. An output pin is configured to drive an LED using pinMode () function under setup (). Then under loop () this output is set high and then low using digitalWrite () and delay () functions to blink the LED. The community open-source PIC32 code is:
PIC32 EXAMPLE 1 CODE MODIFICATIONS
The open-source example uses D13 or physical pin 13 on the PIC32. In relation to the PIC32MX, the D13 is physical pin 25. Pin 25 will be used in prototyping wiring.
Now, let’s review and understand the PIC32 project template and its associated “wrapping functions.” The PIC32 uses two principal functions: setup () to initialize the system and loop () to run a continuous execution loop. There is no Main function. Using the Microchip Technololgy XC32 C compiler, we are constrained to having a Main function. The PIC32 setup () and loop () functions can be accommodated, but only as part of an overall template Main “wrapping” function. So within our PIC32 template, we accommodate this as follows:
This piece of code is a small but essential part of the template. Note that in this critical wrapping function, setup () is called once as in PIC32 and loop () is configured to be called continuously (simulating the loop () function in PIC32) through the use of a while loop in Main.
The second critical wrapping function for our template is the use of C header files at the beginning of the code. The XC32 C compiler uses the C compiler directive #include reference files within the Main code. PIC32 uses import, which is a similar construct that is used in higher-level languages such as Java and Python, which cannot be used by the MPLAB XC32 C.
The two include files necessary for our first example are as follows:
System.h references all the critical Microchip library functions supporting the PIC32MX250F128B. The Ardunio.h provides the PIC32 specific library function set. Given these two key “wrapper” aspects, where does the PIC32 code go? This is best illustrated with a side-by-side comparison between PIC32 code and its Microchip equivalent. The PIC32 code is essentially positioned between the wrapper codes as part of the Main function.
This approach enables PIC32 code to execute on a Microchip PIC32 within an MPLAB X environment. Note that the PIC32 code void setup () now appears as void setup (void), and void loop () appears as void loop (void). This is a minor inconvenience but again necessary for our C environment syntax for C prototype function definitions. Once the code is successfully compiled, the environment enables you to have access to the entire built-in tool suite of the MPLAB X and its debugger tool suite.
RUNNING EXAMPLE 1 CODE
Configure the Microstick II prototype as in the following schematic. Both the schematic and prototype are shown below:
Table 1 compares PIC32 core functionality to what is contained in the Microchip PIC32 expanded beta library. In the beta version, I added additional C header files to accomplish the necessary library functionality. Table 2 compares variable types between PIC32 and PIC32 variable types. Both Table 1 and Table 2 show the current beta version has a high degree of PIC32 core library functionality. Current limitations are the use of only one serial port, interrupt with INT0 only, and no stream capability. In addition, with C the “!” operator is used for conditional test only and not as a complement function, as in PIC32. To use the complement function in C, the “~” operator is used. The library is easily adapted to other PIC32 devices or board types.
EasyEDA: Ideas for Circuit Design, Innovation for Electronics Access
Free Circuit Design: Schematic – Simulation – PCB Layout – Gerber Viewer
JLCPCB Prototype: Only $2 for 10pcs 10×10cm, 24 hours Quick Turn, DHL Delivery in 3 days