Setting up MPLABX with XC8 Compile for PIC8

MPLAB X IDE and XC8 C Compiler Setup for PIC 8 Bit Development

By Timothy Coyle

The PIC family of microcontrollers from Microchip is well known in the embedded design world and I remember working on the PIC in my microcontroller class in college. You will be hard pressed to find an embedded designer or maker that hasn't used or at least come across the PIC. So let's go through the installation and setup of the Microchip tools to get started programming on the 8 Bit PIC. For this tutorial I chose the 8 Bit PIC16F877A (get the datasheet here).

Installing MPLAB X IDE and XC8 C Compiler

Go to the Microchip website and first download the latest version of the free Microchip development tool MPLAB X IDE.

microchip pic

After you run the installation you will have a few things left to install. Use the provided links to download and install the XC8 C compiler (free version) and also the Code Configurator.

microchip pic microchip pic

The Code Configurator can be used for 8 and 16 bit PIC development to leverage drivers for peripheral devices and the GUI interface helps reduce development time.

Setting Up a Blink LED Project for the PIC16F877A in MPLAB X IDE

microchip pic

First create a new project.

microchip pic

Select your PIC device. In our case we are using the PIC16F877A.

microchip pic

Select your development environment. You can select simulator if you don't have any hardware or something like the PICKIT3 if that's what you are using to connect to your PIC device.

microchip pic

Select the XC8 compiler that you previously installed.

microchip pic

Choose your project location.

microchip pic

Now you project is created.

microchip pic

Create your main.c file.

microchip pic

Now your project is all set up and you are ready to start coding!

Blink LED Sample C Code for PIC16F877A

When you are using 8 bit PIC devices the first thing you need to do is to configure the device to work properly. You need to set some registers and make sure those settings are passed to the compiler. First you need to look at the PIC16F877A datasheet and search for the configuration bits section to understand what options are available to you.

microchip pic microchip pic

From the datasheet you can look to see what the different bits are in the configuration register. You then use these values in the Configuration Bits window.

microchip pic

You need to configure these configuration bits to match your design. For example you would want to disable the watchdog timer unless you are using it. The osciallator should match the hardware oscillator which is usually a high-speed crystal oscillator. (In our example we assume it's an 8 Mhz external oscillator). Once you are done with your setting you can copy the code to your main.c file.

The next thing you need to do is to look at the datasheet again to determine which registers (SFR are special function registers) you are going to access in order to drive an externally connected LED.

microchip pic

Looking at the device pinout you can see the different ports available on the PIC16F877A.

microchip pic

We are giong to take a closer look at Port A and how we can use that to drive the external LED.

microchip pic

So if we want to use RA0 on Port A we need to use the TRISA register to set the Port A pins to be output pins. A lot of pins on the 8 bit PICs share functions so often times you also need to disable/set other shared pin functions. In our case we need to set the ADC shared function so it's a digital output and not an analog input. Then you can use a while loop and write to the bit RA0 on Port A to make it go high and turn on the LED.

Below is an example of how the code could look to blink an external LED connected to RA0 pin. This is just an example (not tested on real hardwre but did compile error free) so you may need to change the settings to work on your hardware.

 * File:   led_flash.c
 * Author: Timothy
 * Created on July 26, 2015, 11:41 AM

// External crystal oscillator 8Mhz 
#define _XTAL_FREQ 8000000



// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON         // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)


int main(void)
  // set Port A to be digital output  
  TRISA = 0; //RA0 as an output pin
  // configure shared pin functions
  ADCON1 = 0b00000110;    // set ADC shared pin to be digital output
    PORTAbits.RA0 = 1;  // LED ON
    __delay_ms(1000); // 1 Second Delay
    PORTAbits.RA0 = 0;  // LED OFF
    __delay_ms(1000); // 1 Second Delay
  return 0;

Microchip offers some great development tools for your PIC projects and once you have them setup it's worth to explore some of the advanced features to make your coding sessions more productive.


Sign up for my monthly email newsletter Signal Bytes to get the latest on embedded hardware design including:

  • Signal Integrity and High-Speed Design
  • Embedded design trends
  • Product reviews & tool tips
  • MCU dev kits and programming and more!

Dont' wait, sign up today and get the best hardware design resource in the industry for free!