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:
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:
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: