PIC32 Tutorial – Part 7 – Get Coding


Josef van Niekerk" rel="author">Josef van Niekerk

Now we’re finally ready to start writing our first tiny little application. This trivial implementation is not going to do much, all we want is to test our setup and see that we’re actually able to write, compile and flash a binary image onto the MCU’s memory.

You’ll be needing the MPLABX IDE to complete this part of the tutorial. Download the file from the Microchip’s site or refer back to Part 2 – Ingredients Once you have the file, simply run the installer. I’m not going to cover the installation in depth, as it’s pretty straight forward. Click a couple of Next buttons, and you’re good to go.

You’re also going to need a compiler. I do recommend you get the Microchip XC32 compiler downloadable at this page. The compiler is semi-free, and should be adequate to get your started. Well, “semi-free” you ask? Yes, the compiler is fully functional but it has limited optimisation capabilities. Limitation of the free version doesn’t have a time limit restriction, or a limitation in size of compiled binaries, the only compromise is basically the amount of optimisation it offers.

With the Standard version of the XC32 compiler, you apparently get a 20-25% reduction in binary size, and with the pro version, up to 50% reduction. This becomes important when your applications become larger, and you want to utilise the amount of memory you have more efficiently. My understanding is also that the optimized code runs a bit faster, but by how much, I’ll leave to a later discussion.

Once you’ve downloaded XC32, run the installer and complete the indicated steps.

The source code for the LED blinky application is going to come from a Mercurial repository I created on BitBucket.

Mercurial is a cross-platform, distributed revision control tool for software developers – Wikipedia.

If you don’t have Mercurial installed on your machine, you can install it from http://mercurial.selenic.com/. If you don’t want Mercurial on your machine, that’s not a train smash, you can manually download the source code project directly from https://bitbucket.org/sparkbuzz/pic32_tutorial_1.x.

Assuming you have Mercurial installed, let’s carry on by starting MPLABX. From the MPLABX main menu select Team > Mercurial > Clone Other. This will present you with the following dialog box:

[responsive imageid=’121′ size1=’0′ size2=’600′ size3=’1000′]

Enter ssh://bitbucket.org/sparkbuzz/pic32_tutorial_1.x into the Repository URL text box, and click Next. Leave the User and Password text boxes empty, as you’ll only be accessing a read only copy of the source code.

[responsive imageid=’122′ size1=’0′ size2=’600′ size3=’1000′]

In Step 2, you can accept the defaults, and click Next.

[responsive imageid=’125′ size1=’0′ size2=’600′ size3=’1000′]

Select a location where you would like to clone the project on your PC, click Finish and click Open Project in the Checkout Completed message box.

[responsive imageid=’124′ size1=’0′ size2=’600′ size3=’1000′]

The LED Blinky project is extremely simple. Navigate to the Source Files folder in the MPLABX Projects panel, and you’ll notice main.c sitting there all lonely by itself. This is the only source file that is of any relevance in this project. It contains the following code:

#include <plib.h>
#define SYS_FREQ 80000000

int main(void)
{
    SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

    mPORTAClearBits(BIT_0);
    mPORTASetPinsDigitalOut(BIT_0);
    while(1) {
        unsigned int t = 0;
        mPORTAToggleBits(BIT_0);
        while(t < 100000) {
            t++;
        }
    }
}

What the above code snippet does is configure Pin 2 on the PIC32MX250F128B MCU to become a digital output pin. The while(1) loop executes indefinitely and is basically slowed down by another nested while(t < 100000) loop that allows the mPORTAToggleBits(BIT_0); function to toggle the output of Pin 2 high/low, slowly enough so we can perceive the toggling of the LED state with our human eyes.

Next, we need to set the default project by clicking Run > Set Main Project > pic32_tutorial_1 from the MPLABX menu. Next, the source code needs to be compiled, and flashed to the MCU’s memory. Make sure that the PICkit 3 is plugged into the breadboard, and connected to your machine via a USB cable. On the MPLABX main toolbar, click the Make and Program Device Main Project icon, the fourth icon from the right with the green down arrow.

MPLABX will start chugging away at firstly compiling, and then programming the compiled binary to memory on the MCU. You should see the following output:

*****************************************************
Connecting to MPLAB PICkit 3...
Firmware Suite Version.....01.28.92
Firmware type..............PIC32MX
Target detected
Device ID Revision = A1
The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x1ffff
boot config memory
Programming...
Programming/Verify complete

If you see the above, and the LED starts flashing, you deserve a pat on the back, as our hard work has paid off, and you’ve programmed the PIC32MX250 with great success.

We now know that everything works, and basically, the road is paved for further exploration and experimentation.