PDA

View Full Version : Help with two lookup tables?



awdgsx
- 9th February 2012, 16:04
I'm at a loss. I have figured out how to make a lookup table with eeprom, but i need to impliment a table with the program memory and can't quite figure out how to do this. Any help would be greatly appreciated.

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]
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 = 000010 'Set PORTA to analog and PORTE to digital
TRISB = 000000 '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, 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
'program memory 65 point lookup table goes here

' take adval ,use interpolation, convert adval into number that could be from
' 0-8000 and call it flow.
' do some math to the flow variable..
' call it flowfinal

'below my code should be correct to take flowfinal and convert it
'back to a voltage thru the DAC with a lookup table in the eeprom.



tempval = flowfinal > > 3 'shift input for table lookup
tempval2 = tempval & 11111111111110 '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 & 00000000001111 '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 + 01000000000000 '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

MikeBZH
- 9th February 2012, 16:14
Hi awdgsx

Just have a look to :

http://www.picbasic.co.uk/forum/showthread.php?t=11212

w (http://www.picbasic.co.uk/forum/showthread.php?t=11212)here Darel explains in details how to use the EXT instruction to do what you want.

Best regards

MikeBZH

awdgsx
- 9th February 2012, 22:27
So it's used like this?

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,11 400,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?

MikeBZH
- 10th February 2012, 09:13
Awdgsx,

If you want to write data (at programming) in the Flash program memory space you should open an ASM section in your code and use a DB or DW list of values, separated by commas. Please refer to lhe link I passed to you and follow Darrel Taylor's recommandations. This was my reference when I had to do the same thing. Everything is there.

Mike BZH