The PIC16F877A microcontroller with MPLAB X IDE and XC8 compiler is a foundational combination for embedded systems education in India. PIC microcontrollers from Microchip Technology are widely used in Indian engineering curricula alongside 8051, and the PIC16F877A specifically is covered in numerous BE/BTech, diploma, and AMIE examinations. This tutorial guides Indian beginners through setting up MPLAB X and writing their first XC8 C programs for PIC16F877A.
Table of Contents
- Why PIC16F877A in India
- PIC16F877A Specifications
- MPLAB X IDE Installation
- Creating Your First Project
- LED Blink Program in XC8
- ADC and UART Examples
- Programming with PICkit
- Frequently Asked Questions
Why PIC16F877A in India
PIC16F877A is one of the most taught microcontrollers in Indian technical education for several reasons:
- Curriculum coverage: Featured in RGPV, SPPU, VTU, and most Indian university syllabi for Microcontroller courses
- Rich peripherals: Has ADC, UART, SPI, I2C, PWM and timers all in one 40-pin DIP package
- Cost-effective: Available in India for ₹80–₹150 per chip
- Industry usage: Widely deployed in industrial control, consumer electronics, and automotive applications
- Proteus simulation: Excellent Proteus ISIS support for simulation before hardware testing
PIC16F877A Specifications
- Architecture: 8-bit RISC PIC mid-range
- Clock: Up to 20 MHz (4 MHz internal oscillator available)
- Program Memory: 14,336 words (14KB) flash
- Data Memory: 368 bytes RAM + 256 bytes EEPROM
- I/O Pins: 33 across PORTA–PORTE
- ADC: 8-channel, 10-bit
- Timers: Timer0 (8/16-bit), Timer1 (16-bit), Timer2 (8-bit)
- Communication: UART (USART), SPI, I2C (MSSP module)
- PWM: 2 CCP modules for PWM/Compare/Capture
- Package: 40-pin DIP
MPLAB X IDE Installation
- Download MPLAB X IDE from microchip.com/mplab/mplab-x-ide (free, ~1.5GB)
- Download XC8 Compiler from microchip.com/mplab/compilers (free community version)
- Install MPLAB X first, then XC8
- Launch MPLAB X — it should detect XC8 automatically
- Create New Project → Standalone Project → select PIC16F877A
- Select XC8 as compiler
- Select PICkit 3 or 4 as programmer (or Simulator for testing)
Note: The free XC8 compiler optimisation level is “0” (no optimisation). Pro licence enables optimisation but is not required for learning and most projects.
Creating Your First Project
Every PIC16F877A project needs configuration bits set. In MPLAB X, go to Window → PIC Memory Views → Configuration Bits:
// Configuration bits for PIC16F877A
// Typical settings for 20 MHz crystal with MPLAB X
// pragma config generates the configuration words
#pragma config FOSC = HS // High-Speed crystal/resonator
#pragma config WDTE = OFF // Watchdog Timer disabled
#pragma config PWRTE = OFF // Power-up Timer disabled
#pragma config BOREN = ON // Brown-out Reset enabled
#pragma config LVP = OFF // Low-Voltage Programming disabled
#pragma config CPD = OFF // Data EEPROM code protection off
#pragma config WRT = OFF // Flash Write protection off
#pragma config CP = OFF // Code protection off
#include <xc.h>
#define _XTAL_FREQ 20000000 // Define crystal frequency for __delay_ms
LED Blink Program in XC8
// PIC16F877A LED Blink - MPLAB X / XC8
#pragma config FOSC = HS
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config BOREN = ON
#pragma config LVP = OFF
#pragma config CPD = OFF
#pragma config WRT = OFF
#pragma config CP = OFF
#include <xc.h>
#define _XTAL_FREQ 20000000
void main(void) {
TRISB = 0; // PORTB as output (all pins)
PORTB = 0; // Clear all outputs
while(1) {
PORTB = 0xFF; // All LEDs ON
__delay_ms(500); // 500ms delay
PORTB = 0x00; // All LEDs OFF
__delay_ms(500);
}
}
// Individual bit manipulation
void main(void) {
TRISBbits.TRISB0 = 0; // RB0 as output
TRISBbits.TRISB1 = 1; // RB1 as input (button)
while(1) {
if (PORTBbits.RB1 == 0) { // Button pressed
LATBbits.LATB0 = 1; // LED ON
} else {
LATBbits.LATB0 = 0; // LED OFF
}
}
}
ADC and UART Examples
// ADC Reading on PIC16F877A
void ADC_Init() {
ADCON1 = 0x80; // Right justify, all pins analog, Vref=VDD
ADCON0 = 0x41; // ADC ON, channel 0 (RA0), Fosc/16
}
unsigned int ADC_Read(unsigned char channel) {
ADCON0 &= 0xC5; // Clear channel bits
ADCON0 |= (channel << 3); // Select channel
__delay_us(20); // Acquisition time
GO_nDONE = 1; // Start conversion
while(GO_nDONE); // Wait for completion
return ((ADRESH << 8) | ADRESL);
}
// UART on PIC16F877A at 9600 baud with 20 MHz crystal
void UART_Init() {
TRISC6 = 0; // TX as output
TRISC7 = 1; // RX as input
SPBRG = 129; // Baud rate 9600 @ 20MHz: SPBRG = (20MHz/(64*9600))-1 = 32... use 129 for high speed
BRGH = 1; // High baud rate mode: SPBRG = (Fosc/(16*baud))-1 = 129
TXEN = 1; // Enable transmitter
SPEN = 1; // Enable serial port
}
void UART_TxChar(char c) {
while(!TXIF); // Wait until transmit buffer empty
TXREG = c;
}
void UART_SendString(const char *str) {
while(*str) UART_TxChar(*str++);
}
Programming with PICkit
Microchip’s PICkit 3 or PICkit 4 are the standard programming tools for PIC microcontrollers:
- PICkit 3: Available in India for ₹800–₹1200 (original) or ₹300–₹600 (clone). MPLAB X supports it.
- PICkit 4: Faster, supports more devices. ₹3000–₹5000 for original.
- Clone PICkit 3 tools from India work for basic programming but may have issues with some newer devices.
ICSP (In-Circuit Serial Programming) connections for PIC16F877A:
- MCLR/VPP (pin 1) → PICkit VPP
- PGD (RB7, pin 40) → PICkit ICSPDAT
- PGC (RB6, pin 39) → PICkit ICSPCLK
- VDD (pin 11, 32) → PICkit VDD
- VSS (pin 12, 31) → PICkit GND
Frequently Asked Questions
Is PIC16F877A still used in industries in India?
Yes — PIC16F877A is still actively used in Indian industrial equipment, medical devices, and consumer electronics for its proven reliability, extensive documentation, and long production lifecycle. Microchip continues manufacturing it, making it a safe choice for production designs.
Can I use MPLAB X on Linux for PIC16F877A development?
Yes — MPLAB X runs on Linux, macOS, and Windows. The XC8 compiler also supports Linux. This makes it accessible for Indian students using Linux-based college systems or personal laptops running Ubuntu.
What is the difference between XC8 and HITECH-C for PIC?
Microchip acquired HI-TECH Software and their PICC compiler evolved into XC8. XC8 is the current supported compiler. Legacy code written in HITECH-C is largely compatible with XC8 with minor changes. New projects should use XC8.
How to debug PIC16F877A programs in MPLAB X without hardware?
MPLAB X has a built-in software simulator. Go to Project Properties → select “Simulator” as Hardware Tool. You can set breakpoints, watch variables, and single-step through code. Proteus ISIS provides more realistic simulation including virtual LCD, UART terminal, and external components.
What oscillator should I use for PIC16F877A in Indian college projects?
Use a 20 MHz crystal oscillator with two 22pF capacitors connected to OSC1 and OSC2 pins with the other ends to ground. This gives you maximum speed. For projects needing accurate UART timing, 4 MHz or 8 MHz with appropriate SPBRG calculation also works well.
Add comment