PIC16F877A LED Blinking Project

How to blink LED connected to PIC16F877A microcontroller using C programming language in MPLAB X IDE

In this PIC16F877A LED Blinking Project we will show how to create a LED blinking program in C programming language using MPLAB X IDE and how to generate HEX code using IDE which in turn uses the XC8 compiler. The HEX code generated by the software is then downloaded into PIC16F877A microcontroller uisng PICKIT 3 programmer.

First of all you have to create a new project in MPLAB X IDE. Doing that is fairly simple. Just click on the New > Project as shown below.

MPLABX project

In the next window, select Microchip Embedded project and on the right pane select Standalone Project then click Next.

In the next window select Mid Range Family in the Family field and PIC16F877A in the Device field. This is shown below. Then click Next.

In the next window you have to select the programming tool. In this tutorial we are using the PICKIT 3 programmer and debugger. It is connected to the computer and there the PICKIT 3 default name is shown in the window as SN:Default_PK3. If you have not connected a programmer then you won’t see it under the programmer. You can later connect your programmer in which case you have to manually select the programmer in the Project properties.

Selecting PICKIT 3

Once you selected your programmer Tool click on Next.

In the next window you should select the compiler which you want to use. There are number of compilers available for 8 bit microcontrollers like PIC16 but here we have used the free version XC8 compiler. Select your compiler and click on Next again.

In the last step you have to provide project name and the directory where you want to save the project. Once you have done that click on Finish. This completes the Project creation in MPLAB X IDE.

After you have created a new Project you will see the following project directory structure. On the left pane you can see the different folders. In his different folders you can put and organize files.

For our purpose which is a simple C program to turn on and off a LED we don’t need many files, just one which is main.c. To add the main C program file we right click on the Source Files folder and select main.c… as shown below.

dong that brings up a window where we can specify the name of the file and also select the extension(c or c++). In this tutorial we name the main c program file as main.c.

After that you will see the main.c file inside Source Folder. And on the right pane you will see the default c program code that comes along with the program file.

Now edit or copy/paste the following C program code into the main.c. The program code is then explained.

The C program code is as follows.

This program code is a simple LED blinking program.

In his case, we have configured to use 4MHz external crystal using the statement at the top- #define _XTAL_FREQ 4000000. The external 4MHz crystal is connected to the microcontroller using 15pF as shown below.

Then we have configured the microcontroller to use external oscillator and other features using the #pragma statements.

For example, the line #pragma config FOSC = XT tells that we will be using external oscillator where XT has meaning that we will use low speed. HS is another option herein which mean high speed.

The other pragma statements turns off mostly the other microcontroller features such as the watchdog timer, code protection, write protection, power up timer and brown out features.

All this pragma statements is used to set what is called configuration bits of the microcontroller. It is easy to generate this lines of statements using the MPLAB X IDE. To generate this statements, go to Window > PIC Memory Views > Configuration Bits as shown below.

After that you will see the configuration bit window in the lower half of the IDE. There you can simply select the various options. Once done that just click on the Generate Source Code to Output and then copy paste the code into the C main.c program file.

Then in the main program we have first made the Port B as output port using the statement TRISB = 0; This is because we want to send high and low signals to the Port B pin 0. Using the while(1) statement we send logic high and logic low signals to the Port B pin 0. This is done using the RB0 = 1 and RB0 = 0 statements. In between the signals level we want to insert a certain delay, in this case 1 sec or 1000ms. This is done using the __delay_ms() function.

The next step is to see whether the program will compile. Right click on the main.c file and select compile as shown.

If you see Build Successful message in the output window then there is no error in your program code.

At this point the software has also generated hex file of the corresponding main.c file. This hex file must now be uploaded to the PIC16F877A microcontroller using the PICKIT 3 programmer.

You should now connect the PIC16F877A to the PICKIT 3 programmer. In order to ease the programming we use here a ZIF socket circuit onto which the PIC16F877A is placed. Then the ZIF socket circuit ICSP pins are connected to the PICKit 3 programmer. The PICKit 3 is connected to the PC using the USB connection. This is shown below.

Before we can upload the hex file we have to configure the software to use the voltage from the USB. Go to Run > Set Project Configuration > Customize.

In the windows that comes up, select PICKIT 3 in the Categories, then select Power in the Option categories.

Initially, you might see that the Power target circuit from PICKit 3 is unchecked. Check this box because we want to supply the voltage from the Pickit 3 programmer. Then below you see that the voltage to be used is 5V. Ideally this is the value we should be using but in many cases such 5V voltage is not supplied by the USB. Instead a voltage between 4.5V and 5V is generated in the USB. The suggestion is to use first 5V and if you get error message later that the voltage is not sufficient then change the voltage to 4.875V as shown below.

Then click Apply and exit the window by clicking on OK.

Now click on the Make and Program Device Main Project that is available in the top toolbar. If everything is right you should see message in the output pane: Programming/Verify complete.

At this point we have programmed the PIC16F877A microcontroller sucessfully. Now we take out the PIC16F877A micro from the ZIF socket and create the following circuit on the breadboard.

Note that in this circuit diagram the Vdd(pin 11) and Vss(pin 12) pins are not shown connected to the +5V supply and ground. This needs to be done in the breadboard. The breadboard with microcontroller, power and ground connection, oscillator with 15pF capacitors, LED and resistor connected to port B pin 0 is shown below.

If you have done all correctly you should see the LED blinking ON and OFF with 1 second interval. This is shown in the following video.

What do you think?

0 points
Upvote Downvote

Total votes: 0

Upvotes: 0

Upvotes percentage: 0.000000%

Downvotes: 0

Downvotes percentage: 0.000000%

Leave a Reply

Your email address will not be published. Required fields are marked *

Common Communication Protocols used by microcontroller

What is Static and Dynamic Implementation in MPLAB Harmony