PIC16F87X Programming Tutorials

Learn microcontroller programming with detailed examples and explanations

Master PIC Microcontroller Programming

A comprehensive collection of tutorials, code examples, and explanations for the PIC16F87X series

Start Learning

About PIC16F87X Microcontrollers

Understanding the basics of PIC16F877 and related microcontrollers

The PIC16F87X series, including the PIC16F877, are powerful 8-bit microcontrollers from Microchip Technology widely used in embedded systems and electronic projects. With their versatile features and ease of programming, they're perfect for learning microcontroller concepts and building practical applications.

40-Pin Architecture

The PIC16F877 comes in a 40-pin package, providing numerous I/O pins and peripherals for interfacing with external devices and sensors.

RISC Architecture

Features a high-performance RISC CPU with only 35 single-word instructions, most executing in a single cycle for efficient processing.

Memory

Includes 8K words of Flash Program Memory, 368 bytes of Data Memory (RAM), and 256 bytes of EEPROM data memory for flexible storage options.

Multiple I/O Ports

Features multiple I/O ports (PORTA through PORTE) that can be individually configured as input or output pins for interfacing with external devices.

PIC Programming Fundamentals

Essential concepts for programming PIC microcontrollers

Bank Switching in PIC Microcontrollers

The PIC16F877 organizes its registers into different banks. To access registers in different banks, you need to switch between them using the RP0 and RP1 bits in the STATUS register.

Why Bank Switching?

The PIC16F877 has more registers than can be addressed in its limited address space. By organizing registers into banks, it can access more registers using the same address space.

Bank RP1 (Bit 6) RP0 (Bit 5) Registers
Bank 0 0 0 PORTA, PORTB, PORTC, PORTD, etc.
Bank 1 0 1 TRISA, TRISB, TRISC, TRISD, ADCON1, etc.
Bank 2 1 0 Other special function registers
Bank 3 1 1 Additional special function registers

Here are the macros commonly used for bank switching:

Bank Switching Macros
BANK0   MACRO
        BCF STATUS, RP0             ; RP0 = 0
        BCF STATUS, RP1             ; RP1 = 0 --> BANK 0
        ENDM

BANK1   MACRO
        BSF STATUS, RP0             ; RP0 = 1
        BCF STATUS, RP1             ; RP1 = 0 --> BANK 1
        ENDM

BANK2   MACRO
        BCF STATUS, RP0             ; RP0 = 0
        BSF STATUS, RP1             ; RP1 = 1 --> BANK 2
        ENDM

BANK3   MACRO
        BSF STATUS, RP0             ; RP0 = 1
        BSF STATUS, RP1             ; RP1 = 1 --> BANK 3
        ENDM

Configuring I/O Ports

Properly configuring input and output ports is essential for PIC microcontroller programming. This involves setting the direction of each pin using the TRIS registers.

TRIS Registers

Each bit in a TRIS register determines whether the corresponding pin is an input or output:

  • 1 (Set): Configures the pin as an input
  • 0 (Clear): Configures the pin as an output

Common Port Configuration Examples:

Setting PORTD as Output
INITP
    MOVLW   0x00                ; WREG = 0 (all zeros)
    MOVWF   TRISD               ; Make PORTD pins output (TRIS = 0 → output)
    RETURN
Setting PORTA as Input
INITP
    BANK1                       ; Switch to Bank 1 (TRISA is in Bank 1)
    MOVLW   0x06                ; Configure ADCON1 for digital I/O
    MOVWF   ADCON1
    MOVLW   0xFF                ; WREG = 0xFF (all ones)
    MOVWF   TRISA               ; Make PORTA all INPUT (TRIS=1 means input)
    BANK0                       ; Back to Bank 0
    RETURN

Important Note on PORTA

When using PORTA as digital I/O, you must also configure the ADCON1 register to disable the analog inputs on those pins. Otherwise, they will function as analog inputs by default.

PIC Assembly Program Structure

Understanding the standard structure of a PIC assembly program helps organize your code effectively.

1

Header and Configuration

Every program starts with comments describing the program and processor directives:

; ########## PROGRAM_NAME.ASM 
; WRITTEN BY: PramithaMJ
; DATE: 2024-04-29
; DEVICE: PIC16F877
; TITLE: "Program Purpose"

    LIST    P=16F877            ; Set PIC model
    INCLUDE        ; Include register names
2

Macros and Variable Definitions

Define any macros (like bank switching) and variables (using EQU directive):

; Bank switching macros here...

; Memory location definitions
STOREDATA EQU 0x20              ; Define memory location for data
3

Reset Vector

The program always starts at address 0x00 when the microcontroller resets:

    ORG     0x00                ; Reset vector
    NOP                         ; Optional No Operation
    GOTO    START               ; Jump to main program
4

Main Program

The main program typically starts at address 0x20:

    ORG     0x20                ; Main program starts

START
    CALL    INITP               ; Initialize ports
    
LOOP
    ; Main program logic here
    GOTO    LOOP                ; Continuous loop
5

Subroutines

Define subroutines for specific tasks:

; --- Initialization Subroutine ---
INITP
    ; Port initialization code
    RETURN

; --- Other subroutines ---
6

End of Program

Every program must end with the END directive:

    END                         ; End of program

Key PIC Assembly Instructions

Understanding common instructions used in PIC assembly programming:

Instruction Description Example
MOVLW Move Literal to W register MOVLW 0x55 - Load W with value 0x55
MOVWF Move W to File register MOVWF PORTD - Write W to PORTD
MOVF Move File register MOVF PORTA, W - Read PORTA into W
CLRF Clear File register CLRF PORTD - Clear all bits in PORTD
BSF Bit Set File register BSF STATUS, RP0 - Set bit RP0 in STATUS
BCF Bit Clear File register BCF STATUS, RP0 - Clear bit RP0 in STATUS
GOTO Go to address/label GOTO START - Jump to label START
CALL Call subroutine CALL INITP - Call subroutine INITP
RETURN Return from subroutine RETURN - Return to caller
NOP No Operation NOP - Do nothing for one cycle