cblock h'20' ;start of general purpose registers
Buffer:8
Average:2
endc
movlw Buffer ;get the address of Buffer
movwf FSR ;and point FSR at Buffer
Main call GetADC ;read the ADC
bsf STATUS,RP0 ;bank 1
movwf ADRESL ;get the low byte
bcf STATUS,RP0 ;back to bank 0
movwf INDF ;store in buffer
incf FSR,F ;and point to next location
movwf ADRESH ;get high byte
movwf INDF ;and store it
incf FSR,F ;point to next location
movfw FSR ;see if FSR has reached
xorlw Buffer+8 ;the end of the buffer
movlw Buffer ;prepare to reset FSR
btfsc STATUS,Z ;if it's at the end
movwf FSR ;reset it
movfw Buffer ;move first value to average
movwf Average
movfw Buffer+1 ;and high byte
movwf Average+1
movfw Buffer+2 ;add second value
addwf Average,f ;first low byte
btfsc STATUS,C ;if there was an overflow
incf Average+1,f ;increment high byte
movfw Buffer+3 ;and add in high byte
addwf Average+1,f
movfw Buffer+4 ;add third value
addwf Average,f
btfsc STATUS,C
incf Average+1,f
movfw Buffer+5
addwf Average+1,f
movfw Buffer+6 ;add fourth value
addwf Average,f
btfsc STATUS,C
incf Average+1,f
movfw Buffer+7
addwf Average+1,f
bcf STATUS,C ;divide by 2
rrf Average+1,f ;by shifting high byte
rrf Average,f ;and then low byte
bcf STATUS,C ;divide by 4
rrf Average+1,f
rrf Average,f
goto Main ;loop for ever