Code:
	
' Uses 18F252 and 20MHz external clock.
' The lookup table contains 65 values, from 0 to 6V input.
' communicates with PC through serial port. 
' Has an added 32 sample average on the input.
Define OSC 20   ' use 20 Mhz clock
Define  ADC_BITS     10
Define  ADC_CLOCK    3
Define  ADC_SAMPLEUS 24
' initial EEPROM values (output = input) 
EEPROM [216,0,217,0,219,0,223,0,229,0,237,0,246,0,0,1,12,1,_
          22,1,30,1,42,1,55,1,69,1,86,1,104,1,_
          124,1,143,1,163,1,184,1,202,1,219,1,237,1,254,1,15,2,_
          32,2,49,2,66,2,82,2,98,2,114,2,130,2]         
EEPROM 128,[146,2,161,2,177,2,192,2,207,2,222,2,237,2,252,2,11,3,_
          26,3,40,3,55,3,70,3,84,3,99,3,113,3,_
          128,3,142,3,156,3,170,3,183,3,197,3,210,3,223,3,236,3,_
          249,3,255,3,255,3,255,3,255,3,255,3,255,3,0,4]
          
adval VAR WORD      ' analog input value
outputval VAR WORD  ' output value
tempval VAR WORD    ' temporary
tablehigh VAR WORD  ' high table value
tablelow VAR WORD   ' low table value
tempval2 VAR WORD   ' temporary #2
avgarray VAR WORD[32]' array for averaging input samples
countnum VAR byte   ' integer for counting sample array
arraysum VAR WORD   ' summing variable for average
TRISA = 255         ' portA => input
ADCON1 = %10000010  ' Set PORTA to analog and PORTE to digital
TRISB = %00000000 ' Port B set for output
HIGH PORTB.2      ' Serial D-A chip select off
countnum = 0
clearloop:          ' zero out array
if countnum != 32 then
    avgarray[countnum]=0
    countnum = countnum + 1
    goto clearloop
endif
countnum = 0
arraysum = 0
loop:  ' start of main loop
    
    Adcin 0, adval
    arraysum = arraysum - avgarray[countnum]  'subtract value at countnum
    avgarray[countnum] = adval                'store new value in array 
    arraysum = arraysum + adval               'add to sum value
    adval = arraysum >> 5                     'divide by 32
    countnum = countnum + 1
    if countnum = 32 then
    countnum = 0
    endif
    
    tempval = adval >> 3    ' shift input for table lookup
    tempval2 = tempval & %1111111111111110 ' clear last bit for WORD lookup
    
    'get table high and low values
    READ tempval2+2,tablehigh.BYTE0
    READ tempval2+3,tablehigh.BYTE1
    READ tempval2,tablelow.BYTE0
    READ tempval2+1,tablelow.BYTE1
    
    'Table Interpolation
    tempval = adval & %0000000000001111 ' get interpolation value
    IF tablehigh >= tablelow THEN       ' get space between table values
    tempval2 = tablehigh - tablelow     '
    ELSE                                '
    tempval2 = tablelow - tablehigh     '
    ENDIF                               '
    tempval = tempval2 * tempval        'multiply high/low diff by interp value
    outputval = tempval >> 4            'divide by table spacing
    
    IF tablehigh >= tablelow THEN       ' offset table value by calculated
    outputval = outputval + tablelow    ' interpolation value
    ELSE                                '
    outputval = tablelow - outputval    '
    ENDIF                               '
    
    tempval = outputval << 2   ' shift output left 2 bits(LTC1661 DtoA)
    tempval = tempval + %1001000000000000   ' add control code(load DAC A)
    LOW PortB.2     ' Enable DtoA chip
    Shiftout PORTB.0,PORTB.1,1,[tempval\16] ' Serial output to DtoA conv
    High PORTB.2    ' Deselect DtoA chip
    
    goto loop       ' repeat main loop "forever"
End
 
				
Bookmarks