Friends,
With the tips of my friends and esteemed seniors of our forum I took up the experiment to average multiple samples of ADC. But unfortunately my code does not respond. Please see the code and suggest how to set the code right. Please note that if single ADC is compared, the code is OK. When I take multiple sample and average it, the code fails.
Regards,
swapan
With the tips of my friends and esteemed seniors of our forum I took up the experiment to average multiple samples of ADC. But unfortunately my code does not respond. Please see the code and suggest how to set the code right. Please note that if single ADC is compared, the code is OK. When I take multiple sample and average it, the code fails.
Regards,
swapan
Code:
list p=16F676 ; list directive to define processor
#include <p16F676.inc> ; processor specific variable definitions
errorlevel -302 ; suppress message 302 from list file
__CONFIG _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT
; '__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.
;***** VARIABLE DEFINITIONS (examples)
; example of using Shared Uninitialized Data Section
CBLOCK 0x20
COUNT
HIGH_BIT
COUNT1
ENDC
;**********************************************************************
RESET_VECTOR CODE 0x0000 ; processor reset vector
goto start ; go to beginning of program
INT_VECTOR CODE 0x0004 ; interrupt vector location
MAIN_PROG CODE
; these first 4 instructions are not required if the internal oscillator is not used
start
call 0x3FF ; retrieve factory calibration value
bsf STATUS,RP0 ; set file register bank to 1
movwf OSCCAL ; update register with factory cal value
bcf STATUS,RP0 ; set file register bank to 0
; remaining code goes here
BSF STATUS, RP0
MOVLW b'00000001'
MOVWF TRISA
MOVLW b'00000001'
MOVWF ANSEL
MOVLW b'01010000' ; 16 T osc.
MOVWF ADCON1
BCF STATUS, RP0
MOVLW 0X07
MOVWF CMCON
BEGIN:
CALL ADC
MOVLW D'128'
SUBWF HIGH_BIT,0
BTFSC STATUS,C ; compare with a pre-loaded value.
GOTO RLY_ON ; If average ADC is higher than the value, held
GOTO RLY_OFF ; PORTA,1 high, otherwise clear it.
RLY_ON:
MOVLW b'00000010'
MOVWF PORTA
GOTO BEGIN
RLY_OFF:
CLRF PORTA
GOTO BEGIN
ADC:
CLRF HIGH_BIT
MOVLW b'00000001'
MOVWF ADCON0
CALL DLY_20
SAMPL: BSF ADCON0,GO
BTFSC ADCON0,GO
GOTO $-1
INCF COUNT
MOVF ADRESH,W
ADDWF HIGH_BIT,1
MOVLW D'32'
SUBWF COUNT,W
BTFSS STATUS,Z
GOTO SAMPL
CLRF COUNT
BCF STATUS,C
RRF HIGH_BIT ; devide by 2
BCF STATUS,C
RRF HIGH_BIT ;devide by 4
BCF STATUS,C
RRF HIGH_BIT ; devide by 8
BCF STATUS,C
RRF HIGH_BIT ; devide by 16
BCF STATUS,C
RRF HIGH_BIT ; devide by 32
RETURN
DLY_20
MOVLW D'20'
MOVWF COUNT1
DECFSZ COUNT1,1
GOTO $-1
RETURN
; initialize eeprom locations
END ; directive 'end of program'