;**********************************************************************
; This file is a basic code template for assembly code generation *
; on the PIC16F690. 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 (Document DS33014). *
; *
; Refer to the respective PIC data sheet for additional *
; information on the instruction set. *
; *
;**********************************************************************
; *
; Filename: ADC - Battery Voltage Monitor.asm *
; Date: *
; File Version: *
; *
; Author: Phindulo Owen Mulaudzi (603854) *
; Company: *
; *
; *
;**********************************************************************
; *
; Files Required: P16F690.INC *
; *
;**********************************************************************
; *
; Notes: *
; *
;**********************************************************************
list p=16f690 ; list directive to define processor
#include <P16F690.inc> ; processor specific variable definitions
errorlevel -203
errorlevel -205
errorlevel -302
__CONFIG _CP_OFF & _CPD_OFF & _BOR_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _MCLRE_ON & _FCMEN_OFF & _IESO_OFF
; '__CONFIG' directive is used to embed configuration data within .asm file.
; The labels following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.
;***** VARIABLE DEFINITIONS
w_temp EQU 0x7D ; variable used for context saving
status_temp EQU 0x7E ; variable used for context saving
pclath_temp EQU 0x7F ; variable used for context saving
;**********************************************************************
cblock 0x20
d1
d2
RESULTHI
RESULTLO
CONVERSION
endc
;**********************************************************************
ORG 0x000 ; processor reset vector
goto main ; go to beginning of program
ORG 0x004 ; interrupt vector location
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 ; move pclath register into W register
movwf pclath_temp ; save off contents of PCLATH register
; isr code can go here or be located as a call subroutine elsewhere
movf pclath_temp,w ; retrieve copy of PCLATH register
movwf PCLATH ; restore pre-isr PCLATH register contents
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
main
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;STARTING ADC CONVERSION PROCEDURE
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;1. Configure Port:
;• Disable pin output driver (See TRIS register)
;• Configure pin as analog
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
banksel TRISA;
bsf TRISC,0 ;disable output driver by making pin an input
banksel ANSEL;
bcf ANSEL,0 ;set RCO/AN4 as analogue input
banksel ANSELH;
clrf ANSELH
banksel ADRESH
clrf ADRESH ;init
banksel ADRESL
clrf ADRESL ;init
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;2. Configure the ADC module:
;• Select ADC conversion clock
;• Configure voltage reference
;• Select ADC input channel
;• Select result format
;• Turn on ADC module
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
banksel ADCON1;
bcf ADCON1,ADCS0;
bsf ADCON1,ADCS1;
bcf ADCON1,ADCS2 ;select Fosc/32 : 110
banksel ADCON0;
bcf ADCON0,VCFG ;use VDD pin as reference voltage
banksel ADCON0;
bcf ADCON0,CHS0;
bcf ADCON0,CHS1;
bcf ADCON0,CHS2;
bcf ADCON0,CHS3; ;select pin AN0 : 0000
banksel ADCON0;
bcf ADCON0,ADFM; Left justify A/D conversion
banksel ADCON0
bsf ADCON0,ADON; Enable ADC
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;3. Configure ADC interrupt (optional):
;• Clear ADC interrupt flag
;• Enable ADC interrupt
;• Enable peripheral interrupt
;• Enable global interrupt
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
banksel PIR1;
bcf PIR1,ADIF ;clear ADC interrupt flag
banksel PIE1;
bsf PIE1,ADIE ; enable ADC interrupt
banksel INTCON
bsf INTCON,PEIE; enable peripheral interrupt
banksel INTCON;
bcf INTCON,GIE; enable global interrupt
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;4. Wait the required acquisition time.
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
banksel OSCCON;
bcf OSCCON,IRCF0;
bsf OSCCON,IRCF1;
bsf OSCCON,IRCF2; ;set internal oscillator to 4MHz, uhmmm 1 instruction = 250us??
; Delay = 0.001 seconds = 1,000uS
movlw 0xC7;
movwf d1;
movlw 0x01;
movwf d2;
Delay_1us:
decfsz d1, f;
goto $+2;
decfsz d2, f;
goto Delay_1us;
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;5. Start conversion by setting the GO/DONEbit.
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
banksel ADCON0;
bsf ADCON0,1
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;6. Wait for ADC conversion to complete by one of the following:
;• Polling the GO/DONEbit
;• Waiting for the ADC interrupt (interrupts enabled)
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
banksel PIR1;
btfss PIR1, ADIF;
goto $-1; //wait for ADC to complete
banksel PIR1;
bcf PIR1, ADIF;
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;7. Read ADC Result
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
banksel ADRESL ;
movf ADRESL,W ;Read lower 8 bits
movwf RESULTLO ;Store in GPR space
banksel ADRESH ;
movf ADRESH,W ;Read upper 2 bits
movwf RESULTHI ;store in GPR space
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;8. Clear the ADC interrupt flag (required if interrupt is enabled).
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
banksel PIR1;
bcf PIR1, ADIF; clear ADC flag
banksel ADCON0
bcf ADCON0,ADON; Disable ADC
goto $
ORG 0x2100 ; data EEPROM location
DE 1,2,3,4 ; define first four EEPROM locations as 1, 2, 3, and 4
END ; directive 'end of program'
[\code]