In this post we shall work with the HELLO WORLD program ,blinking an LED.
Port Pin RD7 is declared as output where an LED is connected through a current limiting resistor of 470 ohms.External crystal of 16MHZ is used in the circuit & this is defined in the configuration settings.
Open the MPLABX IDE.
Click on File – > New Project
A New Project window opens.Select the category “Microchip Embedded “ & under projects select “Standalone Project” . Click on Next.
Next step is to select your target device.Under Device bar you can type in PIC16F877A or you can select from the list of drop down menu.
Now select the Tool as PICKIT2 . Other options are PICKIT3,REAL ICE & Simulator.In this demo we’re using a PICKIT2 hardware.
Next is the selection of Compiler.You can see a list of compilers installed including MPASM (assembly language) . MPLABX allows usage of old HITECH C compiler also.
The XC8 will show up in the list if you had installed it correctly.
Select the XC8 compiler.
Now provide a Project name “LedBlink” & click on Browse to select a location to store the Project.
Click on Finish.
The MPLAB project IDE opens with 4 panes.FILE pane, Navigation pane, Editor pane & Task pane.
Inside FILE pane you can see your project LedBlink.
To write the C code we need to create a C main file.
Right click on Source Files & click on New—> C Main File
Provide a File name & ensure that the Extension is .c
Do not add the .c extension next to your filename.It is selected from the drop down menu of Extension, seen below the Filename.
Click on Finish to see the code window on Editor pane.
You can just delete the default code presented ( the #include declarations & int main statements)
Before writing the main code we’ve to program the configuration bits.Click on
Window –> PIC Memory Views –> Configuration Bits
On the Task Pane area at the bottom the Configuration options are displayed.
As we’re using an external High frequency crystal on the Demo board , select from the drop down menu against FOSC as HS .
Other options you can leave for defaults.But the LVP should be selected OFF.
Now click on the button which says “ Generate Source Code to Output”
You can now see the Configuration code generated as per your settings.
Copy the Configuration code generated & paste it on to the Code window of Editor pane .
Before the #include <xc.h> statement you’ve to define the crystal frequency.
#define _XTAL_FREQ 16000000
In this demo an external crystal value of 16MHz is used.This must be declared by the #define statement.If you miss this statement , any Delay statements in your code will show up a red squiggly mark.
Now type in the following code just below the configuration code.
—————————————
int main()
{
TRISD7=0; //RD7 declared as Output pin
while(1)
{
RD7=1; //make RD7 pin High to glow LED
__delay_ms(1000); // 1 second delay
RD7=0; // make RD7 pin Low to Off LED
__delay_ms(1000);
}
return 0;
}
——————————————–
Port pin RD7 is declared as Output by TRISD7 = 0
Never ending loop starts by while (1)
RD7 is made High by RD7=1 ,in turn makes the LED glow.
A delay of 1 second is given by __delay_ms(1000) Note the usage of double underscore before delay statement.
RD7 pin is then made low by RD7=0 , switching OFF the LED.
Again a delay of 1 sec is given.
The never ending loop repeats , as a result you see the LED blinking at 1 sec interval.
Click on Run —> Build Main Project to build the project.
If you’ve followed the Syntax properly , you can see Build Successful
Now click on Run –> Run Main Project
MPLABX will connect to the hardware PICKIT2 & program the target IC .The .Hex file is loaded on to the Target IC & you can see the LED connected at port pin RD7 blinking.
The .hex file generated is located at folder
projects->ledblink.X->dist->default->production
Below is the picture of Development board connected with PICKIT2 .An LED is wired to port pin RD7 .
Watch this Tutorial Video :