Code:
Define osc 20 'use 20 Mhz clock
Define adc_bits 10
Define ADC_CLOCK 3
Define ADC_SAMPLEUS 24
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, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5_
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5_
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 ,5 ,5 ,5 ,5 ,5 ,5 ,5 ,5 ,5]
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]
WRITECODE 0,0,19,24,38,48,58,66,77,80,96,110,115,200,234,250,254,300,_
400,420,500,600,800,900,950,1000,1100,1200,1300,11400,1500,1600,_
1700,1800,1900,2000,2100,2200,2300,2400,2500,2600,2700,2800,2900,_
3000,3100,3200,3300,3400,3500,3600,3700,3800,3900,4000,4100,4200,_
4300,4400,4500,4600,4700,4800,4900,5000,5100,5200,5300,5400,5500,_
5600,5700,5800,5900,6000,6100,6200,6300,6400,6500,6600,_
6700,6800,6900,7000,7100,7200,7300,7400,7500,7600,7700,7800,7900,_
8000,8100,8200,8300,8300,8300,8300,8300,8300,8300,8300,8300,8300,_
8300,8300,8300,8300,8300,8300,8300,8300,8300,8300,8300,8300,8300,_
8300,8300,8300,8300,8300,8300,8300,8300,8300]
mafin var Word 'analog input value
flow var Word 'output value
flowfinal var word
mafout var word
tempval var Word 'temporary
tempval4 var word 'temporary 4
tablehigh var Word 'high table value
tablelow var Word 'low table value
tablehigh2 var word
tablelow2 var word
tempval2 var Word 'temporary #2
tempval3 var word 'temporary #3
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
DOloop: 'start of main loop
Adcin 0, mafin
arraysum = arraysum - avgarray[countnum] 'subtract value at countnum
avgarray[countnum] = mafin 'store new value in array
arraysum = arraysum + mafin 'add to sum value
mafin = arraysum > > 5 'divide by 32
countnum = countnum + 1
If countnum = 32 Then
countnum = 0
Endif
tempval4 = mafin > > 3 'shift input for table lookup
tempval3 = tempval4 & %1111111111111110 'clear last bit for WORD lookup
READCODE tempval3 + 1, tablehigh
READCODE tempval3 + 2, tablelow
tempval4 = mafin & %0000000000001111 'get interpolation value
If tablehigh >= tablelow Then 'get space between table values
tempval3 = tablehigh - tablelow
Else
tempval3 = tablelow - tablehigh
Endif
tempval4 = tempval2 * tempval4 'multiply high/low diff by interp value
outputval = tempval4 > > 4 'divide by table spacing
If tablehigh >= tablelow Then 'offset table value by calculated
flow = flow + tablelow 'interpolation value
Else
flow = tablelow - flow
flowfinal=flow/2
tempval = flowfinal > > 3 'shift input for table lookup
tempval2 = tempval & %1111111111111110 'clear last bit for WORD lookup
Read tempval2 + 2, tablehigh.byte0
Read tempval2 + 3, tablehigh.byte1
Read tempval2, tablelow.byte0
Read tempval2 + 1, tablelow.byte1
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"
Now i'm confused how to set up the output lookup table to convert the flow number from the first table back into a 10 bit numfor the dac?
Bookmarks