Menu

Arithmetic with words, integers and signed long integers?

Help
Peter
2015-01-11
2015-01-12
  • Peter

    Peter - 2015-01-11

    I want to create a library for the Bosch BMP180 sensor.

    I've got no problem communicating with the device and can read the pressure and temperature values from it, however these values need converting to actual values using the devices calibration data.

    The data sheet gives the method for doing this, but it involves arithmetic with signed 16 bit numbers (GCB integer), unsigned 16 bit numbers (GCB word) and 'long' numbers which appear to be 32 bit signed.

    First of all, I'm having problems with the signed/unsigned maths in GCB.
    If

     MC = -8711 '(integer)
     MD = 2868 '(integer)
     X1 = 4743 '(can be a long)
    

    how do I do: X2 = MC * 32768 / (X1 + MD)
    Is it like this:

    X2 = [long]([integer]MC * 32768) / ([long]X1 + [integer]MD))
    

    I guess ideally I should convert everything to longs first. Do the maths functions in GCB work with signed Long integers?

    Does the standard LCD library work with signed values, or do I need to check bit 15 and add a "-" if necessary?

     

    Last edit: Peter 2015-01-11
  • Anobium

    Anobium - 2015-01-12

    Variable Types

    There are several different types of variable, and each type can store a different sort of information. These are the variable types that Great Cow BASIC can currently use:
    Variable type Information that this variable can store Example uses for this type of variable

    • Bit. A bit (0 or 1)
    • Byte. A whole number between 0 and 255
    • Word. A whole number between 0 and 65535 Storage of extra large numbers
    • Integer. A whole number between -32768 and 32767
    • Long. A whole number between 0 and 2^32 (4.29 billion)
    • Array. A list of whole numbers ranging from 0 to 255
    • String. A series of letters, numbers and symbols.

    Look like you should be using Integer.

     
  • Peter

    Peter - 2015-01-12

    The problem is, if I do [Integer] * [Word], I will get a signed version of [long] e.g. -1.25 billion.

    The signed longs need to be added/multipled/subtracted later on in the calculation.

     
  • Anobium

    Anobium - 2015-01-12

    You could use GCB LOG tables and (optionally) LOG Transformation to handle the negative numbers but this depends on accuracy you need.

    A question: How did others create/handle the Bosch BMP180 sensor as the most of the limitations of GCB with respect to numbers handling exists in other solutions.

     
  • Peter

    Peter - 2015-01-12

    The Adafruit Arduino library uses an [int32] for some of the values. Bosch's C library uses [s32] for these values i.e. signed 32 bit numbers.

    SparkFun's Ardunio library uses an alternative method using floating point arithmetic. I thought this might use too much RAM but I had a go at doing it anyway in GCB using the float.h library, however the compiler kept crashing - probably a user error :)

    I guess another alternative would be to add 2^15 to all the negative numbers and sort it out at the end.

     

Log in to post a comment.