Shop OBEX P1 Docs P2 Docs Learn Events
SX/B Maths — Parallax Forums

SX/B Maths

CrustyNoodleCrustyNoodle Posts: 15
edited 2008-06-10 22:00 in General Discussion
Hi,

I'm trying to get the sensirion SHT1x humidity sensor up and running with my SX28 and I'm having a little trouble...

I've adapted the example Basic Stamp code to mostly work under SX/B to the point whereby·I am able to get readings from the SHT1x and I am able to do the calcs to get the correct temperature however I can't get the humidity calcs to work - due to my inexperience/ignorance I suspect.

The code example to linearize the humidity reading is:
 ' linearize humidity:
 '  rhLin = (soRH * 0.0405) - (soRH * 0.004 * soRH * 0.0007) - 4
 '


Note that soRH is a word variable that holds the reading from the SHT1x and rhLin is also defined as a word variable.

With Basic Stamp code to do this:
rhLin = soRH ** 26542
rhLin = rhLin - ((soRH ** 3468) * (soRH ** 3468) + 50 / 100)
rhLin = rhLin - 40


·So apart from the fact that I haven't managed to work out how we got from the first box to the second (I need to do more reading here) and that it would appear that SX/B can only do one math operation per line (I probably should have read this but ended up working it out by trial and error), I am still unable to replicate this code (or should I say the result from this code) using SX/B.

Is the answer straight forward or do I need to go back to school to learn about maths in binary and hex?

Comments

  • VelocitVelocit Posts: 119
    edited 2008-06-10 05:00
    You can still do everything in SX/B and decimal format without having to think about it too much... just replicate the equation in single steps using parenthetical order of operations, followed by "computer order of operations" (i.e. left to right). They made it pretty easy by putting everything in parentheses though. You might need to utilize a couple temporary word variables to get to the final value, though:

    rhLin = soRh ** 26542
    
    tmpW1 = soRh ** 3468 
    tmpW2 = soRh ** 3468
    tmpW1 = tmpW1 * tmpW2
    tmpW1 = tmpW1 + 50
    tmpW1 = tmpW1 / 100
    
    rhLin = rhLin - tmpW1
    rhLin = rhLin - 40
    
    



    The above equation can be further reduced to save memory space by eliminating the tmpW2 variable and re-using soRh:

    rhLin = soRh ** 26542
    
    tmpW1 = soRh ** 3468 
    soRh = soRh ** 3468
    tmpW1 = tmpW1 * soRh
    tmpW1 = tmpW1 + 50
    tmpW1 = tmpW1 / 100
    
    rhLin = rhLin - tmpW1
    rhLin = rhLin - 40
    
    



    You don't need a complete understanding of binary to understand what they're doing (except for the multiply high operator, maybe). The main thing is to remember that the SX (and in turn, the BS2) can only do integer math. In order to complete mathematical operations such as multiplying by a decimal, they first multiply one of the multiplicands by a factor of ten, or however much it takes to make the decimal a whole number. The result is later divided by that same factor.

    My code might be able to be even further simplified, but I think this way might be the easiest for you to see...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Paul
  • JonnyMacJonnyMac Posts: 9,216
    edited 2008-06-10 05:46
    Multiplication, division, and related operators generate a fair bit of code; you might want to encapsulate ** into a function. Delcare it like this:

    MULT_HIGH       FUNC    2, 2, 4                 ' shell for **
    


    The function code looks like this:

    ' Use: result = MULT_HIGH value1, value2
    ' -- if passing word and byte, pass word value as first parameter
    
    FUNC MULT_HIGH
      IF __paramcnt = 2 THEN
        tmpW2 = __param1
        tmpW3 = __param2
      ELSEIF __paramcnt = 3 THEN
        tmpW2 = __wparam12
        tmpW3 = __param3
      ELSE
        tmpW2 = __wparam12
        tmpW3 = __wparam34
      ENDIF
      tmpW2 = tmpW2 ** tmpW3
      RETURN tmpW2
      ENDFUNC
    
  • CrustyNoodleCrustyNoodle Posts: 15
    edited 2008-06-10 22:00
    OK thanks for all your help - I've got it working.

    In my travels to find information on SX/B Maths I found Tracy Allen's website (click the link) that has a wealth of information on the Basic Stamp including explanations on how to do complex maths.· For anyone who is struggling with the understanding of how to do floating point maths in an integer world this will help.
Sign In or Register to comment.