Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Oshonsoft Lookup Table

Status
Not open for further replies.

Mity Eltu

Member
Allright, I'm not new to Oshonsoft or programming in C/C++, but I have never (that I can recall) used a lookup table. I've read up on them some, but I can't find how to implement one in Oshonsoft Basic Compiler.

Here's what I'm trying to accomplish. I need a datalogger for a fuse testing rig based on UL 248 (that's not really important). I have managed to get the datalogging part working such that I can write all my data to a file on an SD card and I am now working on the thermocouple design. The temp range for the testing will be from near room temp (I'm going to start at 18°C) up to about 200°C (I'm going to go up to 250 for margin's sake). I'm planning on type T thermocouples. I figure on just scaling all the values up by 350 and feeding them directly into a 18F4580. That should put all the values just under 5V. I'll add circuit protection for the pins later - so no worries there.

The rub is the conversion from a/d to °C. The math is atrocious and I'm not sure the chip can handle the 7th degree eqn with single precision numbers (any thoughts on that?). So I thought about using a lookup table and including most of the singe degree mV values from the TC chart in the range I'm looking at. The problem is the table. How do I do this? The only example I can find that looks like a lookup table uses some kind of function call (ie mask = lookup(0x23, 0x....). Will this work with 250 entries? Would this work better loading into an external EPROM? Is there a better way?

Any advice you guys have will be great.

Thanks
 
Hi,
Oshonsoft uses this method.
Extract from manual.
Eric

LookUp,

LOOKUP function can be used to select one from the list of Byte constants, based on the value in the index Byte variable, that is supplied as the last separated argument of the function.
The first constant in the list has index value 0.
The selected constant will be loaded into the result Byte data type variable.
If the value in the index variable goes beyond the number of constants in the list, the result variable will not be affected by the function.

Here is one small example for a 7-segment LED display:

Dim digit As Byte
Dim mask As Byte
TRISB = %00000000
loop:
For digit = 0 To 9
mask = LookUp(0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f), digit
PORTB = mask
WaitMs 1000
Next digit
Goto loop

If all constants in the list (or part of them) are ASCII values, then shorter form of the list can be created by using string arguments. For example:
mask = LookUp("ABCDEFGHIJK"), index

EDIT:
You could consider using an assembler block, ie: ASM: ..............
 
Well, I tried that and it will not work for what I need. I need the lookup table to be either string or single type. The values will be the deg C equivalent of the a/d converted value from the thermocouple. If I sore the vlues in eeprom I can only get 64 single precision numbers in, right? 4 bytes per single? I can use off chip eeprom I guess. Is there a better way to do this?
 
I've looked at this again and I'm confusing myself. I need some guidance to understand this.

If I use a thermocouple to measure temperature and pass the voltage signal through an amplifier with a gain of 400, I'll get a lovely scaled version of the voltage that should (based on the temperature range I'll be working in) be very close to 0-5v for my A/D converter. This is great, but now the problem comes in how do I convert the 10-bit a/d result into a temperature?

I've looked at the thermocouple polynomials, but so far I don't see how they can help me convert the a/d result into anything useful. How in the world do you do this? I just don't get this rioght now and I would really like some help.
 
It's called mapping.... You map the temperature to the ADC result...

You have a low temp and a high temp.... Assume a low temp yields 1 volt and a high temp yields 4 volts

with a 10 bit ADC that will represent 204 bits for low and 819 bits for the high that's a span of 615 bits..

Read the ADC.... remove the 204 ( low value ) and multiply by your temperature span ( high temp - low temp).. then divide by the ADC span (615) and add your low temp.... Converted!!!
 
Thank you for that, that did help me understand it.

This assumes linearity, and the thermocouple is anything but linear. I'm planning on scaling the thermocouple voltage by about 416 using op amps so that the full scale of 0-250 deg c gets me about 0-5V on the A/D. Using this method I'll be pretty far off (i'm guessing) in the mid range on the scale. I'm thinking need to take the A/D result back to the mV value so that I can use the thermocouple coefficients to get the actual temp.

If I follow your math right, I think I can go backwards by taking 4.8828e-3 (volts per bit), multiply by the A/D result and then divide by 416. This should get me the thermocouple voltage, right? (I think). Then I can use that voltage in the thermocouple polynomial to get the actual temp. Man that's alot of math with very small numbers.

What do you think? Is that doable? If not, I'll take the linear idea you gave me and just run with that. Accuracy is not paramount, but it is desirable.

Thanks again. I really do appreciate that.
 
hi M,
Why do want to have the 'Table' contents as a String,which as you say will require 4 Bytes of EPROM memory.?

If I store the values in eeprom I can only get 64 single precision numbers in, right? 4 bytes per single?

Why not store as a Word in a Lookup Table, address the Table with the ADC count as a Pointer, then convert from Bin to ASCII for your display.?
This will give you 128 Table values in Table #1
Use a second Table #2 for the remaining 128 Values.

Selecting Table #1 or #2 is a simple procedure when using the ADC count value as Pointer to the Look Up Tables.

E
 
What's the thermocouples part number???

There is a post here somewhere... I think Eric was involved, where a temperature sensor was linearized BEFORE amplification so the output temperature was linear.... It makes the math so much easier...
 
Why do want to have the 'Table' contents as a String

Why not store as a Word in a Lookup Table, address the Table with the ADC count as a Pointer
E

Well, there are functions to convert string to single precision numbers.
I also have no idea how to store single precision floating point numbers as words. I also don't think Oshonsoft Basic can do pointers. Am I wrong on that? I usually tried to avoid pointers in C anyway as they typically muddied the water for me as far as understanding what the code is really doing.

Is it a bead idea to back calculating the voltage and using the tc polynomial? I know it will slow things down considerably, but since I'm only going to be taking a total of 9 samples every 5 minutes (UL 248 fuse testing) I figure I have plenty of time.

I'll still defer to better judgement. If you guys say it's better to do it with tables, I'll try it, but can you explain to me how I store these decimal numbers in a form that Oshonsoft Basic will understand? I think the only format that the lookup table understands is Byte.
 
I also have no idea how to store single precision floating point numbers as words.
They are 32 bits so you would need longs ( double word )... Reading through these posts... If you can program in C.... Why don't you? Array's of floats are possible...

Each time I use Oshonsoft I tend to use fixed point arithmetic for this exact reason... As its only the visual element that requires the floating point.... Convert to floats after all calculations...
 
hi Mity,
Unless I am missing something, why are you planning to store the 'T' Type T/C mV values in an Array.???
I read from the T/C chart, that the mV range from 0.000mV to +12.013mV, for 0Cdeg thru +250Cdeg.

Have you considered initially using a PC to pre-calculate the 250 temperature values that you require and loading these 250 actual Cdeg values into a Table.
Choose the preamp factor for scaling up the mV signals prior to the ADC conversion to an ADC count which will be used to address the correct Table Cdeg value.

E
 

Attachments

  • z207.pdf
    137.4 KB · Views: 281
If you can program in C.... Why don't you?

I can, but that brings up a whole other slue of problems. The only C compiler I have ready access to is from microchip and frankly, I'd rather try to program this in assembly than go back to xc8. I have yet to figure out how to get that thing to allow me to use an lcd on anything other than portb. And don't get me started on the de-optimizer included in the free version of the compiler. Terrible thing to do to people who don't have the thousands neccessary to buy the license - reminds me a bit of mircoshaft's bloatware aka windows 7.

I have not looked and don't have the option right now, does oshonsoft have a way of storing data directly to EEPROM? Like the Data command in other languages?

why are you planning to store the 'T' Type T/C mV values in an Array.???

Not the mV values but the temps associated with the ad result. When I scale up the mV input from the 0-12.013mV to 0-4.997V (scaling by 416) then I get fractional degrees from the various a/d results and none of them fall directly on any of the results. That is none tc inputs will be a multiple of the 4.88mV bit value from a 10 bit a/d converter with an input of 0-5V. That's a bit wordy. I hope that made sense.

What I understood you to mean was to have a table of temperatures (xxx.x format) that matches the a/d result of 0-1024. Is that not right? So that an a/d result of 512 read a value of (for argument's sake) 120.5 deg C from the table. The problem is the table will be composed of numbers of Single data type and not byte. I guess I can just interpolate based on the between the 64 evenly spaced values I can store in the table. Is that more like what you meant?
 
I have not looked and don't have the option right now, does oshonsoft have a way of storing data directly to EEPROM? Like the Data command in other languages?
Yes!! Read location ,byte and Write location, byte ...

If you need to store anything other than a byte you have to organize it yourself....
 
hi,
This is the OSoft example for Array for Single

Code:
Dim i As Bit
Dim j As Byte
Dim k As Word
Dim x As Long
'Dim y As Single

''If necessary, variable address can be specified during declaration:
''Dim x As Byte @ 0x050

''It is possible To use one-dimensional arrays For Byte, Word, Long And Single variables. For example:
''Dim x(10) As Byte
''declares an array of 10 Byte variables with array index in the range [0-9].

Dim y(250) As Single

For j = 0 To 9
y(j) = 1.2345 + j
Next j


End
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top