Lab 4 (35,66)
Lab 4 (35,66)
Lab No: 04
Equipment:
PIC 16F877A microcontroller
Computer
Proteus Software
MPLAB X IDE Software
Introduction:
There are a total of 5 ports in the PIC 16F877A microcontroller which are named
PORT A, B, C, D and E. In this lab we will program the ports to run several
programs onto it. Firstly, we will program delay routines and implement the LED
flasher counter and secondly, we will implement it with 2 switches which will be
used to increment and decrement the counting. The detailed pinout diagram of the
microcontroller is shown below which shows the ports and the power pins.
Programming the ports of a microcontroller such as the PIC16F877A using
assembly language provides a deep understanding of embedded systems and low-
level hardware interactions. In this introduction, we'll look into the basics of
programming the ports of the PIC16F877A microcontroller, which involves
configuring the Input/Output (I/O) ports to interact with external devices.
Procedure:
Open the MPLAB X IDE software.
Create a new project and name it LED flasher circuit.
Create a new source file and select assembly file.
Write down the code for the LED flasher program.
Build the program.
Open the Proteus software and implement the LED flasher circuit.
Open the HEX file on the microcontroller in the proteus software.
Run the program.
Verify the results.
Code:
PORTB EQU 06H
TRISB EQU 86H
VALUE EQU 20H
count1 EQU 21H
count2 EQU 22H
STATUS EQU 03H
LIST P=16F877A
ORG 0
GOTO START
CONFIG 3F79H
DELAY_1S
movlw .255
movwf count1
movlw .255
movwf count2
a
decfsz count1,1
goto a
decfsz count2,1
goto a
RETLW 0
START
BANKSEL TRISB
MOVLW B'00000000'
MOVWF TRISB
BANKSEL PORTB
CLRF PORTB
CLRF VALUE
BEGIN
MOVF VALUE,W
SUBLW .15
BTFSC STATUS,2
CLRF VALUE
MOVF VALUE,W
MOVWF PORTB
CALL DELAY_1S
INCF VALUE
GOTO BEGIN
END
Proteus Circuit:
Count 1:
Count 2:
Count 3:
Continued till Count 15:
Task 2: Modify the LED Flasher circuit program that counts from 0-15
when increment switch is pressed and counts backwards when decrement
switch is pressed.
Procedure:
Open the MPLAB X IDE software.
In the same project modify the assembly code with inputs of 2 switches.
Build the program.
Open the HEX file on the microcontroller in the proteus software.
Run the program.
Verify the results.
Code:
PORTB EQU 06H
TRISB EQU 86H
count1 EQU 20H
count2 EQU 21H
VALUE EQU 22H
STATUS EQU 03H
LIST P=16F877A
ORG 0
GOTO START
CONFIG 3F79H
DELAY_1S
movlw.255
movwfcount1
movlw.255
movwfcount2
a
decfsz count1,1
goto a
decfsz count2,1
goto a
RETLW 0
START
BANKSEL TRISB
MOVLW B'11000000'
MOVWF TRISB
BANKSEL PORTB
CLRF PORTB
CLRF VALUE
BEGIN
BTFSC PORTB,7
INCF VALUE
BTFSC PORTB,6
DECF VALUE
MOVF VALUE,W
SUBLW .15
BTFSC STATUS,2
CLRF VALUE
MOVF VALUE,W
MOVWF PORTB
CALL DELAY_1S
GOTOBEGIN
END
Proteus Circuit:
Count 1 when increment switch is pressed:
Count 2 when increment switch is pressed: