Parts

PIC16F877A: DIP-40 pinout, MCLR reset, 20 MHz crystal and ICSP pins

By

The PIC16F877A in its 40-pin DIP has MCLR on pin 1, PORTA and PORTE on pins 2 to 10, the first supply pair on 11 (VDD) and 12 (VSS), the oscillator on 13 and 14, PORTC and PORTD on 15 to 30, the second supply pair on 31 (VSS) and 32 (VDD), and PORTB on 33 to 40. The programmer connects to MCLR (pin 1), RB6/PGC (39) and RB7/PGD (40). Both supply pairs must be wired.

Below: which chip you have, the ratings from Microchip's PIC16F87XA datasheet (DS39582C), what the ports do, and a minimal circuit with a 20 MHz crystal, the MCLR network Microchip recommends, the UART baud error, PWM and A/D timing, all worked out. It also covers RB3 and low-voltage programming, which can stop a new chip from running at all.

Datasheet: Microchip PIC16F87XA 28/40/44-Pin Enhanced Flash Microcontrollers (PDF)Ask Datasheet Guru about the PIC16F877A-I/P

Advertisement

Pinout by package

PIC16F877A-I/P DIP-40 · Microchip1=MCLR 2=RA0 3=RA1 4=RA2 5=RA3 6=RA4 7=RA5 8=RE0 9=RE1 10=RE2 11=VDD 12=VSS 13=OSC1 14=OSC2 15=RC0 16=RC1 17=RC2 18=RC3 19=RD0 20=RD1 21=RD2 22=RD3 23=RC4 24=RC5 25=RC6 26=RC7 27=RD4 28=RD5 29=RD6 30=RD7 31=VSS 32=VDD 33=RB0 34=RB1 35=RB2 36=RB3 37=RB4 38=RB5 39=RB6 40=RB7
top view, notch upPIC16F877A-I/P140MCLRRB7239RA0RB6338RA1RB5437RA2RB4536RA3RB3635RA4RB2734RA5RB1833RE0RB0932RE1VDD1031RE2VSS1130VDDRD71229VSSRD61328OSC1RD51427OSC2RD41526RC0RC71625RC1RC61724RC2RC51823RC3RC41922RD0RD32021RD1RD2

Top view, notch up: pin 1 (MCLR) top left, pins 1 to 20 down the left side and 21 to 40 back up the right.

Both VDD pins (11 and 32) and both VSS pins (12 and 31) must be connected, each pair with its own 100 nF capacitor.

RA4 (pin 6) is open drain: as an output it needs a pull-up.

Source: Microchip PIC16F87XA 28/40/44-Pin Enhanced Flash Microcontrollers, p. 3 (40-pin PDIP pin diagram for the PIC16F874A/877A with every alternate function); Microchip PIC16F87XA 28/40/44-Pin Enhanced Flash Microcontrollers, p. 11 (pinout description table: RB3/PGM on PDIP pin 36, RB6/PGC 39, RB7/PGD 40); Microchip PIC16F87XA 28/40/44-Pin Enhanced Flash Microcontrollers, p. 231 (product identification: -I = −40 to +85 °C, P = PDIP)

Which one do you have?

A through-hole chip is marked PIC16F877A-I/P: I is the −40 to +85 °C industrial range and P the 40-pin PDIP. Other suffixes are other packages (PT for the 44-lead TQFP, L for PLCC, ML for QFN), which number their pins differently.

PIC16LF877A is the low-voltage grade (2.0 to 5.5 V, 10 MHz at most). The PIC16F874A has the same pins with half the memory. The older PIC16F877 without the A has a separate datasheet and programming specification, so read the marking and pick the exact device in the programming software.

Absolute maximum ratings

Microchip is the only maker, so there is one table. The figures to design around are the current ones: 25 mA per pin is the absolute limit, not a working figure, and the output voltages are specified only at 8.5 mA (sinking) and 3.0 mA (sourcing). Pins share two 200 mA groups, and the whole chip may pass 300 mA out of VSS and 250 mA into VDD.

Absolute maximum ratings (DS39582C p. 173)
ParameterPIC16F877A-I/PDIP-40
Voltage on VDD−0.3 V to +7.5 V
Voltage on any pin except VDD, MCLR and RA4−0.3 V to VDD + 0.3 V
Voltage on MCLR0 to +14 V
Voltage on RA40 to +8.5 V
Current sunk or sourced by any I/O pin25 mA
PORTA + PORTB + PORTE, sunk or sourced200 mA
PORTC + PORTD, sunk or sourced200 mA
Current out of VSS / into VDD300 mA / 250 mA
Total power dissipation1.0 W
Ambient temperature under bias−55 to +125 °C
Storage temperature−65 to +150 °C

Ports, pin levels and supply current

Limits at −40 to 85 °C, PIC16F877A unless noted (pp. 175 to 179, 184, 195)
ParameterPIC16F877A-I/PDIP-40
Operating supply VDDPIC16F877A / PIC16LF877A4.0 to 5.5 V / 2.0 to 5.5 V
ClockPIC16F877A / PIC16LF877ADC to 20 MHz / DC to 10 MHz
Instruction cycle20 MHz200 ns
Brown-out reset trip VBOR3.65 to 4.35 V (4.0 V typ)
Power-up timer TPWRTVDD = 5 V28 to 132 ms (72 ms typ)
MCLR input high VIH0.8 VDD min
Input low, TTL buffer VILVDD = 4.5 to 5.5 V0.8 V max
Input high, TTL buffer VIHVDD = 4.5 to 5.5 V2.0 V min
Input high, Schmitt trigger VIH0.8 VDD min
Output low VOLIOL = 8.5 mA, VDD = 4.5 V0.6 V max
Output high VOHIOH = −3.0 mA, VDD = 4.5 VVDD − 0.7 V min
PORTB weak pull-up current IPURBVDD = 5 V, pin at VSS50 to 400 µA (250 µA typ)
Supply current IDDHS, 20 MHz, VDD = 5.5 V7 mA typ, 15 mA max
Supply current IDDXT, 4 MHz, VDD = 5.5 V1.6 mA typ, 4 mA max
Sleep current IPDVDD = 4.0 V, WDT off / on16 µA / 42 µA max
A/D converter8 channels10 bits, TAD 1.6 µs min, 12 TAD a conversion

PORTA and PORTE carry the eight analog inputs AN0 to AN7, and they come up as analog after reset, so a pin you want as digital I/O needs ADCON1 set first. RA4 is open drain, so as an output it needs a pull-up. PORTB has weak pull-ups you can switch on in software, interrupt-on-change on RB4 to RB7, and the programming pins RB3, RB6 and RB7. PORTC holds the peripherals: the two PWM outputs (RC1, RC2), SPI and I2C (RC3 to RC5) and the UART (RC6 TX, RC7 RX). PORTD is plain I/O, or the 8-bit parallel slave port.

4102023.345.5PIC16F877A20 MHzPIC16LF877A3.3 V:10 MHzMHzsupply voltage VDD, volts
Figure 1. Clock against supply, from Figures 17-1 and 17-2 on p. 174. The standard part needs 4.0 V even at a low clock; only the LF grade runs on 3.3 V.

What it is good for

  • Course and lab work built around the mid-range PIC instruction set, MPLAB and a PICkit-style programmer, on a chip that sits in a breadboard.
  • 5 V control boards that need a lot of pins at once: 33 I/O lines, eight 10-bit analog inputs, two PWM outputs, a UART and SPI or I2C.
  • Keeping an existing PIC16F877A design or project kit running.

For a new design on 3.3 V, with USB, or with more RAM than 368 bytes, a newer chip is the better start. The ATmega328P covers similar ground in 28 pins.

Worked example: the minimal PIC16F877A circuit

A 5 V supply, a 20 MHz crystal, the MCLR network from Figure 14-5 and both supply pairs decoupled: the least the chip needs to run its program, plus what that clock gives the UART, PWM and A/D.

1. Supply and clock. At 20 MHz the standard PIC16F877A needs 4.0 to 5.5 V (p. 175), so 5 V. One instruction takes four oscillator periods: 200 ns.

2. Crystal capacitors. In HS mode at 20 MHz, Table 14-2 (p. 146) gives 15 to 33 pF on each side. We use 22 pF, and the HS oscillator bit in the configuration word.

3. MCLR. Microchip no longer recommends tying MCLR straight to VDD (p. 148). R1 from VDD must be under 40 kΩ, and R2 into the pin over 1 kΩ. We use 10 kΩ, 1.5 kΩ and 100 nF. With the pin's worst leakage of 5 µA flowing through both resistors, MCLR sits at 4.94 V, above the 4.0 V (0.8 VDD) it needs to read high. A reset button across the capacitor draws 0.50 mA through R1 while it is held.

4. Start-up. The power-up timer holds the chip in reset for 72 ms typical (28 to 132 ms, p. 184), then the oscillator start-up timer counts 1024 crystal cycles: 51.2 µs at 20 MHz. The RC on MCLR does not have to make the delay; it filters the pin.

5. UART. With BRGH = 1 the baud rate is FOSC / (16 (X + 1)) (p. 113). For 9600 baud, X = 129, which gives 9615.4 baud (+0.16%). For 115,200, X = 10 gives 113636 (-1.36%). With BRGH = 0 the same 115,200 lands at 104167 (-9.58%), too far off to work.

6. PWM. The period is (PR2 + 1) × 4 × TOSC × prescale (p. 67). With PR2 = 255 and a prescale of 1, that is 51.2 µs, so 19.53 kHz, just above the audible range, which keeps a motor quiet, with 10-bit duty resolution.

7. A/D. At 20 MHz the A/D clock must be 32 TOSC (Table 11-1, p. 131), which makes TAD exactly 1.6 µs, the minimum allowed (p. 195). A 10-bit conversion takes 12 TAD: 19.2 µs, plus the sampling time before it.

PIC16F877A1 MCLR11 VDD12 VSS13 OSC114 OSC2VDD 32VSS 31RB3 36PGC 39PGD 401.5 kΩ10 kΩ+5 V100 nF+5 V100 nF20MHz22 pF each+5 V100 nF10 kΩpull-downto ICSP
Figure 2. The minimal circuit. The resistor and capacitor values are ours, inside the datasheet ranges given in the steps above.
03570105140PWRT, minimum28 msPWRT, typical72 msPWRT, maximum132 msOST at 20 MHz51.2 µsdelay after power-up, ms
Figure 3. Start-up delays from parameters 32 and 33 on p. 184. The power-up timer is most of the wait; the 1024-cycle crystal count adds 51.2 µs.
BRGH = 1BRGH = 0-10-50+5+109600-1.36 %+0.16 %19200+1.73 %+0.16 %57600+8.51 %-1.36 %115200-9.58 %-1.36 %baud error at 20 MHz, per cent
Figure 4. Baud error from the formulas on p. 113. With BRGH = 1 every rate stays within 1.36%; with BRGH = 0 the fast rates fall apart.

Worked example in numbers

Instruction cycle
200 ns
MCLR at worst leakage
4.94 V
9600 baud error
+0.16%
PWM at PR2 = 255
19.53 kHz

Tips and checks

  • Sink LEDs rather than source them: the low output is specified at 8.5 mA, the high one only at 3.0 mA. For a red LED of about 2 V, (5 − 2 − 0.6) V / 8.5 mA = 282 Ω, so 330 Ω: 7.27 mA at the VOL limit and 9.09 mA with the pin at 0 V. All 8 on PORTD come to 72.73 mA, well inside the 200 mA PORTC + PORTD group (Figure 5).
  • Set ADCON1 before using PORTA or PORTE as digital pins. After reset they are analog inputs, and a digital read of an analog pin returns 0.
  • Wire both VDD pins and both VSS pins, with a 100 nF capacitor across each pair at the chip.
  • Erasing the whole chip through ICSP needs VDD at 4.5 to 5.5 V (p. 158). On a board that runs at 4.0 V, raise the supply for that step.
  • Keep the crystal and its two capacitors within a couple of centimetres of pins 13 and 14, and select HS (not XT) in the configuration word for a 20 MHz crystal.
0100200300One pin25 mAPORTC + PORTD200 mAPORTA + B + E200 mAInto VDD250 mAOut of VSS300 mA8 LEDs on PORTD72.7 mAabsolute maximum current, mA
Figure 5. Absolute maximum currents from p. 173 against the eight-LED load. The chip itself adds 7 mA typical, 15 mA at most, at 20 MHz (p. 176).
Advertisement

Substitutes: PIC16F874A and PIC16LF877A

Both share the 40-pin layout and the same datasheet. The 874A loses half the memory; the LF grade trades top speed for a 2.0 V minimum supply.

Pin numbers as on the 40-pin PDIP top view, notch up, pin 1 top left
PartPinsVerdict
PIC16F874A-I/PSame 40 pinsSame pins, peripherals and limits from the same datasheet, with half the memory: 4096 words of flash, 192 bytes of RAM and 128 bytes of EEPROM. Fine for a program that fits; set the programmer for the 874A.Microchip PIC16F87XA 28/40/44-Pin Enhanced Flash Microcontrollers, p. 1; Microchip PIC16F87XA 28/40/44-Pin Enhanced Flash Microcontrollers, p. 3
PIC16LF877A-I/PSame 40 pinsThe low-voltage grade: it runs from 2.0 V, but its clock is capped by FMAX = 6.0 MHz/V × (VDD − 2.0 V) + 4 MHz and never above 10 MHz. Use it for 3.3 V boards at 10 MHz or less.Microchip PIC16F87XA 28/40/44-Pin Enhanced Flash Microcontrollers, p. 174; Microchip PIC16F87XA 28/40/44-Pin Enhanced Flash Microcontrollers, p. 175

Bench check: is it running?

  1. Measure 5 V from pin 11 to pin 12 and from pin 32 to pin 31.
  2. MCLR (pin 1) should read close to 5 V (4.94 V or more with these resistors). Near 0 V means the reset button or capacitor is shorted.
  3. RB3 (pin 36) should read 0 V if LVP is still on. If it sits high, the chip may be in programming mode and will not run code.
  4. Load a program that toggles a pin and watch it on a scope or a logic probe. A frequency counter on the crystal pins can stop a weak oscillator, so probe the output pin instead.
  5. If the programmer cannot find the chip, check that it is set for the PIC16F877A (not the 877 or 874A), that MCLR, PGC and PGD reach pins 1, 39 and 40, and that the board is powered at 4.5 V or more if the programmer erases the chip first.

What we'd do

Run the PIC16F877A-I/P at 5 V on a 20 MHz crystal with 22 pF capacitors, use the 10 kΩ / 1.5 kΩ / 100 nF MCLR network, decouple both supply pairs, put 10 kΩ from RB3 to ground unless LVP is switched off, and bring MCLR, PGC and PGD to a 6-pin ICSP header. Set BRGH = 1 for the UART, set ADCON1 before touching PORTA, and sink LEDs at about 7 to 9 mA each.

Questions people ask

What is the PIC16F877A pinout?

Pin 1 MCLR, 2 to 7 RA0 to RA5, 8 to 10 RE0 to RE2, 11 VDD, 12 VSS, 13 OSC1, 14 OSC2, 15 to 18 RC0 to RC3, 19 to 22 RD0 to RD3, 23 to 26 RC4 to RC7, 27 to 30 RD4 to RD7, 31 VSS, 32 VDD and 33 to 40 RB0 to RB7.

Which pins are used to program it?

MCLR/VPP (pin 1), RB6/PGC (pin 39) and RB7/PGD (pin 40), plus VDD and VSS. With low-voltage programming on, RB3/PGM (pin 36) is used as well.

What capacitors go with a 20 MHz crystal?

Microchip gives 15 to 33 pF on each side for HS mode at 20 MHz. 22 pF is a common choice; the crystal's own load capacitance decides the best value.

Can a PIC16F877A run on 3.3 V?

The standard part needs 4.0 V. The PIC16LF877A runs from 2.0 V, and at 3.3 V its clock limit works out to 10 MHz.

What is the difference between the PIC16F877 and the PIC16F877A?

They share the 40-pin layout, but the A has its own datasheet and its own programming specification (DS39589, named on p. 158), so the programmer must be told which one it is.

Why does my PIC16F877A not start?

The usual causes are MCLR held low, the wrong oscillator mode (XT instead of HS for a 20 MHz crystal), one supply pair left unconnected, or RB3 floating high with low-voltage programming still on.

Buying the PIC16F877A in India

The PIC16F877A-I/P is stocked by most Indian component shops and comes in many PIC starter kits. Read the full marking, since an 877 and an 877A look alike, and buy a 40-pin socket with it so the chip can come out for a programmer that has no ICSP lead. Our guide to buying electronic components in India compares ten online sellers and covers how to spot fakes.

Advertisement

One email a month from the bench

New circuits, lessons, part guides and calculators, on the first Sunday of each month.

We'll send one email to confirm. Every email has a one-click unsubscribe. Privacy policy

Used in these lessons

Lessons that go deeper

Datasheets this page was checked against

Makers revise datasheets. If the copy you download has a newer revision and a number here disagrees with it, the datasheet wins, and we would like to hear about it.