16f877a Pic Controller
16f877a Pic Controller
All single-cycle instructions except for program branches, which are two-cycle
Peripheral Features:
Timer1: 16-bit timer/counter with prescaler, can be incremented during Sleep via
external crystal/clock
Timer2: 8-bit timer/counter with 8-bit period register, prescaler and postscaler
Two Capture, Compare, PWM modules Capture is 16-bit, max. resolution is 12.5
ns Compare is 16-bit, max. resolution is 200 ns. PWM max. resolution is 10-bit
Synchronous Serial Port (SSP) with SPI (Master mode) and I2C
(Master/Slave)
Parallel Slave Port (PSP) 8 bits wide with external RD, WR and CS controls
(40/44-pin only)
Analog Features:
10-bit, up to 8-channel Analog-to-Digital Converter (A/D)
Brown-out Reset (BOR)
Analog Comparator module with:
- Two analog comparators
Pin diagram:
Device features
Block diagram
Pin description:
Pin 13
OSC1/CLKI Oscillator crystal or external clock input.
OSC1 Oscillator crystal input or external clock source input. ST buffer when configured
in RC mode, otherwise CMOS.
CLKI External clock source input. Always associated with pin function OSC1
Pin 14
OSC2/CLKO Oscillator crystal or clock output
OSC2 Oscillator crystal output- Connects to crystal or resonator in Crystal Oscillator
mode.
CLKO - In RC mode, OSC2 pin outputs CLKO, which has 1/4 the frequency of OSC1
and denotes the instruction cycle rate.
Pin 1
MCLR/VPP Master Clear (input) or programming voltage (output)
MCLR Master Clear (Reset) input. This pin is an active low Reset to the device
VPP Programming voltage input
Pin 2
RA0/AN0
RA0 Digital I/O
AN0 Analog input 0
PORTA is a bidirectional I/O port
Pin 3
RA1/AN1
RA1 Digital I/O
AN1 Analog input 1
Pin 4
RA2/AN2/VREF-/CVREF
RA2 Digital I/O
AN2 Analog input 2.
VREF A/D reference voltage (Low) input.
CVREF Comparator VREF output
Pin 5
RA3/AN3/VREF+
RA3 Digital I/O
AN3 Analog input 3
VREF+ A/D reference voltage (High) input
Pin 6
RA4/T0CKI/C1OUT
RA4 Digital I/O Open-drain when configured as output.
T0CKI Timer0 external clock input
C1OUT Comparator 1 output
Pin 7
RA5/AN4/SS/C2OUT
RA5 Digital I/O
AN4 Analog input 4.
SS SPI slave select input
C2OUT Comparator 2 output
PORTB is a bidirectional I/O port. PORTB can be software programmed for
internal weak pull-up on all inputs.
Pin 33
RB0/INT
RB0 Digital I/O
INT External interrupt
Pin 34
RB1 Digital I/O
Pin 35
RB2 Digital I/O
Pin 36
RB3/PGM
RB3 Digital I/O
PGM Low-voltage ICSP programming enable pin
Pin 37
RB4 Digital I/O
Pin 38
RB5 Digital I/O
Pin 39
RB6/PGC
RB6 Digital I/O
PGC In-circuit debugger and ICSP programming clock
Pin 40
RB7/PGD
RB7 Digital I/O
PGD In-circuit debugger and ICSP programming data
Pin 24
RC5/SDO
RC5 Digital I/O
SDO SPI data out
Pin 25
RC6/TX/CK
RC6 Digital I/O
TX USART asynchronous transmit
CK USART1 synchronous clock
Pin 26
RC7/RX/DT
RC7 Digital I/O
RX USART asynchronous receive
DT USART synchronous data
PORTD is a bidirectional I/O port or Parallel Slave Port when interfacing to a
microprocessor bus.
Pin 19
RD0/PSP0
RD0 Digital I/O
PSP0 Parallel Slave Port data
Pin 20
RD1/PSP1
RD1 Digital I/O
PSP1 Parallel Slave Port data
Pin 21
RD2/PSP2
RD2 Digital I/O
PSP2 Parallel Slave Port data
Pin 22
RD3/PSP3
RD3 Digital I/O
PSP3 Parallel Slave Port data
Pin 27
RD4/PSP4
RD4 Digital I/O
PSP4 Parallel Slave Port data
Pin 28
RD5/PSP5
RD5 Digital I/O
PSP5 Parallel Slave Port data
Pin 29
RD6/PSP6
RD6 Digital I/O
PSP6 Parallel Slave Port data
Pin 30
RD7/PSP7
RD7 Digital I/O
PSP7 Parallel Slave Port data
MEMORY ORGANIZATION
There are three memory blocks in each of the PIC16F87XA devices. The program
memory and data memory have separate buses so that concurrent access can occur and is
detailed in this section.
The data memory is partitioned into multiple banks which contain the General Purpose
Registers and the Special Function Registers. Bits RP1 (Status<6>) and RP0 (Status<5>)
are the bank select bits.
Each bank extends up to 7Fh (128 bytes). The lower locations of each bank are reserved
for the Special Function Registers. Above the Special Function Registers are General
Purpose Registers, implemented as static RAM. All implemented banks contain Special
Function Registers. Some frequently used Special Function Registers from one bank may
be mirrored in another bank for code reduction and quicker access.
Register File Map
bits are not writable, therefore, the result of an instruction with the Status register as
destination may be different than intended.
For example, CLRF STATUS will clear the upper three bits and set the Z bit. This leaves
the Status register as 000u u1uu (where u = unchanged). It is recommended, therefore,
that only BCF, BSF, SWAPF and MOVWF instructions are used to alter the Status
register because these instructions do not affect the Z, C or DC bits from the Status
register.
OPTION_REG Register
The OPTION_REG Register is a readable and writable register, which contains various
control bits to configure the TMR0 prescaler/WDT post scaler (single assignable register
known also as the prescaler), the external INT interrupt, TMR0 and the weak pull-ups on
PORTB.
INTCON Register
The INTCON register is a readable and writable register, which contains various enable
and flag bits for the TMR0 register overflow, RB port change and external RB0/INT pin
interrupts.
PCON Register
The Power Control (PCON) register contains flag bits to allow differentiation between a
Power-on Reset (POR), a Brown-out Reset (BOR), a Watchdog Reset (WDT) and an
external MCLR Reset.
0 = A Power-on Reset occurred (must be set in software after a Power-on Reset occurs)
bit 0 BOR: Brown-out Reset Status bit
1 = No Brown-out Reset occurred
0 = A Brown-out Reset occurred (must be set in software after a Brown-out Reset occurs)
STACK
The PIC16F87XA family has an 8-level deep x 13-bit wide hardware stack. The stack
space is not part of either program or data space and the stack pointer is not readable or
writable. The PC is PUSHed onto the stack when a CALL instruction is executed, or an
interrupt causes a branch. The stack is POPed in the event of a RETURN, RETLW or a
RETFIE instruction execution. PCLATH is not affected by a PUSH or POP operation.
The stack operates as a circular buffer. This means that after the stack has been PUSHed
eight times, the ninth push overwrites the value that was stored from the first push. The
tenth push overwrites the second push (and so on).
Program Memory Paging
All PIC16F87XA devices are capable of addressing a continuous 8K word block of
program memory. The CALL and GOTO instructions provide only 11 bits of address to
allow branching within any 2K program memory page. When doing a CALL or GOTO
instruction, the upper 2 bits of the address are provided by PCLATH<4:3>. When doing a
CALL or GOTO instruction, the user must ensure that the page select bits are
programmed so that the desired program memory page is addressed. If a return from a
CALL instruction (or interrupt) is executed, the entire 13-bit PC is popped off the stack.
Therefore, manipulation of the PCLATH<4:3> bits is not required for the RETURN
instructions (which POPs the address from the stack).
Note: The contents of the PCLATH register are unchanged after a RETURN or RETFIE
instruction is executed. The user must rewrite the contents of the PCLATH register for
any subsequent subroutine calls or GOTO instructions.
The below example shows the calling of a subroutine in page 1 of the program memory.
This example assumes that PCLATH is saved and restored by the Interrupt Service
Routine (if interrupts are used).
Example: CALL OF A SUBROUTINE IN PAGE 1 FROM PAGE 0
ORG 0x500
BCF PCLATH, 4
BSF PCLATH, 3; Select page 1
;( 800h-FFFh)
CALL SUB1_P1; Call subroutine in
: page 1 (800h-FFFh)
:
Note: On a Power-on Reset, these pins are configured as analog inputs and read as 0.
The comparators are in the off (digital) state.
The TRISA register controls the direction of the port pins even when they are being used
as analog inputs. The user must ensure the bits in the TRISA register are maintained set
when using them as analog inputs.
depression. RB0/INT is an external interrupt input pin and is configured using the
INTEDG bit (OPTION_REG<6>).
PORTD is an 8-bit port with Schmitt Trigger input buffers. Each pin is individually
configurable as an input or output.
PORTD can be configured as an 8-bit wide microprocessor port (Parallel Slave Port) by
setting control bit, PSPMODE (TRISE<4>). In this mode, the input buffers are TTL.
buffers. The PORTE pins become the I/O control inputs for the microprocessor port
when bit PSPMODE (TRISE<4>) is set. In this mode, the user must make certain that
the TRISE<2:0> bits are set and that the pins are configured as digital inputs. Also,
ensure that ADCON1 is configured for digital I/O. In this mode, the input buffers are
TTL. PORTE pins are multiplexed with analog inputs. When selected for analog input,
these pins will read as 0s. TRISE controls the direction of the RE pins, even when they
are being used as analog inputs. The user must make sure to keep the pins configured as
inputs when using them as analog inputs.