Pages Menu
TwitterRssFacebook
Categories Menu

Posted by on Mar 24, 2016 in Featured, Getting Started, Microchip PIC, Microcontrollers | 0 comments

Using Microchip MPLAB Xpress IDE and Evaluation Board

Using Microchip MPLAB Xpress IDE and Evaluation Board

Microchip recently launched its new MPLAB Xpress cloud-based IDE and also a supplemental $10 Xpress development board. I was able to get my hands onto one of the boards (thanks Microchip). So here’s a short post on what it is and how to use it.

Contents

Cloud is here!


Cloud based tools are becoming more and more popular these days because.. well.. it’s 2016 and cloud’s everywhere – from Dropbox to Google Drive to servers and databases. So much that I don’t have a single byte of data stored on my computer locally. All my documents/files are on Google Drive, and all my code is in either Github or AWS or other cloud based tools.

The reason cloud has become so popular is because of its storage and performance. Clearly the cloud servers have terabytes of storage available (you gotta pay for it though) and have much more powerful processors with greater memory available. So why should I use my own computer for anything? There’s an exception for security though – cuz here’s a truth that you should know (get it on stickermule):

no-cloud-sticker

So why don’t we use cloud to write and compile our code? We have been doing this historically by logging into remote servers and executing code, but there wasn’t any GUI based IDE around it and using vim would be a pain in the neck.

Enter cloud IDEs. They are becoming quite popular these days. Take Cloud9 for example which allows you create a fully functional Linux virtual machine and an IDE to edit and manage the project right in your browser. Such IDEs can compile and run your code in the browser itself (actually the virtual machine). And you skip the hassle of installing/updating bulky IDEs on your computer like Visual Studio, Xcode, etc.

But what happens in the case of a hardware project is that you can’t run the executable on the online virtual machine instance – you can simulate it, but not run it. Somehow you need to get that executable onto the hardware you’re working with. Just because there’s the hardware connected, the online virtual machine running the cloud IDE cannot talk to it directly – which could be challenging especially while hardware testing and debugging. There are many ways to do it (like installing a browser plugin, using software USB bridges, or downloading the code and flashing it using traditional methods).

The MPLAB Xpress IDE


The MPLAB Xpress IDE is Microchip’s cloud based IDE to program 8-bit PIC microcontrollers (16-bit and 32-bit functionality to be added by the end of 2016 as per Microchip’s website). The IDE lets you write code, create/manage projects, and build executables. There’s more – like browsing code examples, connecting to forums for community support, etc. And all of this without installing/updating any kind of software/toolchain on your computer. You don’t even need a hardware to get started with it. Neat.

You can also connect a hardware programming tool like the PICkit 3 to MPLAB Xpress via a software USB bridge and program any PIC device.

MPLAB Xpress IDE

MPLAB Xpress IDE

The MPLAB Xpress Development Board


Microchip also launched the $10 Xpress Evaluation Board which has a PIC16F18855 onboard. You can order one from the same page (there’s a promotional offer going on as of today – March 2016). The PIC16F18855 has 14KB flash, 1KB SRAM and 256B EEPROM – not a whole lot of memories, but has almost every peripheral that you’d need to evaluate/ learn about PIC microcontrollers.

Xpress Development Board

Xpress Development Board

Something that’s interesting about this board is that the onboard programmer comes with a USB driver on it, which basically lets you transfer the execuatable to PIC’s flash just by dragging and dropping. We’ll talk more about it later in the post. Without further adieu, let’s get our hands dirty with this board using the cloud tools.

Hello World – The LED Blinky


Let’s build a Hello World program to test the board. For embedded systems, this is usually lighting up and blinking an LED connected to the microcontroller.

Step 1: Create New Project


You should create a new project for every new application you’re building. Don’t mix and match – it usually doesn’t turn out well. Creating a new project is simple.

First go to Microchip’s website and log into the MPLAB Xpress IDE (click on the big blue “My Account” button – no it’s not required for you to create an account, but it is recommended so that you can save your files and some additional features).

Then go to File > New Project. Do it in the IDE, and NOT in your browser. You’d be surprised how many people get confused between the two! Here’s a picture so that you don’t mess up:

New Project 1

In the popup window that appears, choose Microchip Embedded as the category and the Standalone Project. You can choose Microchip Examples if you want, but we won’t be using it here. Then click Next.

New Project 2

In the next window, type in the device name as PIC16F18855. Why? Because this is what’s on the Xpress board. Click Next.

New Project 3

Then name your project. For the love of god, please use meaningful names! Need help? Name it LEDBlinky. Then click Finish.

New Project 4

That’s it. Your project is now created. Time to add files!

Step 2: Add main Source File


Now we need to add files to the project. In the Project section, you’ll find your project listed (LEDBlinky) with two sub-folders – header files and source files. Right click on Source Files > New File…

New File 0

In the popup window, choose category as Microchip Embedded > XC8 Compiler and the type of file as C Main File. You can also choose C Source File, in which case you’ll have to write the main function yourself. XC8 is the MCU8 compiler for PIC microcontrollers. Click Next to proceed.

New File 1

Then name the file and the extension. Once again, choose meaningful names (like main.c or led.c or ledblinky.c). Click Finish and you’re good to go.

New File 2

You should see the file created as follows:

New File 3

Step 3: Write Code!


Time to write some code! Before we start, there are a few things you should know. First is that there are four red LEDs on the Xpress Board (D2 through D5) connected to pins RA0 through RA3 (view the schematic). Second is that the PIC is running at 4MHz internal clock frequency (at least it does for me, let me know if it is different for you).

We start with the header and macros. You have to include xc.h header file, which contains the definitions of all the registers of PIC16F18855 and also some library functions. Also define the _XTAL_FREQ and set it to 4MHz since it will be used by the delay functions we’ll be using.

#include <xc.h>
#define _XTAL_FREQ 4000000 // clock freq

Next we need to set RA0 pin to output. By default it is set to input. In order to do so, you’ll have to set zeroth bit of TRISA register to 0. Setting to 1 will make it an input pin.

TRISA &= ~(0x01);    // or TRISA &= 0xFE; 

This just sets the RA0 pin to output and leaves the other pins as input (bit manipulations!). If you’d like to light up all the four LEDs, then set the entire port as output port.

TRISA = 0x00;

The 0x in the above code means that anything that follows is in hexadecimal number format. Also you may be wondering if TRISA = 0x00; is even a valid C syntax (if you’re not, then you should!). Somewhere buried deep down in one of the header files included by xc.h is a line like this:

#define TRISA    (*((uint16_t volatile *) 0x011))

Which means basically you’re writing 0x00 to a volatile memory location 0x011. TRISA register is memory mapped to this location in the hardware. So basically this:

(*((uint16_t volatile *) 0x011)) = 0x00;

This might still look weird to you, but this is precisely why C is sometimes called a “low-level” high-level language. It gives you access to the low level memory while still providing you with high level functionality.

By default the pins are low. In order to toggle them, we can use bitwise XOR operation. We do this on PORTA’s latch register: the LATA register.

LATA ^= 0x01;

You might find some places where they modify PORTA register (instead of LATA register) to change the state of the pins. Well technically you use LATx register to write to the port and PORTx register to read the current state of the pins. Using PORTx to write could land you into read-modify-write problems.

And of course, you’d like to “see” the LED blink. So we need a delay. Depends upon you what you want to set it to. You can either write your own delay function using timers, or use one of the library functions from xc.h__delay_ms() for milliseconds delay and __delay_us() for microseconds delay. In order for them to work, you must specify the correct clock frequency as _XTAL_FREQ.

__delay_ms(100);    // 100 ms delay
__delay_us(100000); // also 100 ms delay

And all of this (the toggle and delay) needs to go into an infinite loop, just because we don’t want it to stop blinking after a while – we want it to blink forever (until the power is turned off). This is pretty standard in embedded applications where you don’t know how long the device will be operational – could be seconds, minutes, days, weeks, months, years, or even decades!

// Infinite loop
while(1) {
    // application code goes here
    // ...
}

Putting it all together…

#include <xc.h>
#define _XTAL_FREQ 4000000 // clock freq

void main(void) {
    
    // Pin RA0 connected to D2 LED
    // Set RA0 as output pin
    TRISA &= ~(0x01);    // or TRISA &= 0xFE; 

    // Infinite loop
    while(1) {
  
        // Toggle RA0
        LATA ^= 0x01;
        __delay_ms(500); // delay
    }           
    return;
}

Step 4: Build Project


Once you have written the code, save it. Next build the project, which will execute the makefile that was created with the project. The makefile will compile all the files in the project and build an executable from it that can be run on the target hardware (the Xpress board). You can build the project by going to Run > Build Project from menu or by clicking on the hammer icon: Build Icon

You can also choose to Clean and Build Project if you’re having any trouble. If it shows a successful build message in the output window, you’re good. Otherwise it’ll show you the errors – types, file and line number. Fix them and rebuild the project.

Step 5: Download Executable


Next step is to download the executable. If you’re using an external programming tool like PICkit 3 to program, then skip to step 6a.

For the Xpress board, click on the Make and Program Device icon: Make and Program Icon

This basically rebuilds the projects and downloads a .hex file. Since it rebuilds the project, it makes step 4 kind of optional. But you wouldn’t want to skip step 4, especially when working on larger projects where you’ll have to build multiple times before deploying it in the hardware.

The .hex file is the executable that the PIC microcontroller understands. Think of it like .exe files for Windows. This is the machine level code that humans have dreaded since the day the first computer was made in 1950s.

Step 6: Transfer Executable to Target Board


The .hex file does no good by sitting on your local computer. Your computer can’t understand it, only the PIC does (because your computer most likely has an x86 based processor, which is a completely different architecture as compared to PIC’s architecture). This file must go into the 14KB flash memory of the PIC16F18855.

Connect the Xpress board to your computer via a USB cable. Make sure you choose the right cable, some USB cables only have the power lines connected, so they won’t be any good.

The programmer on the Xpress board has an awesome USB driver that allows the board to load as a mass storage device when connected to your computer. All you have to do is to drag and drop (or copy-paste) the .hex file into the mounted directory of the mass storage. The programmer takes care of the rest. You don’t need to install anything on your computer.

Once done, you should see the D2 LED blinking as soon as it finishes. If not, press the hard reset button. If the LED is not blinking, check your code, check your delay, and check your XTAL frequency.

Step 6a: Transfer Executable to Target via External Programming Tool (not for Xpress Board)


If you’re using the Xpress board, you don’t need this step, skip to step 7. If you’re using an external programming tool to program your PIC (only PICkit 3 supported at the time of writing this), this is what you should do.

Right click on your project name in the project window (LEDBlinky in this case) in this case and then choose Project Properties. Select PICkit 3 as the Hardware Tool. You can also choose an updated version of the XC8 compiler in Compiler Toolchain. Click Apply/OK.

Choosing Hardware Tools

Choosing Hardware Tools

Click on the Make and Program Device icon: Make and Program Icon

If this is your first time doing it, you’ll see a dialog box like this:

USBDebug-Microchip

Prompt to connect USB Bridge

The USB Bridge is a software tool that connects your browser to your hardware – just like a bridge! Go ahead and update Java if needed. Download and run the USB Bridge Tool. Enter the unique token into the USB Bridge tool when prompted. If all goes well and your PICkit 3 is connected, then you should see the message “Everything is looking good” in the tool. The USB Bridge tool must stay open and connected to your hardware every time you program your device.

USB Bridge Tool - All Good

USB Bridge Tool – All Good

Additionally you should also see the green lights in your MPLAB Xpress status bar on the lower right corner of your browser window.

MPLAB Xpress Hardware Tools Status

MPLAB Xpress Hardware Tools Status

Click on the Make and Program Device icon again: Make and Program Icon

Upon successful completion, you should see “Programming/Verify Complete” message in the Debugger Console. Once done, you should see the D2 LED blinking as soon as it finishes. If not, press the hard reset button. If the LED is not blinking, check your code, check your delay, check your XTAL frequency, and check if you programmed it correctly.

Step 7: Enjoy!


That’s it. What are you waiting for? Make some patterns out of those LEDs. If you have used PICs before, then do something with the switch, the pot, the ADC, timers, etc.

IMG_20160323_234354010

Summary

  • Cloud is here! Deal with it.
  • Microchip launched it’s new cloud based MPLAB Xpress IDE recently along with a shiny new Xpress development board.
  • Creating and building projects on the cloud IDE isn’t much different from a regular desktop MPLAB X IDE. You can also download the entire project from the cloud and run in on desktop MPLAB IDE and it’ll run just fine.
  • Transfer the code to the flash by simply dragging and dropping – thanks to the USB driver in the programmer – or use a PICkit 3 programming tool.

Thank you for sticking till the end and reading this 2500+ word article. Any questions or comments, post it down here! Subscribe to stay updated! Adios.

Written by Mayank Prasad (Max)
Last updated on July 6, 2016

Leave a Reply

%d bloggers like this: