Creating a new XC16 project in MPLAB X

These are the steps I use to create a new MPLAB X project for a C program compiled with Microchip’s XC16 compiler:

  1. From the File menu, select “New Project…”. The new project dialog box will appear.
  2. Choose Project: Select “Microchip Embedded” and “Standalone Project”, then click “Next”.
  3. Select Device: Set Family to “16-bit DSCs (dsPIC30)” and Device to “dsPIC30F4011”, then click “Next”.
  4. Select Tool: Click “PICkit 2”, then click “Next”.
  5. Select Compiler: Select “XC16 (v1.00) [D:\Program Files\Microchip\xc16\v1.00\bin]” (the folder location will probably be different on your PC), then click “Next”.
  6. Select Project Name and Folder: Set Project Location to whatever you want. I make a new folder for each program inside a folder called “progs” which is on my desktop. The Project Folder should be automatically set to the same location you specified for the Project Location. Set the Project Name to whatever you like, but it’s probably bext to choose something short and simple without spaces (e.g. “LEDflash”). Then click “Finish”.

Now that the project has been created, a source code file needs to be added to it. You can do this by right clicking on the source code folder icon in the project view. Click “Add new file”.

Here’s the above process in a series of screenshots:

Please note that I didn’t forget step 3 (“Select Header”) – the new project wizard seems to skip that step when you create this type of project.

Select Device:


Select Tool:

Select Compiler:

Select Project Name and Folder:

Create a new empty file:

Call the new file “main.c” and store it in the project folder:

Here’s a really simple example program which you can paste into your new file:

// dsPIC30F4011 LED flash
// Written by Ted Burke
// Last updated 27-8-2012

#include <xc.h>
#include <libpic30.h>

// Configuration settings
_FOSC(CSW_FSCM_OFF & FRC_PLL16); // Fosc=16x7.5MHz, Fcy=30MHz
_FWDT(WDT_OFF);                  // Watchdog timer off
_FBORPOR(MCLR_DIS);              // Disable reset pin

int main()
	TRISD = 0; // Set all port D pins as outputs

	// Loop to flash an LED on RD0 (pin 23)
		_LATD0 = 1;          // Set RD0 high
		__delay32(15000000); // 500ms delay
		_LATD0 = 0;          // Set RD0 low
		__delay32(15000000); // 500ms delay

	return 0;

Save the file, then compile the program by clicking on the “Build Project” button (circled in red below).

Hopefully, the program will compile without errors. If so, you will see a message saying “BUILD SUCCESSFUL”, as shown below:

Rather than using MPLAB X to transfer the compiled program onto the dsPIC, I use the PICkit2 helper application. You can do it with MPLAB X, but I find that it crashes all the time, which is very frustrating. The PICkit2 application is much more reliable. In the PICkit2 program, click the “Auto Import Hex + Write Device” button, circled in red below. You need to tell it the location of the compiled hex file, which will be buried in a subfolder somewhere inside your project folder. This is the location of my hex file:


Once you find and select the hex file, it should be automatically transferred onto the chip. If the transfer is successful, you’ll see the message circled in blue below.

Hopefully at this point, the program should be running. If you have an LED attached to RD0 (pin 23), you should now see it flashing.

This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

6 Responses to Creating a new XC16 project in MPLAB X

  1. Elovan says:

    Thanks a lot!!!

  2. Mohsen Alamian says:

    Thanks for your helpful guide!

    I want to read a text file from a USB disk drive using PIC18F2550 or PIC24 series, I don’t know how to start the project and how to handle it. Would you please help me with that?

    • batchloaf says:

      Hi Mohsen,

      Sorry, but I don’t know much about low-level USB communications. Whenever I’ve looked at it, I’ve been put off by how complicated it seems, and I’ve only ever looked at creating a USB perihperal rather than a USB host which is what you need in this case.

      If you’re not sure how to start, this may turn out to be a very difficult challenge. Perhaps you might consider one of the following alternative approaches:

      • Use an SD card instead of a USB drive. It’s an awful lot easier to access an SD card from a microcontroller because part of the SD standard allows access to the device using SPI, which is a relatively straightforward serial connection. Also, this approach is possible using most PICs, rather than just the one or two that support USB.
      • Use a higher-end device, like a Raspberry Pi or similar, which runs Linux and has a USB host built-in. Would this be an option? I think it would probably be way easier to do this than to create a USB host from the ground up using a PIC.


  3. Mohsen Alamian says:

    Hi Ted,
    Thanks for your quick reply.
    You’re right it’s a tough task to do and I can replace the USB drive with a SD card to meet my requirements, but the use of USB flash drive is more feasible, because you can easily hook it up to any computer and there’s no need to have any special port on it.

    • batchloaf says:

      Hi Mohsen,

      Yes, I agree it would be nice if there was a convenient way to read data from a USB key. However, USB SD card readers are now extremely cheap. I can buy one in my local discount store for 2 euro! That way, you can access it just like a regular USB key on any PC, but then slide out the SD card and insert it into your microcontroller device.

      Anyway, best of luck working things out, whatever approach you decide on.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s