Write a code on the PIC microcontroller PIC16F877A Imagine you are writing code
ID: 3599572 • Letter: W
Question
Write a code on the PIC microcontroller PIC16F877A
Imagine you are writing code to display a bar graph for the volume control of a stereo amplifier. Modify the code from assignment 14 to read voltage from POT0 instead of the count variable, and display the bar graph on the LED's.
;*********************************************************************
; This file is a basic code template for object module code *
; generation on the PIC16F877A. This file contains the *
; basic code building blocks to build upon. *
; *
; Refer to the MPASM User's Guide for additional information on *
; features of the assembler and linker (Document DS33014). *
; *
; Refer to the respective PIC data sheet for additional *
; information on the instruction set. *
; *
;**********************************************************************
; *
; Filename: MAIN.ASM *
; Date: *
; File Version: *
; *
; Author: *
; Company: *
; *
; *
;**********************************************************************
; *
; Files required: p16f877a.INC *
; 16f877a_g.lkr *
; *
; *
;**********************************************************************
; *
; Notes: *
; *
; *
; *
; *
;**********************************************************************
LIST r=dec ; list directive to set the default radix
list p=16F877A ; list directive to define processor
#include "p16f877a.inc" ; processor specific variable definitions
;
; Defines and macros to help with
; bank selection ; Page 17
;
#define BANK0 (h'000')
#define BANK1 (h'080')
#define BANK2 (h'100')
#define BANK3 (h'180')
; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See data sheet for additional information on configuration word settings.
__CONFIG _CP_OFF & _CPD_OFF & _BODEN_OFF & _WDT_OFF & _PWRTE_ON & _HS_OSC & _LVP_OFF
;***** VARIABLE DEFINITIONS
MAIN_DATA udata
count res 1
delay_low res 1
delay_high res 1
; example of using Shared Uninitialized Data Section
INT_DATA UDATA_SHR
w_temp RES 1 ; variable used for context saving
status_temp RES 1 ; variable used for context saving
pclath_temp RES 1 ; variable used for context saving
;**********************************************************************
CODE
org 0x000 ; processor reset vector
RESET_VECTOR:
nop ; ICD2 needs this
goto start ; go to beginning of program
INT_VECTOR:
org 0x004 ; interrupt vector location
INTERRUPT:
movwf w_temp ; save off current W register contents
movf STATUS,w ; move status register into W register
movwf status_temp ; save off contents of STATUS register
movf PCLATH,W
movwf pclath_temp
; isr code can go here or be located as a call subroutine elsewhere
movf pclath_temp,W
movwf PCLATH
movf status_temp,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie ; return from interrupt
start:
;
; Turn off all analog inputs
; and make all pins available
; for digital I/O.
;
banksel BANK1
movlw b'00000110' ; Set all ADC inputs for digital I/O
movwf (ADCON1 ^ BANK1) ;
movlw b'00000000' ; Turn off Comparator voltage reference
movwf (CVRCON ^ BANK1)
movlw 0x07 ; turn off Comparators
movwf (CMCON ^ BANK1)
banksel BANK0
clrf (ADCON0 ^ BANK0) ; Turn off ADC
;
; Set ports for digital output
;
movlw b'00000000'
movwf PORTA
movlw b'00000000'
movwf PORTB
movlw b'00000000'
movwf PORTC
movlw b'00000000'
movwf PORTD
movlw b'00000000'
movwf PORTE
banksel BANK1
movlw b'00000000'
movwf (TRISA ^ BANK1)
movlw b'00000000'
movwf (TRISB ^ BANK1)
movlw b'00000000'
movwf (TRISC ^ BANK1)
movlw b'00000000'
movwf (TRISD ^ BANK1)
movlw b'00000000'
movwf (TRISE ^ BANK1)
banksel BANK0
clrf count
clrf delay_low
clrf delay_high
; remaining code goes here
;
; Make the LEDs on the LAB-X1 count
;
main_loop:
incf count,F
movfw count
movwf PORTD
;
; Pause between counts so humans
; can see the lights changing.
;
delay_loop:
incfsz delay_low
goto delay_loop
incfsz delay_high
goto delay_loop
goto main_loop
END
Explanation / Answer
banksel BANK1
movlw b'00000110' ; Set all ADC inputs for digital I/O
movwf (ADCON1 ^ BANK1) ;
movlw b'00000000' ; Turn off Comparator voltage reference
movwf (CVRCON ^ BANK1)
movlw 0x07 ; turn off Comparators
movwf (CMCON ^ BANK1)
banksel BANK0
clrf (ADCON0 ^ BANK0) ; Turn off ADC
;
; Set ports for digital output
;
movlw b'00000000'
movwf PORTA
movlw b'00000000'
movwf PORTB
movlw b'00000000'
movwf PORTC
movlw b'00000000'
movwf PORTD
movlw b'00000000'
movwf PORTE
banksel BANK1
movlw b'00000000'
movwf (TRISA ^ BANK1)
movlw b'00000000'
movwf (TRISB ^ BANK1)
movlw b'00000000'
movwf (TRISC ^ BANK1)
movlw b'00000000'
movwf (TRISD ^ BANK1)
movlw b'00000000'
movwf (TRISE ^ BANK1)
banksel BANK0
clrf count
clrf delay_low
clrf delay_high
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.