PIC16F877A programming tutorial using MPLAB X IDE and XC8 compiler | applied electronics engineering

Latest

PIC16F877A programming tutorial using MPLAB X IDE and XC8 compiler

By Applied Electronics - Tuesday, May 16, 2017 No Comments
In this tutorial we will show how to program PIC16F877A pic microcontroller using the MPLAB X IDE and XC8 compiler. We will show simple LED blinking program for the purpose of illustration. The PIC16F877A is a popular microcontroller that can be used for variety of microcontroller application.

Before you continue this project it is assumed that you have already downloaded and installed MPLAB X IDE and the XC8 compiler.

1. Open MPLAB X IDE, you should have similar start up page as shown below.


2. Create a new project by going to File > New Project.


3. Choose the type of project. Here we will create a complete new Standalone project, a project without template and premade files.


4. Select the device. In this tutorial we want the PICF81677A microcontroller which belongs to mid-range 8 bit MCU family of PIC microcontrollers.


5. The next step is the select the programmer we will be using. In this case we select the PICKit 3.


6. Select the Compiler which in our case is the XC8 compiler. If you haven't downloaded and installed this compiler it will not be listed.


7. In the next screen you should provide the name of the project and save the project in some desired directory. 


Once you click on Finish, the project is created and you will see the project folder listed in the left pane as shown below.


Click on Project tab.






If you expand the LEDBlink project folder you will see other inner folders as shown.




Most of these folders are empty, that is there is no files because we haven't yet added any file. Now we have to add c program files and headers etc which will be done in the next step.

8. Add main.c file

Now we add a new source file called main.c which is the main c programming file containing our program. To do this right click on the Sources Files folder, then New and select the main.c file. This is shown below.



In the next window that shows up, type the name of the c file like LEDBlinkProgram in this tutorial and click on Finish. The LEDBlinkProgram.c file has now been created and is listed within the Source Folder as shown below.








The bare minimum code in the LedBlinkProgram.c is below.

/*
 * File:   LEDBlinkProgram.c
 * Author: SYSTEM
 *
 * Created on May 16, 2017, 3:34 PM
 */


#include <xc.h>

void main(void) {
    return;
}

In the above code xc.h header file is used which contains some important definition of files such as __delay_ms() function and TRIS and PORT registers.
 
 
Then there is the usual main function.

Now we have to first configure the device by writing oscillator to use and set configuration bits of the device in order that the program works correctly when compiled and uploaded to the device. This is the next step.

9. Oscillator and Configuration bits

The first code we need is the code that defines the oscillator frequency to be used. We will be specifying 20MHz frequency. To do this write the following define preprocessor directive line under the #include <xc.h> as shown below.

#include <xc.h>
#define _XTAL_FREQ 20000000

void main(void) {
    return;
}

Then we have to configure the device using the configuration bits. This is done by using pragmas to tell the compiler XC8 how they are set.

MPLAB X IDE has the Configuration Bits Window which allows you to easily specify the configuration bits. This tool is available under Windows > PIC Memory Views > Configuration bits as shown below.


The configuration window is shown at the bottom pane.


Using this window, select the HS oscillator for external clock of 20MHz. Disable the Watchdog Timer, PWRT and Brown-out Reset Enable Bit. Leave other as default.


Once done click on Generate Source Code to Output.


You will see the configuration code using pragma. Copy those configuration bit setting.


And paste it under the #define _XTAL_FREQ 20000000 as shown below.


10. The main program

Now we will create the main program function. We will create a LED blinking program that turns on/off leds connected to Port B pin 1 of the PIC16F877A microcontroller.

First create a infinite while loop by writing the following code within the main() function.

void main(void) {
   
    while(1){
       
    }
    return;
}

See picture below.


Now the next step is to first configure the port B pin 1 as output because we will be sending HIGH and LOW value to this port pin. Then we have to send the actual values to this pin 1 and insert delay between high and low values.

To set the direction of the port B pin 1, we write TRISB1 = 0; before the while(1) loop as shown below.


Next we write logic high and logic low and insert delay between them. We refer to the port pin using RBx where x is the port pin number. In this example, RB1 means port B pin 1. We create the delay of 1 second using the __delay_ms(1000); Note that there is double underscore before delay_ms.

void main(void) {
  
    TRISB1 = 0;
  
    while(1){
        RB1 = 1;  //write high
        __delay_ms(1000);  //1 second delay
        RB1 = 0;  //write low
        __delay_ms(1000);
    }
    return;
}

See the following.


The whole program code is as follows.

#include <xc.h>
#define _XTAL_FREQ 20000000

#pragma config FOSC = HS        // Oscillator Selection bits (HS 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 = OFF      // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = ON         // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#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)

void main(void) {
   
    TRISB1 = 0;
   
    while(1){
        RB1 = 1;  //write high
        __delay_ms(1000);  //1 second delay
        RB1 = 0;  //write low
        __delay_ms(1000);
    }
    return;
}

11. Compile/Build the Project

The final step is to compile and build the project. This is done by clicking on the Build icon in the toolbar.


After you click on it MPLAB X will use the XC8 compiler and compile the program file and build the project. You should see Euild Successful in the output pane window as shown above.

At this point your .hex file is ready to be uploaded into your device, that is the PIC16F877A microcontroller using the PICKit3 we opted in the beginning.

Another way to check your program is to use the proteus simulation software. The following illustrates led blinking at port B pin 1 in proteus.



No Comment to " PIC16F877A programming tutorial using MPLAB X IDE and XC8 compiler "