08M and large lookup table?

sccoupe

New Member
I have an 08M working that takes a 0-5 volt dc signal and displays the adc value (0-255) on a 3 digit 7 segment display. I am trying to display an air to fuel ratio from an oxygen sensor that has a 0 to 5 volt output. I cannot just convert the voltage to an AFR because its not linear. I have a 15 point table for the oxygen sensor that shows what voltage equals what AFR. The AFR is displayed between 10.0 and 20.0 (or 100 to 200 in my case since the picaxe likes integers and the decimal point will always stay on). How to I best get from the 0-255 integer in the ADC register to a 100-200 value to display using the 15 point voltage to AFR table that I have that isnt linear? Will the 08M hold a 100 point lookup table? Is that the best easiest route?

Thanks all.
 

BCJKiwi

Senior Member
eeprom for an 08M covers from 0 to 255 MINUS the size of the program.

See page 42 of Manual 2 (eeprom)

So check the size of your program to get an INDICATION (not precise) to see if there is enough room.

It would be unlikely that there would be sufficient room for both code to do what you are describing, and, a 100 byte lookup table.

However there may be options - math instead of a lookup table, more optimised code, etc.
 
Last edited:

westaust55

Moderator
08M and large lookup table

How to I best get from the 0-255 integer in the ADC register
to a 100-200 value to display using the 15 point voltage to AFR table
that I have that isn't linear?
Will the 08M hold a 100 point lookup table? Is that the best easiest route?
If you have only 100 of data then 255-100 = 155 bytes left for the program.

You mention 15 point voltage. Please explain this.

Maybe if you provide the conversion table a maths formula can be devised as BCJKiwi suggests. In which case, what level of accuracy is required.
 

Dippy

Moderator
10 point A?
I thought he said 15 points?
i.e. a 15 point X/Y graph type thing? (Only my assumption).

Will you be doing simple linear interpolation between lookup table data points?
Saves on data but loses on code space.
Some use the Lookup, Lookdown method.

Space-wise it depends on the rest of your code too obv.
If things get complicated it maybe time to consider a bigger PICAXE.

But as mentioned, the Data in your table so far may prompt some helpful ideas.
 

sccoupe

New Member
Below is the voltage to AFR conversion numbers. My current code to display the 0-255 ADC value is 109 bytes, so perhaps there may be room for the large lookup table. Or maybe I could convert the 0-255 to a voltage via a calculation, then use a lookup and lookdown on the table below to calculate the AFR for voltage between points?


Voltage,0.16,0.62,0.94,1.4,1.72,2.18,2.5,2.96,3.27,3.74,4.05,4.37,4.68,4.99,5
AFR,9.99,10.98,11.55,12.46,13.11,14.00,14.64,15.55,16.17,17.09,17.72,18.36,18.93,19.56,20.02
 

Jeremy Leach

Senior Member
Ok, this actually is a straight line graph ;) See attached which I've plotted out for you. Well it is apart from your last reading, which might/hopefully be an error?

So straight line formula y=mx + c. In your terms AFR = mV + C

From your data a change of (4.99 - 0.16) in V gives a change of (19.56 - 9.99) in AFR. So the slope m = (19.56 - 9.99) / (4.99 - 0.16) = 9.57/4.83 = 1.981

Also when v = 4.99, AFR = 19.56, so 19.56 = m*4.99 + C.
Therefore C = 19.56 - 1.981*4.99 = 9.675

So your straight line equation is: AFR = (1.981 * V) + 9.675


You have an ADC value 0 to 255 for your voltage reading. But V = (ADCValue/255) * 5.
So AFR = (1.981 * (ADCValue / 255) * 5) + 9.675
AFR = 0.038843 * ADCValue + 9.675

The max AFR value is 20.02, but let's scale everything up by 3000 so the value you get is actually 20020 * 3 = 60060. I've just picked a nice number to scale it up as much as possible but also to avoid calculation overflow.
So 3000 * AFR = 117 * ADCValue + 29025
So 1000 * AFR = 117 * ADCValue + 29025 / 3

Which, unless I've made a cock-up, is the formula you can use directly in picaxe code to give the AFR value * 1000 (handy for display purposes).

Note that the AFR value must be a word variable. e.g Symbol AFR1000 = w0
 

Attachments

Last edited:

sccoupe

New Member
Thanks Jeremy, that seems to do the trick in excel as far as converting a voltage to an accurate AFR. Now comes getting the math done with 16bit numbers. Basically in using ADCIN = ADCIN * 196. This leaves me with my actual voltage times 100 (4.03 volts is displayed as 403, this is good).

What I invisioned was.

Code:
	readadc 4, adcin
	adcin = adcin * 196 / 100
	adcin = adcin * 1.981 + 967.5 / 10
Which gets me 176 (17.6AFR), but fractions arnt allowed, so I go with...

Code:
	readadc 4, adcin
	adcin = adcin * 196 / 100
	adcin = adcin * 1000 * 1981 / 100000 + 9675 /100
Which also gets me 176, but the numbers are beyond 16bit. What to do?
 

Jeremy Leach

Senior Member
Ok, I'm not sure what you mean, but if I'm grasping this thread, you've got a 3 digit display and want to display AFR on it. So changing my last formula a bit ...
AFR10 = 117 * ADCin + 29025/300

Where AFR10 is ten times the AFR, and since the max AFR is 20.02 then the highest value of AFR10 will be 200, and so fit into a byte variable. So ..

Code:
Symbol AFR10 = b0
Symbol ADCin = b1


readadc 4, ADCin
AFR10 = 117 * ADCin + 29025 / 300
and then goto a subroutine to display the 3 digit AFR value.
 

sccoupe

New Member
That done the trick. Now I just need to get my head around how that calculation works so it can be used again. :)
 

Jeremy Leach

Senior Member
Well, one thing to note is that picaxe maths works left to right, calculating as it goes (and also overflowing as it goes if you're not careful ! )
 
Top