PIC16F877A C Programming

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 49

The Microchip Technology

PIC16F877 Microcontroller
C Programming
www.microchip.com

05/16/24 ECE331 (KEH) 1


PIC16F877 Summary
This powerful (200 nanosecond instruction execution) yet easy-to-
program (only 35 single word (14-bit wordlength) RISC instructions)
CMOS FLASH-based 8-bit microcontroller packs Microchip's powerful
PIC® architecture into an 40-pin dual in-line (DIP) or 44-pin plastic
leadless chip carrier (PLCC) or 44-pin Thin Quad Flat Pack (TQFP)
package.

The PIC16F877A features 256 bytes of EEPROM data memory, self


programming, an ICD, 2 Comparators, 8 channels of 10-bit Analog-to-
Digital (A/D) converter, 2 input capture/output compare/PWM functions,
the synchronous serial port can be configured as either 3-wire Serial
Peripheral Interface (SPI™) or the 2-wire Inter-Integrated Circuit (I²C™)
bus and a Universal Asynchronous Receiver Transmitter (USART).

All of these features make it ideal for more advanced level A/D
applications in automotive, industrial, appliances and consumer
applications.
05/16/24 ECE331 (KEH) 2
There are three
Choices of
PIC16F877
packages… Note
that the 40-pin DIP
package is very
wasteful of space,
but it is IDEAL for
breadboarding
prototype circuits!
We need to THANK
Microchip for
retaining this
outdated (but VERY
helpful package)!

05/16/24 ECE331 (KEH) 3


PIC16F877A Memory
• 8 K of flash program ROM (14-bits wide). Each of
the 35 RISC instructions fits in just one 14-bit
wide location, permitting fast (200 ns) instruction
execution time, if microcontroller is clocked at its
maximum allowable clock rate of 20 MHz.

• 368 bytes of volatile data RAM “File Register”


locations (8-bits wide) for temporary storage of
data.

• 256 bytes of nonvolatile EEROM for storage of


calibration data, user-customized option settings,
etc.
05/16/24 ECE331 (KEH) 4
PIC16F877 (40-pin DIP) Pinout

05/16/24 ECE331 (KEH) 5


PIC16F877A Hardware
Block Diagram
(Figure 2.1 of PIC16F877A
Datasheet)

05/16/24 ECE331 (KEH) 6


05/16/24 ECE331 (KEH) 7
PIC16F877A Block Diagram
(Cont’d)

05/16/24 ECE331 (KEH) 8


PIC16F877 Program Memory and Stack

05/16/24 ECE331 (KEH) 9


Data Memory Organization
-Divided into FOUR banks, selected by
RP1:RP0, STATUS register bits 5:6

05/16/24 ECE331 (KEH) 10


05/16/24 ECE331 (KEH) 11
PIC16F877 STATUS register

05/16/24 ECE331 (KEH) 12


PIC16F877 General Purpose I/O Pins
• Up to 33 I/O pins available (assuming that the
pin’s special function(s) are not needed.)
• RA5:0 (PORTA) (Note: the programmer must
set ADCON1 = 6 in order to configure Port A for
digital I/O, since, out of reset, PORTA is
configured as analog inputs.
• RB7:0 (PORTB) (Note: when using the ICD-2 in-
circuit debugger “hockey puck”, RB6:7 are
needed for in-circuit debugging and should not
be used in your application.)
• RC7:0 (PORTC)
• RD7:0 (PORTD)
• RE2:0 (PORTE)

05/16/24 ECE331 (KEH) 13


Simple Parallel Digital I/O
• Each port has a data direction register (called
TRISA, TRISB, etc.) whose bits are set to “1” for
input (note “1” resembles “I”) and to “O” for
output (note “0” resembles “O”) to set the
direction of the corresponding I/O pin. (Note this
is opposite from the 9S12C32!)
• Each port has a data I/O register (called
PORTA, PORTB, etc.) whose (input) bits may be
read from, and whose output bits may be written
to. The pins of PORTA are called RA7, RA6, etc.
• Remember that PORTA cannot be used for
general purpose digital I/O unless ADCON1 is
first set to a value of 6 to disable the analog
input special function that is set out of reset.
05/16/24 ECE331 (KEH) 14
Parallel Digital I/O (Cont’d)
• In Hi-Tech PIC-C Lite, individual bits may be
very CONVENIENTLY read or written by
referring to them as RA0, RB1, RC2, etc.
• Examples (there are LEDs on RD3:0 and switches on RD7:4)

PORTD = 0; //Ensure all 4 LEDs OFF after TRISD set.


TRISD=0xF0; //Make RD7:4 inputs, RD3:0 outputs.
while (PORTD&$40==$40); //Wait here until switch on
// RD6 goes low.
while (RD5 == 1); //Wait here until switch on RD5 goes low.
PORTD = PORTD | 0b00000100 //Make LED on RD2 turn ON
PORTD = PORTD &0b11111011 //Make LED on RD2 turn OFF
RD1 = 1; //Make LED on RD1 turn ON
RD1 = 0; //Make LED on RD1 turn OFF

05/16/24 ECE331 (KEH) 15


Some Parallel I/O Caveats
If you read about PORTA in the PIC16F877A datasheet (see datasheet
Fig. 4-2), you will find that RA4, when used as an output, is OPEN DRAIN.
This means that the logic 0 output condition is like an internal connection
to ground, as usual; but the logic 1 output condition is a floating
connection, rather than the normal internal connection to Vdd.

Likewise, you will see in datasheet Section 2.2.2.2 that there is an RBPU\
(Port B pull-up enable) bit that can be CLEARED in the OPTION register
to allow PORTB to have internal pull-ups enabled on the PORTB input
pins. This is quite handy if one wants to interface an X-Y keyswitch matrix
to PORTB! Other port inputs besides PORTB must be externally pulled
up.

Another caveat that was already mentioned, but bears repeating, involves
the use of PORTA for general-purpose digital I/O. The ADCON1 register
must be configured to permit various PORTA inputs to be used for
general-purpose parallel I/O. Be sure to consult the PIC16F877A data
sheet before using any of the PORTA pins as general-purpose I/O.

05/16/24 ECE331 (KEH) 16


Special Functions of PIC16F877A I/O Pins
--Many of these functions will be introduced in later examples
• Three hardware timers: Timer 0 (RA4/TOCK1 pin), Timer 1 (RC0/T1OSO/TICKI
and RC1/T1OSI pins), and Timer 2 (no external pins are associated with this
timer – this timer can be set to periodically interrupt via the PR2 period
register) See PIC16F877 Datasheet Sections 5.0, 6.0, and 7.0.
• 10-bit, 8-channel analog-to-digital converter (analog inputs on Ports A and E)
See PIC16F877 Datasheet Section 11.0.
• 8-bit parallel slave port (PSP) for making the PIC look like an addressable 8-bit
peripheral register on a host computer’s bus (Port D for the 8-bit data bus
connection and PORT E for the RD\, WR\, and CS\ bus control lines). See
PIC16F877 Datasheet Section 4.6.
• Input Capture – Output Compare – Pulse Width Modulation pins (RC2/CCP1
and RC1/T1OSI/CCP2). See PIC16F877 Datasheet Section 8.0.
• Synchronous serial data port (SDI, SDO, and SCK pins) used for implementing
master and slave serial peripheral interface SPI and I2C serial interfaces. See
PIC16F877 Datasheet Section 9.0.
• Asynchronous serial data port, or Universal Asynchronous Receiver-
Transmitter, (RC6/TX and RC7/RX pins). See PIC16F877 Datasheet Section
10.0.
• Two analog comparators with programmable analog reference voltage (PORT A
pins are used for the analog comparator inputs and also the digital comparator
outputs. See PIC16F877 Datasheet Section 12.0.

05/16/24 ECE331 (KEH) 17


Example 1. PIC16F877 LED Flasher Circuit
V D D = +5V

R1
SW 1
10 K

S W P U S H B U TTO N
P IC 16F877A D1

2 15
3 R A0/AN 0 R C 0 / T1 O S O / T1 C K I 16
4 R A1/AN 1 R C 1 / T1 O S I / C C P 2 17 R3
R A2/AN 2/VR EF - R C 2/C C P1
ICD-2 Connector 5
6 R A3/AN 3/VR EF +
R A 4 / T0 C K I
R C 3/SC K/SC L
R C 4/SD I/SD A
18
23
LED
220 O hm s
Not Used 7
R A5/AN 4/SS R C 5/SD O
24
6 (WHI) R C 6 / TX/ C K
25
33 26
R B0/IN T R C 7 / R X/ D T
Program Clock 34
R B1
5 (BLK) 35
R B2 R D 0/PSP0
19
36 20
R B3/PG M R D 1/PSP1
Program Data 37
R B4 R D 2/PSP2
21
4 (RED) 38
R B5 R D 3/PSP3
22
39 27
R B6/PG C R D 4/PSP4
Ground C1 C R Y S TA L 40
R B7/PG D R D 5/PSP5
28
3 (GRN) R D 6/PSP6
29
13 30
O SC 1/C LKIN R D 7/PSP7
+5V 22 PF Y1 14
O SC 2/C LKO U T
2 (YEL) R E0/R D /AN 5
8
1 9
M C L R / V P P / TH V R E1/W R /AN 6 10
C2 22 PF 11 R E2/C S/AN 7
VD D
1 (BLU) Reset 32
VD D VSS VSS

VD D = 5 V 10 K R2 PIC 16F 877 31 12

0.1 U F C3

05/16/24 ECE331 (KEH) 18


Example 1 LED Flasher Example C Program

05/16/24 ECE331 (KEH) 19


05/16/24 ECE331 (KEH) 20
Using MPLAB V. 6.30 Integrated
Debugging Environment
• CAREFULLY follow the steps listed in the “MPLAB V6.30,
PicC-Lite C Compiler, ICD-2 Debugger” tutorial that is found
on your class CDROM to install Microchip’s MPLAB
V6.30 and also Hi-Tech’s PIC-C Lite on your laptop.

• Follow the steps that illustrate how to use the MPLAB


simulator and also the Microchip ICD-2 in-circuit
debugger to debug the program of Example 1.

• There is one important step (Step 12) that I want to


emphasize:
05/16/24 ECE331 (KEH) 21
Important step before using the MPLAB simulator
or before using the ICD-2 for in-circuit debugging.

05/16/24 ECE331 (KEH) 22


Example 2: College Bowl Lockout System

05/16/24 ECE331 (KEH) 23


Example 2: College Bowl Lockout System Hardware Diagram

05/16/24 ECE331 (KEH) 24


Example 2. College Bowl Lockout System Software

05/16/24 ECE331 (KEH) 25


05/16/24 ECE331 (KEH) 26
Example 3. Musical Tone
Sequencing Program

05/16/24 ECE331 (KEH) 27


Example 3. Musical Tone Circuit Diagram

05/16/24 ECE331 (KEH) 28


Ex. 3. Musical Tone Generating Software

05/16/24 ECE331 (KEH) 29


05/16/24 ECE331 (KEH) 30
Example 4. Stepper Motor Light Seeker

05/16/24 ECE331 (KEH) 31


Example 4. Stepping Motor Light Seeker Circuit Diagram

05/16/24 ECE331 (KEH) 32


Example 4. Light Seeker Software

05/16/24 ECE331 (KEH) 33


05/16/24 ECE331 (KEH) 34
Example 5. Interrupt-Driven PWM DC Motor Speed Controller

05/16/24 ECE331 (KEH) 35


RB0
PWM (0.2 ms Time Slots) Vcc DC Power
Supply
(PWM)
DC Motor or BJT driven
1 kilohm
Light Bulb between cutoff
and saturation, so
it never gets hot!
Motor Stopped
0% Vcc 0 all 10 time slots

Slow speed 1 for 5 time slots


0 for 5 time slots
(50% Vcc)
0 ms 2 ms 4 ms
Faster Speed 1 for 7 time slots
0 for 3 time slots
(75% Vcc)

Fastest Speed
1 for all 10 time slots
100% Vcc

05/16/24 ECE331 (KEH) 36


INTCON – Interrupt Control Register

05/16/24 ECE331 (KEH) 37


INTCON Register Format
– Enables Interrupts Globally and Locally

05/16/24 ECE331 (KEH) 38


Setting up the Prescaler to cause
the TMR0 Overflow to interrupt at
the desired 0.20 ms interval.

05/16/24 ECE331 (KEH) 39


TMR0 and Prescaler Configuration

05/16/24 ECE331 (KEH) 40


Option Register Format

05/16/24 ECE331 (KEH) 41


PWM Program Operation

05/16/24 ECE331 (KEH) 42


Example 5 PWM Program

05/16/24 ECE331 (KEH) 43


05/16/24 ECE331 (KEH) 44
05/16/24 ECE331 (KEH) 45
05/16/24 ECE331 (KEH) 46
Example 6. Use of the A/D and USART

05/16/24 ECE331 (KEH) 47


05/16/24 ECE331 (KEH) 48
05/16/24 ECE331 (KEH) 49

You might also like