Shop OBEX P1 Docs P2 Docs Learn Events
interger math question — Parallax Forums

interger math question

AndywatsonAndywatson Posts: 6
edited 2004-09-07 19:14 in BASIC Stamp
I've got a MAX186 12-bit A/D converter, which is reading in a voltage from a pot between 0 and 4.096 volts. The pot, which is acting as a tuning knob in my case, only needs to have a total of 82 distinct positions, meaning that a voltage between 0 and 0.05v would correspond to position 1, a voltage between 0.05v and 0.1v would be position 2, etc.

So the A/D will have 50 bits for each of the different positions, so what's the easiest way to read in the bit value (between 0 and 4096) and turn that into a position value (between 1 and 82)? If I simply divide the bit value by 50, I'm left with a floating point number. How can I get just an interger value?

Thanks.

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-09-07 18:40
    Since the BASIC Stamp truncates to integers, you can do this:

    · position = adcVal / 50 + 1

    This will give you 1 to·82 across the range of your ADC, like this:

    · 4095 / 50 = 81.9 --> BASIC Stamp truncates to 81
    · 81 + 1 = 82



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • AndywatsonAndywatson Posts: 6
    edited 2004-09-07 18:54
    I didn't realize it automatically truncated to intergers. No wonder I couldn't find a command or operator to do that. Thanks Jon.
  • AlWilliamsAWCAlWilliamsAWC Posts: 135
    edited 2004-09-07 19:14
    Since your numbers are of a small range, consider implementing rounding. For example:

    4050/50 = ·81.0
    4055/50 = 81.1
    4075/50 = 81.5
    4095/50 = 81.9
    The Stamp computes all of these as 81.

    Instead compute x/5:
    4050/5 = 810
    4055/5 = 811
    4075/5 = 815
    4095/5 = 819

    Then add 5 and divide by 10. So:
    (4050/5+5)/10 = 81
    (4055/5+5)/10 = 81
    (4075/5+5)/10 = 82
    (4095/5+5)/10 = 82

    This works nicely because you are dividing by 50. However, if you wanted to use, say 52, you would multiply x by 10 instead (which is OK since 4095 * 10 = 40950 which still fits in a word). So:

    (4055*10/52+5)/10 = 78 (should be 77.98 which rounds to 78).

    Of course, that being said, a PAK-XII (http://www.awce.com/pak12.htm) makes floating point pretty simple and adds A/D so you can do all your A/D manipulation on the PAK in 32-bit floating point.

    Regards,

    Al Williams
    AWC
    Kits
    http://www.awce.com/kits.htm
Sign In or Register to comment.