Changing a value from base2 to base10?
Steel
Posts: 313
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
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
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.
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
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
I overlooked values for ADC_VALUE >= 250. So the correct formula would be ADC_VALUE = (ADC_VALUE / 25) MAX 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:
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
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
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
Beau, Thanks for pointing out the obvious to us.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
John R.
8 + 8 = 10
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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 Schwabe
IC Layout Engineer
Parallax, Inc.