These are the steps I use to create a new MPLAB X project for a C program compiled with Microchip’s XC16 compiler:
- From the File menu, select “New Project…”. The new project dialog box will appear.
- Choose Project: Select “Microchip Embedded” and “Standalone Project”, then click “Next”.
- Select Device: Set Family to “16-bit DSCs (dsPIC30)” and Device to “dsPIC30F4011”, then click “Next”.
- Select Tool: Click “PICkit 2”, then click “Next”.
- 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”.
- 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) while(1) { _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:
...\LEDflash\LEDflash.X\dist\default\production\LEDflash.X.production.hex
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.
Thanks a lot!!!
You’re welcome!
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?
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:
Ted
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.
Cheers,
Mohsen
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.
Ted
Why can’t we use __delay_ms(1000)?(for 1 sec delay)
Hi Vamsi,
You can use __delay_ms() to create delays if you want, but I never use it. For __delay_ms to work, you need to define a constant called FCY that tells the compiler how many machine instructions are carried out every second (I suppose I would call that the “instruction cycle frequency”). The description of the __delay_ms function (which is actually implemented as a macro) in Microchip’s 16-Bit Language Tools Libraries Reference Manual includes the following explanation:
So, if you want to use __delay_ms, you’ll need to #define FCY and you’ll need to include the libpic30.h header file.
Ted
Thank you.
You’re welcome.
Ted