03. First Program
Power Regulation
The very first thing that I do when I have a circuit running is verify the power consumption. The reason this is important is because you want to know if anything on your circuit is shorted out. In my lab, I have a BK Precision DC regulated power supply. I can fix either the current or the voltage of the output. If I run the power supply at a fixed voltage, it will tell me the current drain that my circuit is taking. The circuit I have shown in the previous tutorial section should drain less than 10 mA. If you see that the ampere readings oscillate in large jumps or is draining a lot of power, say 100 mA or more, you probably shorted something.
It is important to have proper power regulation on any digital circuit. This is because digital circuits have a large number of switching electronics. Switching electronics tend to drain current erratically. The result, if you have ever measured a Vdd line on digital circuit, it is very noisy. To avoid a glitch on the power line that could possibly reset the microcontroller, it is important to do two things:
- 1. Proper bypass capacitors on power pins – The capacitors on a digital circuit act as tiny little temporary batteries. This allows the erratic drain of current to be smoothed out so that inadvertent resets can be avoided. Remember to place the capacitors as close to the power pins as possible.
- 2. Onboard voltage regulator with proper bypass capacitors – This should be pretty straight forward if you’ve ever done any electronic designs. A voltage regulator is essential to ensuring that your power line retains the proper potential during operations. Typically if my total circuit drain for 3.3V is less than 150 mA, I use the LP-2985-3.3 by National Semiconductor. They are cheap, have a very low dropout voltage, and only require 3 external capacitors. However it is a linear regulator, and should probably be avoided if you are using it for any mobile applications where power consumption is a priority.
Using MPLAB IDE
Before we can check out the microcontroller, we need to learn some basic functions in MPLAB IDE. The MPLAB IDE is a FREE integrated development environment (IDE) distributed by Microchip. It has some neat features that I’ll get into later in the tutorial.
The latest version is v8.00, which has support for the new 32 bit microcontrollers that just came out in November of 2007. However, downloading this latest version does not guarantee that you have the latest version of the compiler. You will need to download the latest version of the C30 compiler and properly install it before proceeding.
Starting a project
Now you can start a project. Click on the project wizard to proceed.

Pick the device you have on your circuit, and when the suite selection comes up, click on the C30 C compiler that you just installed. Click on the directory you want your files to lay.

Next, you will be asked which files you want to be included. This is like the “import” keyword in java, or the “#include” directive in C++. You don’t need to add it now. I usually leave this blank and add it later on manually. Now you are done with your project template.You will now have an empty project.
You will first need to link the header definitions for your particular chip, and then the linker definitions. To do this, first make sure your project view is turned on.

Next right click on header files, and add the header file that corresponds to the microcontroller that you have chosen. These files are usually located in C:\Program Files\Microchip\MPLAB C30\support\h\[name of device].h or whichever directory the MPLAB program was installed.


You will then need to do the same for the linker script, which is usually located at C:\Program Files\Microchip\MPLAB C30\support\gld\[name of device].gld or whichever directory the MPLAB program was installed. You are now ready to do a device ID check on your microcontroller.

Checking out the Microcontroller
Before preceding it is a good idea to check if your programming connections to PGD and PGC are properly connected, and to see if the power voltages are correct. To do so, perform the following. Click on Debugger and select the ICD 2.

If you connected everything properly, you will see that the ICD will do a check on the Vdd, and the device ID check to see if the device detected matches the device you selected for the project. If there is a mismatch, or if the power is not on, you will get an error. Once you have a working circuit, you can start programming.
The first thing that I like to do is to verify that the oscillator is working. The first program that I write for any new circuit is an empty infinite loop. This may sound kind of stupid, but it is wise to have your configurations correct before doing any actual coding.
You will need to add a new file to the “Source Files” section of the Project View. Create a new .c file and add it to the source files.
Sample Program
This is what the bare bone program looks like:
/*
Engscope
Author: J. L.
Date: Feb 1, 2008
Test program
*/
//include basic header definition
#include “p24FJ64GA004.h”
//config
_CONFIG2(0xF9FC);
_CONFIG1(0x377F);
//main loop
int main(void)
{
//Set up Clock
OSCCON = 0x11C0; //select INTERNAL RC, Post Scale PPL
//Loop forever
while(1)
{
}
}
As you can see, I put in a “#include” directive to tell the compiler to include the file I added to the “Header Files” section of the Project View. I have two “_CONFIGx” functions that configure certain things for the microcontroller. I will talk more about it in the next tutorial section. After the configuration functions there is a main loop in the “main” body function. In it, I have a while loop that is always true. Before going into the loop however, I set the OSCCON register to some hex value.
If you selected the correct devices and the correct linker file, the code should compile. In MPLAB IDE, compile is marked as “MAKE” and is the icon that looks like layers of paper with an arrow on top. If the compilation goes through, you will get a compilation summary with the last line saying “BUILD SUCCEEDED”.
Next, click on the icon “PROGRAM TARGET DEVICE”, which is an icon of a microchip with an arrow pointing to it. If there are no errors, you will get a screen saying the programming was successful.

Now, the reason I programmed the empty code was to evaluate the oscillator out. In the _CONFIG1 and _CONFIG2 functions, I have set the oscillator to be internally generated, and to output the result of the oscillator to the OSCO pin. If you have an oscilloscope, clamp the probe to the OSCO pin and see if you have a square wave at 8 Mhz. If you don’t have an oscillation at that frequency for this particular device, then you’ve done something wrong. Check the datasheet for your particular device to see which pin OSCO outputs.
Congratulations, you’ve just programmed your first PIC microcontroller. We’ll look into some of the configurations available on the next section of the tutorial.
Table of Contents
Previous – Getting Started
Next – Configuration

Entries (RSS)
GREAT TUTORIAL!!! Munching through all of the documentation relative to the PIC24 is a painful and slow process !
This tutorial is great for a fast jumpstart.
Hi!
Thank you very much for your effort of sharing with us. Good stuff!
Hi, Please help me. As I compile the file, I get the message saying:
AsuanSourceFile.c:4: error: syntax error before numeric constant
AsuanSourceFile.c:4: warning: type defaults to ‘int’ in declaration of ‘_CONFIG2′
AsuanSourceFile.c:4: warning: data definition has no type or storage class
AsuanSourceFile.c:5: error: syntax error before numeric constant
AsuanSourceFile.c:5: warning: type defaults to ‘int’ in declaration of ‘_CONFIG1′
AsuanSourceFile.c:5: warning: data definition has no type or storage class
If I commented out the
_CONFIG2(0xF9FC);
_CONFIG1(0x377F);
The message I get is BUILD SUCCEEDED.
What must I do?
It seems to me you are not including some header files. In the compiler messages, does it find the file p24F32… .h? Is the file even included in your main header declarations? Your compiler is saying that it is looking for the function _config1() and _config2() but it cannot find location of the definition of the files.
-J
Actually, if you copied the code directly from the website then most likely those “x” symbols are not the character “x”. So just delete them and retype “x”.
Wordpress formating… It’s using multiply symbol when it sees numbers to the left and right of “x”. It is fixed now. Enjoy.
-J
You will need to add a new file to the “Source Files” section of the Project View. Create a new .c file and add it to the source files
so I right clicked on the Source Files folder.. chose Add New File…. when I tried to save it as: firstProejct in a folder on my desktop.. (where I saved my first project Im doing as well)
it makes a new file/entry in the OTHER FILES folder int he Project View window..??
if I manually add the .c extension when creating/adding the new file….or choose C source file from the file type drop down….I see it in the SOURCE folder..but says (file not found) after it??
what am I doing wrong? thanks
Don’t save on your desktop. Make a folder near the root. The compiler has something like a 64 character limit on the absolute path of the files.
For example, make a folder on your C:\ drive called PICProj, and then make another folder called HelloWorld. Save all your files, including the project files in there. You should have the habit of not saving stuff on your desktop. They are called directories for a reason you know…
-J
T H A N K Y O U !!
Hey a fellow Canadian!
Guess you were having trouble with something.
-J
This part of the tutorial requires an oscilloscope. How do I check the results if I don’t have one? You might have stated that the scope is requirement for this part. Are there other parts that need equipment?
Well, technically no. I’m sure you can write a small program to light up an LED or something. You can also use the debugger on the MPLAB IDE, to see if you can detect your controller. If you are able to use the “watch” function in the IDE, you are in business.
However, you really should have a scope for doing this type of work.
-J
Yes! Working on a project and using a PIC24 as the uC. These tutorials are turning out to be very VERY helpful.
Thanks again!
can you give me an example of PIC24FJ16GA002 in asm language
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1408
There’s some ASM for you. I don’t deal with ASM too much. Unless you are strained for memory, there’s no reason to use ASM.
-J
switching from ASM to C, 9S12 to PIC24, excellent tutorial for me to start with, thx..