PIC16F877A an Introduction
The PIC16F877A stands as an iconic member within the Microchip PIC (Peripheral Interface Controller) family, revered for its versatility and functionality in the realm of embedded systems. As an 8-bit microcontroller, it offers a robust blend of features, making it a favored choice among hobbyists, students, and professionals alike. Renowned for its reliability, efficiency, and ease of use, the PIC16F877A boasts a rich set of peripherals, ample memory, and a wide array of I/O capabilities, empowering developers to craft diverse applications spanning from simple LED blinking projects to more complex automation systems and control mechanisms. Its adaptability, along with a robust ecosystem of development tools and resources, renders it a cornerstone in the world of embedded electronics.
PIC16F877A and ATmega328P(Arduino)
While the PIC16F877A and the ATmega328P differ significantly in architecture, instruction sets, and ecosystem, the PIC16F877A can serve as a potential replacement for the ATmega328P in Arduino boards. However, due to the variations in architecture and programming methodologies between the PIC and AVR families, migrating from the ATmega328P to the PIC16F877A might require substantial code rewrites and adjustments. The PIC16F877A offers comparable functionalities, ample memory, and a rich set of peripherals, but its utilization would involve a learning curve for those accustomed to the Arduino ecosystem. Despite these differences, with proper adaptation and adjustments, the PIC16F877A can serve as a competent alternative for applications requiring a microcontroller in Arduino-compatible projects.
Why LED Blink?
Blinking an LED might seem trivial, but it holds immense importance in testing a microcontroller like the PIC16F877A.
Hardware Testing: LED blink tests the fundamental functionality of GPIO (General Purpose Input/Output) pins and their configuration on the microcontroller. It verifies whether the microcontroller is correctly set up and functioning.
Compiler Testing: Running a simple LED blink program helps ensure the compiler, often a critical part of the development process, is correctly configured. It validates the compiler's ability to generate correct machine code for the targeted microcontroller.
Uploading Process: Utilizing this basic program aids in testing the process of transferring code from a PC to the microcontroller. It ensures that the uploading or programming process, often done via USB, serial, or parallel ports using tools like PICkit or other programmers, is functional and successful.
Programmer Validation: Testing LED blink facilitates the validation of the PIC programmer (such as PICkit) used to transfer code to the microcontroller. It ensures the programmer can communicate effectively with the microcontroller, allowing for successful code uploads and device operation.
By confirming these foundational aspects through LED blinking tests, developers can establish a stable starting point for more complex applications and have confidence in the functionality of both the hardware and software elements of the microcontroller system.
LED Blink with PIC16F877A
The following program is written in C and designed for the PIC 16F877A
microcontroller to make an LED connected to pin RB1 blink at a regular
interval.
#define _XTAL_FREQ 4000000
#include <xc.h>
// BEGIN CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#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)
//END CONFIG
void main(void){
TRISB1 = 0; //RB1 as Output PIN
while (1){
RB1 = 1;
__delay_ms(200);
RB1 = 0;
__delay_ms(200);
}
}
Code Explanation:
#define _XTAL_FREQ 4000000
: This line defines the crystal frequency as 4MHz. This value is used for generating delays in the code.
#include <xc.h>
: This line includes the header file for the PIC microcontroller. It contains definitions specific to the PIC 16F877A, such as register addresses and configurations.
Configuration settings: These lines, between // BEGIN CONFIG
and // END CONFIG
, set the configuration bits for the microcontroller. They determine various settings such as oscillator type, watchdog timer, power-up timer, etc.
void main(void)
: This is the main function where the program execution begins.
TRISB1 = 0; //RB1 as Output PIN
: This line configures pin RB1 (port B, pin 1) as an output pin by setting its corresponding TRISB (TRIState Register B) bit to 0.
while(1) {...}
: This is an infinite loop. Inside this loop, the LED connected to RB1 will blink continuously.
RB1 = 1;
: Sets RB1 high, turning the LED ON.
__delay_ms(200);
: Delays the program execution for 200 milliseconds. This creates a pause, keeping the LED on for that duration.
RB1 = 0;
: Sets RB1 low, turning the LED OFF.
__delay_ms(200);
: Delays the program execution for another 200 milliseconds. This keeps the LED off for that duration.
The program continuously toggles the RB1 pin between high and low states with 200ms delays, effectively creating a blinking LED effect.
This code assumes that an LED is connected to RB1 through an appropriate current-limiting resistor to avoid damaging the LED. Also, ensure the microcontroller is properly powered and configured to run at the specified frequency (4MHz in this case) for accurate timing.
Circuit Diagram LED PIC16F877A
The following is the circuit schematic for the above LED blink project.
An external crystal of 4MHz is connected to the PIC microcontroller. The LED is connected to the Port Pin RB1 and grounded using a 220Ohm resistor as shown. The 10kOhm is a pullup resistor and the button is to reset the PICF877A microcontroller.
I hope this short tutorial on blinking LED with PIC16F877A microcontroller was useful to you. Any comments are welcome.