Shop OBEX P1 Docs P2 Docs Learn Events
Changing a value from base2 to base10? — Parallax Forums

Changing a value from base2 to base10?

SteelSteel Posts: 313
edited 2006-07-27 21:58 in General Discussion
I have an ADC that gives me a base2 value (0-FF) and I want to convert it to Base10 (0-9).··Is there a more code-efficient way of doing·so·than·what is below?



If ADC_VALUE < 25 Then
··ADC_VALUE = 0
·ELSEIF ADC_VALUE <50 THEN
··ADC_VALUE = 1
·ELSEIF ADC_VALUE <75 THen
··ADC_VALUE = 2
·ELSEIF ADC_VALUE < 100 THEN
··ADC_VALUE = 3
·ELSEIF ADC_VALUE < 125 THEN
··ADC_VALUE = 4
·ELSEIF ADC_VALUE < 150 THEN
··ADC_VALUE = 5
·ELSEIF ADC_VALUE < 175 THEN
··ADC_VALUE = 6
·ELSEIF ADC_VALUE < 200 THEN
··ADC_VALUE = 7
·ELSEIF ADC_VALUE < 225 THEN
··ADC_VALUE = 8
·ELSEIF ADC_VALUE < 255 THEN
··ADC_VALUE = 9
·ENDIF

Comments

  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2006-07-25 19:06
    Steel,

    base 2 would be 0 or 1
    base 16 would be 0 through F


    What you are doing with the IF/THEN can be accomplished with simple division.

    ADC_VALUE = ADC_VALUE / 28

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • SteelSteel Posts: 313
    edited 2006-07-25 19:23
    umm...oh yeah...thanks Beau...
  • SSteveSSteve Posts: 808
    edited 2006-07-27 18:27
    Beau Schwabe (Parallax) said...
    What you are doing with the IF/THEN can be accomplished with simple division.

    ADC_VALUE = ADC_VALUE / 28

    Don't you mean ADC_VALUE = ADC_VALUE / 25 ?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows

    links:
    My band's website
    Our album on the iTunes Music Store
  • John R.John R. Posts: 1,376
    edited 2006-07-27 19:42
    This post is BAAAAD, see further posts below for corrections:

    Beau used 28 in order to get the same values as the original code.

    I think you could also use 25, but then subtract 1

    ADC_VALUE = ADC_VALUE / 25 Yields the following:
    25 = 1
    50 = 2
    ...
    255 = 10
    Then you have to do this:

    ADC_VALUE = ADC_VALUE - 1

    While Buau uses the magic of integer math to get

    ADC_VALUE = ADC_VALUE / 28 Yields the following:
    25 = 0
    50 = 1
    ...
    255 = 9

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    John R.

    8 + 8 = 10

    Post Edited (John R.) : 7/27/2006 9:31:00 PM GMT
  • SSteveSSteve Posts: 808
    edited 2006-07-27 20:32
    John R. said...

    ADC_VALUE = ADC_VALUE / 25 Yields the following:
    25 = 1
    50 = 2
    ...
    255 = 10

    I overlooked values for ADC_VALUE >= 250. So the correct formula would be ADC_VALUE = (ADC_VALUE / 25) MAX 9
    John R. said...
    While Buau uses the magic of integer math to get

    ADC_VALUE = ADC_VALUE / 28 Yields the following:
    25 = 0
    50 = 1
    ...
    255 = 9

    I understand the integer math part but I think the "28" in Beau's message is a typo. I've attached the output from this program:
    ' {$STAMP BS2sx}
    ' {$PBASIC 2.5}
    
    ADC_VALUE    VAR    BYTE
    
    FOR ADC_VALUE = 0 TO 255
        DEBUG "ADC_VALUE = ", DEC3 ADC_VALUE, ": /25 = "
        DEBUG DEC1 (ADC_VALUE / 25) MAX 9 '<-- here's the conversion forumla
        DEBUG " /28 = ", DEC1 ADC_VALUE / 28, CR
    NEXT
    


    Look at the values for 25, 26, 27, 50, 51, 52, 53, 54, 55, 75, 76, 77, etc. when dividing by 28. They are all incorrect. (I used PBASIC because it was at hand, but the SX/B results should be the same.)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows

    links:
    My band's website
    Our album on the iTunes Music Store
  • John R.John R. Posts: 1,376
    edited 2006-07-27 21:29
    My bad - I "saw" the lines:

    ADC_VALUE < 25 Then

    as

    ADC_VALUE <= 25 Then

    Hence my thougths on the 28 and also the using 25 then subtracting 1.

    Call me an idiot today, and ignore my post. 25 is the correct devisor and I will concurr that Beau had a typo.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    John R.

    8 + 8 = 10
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2006-07-27 21:29
    Since Steel essentially had a range of 0 to 255 (FF) that needed to be scaled to give an output from 0 to 9...


    255 / 9 = 28.33333333333333333

    ...I just used '28'



    JohnR,

    Using 25 as the divisor subtracting 1 is correct, but when you are essentially knocking off 4 bits of resolution anyway
    I figured it would not make a huge difference.

    You could also do something like...

    ADC_VALUE = ADC_VALUE / 16
    ADC_VALUE = ADC_VALUE * 10
    ADC_VALUE = ADC_VALUE / 16

    ...This just looks at the upper nibble creating a range from 0 to F ...or... 0 to 15 . Scaling this to 0 to 9 ...


    16 divided by 10 = 1.6 so if we multiply the ADC_VALUE by 10 now we just need to divide by 16 instead of 1.6








    My error was that I used 0-255 and 0-9 to come up with 255/9 to get 28 ... Where I should have used 256/10 to get 25

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 7/27/2006 9:57:34 PM GMT
  • John R.John R. Posts: 1,376
    edited 2006-07-27 21:32
    Can you say "over thinking a solution"?

    Beau, Thanks for pointing out the obvious to us.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    John R.

    8 + 8 = 10
  • SSteveSSteve Posts: 808
    edited 2006-07-27 21:43
    OK. The way I read the original post, he wanted the value to change at every multiple of 25.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows

    links:
    My band's website
    Our album on the iTunes Music Store
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2006-07-27 21:58
    See edit above

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
Sign In or Register to comment.