Need help with lookup table or direct calculation


Closed Thread
Results 1 to 25 of 25
  1. #1
    Join Date
    Oct 2006
    Posts
    25

    Default Need help with lookup table or direct calculation

    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?

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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,
    DT

  3. #3
    Join Date
    Oct 2006
    Posts
    25


    Did you find this post helpful? Yes | No

    Default

    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

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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>
    DT

  5. #5
    Join Date
    Jul 2005
    Posts
    78


    Did you find this post helpful? Yes | No

    Default

    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).

  6. #6
    Join Date
    Oct 2004
    Location
    Hangover, Germany
    Posts
    289


    Did you find this post helpful? Yes | No

    Default

    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...
    PBP 2.50C, MCS+ 3.0.0.5, MPLAB 8, MPASM 5.14, ASIX Presto, PoScope, mE mikroBasic V7.2, PICKIT2

  7. #7
    Join Date
    Oct 2006
    Posts
    25


    Did you find this post helpful? Yes | No

    Default

    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?

  8. #8
    Join Date
    Oct 2006
    Posts
    25


    Did you find this post helpful? Yes | No

    Default

    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?

  9. #9
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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>
    DT

  10. #10
    Join Date
    Oct 2006
    Posts
    25


    Did you find this post helpful? Yes | No

    Default

    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!!!!!

  11. #11
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Nope,

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

    Cheers,
    DT

  12. #12
    Join Date
    Oct 2006
    Posts
    25


    Did you find this post helpful? Yes | No

    Default

    Well I got a 125KB, I think I should be fine!

    Thanks,
    Nikhil

  13. #13
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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>
    DT

  14. #14
    Join Date
    Jul 2005
    Posts
    78


    Did you find this post helpful? Yes | No

    Default

    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:

    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.

  15. #15
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Uhhh, "Much Cleaner"??

    You're kidding right??

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

  16. #16
    Join Date
    Oct 2006
    Posts
    25


    Did you find this post helpful? Yes | No

    Default

    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?

  17. #17
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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>
    DT

  18. #18
    Join Date
    Oct 2006
    Posts
    25


    Did you find this post helpful? Yes | No

    Default

    Darrel,

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

    Nikhil

  19. #19
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Wink other solution ...

    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
    Attached Files Attached Files
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  20. #20
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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/show...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>
    DT

  21. #21
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Talking a thought for the programmer

    Hi,Darrel

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


    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  22. #22
    Join Date
    Oct 2006
    Posts
    25


    Did you find this post helpful? Yes | No

    Default

    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?

  23. #23
    Join Date
    Oct 2006
    Posts
    25


    Did you find this post helpful? Yes | No

    Default

    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?

  24. #24
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Wink Already cooked !!!

    Hi, Nverma

    Have a look to : http://www.parallax.com/dl/docs/prod...eb-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 ...
    Last edited by Acetronics2; - 11th October 2006 at 08:07.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  25. #25
    Join Date
    Oct 2006
    Posts
    25


    Did you find this post helpful? Yes | No

    Default

    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

Similar Threads

  1. Lookup table syntax.....
    By TerdRatchett in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 19th May 2009, 04:45
  2. Lookup table or Math?
    By ronjodu in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 2nd May 2008, 17:55
  3. Lookup Table
    By yasser hassani in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 11th March 2008, 10:38
  4. problem with USART
    By leemin in forum Serial
    Replies: 4
    Last Post: - 11th December 2006, 17:56
  5. Real Time Clock & Eeprom
    By smart_storm in forum General
    Replies: 8
    Last Post: - 17th February 2006, 19:03

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts