PIC16F877A

Photo of author

By Jackson Taylor

As an Amazon Associate we earn from qualifying purchases.

The PIC16F877A is an 8-bit microcontroller from Microchip. It is one of the most common chips for students and hobbyists learning embedded programming. It has enough memory and peripherals for real projects, and it is easy to find in a 40-pin DIP package that fits a breadboard.

On this page I use the PIC16F877A with a basic circuit, a PicKit2 burner to load the program, and two compilers: HI-TECH C and MIKRO C. Each compiler has a list of working examples below.

What is the PIC16F877A microcontroller?

The PIC16F877A is a flash-based microcontroller in Microchip’s mid-range PIC16 family. Because it uses flash memory, you can erase and reprogram it many times, which suits learning and prototyping.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

Its main features are:

  • 40 pins with up to 33 usable I/O lines, arranged in five ports (PORTA to PORTE).
  • 8K words of flash program memory, plus RAM and a small block of data EEPROM.
  • 10-bit ADC with 8 analog input channels.
  • Three timers: Timer0, Timer1 and Timer2.
  • Two CCP modules for capture, compare and PWM output.
  • Serial communication: a USART for UART and an MSSP module for SPI and I2C.
  • Clock speed of up to 20 MHz with an external crystal.

Check the Microchip datasheet for exact figures before you design a board, especially for current limits and voltage ranges.

Basic circuit for the PIC16F877A

The chip needs very few external parts to run. The circuit below shows the minimum setup used for the examples on this page.

PIC16F877A basic circuit diagram for the 40-pin microcontroller

In general, a working circuit needs:

  • Power: a regulated 5 V supply connected to both VDD pins (11 and 32) and both VSS ground pins (12 and 31).
  • Oscillator: a crystal across OSC1 and OSC2 (pins 13 and 14), with a small capacitor from each pin to ground. The datasheet lists suitable values for each crystal frequency.
  • Reset: a pull-up resistor from MCLR (pin 1) to VDD, so the chip does not reset by accident.
  • Decoupling: a small capacitor across the supply, placed close to the chip.
See also
STM32 BOARDS

Programming the chip with PicKit2

A PicKit2 programmer burns the compiled HEX file into the chip through in-circuit serial programming. It connects to MCLR, VDD, VSS and the two programming pins, PGC and PGD (RB6 and RB7). Keep those two pins free of heavy loads while you program, or the programmer may fail to connect.

The usual workflow is simple: write the code, compile it to a HEX file, then load that file with the programmer software and burn it.

MIKRO C examples

MIKRO C is a user-friendly compiler that comes with many ready-made libraries. Hardware such as LCDs, UART and ADC takes only a few lines of code. This makes it a good choice if you want results quickly. If you want to connect a character LCD or read sensors, start with the examples below.

If you prefer to test in software first, there is also a set of PIC16F877A MIKRO C BASED PROTEUS SIMULATIONS. For a project that combines several parts, see keypad interfacing with PIC16F877A, which uses both Proteus and MIKRO C.

HI-TECH C examples

HI-TECH C is a free compiler that can be integrated with the MPLAB IDE. The programs here are written without library functions. You set the configuration and peripheral registers yourself, so you learn what the chip is actually doing. Microchip’s XC8 compiler later took over from HI-TECH C, and the register-level approach carries over to it.

A sensible order is to start with digital output and input, then move on to reading analog voltages with the ADC, and then to generating a PWM signal. UART and EEPROM follow naturally after that.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works
See also
FTP -SENDING FILE BETWEEN WINDOWS AND RASPBERRY PI

Which compiler should you choose?

MIKRO C saves time because the libraries handle the low-level details. HI-TECH C takes more effort, but you see exactly which registers and bits control each peripheral. Many people start with MIKRO C to get something working, then learn the register-level approach for a deeper understanding.

Proteus simulations

Simulating in Proteus lets you check your code and circuit before you build anything. The video below shows a simulation in Proteus using MIKRO C.

SIMULATION IN PROTEUS USING MIKRO C

FAQ

What is the PIC16F877A used for?

It is used for learning projects and small embedded systems, such as sensor readers, motor controllers, displays and serial communication devices. Its ADC, timers, PWM and UART cover most beginner needs.

How do I program the PIC16F877A?

Write your code in a compiler such as MIKRO C or HI-TECH C, build it into a HEX file, and burn it to the chip with a programmer such as the PicKit2.

Is HI-TECH C free?

Yes, HI-TECH C is a free compiler and it works with the MPLAB IDE.

Can I test PIC16F877A code without hardware?

Yes. Proteus can simulate the chip with your compiled HEX file, so you can check the logic before wiring a real circuit.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works