Searching \ for '[SX] Changing a value from base2 to base10?' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/ubicom/devices.htm?key=sx
Search entire site for: 'Changing a value from base2 to base10?'.

Exact match. Not showing close matches.
PICList Thread
'[SX] Changing a value from base2 to base10?'
2006\07\25@114426 by Steeln/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Steel wrote:

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
---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=137056
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2006 (http://www.dotNetBB.com)

2006\07\25@120721 by Beau Schwaben/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Beau Schwabe wrote:

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
---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=137056#m137061
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2006 (http://www.dotNetBB.com)

2006\07\25@122429 by Steeln/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Steel wrote:

umm...oh yeah...thanks Beau...

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=137056#m137064
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2006 (http://www.dotNetBB.com)

2006\07\27@112815 by SSteven/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, SSteve wrote:

[Quoting: "Beau Schwabe (Parallax)"]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 ?

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=137056#m137395
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2006 (http://www.dotNetBB.com)

2006\07\27@124303 by chipheadn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, chiphead wrote:

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
     25         =        1
     50         =        2
              ...
   255         =       10
ADC_VALUE = ADC_VALUE - 1
While Buau uses the magic of integer math to get
ADC_VALUE = ADC_VALUE / 28
     25         =        0
     50         =        1
             ...
   255         =        9
---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=137056#m137404
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2006 (http://www.dotNetBB.com)

2006\07\27@133323 by SSteven/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, SSteve wrote:

[Quoting: "John R."]
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
[Quoting: "John R."]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.)
---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=137056#m137410
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2006 (http://www.dotNetBB.com)

2006\07\27@142947 by chipheadn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, chiphead wrote:

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.

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=137056#m137419
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2006 (http://www.dotNetBB.com)

2006\07\27@143031 by Beau Schwaben/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Beau Schwabe wrote:

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'
---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=137056#m137420
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2006 (http://www.dotNetBB.com)

2006\07\27@143328 by chipheadn/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, chiphead wrote:

Can you say "over thinking a solution"?  
Beau, Thanks for pointing out the obvious to us.

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=137056#m137421
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2006 (http://www.dotNetBB.com)

2006\07\27@144419 by SSteven/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, SSteve wrote:

OK. The way I read the original post, he wanted the value to change at every multiple of 25.

---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=137056#m137423
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2006 (http://www.dotNetBB.com)

2006\07\27@145919 by Beau Schwaben/a

flavicon
face
In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Beau Schwabe wrote:

See edit above
---------- End of Message ----------

You can view the post on-line at:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=137056#m137428
Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com
The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2006 (http://www.dotNetBB.com)

More... (looser matching)
- Last day of these posts
- In 2006 , 2007 only
- Today
- New search...