Setup a Microchip 8-bit PIC MCU development workflow on Linux

lun. 18 d├ęcembre 2017 by Mick Cherry

In this article, we will describe how to setup an embedded development workflow on Linux targeting the well known PIC 8-bit MCU line from Microchip.

We will first see how to install all the Microchip suite on Linux, then we will develop the mandatory led-blinking example to validate the whole workflow.

This article is (probably) also ok for other 8-bit PIC part numbers.

The PIC12F975

In this example, we are going to compile and flash a custom firmware for the 8-bit MCU PIC12F675. This MCU has a flash memory of 1024 words (of 14 bits each), 64 bytes of RAM and an EEPROM of 128 bytes. It features 6 I/O pins and some timers and analog comparators.

PIC12F675

Install Microchip IDE & compiler

We use the Ubuntu Linux distribution in our example (Ubuntu 17.10). Fortunately, Microchip does provide us with a whole IDE & compiler suite that runs on Linux: the compiler is named XC8 and the IDE MPLAB X.

Follow these steps to get both installed on your Linux distribution:

Download MPLAB X IDE from Microchip.

Download the XC8 compiler (for 8 bits MCUs) from Microchip.

Install the 32 bits compatibility libraries if your Linux distribution is 64 bits:

$ sudo dpkg --add-architecture i386
$ sudo apt-get update
$ sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386
$ sudo apt-get install libexpat1-dev:i386
$ sudo apt-get install libx11-6:i386
$ sudo apt-get install libxtst6:i386

Install MPLAB X

$ tar -xf MPLABX-v4.05-linux-installer.tar
$ sudo ./MPLABX-v4.05-linux-installer.sh 
64 Bit, check libraries
Check for 32 Bit libraries
Verifying archive integrity... All good.
Uncompressing MPLAB X v4.05 Installer....

Install the XC8 compiler

$ chmod u+x xc8-v1.44-full-install-linux-installer.run
$ sudo ./xc8-v1.44-full-install-linux-installer.run

Reboot your computer for the new udev rules to be executed

$ reboot

Start MPLAB X

$ mplab_ide &

Done! You should see MPLAB X launching itself in a nice GUI window.

Hardware setup

The led-blinking example is the hardware equivalent of the software Hello World: a quick way to validate the whole development workflow.

We use a PIC12F675 along with a 5V power supply and a conventional LED.

Here are the (very simple) schematic:

Led-blinking example

and it physical realisation:

Led-blinking example

Dev, flash and compile your first program!

Now that the hardware part is ready, time to code!

Follow these steps to get your first program ready:

Launch MPLAB X

File -> New Project -> Microchip Embedded -> Standalone Project -> Next.

Select your MCU (here, we select "Mid-Range", then "PIC12F675").

Next -> Next -> Select your programmer (Pickit3 here) then Next

Select your toolchain (XC8 here) -> Next -> Add a project name -> Finish.

On the "Project" tab of the left-hand side panel, right-click on "Source Files" then "New", then "main.c".

Name it whatever you want ("main" ?) then "Finish".

You can now write the code you want to compile. Here is the complete main program we use for led-blinking:

/*
 * File:   main.c
 * Author: Mick Cherry
 *
 * Created on December 18, 2017, 9:00 PM
 */

#include <xc.h>

#pragma config FOSC = INTRCIO   /* Use the PIC internal oscillator */
#pragma config MCLRE = OFF  /* Use GP3/MCLR as GP3 IO */

void delay()    /* A simple for loop for creating a human-compliant delay */
{
    int counter = 0;
    for (counter = 0; counter < 10000; counter++) {
        ;
    }
}

void main(void)
{
    TRISIO = 0b001000;  /* Set all the GPIO in output mode except GP3/MCLR as input */

    while(1) {
        GP2 = 1;    /* Set the GP2 pin to 1 */
        delay();
        GP2 = 0;    /* Set the GP2 pin to 0 */
        delay();
    }
    return;
}

In order to get a firmware (cross-compiled binary file for the target, here the PIC12F675) inside the PIC flash memory, we need a special piece of hardware: a flasher. Here we use the Microchip Pickit 3 In-Circuit Debugger.

The procedure to connect the Pickit 3 to the PIC is very easy, just connect each of the 5 pins of the Pickit to their respective pins of the PIC12F (check the Pickit 3 pinout on this document).

Pickit connect

Then we just need to hit the "Make and Program Device Main Project" button on MPLAB X to see this on the breadboard:

Led-blinking gif

Yay it's alive!