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.

led1

Open the MPLABX IDE.

Click on File – > New Project

1.NEW

 

A New Project window opens.Select the category “Microchip Embedded “ & under projects select “Standalone Project” . Click on Next.

 

2.CHOOSE

 

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.

 

3.IC_SELECT

 

Now select the Tool as PICKIT2 . Other options are PICKIT3,REAL ICE & Simulator.In this demo we’re using a PICKIT2 hardware.

 

4.PICKIT2

 

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.

  

5.COMPILER

 

Now provide a Project name “LedBlink” & click on Browse to select a location to store the Project.

Click on Finish.

 

6.projnme

 

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.

 

7.panes

 

To write the C code we need to create a C main file.

Right click on Source Files & click on New—> C Main File

 

8.CFILE

 

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.

 

9..cfile

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)

 

10.screen

 

Before writing the main code we’ve to program the configuration bits.Click on

Window –> PIC Memory Views –> Configuration Bits

 

10.1.confign

On the Task Pane area at the bottom the Configuration options are displayed.

 

10.2

 

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”

 

10.3

 

You can now see the Configuration code generated as per your settings.

 

11.cofigcode

 

Copy the Configuration code generated & paste it on to the Code window of Editor pane .

 

12.paste

 

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.

 

13.define

 

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.

 

14.prog

Click on Run —> Build Main Project  to build the project.

 

15.build

If you’ve followed the Syntax properly , you can see Build Successful

 

16.SUCCESS

Now click on Run –> Run Main Project 

 

17.led

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

Image 11

Below is the picture of Development board connected with PICKIT2 .An LED is wired to port pin RD7 .

IMG

 

Watch this Tutorial Video :

PIC TUTORIALS–MPLABX WITH XC8 & YOUR FIRST C PROGRAM