Let’s Begin
Now that you have already setup your development environment, it is time to create your first project. And this article will teach you step-by-step on how to do it. By the end of this tutorial you will learn how to setup your project, build and run it on a simulator.
Create Your Project
First, launch the MPLABX IDE. This is where we will be developing the software for microcontroller. To crete a new project go to the menu bar and select [File]→[No Project…]

The following context window will appear. Select “Microchip Embedded” under Categories and “Standalone Project” under Projects. Then click next.

On the second step, select the device or the microcontroller that you will be using for the project. Under Family select “Mid-Range 8-bit MCUs” and under Device select “PIC16F877A”. The click Next.

The third step, allows us to select the tool that we will be using for debugging.Since we won’t be connecting any hardware to MPLABX instead we will be using Proteus to simulate, we’ll go ahead and select “Simulator”. Select Next.

The fourth step, provides options on the compiler toolchain that you’d like to use. Let’s go ahead and choose XC8 from Microchip. The click Next.

The final step, asks you to provide the project name and its location. Set it as the main project. And then finally click Finish.

On the project panel of MPLABX IDE, an empty project with the “BlinkingLED” name is created.

Setup Your Configuration Bits
It is now time to setup the configuration bits specific to the microcontroller that we are using. In the menu, select [Window]→[PIC Memory Views]→[Configuration Bits].

The following configuration bit panel will show on the bottom part of the MPLABX Window.

Set your configuration the following and then click “Generate Source Code to Output” button below. Please refer to PIC16F877a datasheet for more information of each configuration.

A “Config Bits Source” panel will appear showing the generated source code, based on the set configuration bits. Copy the generated source code.

On the “Project Panel”, right-click on the “Header Files” folder. Select [New]→[C Header File…]

A “New C Header File” context window will appear, set the file name as “configuration” and then click Finish.

On the configuration.h file, paste the copied configuration bits.
/*
* File: configuration.h
* Author: Jlabs
*
*/
#ifndef CONFIGURATION_H
#define CONFIGURATION_H
// PIC16F877A Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#endif /* CONFIGURATION_H */
Setup the Hardware
Before we write the program, let’s first define the connection to the mcu. In this way we will know exactly where we would like to connect the LED that we will be blinking. In the image below, we chose RB1 or the second pin in PORTB. The rest of the connections are the required connection of the PIC16F877A. Please consult the mcu’s datasheet for more information.

Write the Program
Now, create a new main file under the Source Files directory in the project panel and name in Main.c. And write the following code.
/*
* File: Main.c
* Author: Jlabs
* Description: A program that toggles the state of the LED every one
* second.
*/
#include "stdio.h"
#include "stdlib.h"
#include "configuration.h"
#define _XTAL_FREQ 20000000
int main(int argc, char** argv) {
TRISB = 0x00;
PORTB = 0x00;
while(1)
{
PORTB = 0xFF;
__delay_ms(1000);
PORTB = 0x00;
__delay_ms(1000);
}
return (EXIT_SUCCESS);
}
In lines 8 to 10, you tell the compiler to include the libraries stdio.h and stdlib.h and also the configuration.h which contains the configuration word that you have defined earlier.
#include "stdio.h"
#include "stdlib.h"
#include "configuration.h"
In line #2, we define the crystal frequency that your microcontroller is running on. In this setup we are using 20 MHz. You have to make sure that the definition here is the correct one that your crystal is using or it will go haywire.
#define _XTAL_FREQ 20000000
The code within the main function is, well, the main function of your software. This is the first user-defined function that the microcontroller will execute after reset.
int main(int argc, char** argv) {
// Main Code Here
return (EXIT_SUCCESS);
}
In line #16, we setup the PORTBof the microcontroller as an output and then in line #17 we set the digital logic of PORTB to off. This means that immediately after a reset, the LED that is connected to this pin is initially off. I’d like to state that this is the simplest of all the configuration of a PORT. There are ports that can be multiplexed across several functions, some can produce PWM or an input for ADC, of course that is a topic for another day. For now, let’s just settle for a pin that functions as a digital I/O.
TRISB = 0x00;
PORTB = 0x00;
Then we have lines #19 to #26. This is an infinite loop, making the program run indefinitely. The first part sets the output of PORTB to digital logic high, then waits for 1s. After the delay, it then sets it to digital logic low and then wait again for another 1s. After the delay times-out, it repeats. This effectively, toggles the led every second.
while(1)
{
PORTB = 0xFF;
__delay_ms(1000);
PORTB = 0x00;
__delay_ms(1000);
}
Build the Program
Now that we have a program, let’s build it to create the hex image that we needed to simulate. On the MPLABX IDE go to [Run] -> [Build Main Project]

In the Output Panel on the bottom of the screen you should have similar message as below.

Simulate
And here is the output: