PDA

View Full Version : Need help with lookup table or direct calculation



nverma
- 4th October 2006, 19:48
Hi,

I am trying to allocate my A-D value which goes from 0-1023 to precalculated values. I tried this approach first because the calculation is pretty complex. So once I had my precalculated values, I tried storing them into the EEPROM like this:

DATA @1023,-782
DATA @1022,-696
DATA @1021,-633
DATA @1020,-594
.
.
.
DATA @3,3797
DATA @2,4126
DATA @1,4725
DATA @0,4730

Read these values from EEPROM:

FOR index0 = 0 to 1023
read index0,tem0[index0]
next index0

Then allocated precalulated stored values to A-D value:

ADCIN 2,pot_value
temperature = tem0[pot_value]


However, I think the EEPROM can only store bytes rather than words, thus I cannot store the value I wanted. I tried using lookup tables as well, but the maximum you can store in a lookup table are 256 values, I need 1023.
So, I thought about actually doing the math in the PIC itself. The equation I need to calculate is:

temperature = (80.8321*( (-1*LN( (2490*(Pot_value*(4096/1024)))/(40960-10000*(Pot_value*(4096/1024))))) + ( SQRT( (LN( (2490*pot_value)/(40960-10000*pot_value)) + 7.4063)^2 + 68.2415) -7.4063))) - 273.15

I have got an algorithm for the Ln but I will need to do all this in floating point math. Can someone please suggest what I can do?

Darrel Taylor
- 4th October 2006, 19:57
Hi nverma,

If you're using an 18F this might work for you ...

The EXT (external) modifier.
http://www.picbasic.co.uk/forum/showthread.php?t=3891

In particular, the section on Using EXT with Labels.

HTH,

nverma
- 4th October 2006, 21:27
Thanks Darrel,

I'm not exactly sure what the DW command does. And if you look at the example of the EXT with label, could I write code as:

POT_VALUE CON EXT

POT_VALUE

DW (basically enter my precalculated values in order from the one correposidng to 0 all the way to 1023 here)

And then reading:

READCODE(POT_VALUE + (OFFSET<<1)),Temperature

Darrel Taylor
- 4th October 2006, 21:37
DW is "Declare Data of One Word" in assembly language.

And, sounds like you've got the right idea for the rest of it.
<br>

ErnieM
- 4th October 2006, 22:20
That's a rather involved calculation to be sure. Honestly, I gave up trying to follow it.

You have just 1024 input values, I assume these lead to 1024 discrete output values else you wouldn't be looking for a look-up table solution.

However, can you solve this by some other simpler method? Piecewize linear, interpolation polynomial, or such? Do you have say an excel spreadsheet with the results calculated?

Also, you don't need EEPROM to save values, you can use RETLW (Return with literal in W) to get a byte look up table, that will take 2048 memory locations to get 1024 words (and yeah, its assembler, but not much).

BigWumpus
- 4th October 2006, 22:37
What type of thermometer do you use ?

I always make a table of maybe 30 points and make an interpolation between this points. It is good for me, but I don't know, what you need...

nverma
- 4th October 2006, 22:46
Ernie,

Yes I do have an excel spreadsheet with the values calculated. And I could possibly linearize the curve but loose accuracy. But about this RETLW command, how does it work and where can I find the syntax etc etc for it?

nverma
- 4th October 2006, 22:50
Darrel,

I don't think I can use that approach since the ADCIN command only lets you store the result in a VAR not a CON. How would I tell it what values from 0-1023 to pull out from memory?

Darrel Taylor
- 4th October 2006, 22:56
The reading from the POT should be put in a variable.

The CON is the address of the Data Table that gets stored in program memory.

Then the A/D reading would be used as an offset into the data table in order to find the correct value.
<br>

nverma
- 5th October 2006, 01:01
Thanks Darrel,

I works GREAT! I just hope now there isn't a limit to the number of values I can put in the Datatable. If there is, then i'm praying that its 1024!!!!!

Darrel Taylor
- 5th October 2006, 01:11
Nope,

You are only limited by the amount of available memory in Program Space.

Cheers,

nverma
- 5th October 2006, 17:28
Well I got a 125KB, I think I should be fine!

Thanks,
Nikhil

Darrel Taylor
- 5th October 2006, 18:07
The chip might have 128k, but keep in mind that PicBasic Pro can only address the first 64K of it directly.

Since the table will only use 2K, you should be fine for now.

If the program gets "A LOT" bigger, it's possible to place the table in the upper 64k area, but the readcode would need to be changed to ASM.

But you can worry about that later.
<br>

ErnieM
- 5th October 2006, 18:48
Ahhh... the LOOKUP and LOOKUP2 statements impliment the RETLW method for you. Lots cleaner.

Several problems: LOOKUP2 will return words, but has a limit of 85 values (and wastes 50% more room then LOOKUP). LOOKUP1 will fit 255 values, but they are only a byte each (but 1 byte takes 1 program location).

255 is usefull, but 256 would be lots nicer. Using 255 locations just need a work-around for the 256th value, simple in a SELECT-CASE form.

SO... not the worst, each value gets looked up for it's exact value. You would expect 1024 word lookups to fit in 2K of program memory.

Ready for it?

BUT !!!

The LOOKUP statement needs to start on a even 256 address boundary (don't ask why, it's an assembler issue). To do this, PBP seems to just waste code space till it gets to the next boundary.

I tried a sample program compiled for a PIC16F88, which has a 4K word codespace. The program did not fit. I looked at the disassembly I saw the problem was with this wasted codespace.

Since you have 125K it will fit, and hopefully you can accept the wasted space (basically, you'll need 2:1 to fit the lookups).

Sample code:



D2A_Result var word ' raw data from D2A conversion
D2A_Conversion var word ' converted result
' read the D2A, place Result in D2A_result

select case D2A_result
case is <256
lookup D2A_result.byte0, _
[$00, $00, $00, $00, $00, $00, $00, $00, _ ' 0-7 lo bytes
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 8-15 lo bytes
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 16
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 24
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 32
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 40
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 48
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 56
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 64
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 72
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 80
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 88
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 96
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 104
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 112
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 120
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 128
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 136
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 144
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 152
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 160
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 168
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 176
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 184
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 192
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 200
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 208
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 216
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 224
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 232
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 240
$00, $00, $00, $00, $00, $00, $00], _ ' 248 - 255
D2A_Conversion.byte0
lookup D2A_result.byte0, _
[$00, $00, $00, $00, $00, $00, $00, $00, _ ' 0-7 hi bytes
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 8
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 16
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 24
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 32
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 40
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 48
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 56
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 64
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 72
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 80
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 88
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 96
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 104
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 112
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 120
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 128
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 136
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 144
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 152
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 160
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 168
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 176
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 184
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 192
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 200
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 208
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 216
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 224
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 232
$00, $00, $00, $00, $00, $00, $00, $00, _ ' 240
$00, $00, $00, $00, $00, $00, $00], _ ' 248 - 255
D2A_Conversion.byte1
case 256
D2A_Conversion.byte0 = $00
D2A_Conversion.byte1 = $00
case is < 512
' same as before, 2 LOOKUP tables
case 512
D2A_Conversion.byte0 = $00
D2A_Conversion.byte1 = $00
case is < 768
' same as before, 2 LOOKUP tables
case 768
D2A_Conversion.byte0 = $00
D2A_Conversion.byte1 = $00
case else
' this case handles 769 to 1023
' same as before, 2 LOOKUP tables
end select


If you do run out of codespace, let me know and I'll help with some asm code for this.

Darrel Taylor
- 5th October 2006, 19:49
Uhhh, "Much Cleaner"??

You're kidding right??

I have to wonder if you've actually read my previous posts.

nverma
- 6th October 2006, 02:05
Hey Darrel,

Well I have a huge table with all my values. Great!....but i'm noticing that for some values I store in my table, the program deosn't work. Its really weird....do you think that there might be some memory addressing problems/overwriting program code with the Datatable approach?

Darrel Taylor
- 6th October 2006, 02:13
Well, I wouldn't think so, but can't really say for sure.

Can you post it, or send it to darrel at pbpgroup.com. I'm sure I can get to the bottom of it.
<br>

nverma
- 6th October 2006, 16:36
Darrel,

I have sent you 2 versions of the code. The e-mail explains the prob.

Nikhil

Acetronics2
- 6th October 2006, 17:01
Big Tables ???

No pwôblem, missiou !!! Use an excel sheet creating a text file ( Retlw xx ...) and some asm ( RETLW ...) lines.

Here are ( simple !!! ) examples

Alain

Darrel Taylor
- 6th October 2006, 18:09
Since the RETLW method stores 1 byte per instruction which takes 2 bytes.
A table of 1024 words would use 4096 bytes, instead of the 2048 bytes used by the method shown here ...

Using EXT with Labels
http://www.picbasic.co.uk/forum/showthread.php?t=3891#LAB

If you are using a 16F as the .zip example shows, then RETLW would be better because it can only store 14-bit words, but not for an 18F.
<br>

Acetronics2
- 7th October 2006, 09:18
Hi,Darrel

I think we also must consider the table loading work ... ( humour !!! )


Alain

nverma
- 10th October 2006, 16:55
Hey Darrel,

That table approach works well. now im trying to implement a PID controller to maintain the temperature at a desired temp by switching heaters on/off. I am thinking of using the PWM to do this. But I have to calculate my gain,I and D first. I think this can be done using the step response, but do you know what approximate values i can start out with and how to control the PWM with the output drive?

nverma
- 10th October 2006, 18:37
Hey Darrel,

That table approach works well. now im trying to implement a PID controller to maintain the temperature at a desired temp by switching heaters on/off. I am thinking of using the PWM to do this. But I have to calculate my gain,I and D first. I think this can be done using the step response, but do you know what approximate values i can start out with and how to control the PWM with the output drive?

Acetronics2
- 11th October 2006, 08:01
Hi, Nverma

Have a look to : http://www.parallax.com/dl/docs/prod/sic/Web-PC-v1.0.pdf

there's a PID heather project with ALL the coeff. adjustment procedure ...

AND PBP Basic compatible written overall ...

Alain

PS ... page 245 ...

nverma
- 11th October 2006, 18:27
Thanks Alain,

I have that same document and have been reading through it. Its quite helpful. I will continue working on this and let u know if I have any further questions.

Nikhil