5.3. Compiling a program with MPLAB X and XC8

The P-Star can be programmed using standard development tools from Microchip. This section explains how to get started programming the P-Star in the C language using MPLAB X and XC8. MPLAB X a free integrated development (IDE) from Microchip for programming their PIC microcontrollers. XC8 is a C compiler from Microchip for 8-bit PICs. Both programs run on Windows, Max OS X, and Linux.

  1. Download and install the latest versions of MPLAB X and XC8 .
  2. Find “MPLAB X IDE” in your Start Menu and run it.
  3. From the File menu, select “New Project”.
  4. On the first screen of the New Project wizard, select the “Microchip Embedded” category and then select “Standalone Project”. Click “Next”.

  1. For the Device, type the name of the microcontroller on your P-Star, which is either “PIC18F25K50” or “PIC18F45K50”. Click “Next”.

  1. On the “Select Tool” screen, you can select “PICkit 3” but this choice does not matter because we will not use MPLAB X to the load the program onto the board.
  2. For the compiler, select XC8.

  1. For the Project Name, choose something like “p-star1”, and choose the folder you want it to be in. Click “Finish” to create the project.

  1. We need to configure the project’s linker setting to properly account for the P-Star’s bootloader, which takes up the first 8 KB of flash memory. In the “File” menu, select “Project Properties”. In the “XC8 linker” category, select the “Additional options” sub-category. In the “Codeoffset” box enter 0x2000, which is 8*1024 in hex. Click “OK.”

  1. Now we need to create the C source file. Locate the “Projects” pane. If the “Projects” pane is not visible, you can open it by opening the “Window” menu and selecting “Projects”. Left-click the “+” sign next to “Source Files” to expand it and verify that your project has no source files yet. Then right-click on “Source Files”, select “New”, and then select “C Source File…”.

  1. Choose a file name like “main” and then click Finish.This should create a new file named “main.c” and open it for editing.
  2. Copy and paste the following code into main.c:
#include <xc.h>
#define _XTAL_FREQ 48000000

#define LED_GREEN(v) { TRISB7 = !(v); }
#define LED_YELLOW(v) { TRISB6 = !(v); }
#define LED_RED(v) { TRISC6 = !(v); }

void main()
    // Set up the LEDs
    LATB7 = 1;
    LATB6 = 1;
    LATC6 = 0;

    /* Enable Timer 0 as a 16-bit timer with 1:256 prescaler: since
       the instruction speed is 12 MHz, this overflows about every 1.4
       seconds. */
    T0CON = 0b10000111;

        TMR0L; // trigger an update of TMR0H

        // Blink the green LED with a period of 1.4 s
        LED_GREEN(TMR0H >> 7 & 1);

        // Blink the yellow LED with a period of 0.7 s
        LED_YELLOW(TMR0H >> 6 & 1);

        // Blink the red LED with a period of 0.35 s
        LED_RED(TMR0H >> 5 & 1);
  1. To compile the code, open the “Production” menu and select “Build Main Project”.
  2. The “Output” pane should now show the build output from MPLAB X. This includes all the command-line arguments passed to XC8 to compile the program, and all the output from the compiler. You should see several instances of warnings similar to “warning: (1311) missing configuration setting for config word 0x300000, using default”. This is OK, since the P-Star’s configuration bits are set during manufacturing and they cannot be changed using the bootloader.
  3. One of the last lines of the output should say “Loading code from” and have the full path to the HEX file produced during compilation. This path and filename will be important later when you load the program onto the P-Star.

Where to find more information

For information about the hardware peripherals and registers on the PIC, see the PIC18F25K50/PIC18F45K50 datasheet.

For information about MPLAB X, you can find useful resources under the “Help” menu and in the “docs” directory inside your MPLAB X installation.

For information about XC8, look in the “docs” directory inside your XC8 installation to find its user’s guide.

If you have questions, you can post in Microchip’s XC8 forum or the Pololu Robotics Forum.

Related Products

P-Star 45K50 Mini SV
P-Star 25K50 Micro
Log In
Pololu Robotics & Electronics
Shopping cart
(702) 262-6648
Same-day shipping, worldwide
Shop Blog Forum Support
My account Comments or questions? About Pololu Contact Ordering information Distributors