This code should work with long and word compiler option.
Code:
GOTO OverDTAnalog
ADchan VAR BYTE ; global - A/D channel to use
ADbits VAR BYTE ; global - # of bits in result
ADvalue VAR WORD ; global - the final A/D value
ADmax VAR WORD ; global - Max A/D value at this resolution
;--------------------
DTadCount VAR WORD ; local - sample count
DTadDivisor VAR WORD ; local - averaging divisor
DTadShiftR VAR BYTE ; local - bits to shift for UnderSampling
#IF __LONG__
DTadAccum VAR LONG ; local - 32-bit sample accumulator
#ELSE
DTadAccum VAR WORD[2] ; local - 32-bit sample accumulator
#ENDIF
;---------------------------------------------------------------------------
GetADC:
IF (ADbits >= 10) THEN
DTadShiftR = 0 ; 10 11 12 13 14 15 16
LOOKUP2 ADbits-10,[ 1, 4,16,64,256,1024,4096],DTadCount
LOOKUP ADbits-10,[ 1, 2, 4, 8, 16, 32, 64],DTadDivisor
ELSE
DTadCount = 1
DTadDivisor = 1
DTadShiftR = 10 - ADbits
ENDIF
LOOKUP2 ADbits,[0,1,3,7,15,31,63,127,255,511,1023, _
2046,4092,8184,16368,32736,65472],ADmax
DTadAccum = 0 : DTadAccum[1] = 0 ; clear the accumulator
DTadLoop:
ADCIN ADchan, ADvalue ; get 10-bit sample
DTadAccum = DTadAccum + ADvalue ; add it to the accumulator
#IF not __LONG__
IF DTadAccum < ADvalue THEN ; if low word overflowed
DTadAccum[1] = DTadAccum[1] + 1 ; increment the high word
ENDIF
#ENDIF
DTadCount = DTadCount - 1 ; done with this sample
IF DTadCount > 0 THEN DTadLoop ; loop if not done with ALL samples
#IF __LONG__
ADvalue = DTadAccum / DTadDivisor
#ELSE
R2 = DTadAccum ; put 32-bit accumulated value in PBP
R0 = DTadAccum[1] ; registers, prepare for DIV32
ADvalue = DIV32 DTadDivisor ; get the average value
#ENDIF
ADvalue = ADvalue >> DTadShiftR ; Shift right if < 10-bit
RETURN
OverDTAnalog:
Bookmarks