EmbeddedRelated.com
Forums

Anyone did decimal arithmatic on MSP430 in runtime??

Started by europus July 30, 2007
If an alien told you that they have 10 fingers and they use base 10 in
their numbering system, what would you conclude?

If you later see that they actually have only 7 fingers, would you
think that alien lied to you?

That alien was telling you the truth.

No matter how many fingers they have, it is likely that that they use
that number as the base of their numbering system. Thus under their
numbering system, they have 10 fingers and the base of their numbering
system is 10.

--- In m..., "countryboy1234579"
wrote:
>
> I'm curious why you have chosen to perform math in BCD. It would be
> much more readable if you just do all of your math in decimal and
> convert the result to a hex string just before you display it.
>
> Something like...
> /******************************************************
> * void ConvertInt2Hex(uint16_t value, uint8_t* result)
> * returns : void
> * value : value to convert between 0 and 65535
> * result : points to character array to which result
> * : is stored.
> /******************************************************
> void ConvertInt2Hex(uint16_t value, uint8_t* result)
> {
> const uint8_t HEX[] = "0123456789ABCDEF";
> uint16_t i;
>
> for(i= 0; i < 4; i++)
> {
> result[i] = HEX[value & (0xF000 >> (i * 4))];
> }
> }
>
> I didn't compile that, so it may need some help, but you get the point.
>
> Now, if some college professor really wants you to do math on those
> strings...
>
> /******************************************************
> * uint16_t ConvertHex2Int(uint8_t* input)
> * returns : The 16-bit equivalent of the string to
> * : which result points
> * result : points to the character array holding
> * : the hexidecimal number to be converted
> /******************************************************
> uint16_t ConvertHex2Int(uint8_t* result)
> {
> const uint8_t HEX[] = "0123456789ABCDEF";
> uint8_t i, j;
> uint16_t result = 0;
>
> for(i = 0; input[i]; i++) // Scan string until null
> {
> result <<= 4; // shift earlier result left four bits
>
> for(j = 0; j < 16; j++)
> {
> if(input[i] == HEX[j])// if a match was found
> {
> result += j; // add value of this character
> j = 17; // jump out of loop once match is made
> }
> }
> if(j == 16)
> {
> //add error handling here
> }
> }
>
> return result;
> }
>
> Cheers!
> John
>
> --- In m..., "europus" wrote:
> >
> > Hi there,
> > I want to know if anyone converted hex data into decimal
> > in C compiler ...If yes..how did you manage ? Thanks in
> > advance..
>

Beginning Microcontrollers with the MSP430

So, clearly that guy who waved one finger at me in traffic the other
day is from an alien planet that prefers binary math. ;)

I still think the code will be clearer to earthlings if the math is
decimal. Although I readily shift left or right to multiply by powers
of two because it is more efficient in the MSP430.

(number + 500) / 1000; //takes way more clock cycles than
(number + 512) >> 10;

Cheers!
John

--- In m..., "old_cow_yellow"
wrote:
>
> If an alien told you that they have 10 fingers and they use base 10 in
> their numbering system, what would you conclude?
>
> If you later see that they actually have only 7 fingers, would you
> think that alien lied to you?
>
> That alien was telling you the truth.
>
> No matter how many fingers they have, it is likely that that they use
> that number as the base of their numbering system. Thus under their
> numbering system, they have 10 fingers and the base of their numbering
> system is 10.
>
> --- In m..., "countryboy1234579"
> wrote:
> >
> > I'm curious why you have chosen to perform math in BCD. It would be
> > much more readable if you just do all of your math in decimal and
> > convert the result to a hex string just before you display it.
> >
> > Something like...
> > /******************************************************
> > * void ConvertInt2Hex(uint16_t value, uint8_t* result)
> > * returns : void
> > * value : value to convert between 0 and 65535
> > * result : points to character array to which result
> > * : is stored.
> > /******************************************************
> > void ConvertInt2Hex(uint16_t value, uint8_t* result)
> > {
> > const uint8_t HEX[] = "0123456789ABCDEF";
> > uint16_t i;
> >
> > for(i= 0; i < 4; i++)
> > {
> > result[i] = HEX[value & (0xF000 >> (i * 4))];
> > }
> > }
> >
> > I didn't compile that, so it may need some help, but you get the
point.
> >
> > Now, if some college professor really wants you to do math on those
> > strings...
> >
> > /******************************************************
> > * uint16_t ConvertHex2Int(uint8_t* input)
> > * returns : The 16-bit equivalent of the string to
> > * : which result points
> > * result : points to the character array holding
> > * : the hexidecimal number to be converted
> > /******************************************************
> > uint16_t ConvertHex2Int(uint8_t* result)
> > {
> > const uint8_t HEX[] = "0123456789ABCDEF";
> > uint8_t i, j;
> > uint16_t result = 0;
> >
> > for(i = 0; input[i]; i++) // Scan string until null
> > {
> > result <<= 4; // shift earlier result left four bits
> >
> > for(j = 0; j < 16; j++)
> > {
> > if(input[i] == HEX[j])// if a match was found
> > {
> > result += j; // add value of this character
> > j = 17; // jump out of loop once match is made
> > }
> > }
> > if(j == 16)
> > {
> > //add error handling here
> > }
> > }
> >
> > return result;
> > }
> >
> > Cheers!
> > John
> >
> >
> >
> > --- In m..., "europus" wrote:
> > >
> > > Hi there,
> > > I want to know if anyone converted hex data into decimal
> > > in C compiler ...If yes..how did you manage ? Thanks in
> > > advance..
> > >
>
> The following code will produce the correct decimal digits.
>
> dig1 = value / 10000;
> dig2 = (value % 10000) / 1000;
> dig3 = (value % 1000) / 100;
> dig4 = (value % 100) / 10;
> dig5 = value % 10;

if you loads of program space, and a have a slow processor, with hardware
multiply, but no hardware divide, you might find this runs faster....

dig1 = 0;
if( value > 60000 )
{
dig1 = 6 ;
}
else
if( value > 50000 )
{
dig1 = 5 ;
}
else
if(

[ etc ]

}

value -= dig1 * 10000 ;

repeat for next digits - dig5 will fall out at the bottom without
calculation.

David
Hi david,
Thanks a lot . I have something similar but i am not using %......I was
extracting digit 1 as follow and then try to apply conversion starting from MSB
and then add them all up to get count in decimal equivalent number.
But the compiler is messing up while assigning dig1 decimal equivalent value into
long variable. I am holding dig1_dec as long integer. But still....:( ..I will try your
idea and let you all know how it works....Thanks again...

dig1= (value & 0XF000)/0X1000; //isolate dig 1//

David Collier wrote:

> The following code will produce the correct decimal digits.
>
> dig1 = value / 10000;
> dig2 = (value % 10000) / 1000;
> dig3 = (value % 1000) / 100;
> dig4 = (value % 100) / 10;
> dig5 = value % 10;

if you loads of program space, and a have a slow processor, with hardware
multiply, but no hardware divide, you might find this runs faster....

dig1 = 0;
if( value > 60000 )
{
dig1 = 6 ;
}
else
if( value > 50000 )
{
dig1 = 5 ;
}
else
if(

[ etc ]

}

value -= dig1 * 10000 ;

repeat for next digits - dig5 will fall out at the bottom without
calculation.

David

---------------------------------
Looking for a deal? Find great prices on flights and hotels with Yahoo! FareChase.
No no....see , the idea was to separate the 1's ,10's ,100's and 1000's digits in hex first
and then multiply each one weighted....i.e suppose let;s take 0X4141 example so ....
dig1= 4 ,dig2=1 ,dig3 =4 and dig4=1 so now when you multiply dig1 by 4096 (decimal)....then it will be 16384..i.e decimal equivalent of 0X4000.....then similar for 0X100...
then 0X40 and then 0X01.....so the addition should look like
16384 + 256 + 64 + 1 = 16705......
But while performing the above task, the compiler hold just some random values which do not change. i.e 0X0000030CD..like wise. ...
I guess what i was not able to get was the compiler is handling things differently
than a normal GCC C compiler or a calculator....
But i guess i will try code at the bottom and let you know.....It looks simple enough to
work....Thanks for your help old_cow....

old_cow_yellow wrote:
Let us assume a "value" of 0x4141. The decimal representation of this
"value" is 16705. Thus the first decimal digit is 1 (not 4). The
second decimal digit is 6 (not 1). The third is 7 (not 4). The fourth
is 0 (not 1). And there is a fifth decimal digit, which equals 5.

The following code will produce the correct decimal digits.

dig1 = value / 10000;
dig2 = (value % 10000) / 1000;
dig3 = (value % 1000) / 100;
dig4 = (value % 100) / 10;
dig5 = value % 10;

--- In m..., alex ander wrote:
>
> Thanks a lot Bill, Infact i am doing something similar but not
able to get further than there
> in converting to either BCD or decimal . Here is how i separated
digits. Value is raw ADC
> count in Hex.
> dig1= (value & 0XF000)/0X1000; //isolate dig 1//
> dig2= (value & 0X0F00)/0X100; //isolate dig 2//
> dig3= (value & 0X00F0)/0X10; //isolate dig 3//
> dig4= (value & 0X000F); //isolate dig 4//
>
> In the 1st line , i can clearly see 4 for input of 0X4141
in watch window
> But then stuck on how to obtain 20480 , equivalent decimal
for 0X5000 + 2560 for 0X0A00. ...and so on and then mutually add
them or OR them to obtain 23245 (decimal equivalent of 0X5ACD).
> Bill Knight wrote:
> If is BCD to binary, how about this? (I just hacked it
together and
> have not run it through a compiler or anything, but it will hopefully
> give you the idea.)
>
> Regards
> -Bill Knight
> R O SoftWare
>
> unsigned bcd2bin(unsigned bcd)
> {
> unsigned resut = 0;
> unsigned multiplier = 1;
>
> while (bcd)
> {
> result += ((bcd & 0x0F) * multiplier);
> bcd >>= 4;
> multiplier *= 10;
> }
>
> return result;
> }
>
> Jose I Quinones wrote:
> > Do you mean BCD?
> >
> > Data is always stored in binary. Hexadecimal is just a notation of
four bits
> > to make it easier to write it down.
> >
> > In a way, it really does not matter how the data is stored as
while the
> > program is running, why would we care? Unless of course you have
to display
> > it!
> >
> > What I have done before (this applies to all sorts of programming)
is deduct
> > the digits from the number at hand. Have in mind that this depends on
> > whether your hex is 8 bit *max of 255) or 16 bits (max of 65535).
Then you
> > can see how many times you can subtract a thousand, a hundred, a
ten, etc.
> > from the original number and then the respective reminder as you
go on. Per
> > example, for the number 234, you can subtract a 100 2 times and
now you have
> > 34, from which you can subtract ten three times and now you have
four. These
> > would be your three BCD digits. This of course does not sound too
clean.
> >
> > I am thinking there are a bunch of BCD functions out there to
manipulate the
> > hex numbers. Also make sure the hex number is not already BCD
coded in which
> > case each nibble represents a decimal digit.
> >
> > Hope the info helps. Best regards,
> > JIQ
> > _____
> >
> > From: m... [mailto:m...] On
Behalf Of
> > europus
> > Sent: Monday, July 30, 2007 12:01 PM
> > To: m...
> > Subject: [msp430] Anyone did decimal arithmatic on MSP430 in runtime??
> >
> >
> >
> > Hi there,
> > I want to know if anyone converted hex data into decimal
> > in C compiler ...If yes..how did you manage ? Thanks in
> > advance..
> ---------------------------------
> Ready for the edge of your seat? Check out tonight's top picks on
Yahoo! TV.
>
>
>

---------------------------------
Looking for a deal? Find great prices on flights and hotels with Yahoo! FareChase.
This will only take a few minuets.
Try the following code:

int value;
int dig1, dig2, dig3, dig4, dig5;
void main( void )
{
value = 0x4141; /*** or any other number ***/
dig1 = value/10000;
dig2 = (value%10000)/1000;
dig3 = (value%1000)/100;
dig4 = (value%100)/10;
dig5 = (value%10);
}

Open a Watch Window to show dig1, dig2, dig3, dig4, dig5.
What do you see? (I see 1, 6, 7, 0, 5 respectively.)

--- In m..., alex ander wrote:
>
> No no....see , the idea was to separate the 1's ,10's ,100's and
1000's digits in hex first
> and then multiply each one weighted....i.e suppose let;s take
0X4141 example so ....
> dig1= 4 ,dig2=1 ,dig3 =4 and dig4=1 so now when you multiply
dig1 by 4096 (decimal)....then it will be 16384..i.e decimal
equivalent of 0X4000.....then similar for 0X100...
> then 0X40 and then 0X01.....so the addition should look like
> 16384 + 256 + 64 + 1 = 16705......
> But while performing the above task, the compiler hold
just some random values which do not change. i.e 0X0000030CD..like
wise. ...
> I guess what i was not able to get was the compiler is
handling things differently
> than a normal GCC C compiler or a calculator....
> But i guess i will try code at the bottom and let you
know.....It looks simple enough to
> work....Thanks for your help old_cow....
>
> old_cow_yellow wrote:
> Let us assume a "value" of 0x4141. The decimal
representation of this
> "value" is 16705. Thus the first decimal digit is 1 (not 4). The
> second decimal digit is 6 (not 1). The third is 7 (not 4). The fourth
> is 0 (not 1). And there is a fifth decimal digit, which equals 5.
>
> The following code will produce the correct decimal digits.
>
> dig1 = value / 10000;
> dig2 = (value % 10000) / 1000;
> dig3 = (value % 1000) / 100;
> dig4 = (value % 100) / 10;
> dig5 = value % 10;
>
> --- In m..., alex ander wrote:
> >
> > Thanks a lot Bill, Infact i am doing something similar but not
> able to get further than there
> > in converting to either BCD or decimal . Here is how i separated
> digits. Value is raw ADC
> > count in Hex.
> >
> >
> > dig1= (value & 0XF000)/0X1000; //isolate dig 1//
> > dig2= (value & 0X0F00)/0X100; //isolate dig 2//
> > dig3= (value & 0X00F0)/0X10; //isolate dig 3//
> > dig4= (value & 0X000F); //isolate dig 4//
> >
> > In the 1st line , i can clearly see 4 for input of 0X4141
> in watch window
> > But then stuck on how to obtain 20480 , equivalent decimal
> for 0X5000 + 2560 for 0X0A00. ...and so on and then mutually add
> them or OR them to obtain 23245 (decimal equivalent of 0X5ACD).
> >
> >
> > Bill Knight wrote:
> > If is BCD to binary, how about this? (I just hacked it
> together and
> > have not run it through a compiler or anything, but it will hopefully
> > give you the idea.)
> >
> > Regards
> > -Bill Knight
> > R O SoftWare
> >
> > unsigned bcd2bin(unsigned bcd)
> > {
> > unsigned resut = 0;
> > unsigned multiplier = 1;
> >
> > while (bcd)
> > {
> > result += ((bcd & 0x0F) * multiplier);
> > bcd >>= 4;
> > multiplier *= 10;
> > }
> >
> > return result;
> > }
> >
> > Jose I Quinones wrote:
> > > Do you mean BCD?
> > >
> > > Data is always stored in binary. Hexadecimal is just a notation of
> four bits
> > > to make it easier to write it down.
> > >
> > > In a way, it really does not matter how the data is stored as
> while the
> > > program is running, why would we care? Unless of course you have
> to display
> > > it!
> > >
> > > What I have done before (this applies to all sorts of programming)
> is deduct
> > > the digits from the number at hand. Have in mind that this
depends on
> > > whether your hex is 8 bit *max of 255) or 16 bits (max of 65535).
> Then you
> > > can see how many times you can subtract a thousand, a hundred, a
> ten, etc.
> > > from the original number and then the respective reminder as you
> go on. Per
> > > example, for the number 234, you can subtract a 100 2 times and
> now you have
> > > 34, from which you can subtract ten three times and now you have
> four. These
> > > would be your three BCD digits. This of course does not sound too
> clean.
> > >
> > > I am thinking there are a bunch of BCD functions out there to
> manipulate the
> > > hex numbers. Also make sure the hex number is not already BCD
> coded in which
> > > case each nibble represents a decimal digit.
> > >
> > > Hope the info helps. Best regards,
> > > JIQ
> > > _____
> > >
> > > From: m... [mailto:m...] On
> Behalf Of
> > > europus
> > > Sent: Monday, July 30, 2007 12:01 PM
> > > To: m...
> > > Subject: [msp430] Anyone did decimal arithmatic on MSP430 in
runtime??
> > >
> > >
> > >
> > > Hi there,
> > > I want to know if anyone converted hex data into decimal
> > > in C compiler ...If yes..how did you manage ? Thanks in
> > > advance..
> >
> >
> >
> >
> >
> >
> > ---------------------------------
> > Ready for the edge of your seat? Check out tonight's top picks on
> Yahoo! TV.
> >
> >
> >
>
>
> ---------------------------------
> Looking for a deal? Find great prices on flights and hotels with
Yahoo! FareChase.
>
>
>
Yes, but Europus is doing a modulo 16, not 10 :

value = 0x4141; /*** or any other number ***/
// NOT NEEDED dig1 = value/0x10000;
dig2 = (value%0x10000)/0x1000;
dig3 = (value%0x1000)/0x100;
dig4 = (value%0x100)/0x10;
dig5 = (value%0x10);

Of course it's much easier to just shift right by the proper number of nibbles...
It depends, the modulo lib function might use shift arithmetic when the modulo is a power of 2.

Best Regards,
Kris

-----Original Message-----
From: m... [mailto:m...] On Behalf Of old_cow_yellow
Sent: Saturday, 11 August 2007 2:31 PM
To: m...
Subject: [msp430] Re: Anyone did decimal arithmatic on MSP430 in runtime??

This will only take a few minuets.
Try the following code:

int value;
int dig1, dig2, dig3, dig4, dig5;
void main( void )
{
value = 0x4141; /*** or any other number ***/
dig1 = value/10000;
dig2 = (value%10000)/1000;
dig3 = (value%1000)/100;
dig4 = (value%100)/10;
dig5 = (value%10);
}

Open a Watch Window to show dig1, dig2, dig3, dig4, dig5.
What do you see? (I see 1, 6, 7, 0, 5 respectively.)

--- In m..., alex ander wrote:
>
> No no....see , the idea was to separate the 1's ,10's ,100's and
1000's digits in hex first
> and then multiply each one weighted....i.e suppose let;s take
0X4141 example so ....
> dig1= 4 ,dig2=1 ,dig3 =4 and dig4=1 so now when you multiply
dig1 by 4096 (decimal)....then it will be 16384..i.e decimal
equivalent of 0X4000.....then similar for 0X100...
> then 0X40 and then 0X01.....so the addition should look like
> 16384 + 256 + 64 + 1 = 16705......
> But while performing the above task, the compiler hold
just some random values which do not change. i.e 0X0000030CD..like
wise. ...
> I guess what i was not able to get was the compiler is
handling things differently
> than a normal GCC C compiler or a calculator....
> But i guess i will try code at the bottom and let you
know.....It looks simple enough to
> work....Thanks for your help old_cow....
>
> old_cow_yellow wrote:
> Let us assume a "value" of 0x4141. The decimal
representation of this
> "value" is 16705. Thus the first decimal digit is 1 (not 4). The
> second decimal digit is 6 (not 1). The third is 7 (not 4). The fourth
> is 0 (not 1). And there is a fifth decimal digit, which equals 5.
>
> The following code will produce the correct decimal digits.
>
> dig1 = value / 10000;
> dig2 = (value % 10000) / 1000;
> dig3 = (value % 1000) / 100;
> dig4 = (value % 100) / 10;
> dig5 = value % 10;
>
> --- In m..., alex ander wrote:
> >
> > Thanks a lot Bill, Infact i am doing something similar but not
> able to get further than there
> > in converting to either BCD or decimal . Here is how i separated
> digits. Value is raw ADC
> > count in Hex.
> >
> >
> > dig1= (value & 0XF000)/0X1000; //isolate dig 1//
> > dig2= (value & 0X0F00)/0X100; //isolate dig 2//
> > dig3= (value & 0X00F0)/0X10; //isolate dig 3//
> > dig4= (value & 0X000F); //isolate dig 4//
> >
> > In the 1st line , i can clearly see 4 for input of 0X4141
> in watch window
> > But then stuck on how to obtain 20480 , equivalent decimal
> for 0X5000 + 2560 for 0X0A00. ...and so on and then mutually add
> them or OR them to obtain 23245 (decimal equivalent of 0X5ACD).
> >
> >
> > Bill Knight wrote:
> > If is BCD to binary, how about this? (I just hacked it
> together and
> > have not run it through a compiler or anything, but it will hopefully
> > give you the idea.)
> >
> > Regards
> > -Bill Knight
> > R O SoftWare
> >
> > unsigned bcd2bin(unsigned bcd)
> > {
> > unsigned resut = 0;
> > unsigned multiplier = 1;
> >
> > while (bcd)
> > {
> > result += ((bcd & 0x0F) * multiplier);
> > bcd >>= 4;
> > multiplier *= 10;
> > }
> >
> > return result;
> > }
> >
> > Jose I Quinones wrote:
> > > Do you mean BCD?
> > >
> > > Data is always stored in binary. Hexadecimal is just a notation of
> four bits
> > > to make it easier to write it down.
> > >
> > > In a way, it really does not matter how the data is stored as
> while the
> > > program is running, why would we care? Unless of course you have
> to display
> > > it!
> > >
> > > What I have done before (this applies to all sorts of programming)
> is deduct
> > > the digits from the number at hand. Have in mind that this
depends on
> > > whether your hex is 8 bit *max of 255) or 16 bits (max of 65535).
> Then you
> > > can see how many times you can subtract a thousand, a hundred, a
> ten, etc.
> > > from the original number and then the respective reminder as you
> go on. Per
> > > example, for the number 234, you can subtract a 100 2 times and
> now you have
> > > 34, from which you can subtract ten three times and now you have
> four. These
> > > would be your three BCD digits. This of course does not sound too
> clean.
> > >
> > > I am thinking there are a bunch of BCD functions out there to
> manipulate the
> > > hex numbers. Also make sure the hex number is not already BCD
> coded in which
> > > case each nibble represents a decimal digit.
> > >
> > > Hope the info helps. Best regards,
> > > JIQ
> > > _____
> > >
> > > From: m... [mailto:m...] On
> Behalf Of
> > > europus
> > > Sent: Monday, July 30, 2007 12:01 PM
> > > To: m...
> > > Subject: [msp430] Anyone did decimal arithmatic on MSP430 in
runtime??
> > >
> > >
> > >
> > > Hi there,
> > > I want to know if anyone converted hex data into decimal
> > > in C compiler ...If yes..how did you manage ? Thanks in
> > > advance..
> >
> >
> >
> >
> >
> >
> > ---------------------------------
> > Ready for the edge of your seat? Check out tonight's top picks on
> Yahoo! TV.
> >
> >
> >
>
>
> ---------------------------------
> Looking for a deal? Find great prices on flights and hotels with
Yahoo! FareChase.
>
>
>

Yahoo! Groups Links
Jugging from Kris' comment,it is obvious that I did not express myself
clearly.

The eventual goal is to display a 16-bit integer on a 5-digit
7-segment LCD in "decimal", not in "hexadecimal". Am I incorrect?

In order to convert a 16-bit integer into 5 decimal-digits, you do not
need to first convert into 4 hexadecimal-digits and then convert those
hexadecimal-digits into decimal-digits. You can do it directly.

In the short code example I posted previously, please note that:

(a) I declared value, dig1, etc. as global variables. This way, the
compiler probably will allocate SRAM at 0x0200, 0x202, etc. This makes
Debugging, Watch Windows, etc. easier. After you debugged it, you can
move the globals to locals to save SRAM usage.
(b) I declared value, digi1, etc. as int. You may want to change them
to unsigned int. Both way work (in their respective domain).
(c) What I called "value" is what you called "full_val" in your
earlier code.
(d) What I called "dig1" etc. are what you called "disp_dig1" etc. in
your earlier code.

Anyway, my code is getting the final decimal-digits directly without
going through the intermediate steps you have in mind. Try it, and
look at the Watch Window to see if dig1, etc. are what you want the
7-segment LCD to display.

--- In m..., "Microbit" wrote:
>
> Yes, but Europus is doing a modulo 16, not 10 :
>
> value = 0x4141; /*** or any other number ***/
> // NOT NEEDED dig1 = value/0x10000;
> dig2 = (value%0x10000)/0x1000;
> dig3 = (value%0x1000)/0x100;
> dig4 = (value%0x100)/0x10;
> dig5 = (value%0x10);
>
> Of course it's much easier to just shift right by the proper number
of nibbles...
> It depends, the modulo lib function might use shift arithmetic when
the modulo is a power of 2.
>
> Best Regards,
> Kris
>
>
> -----Original Message-----
> From: m... [mailto:m...] On
Behalf Of old_cow_yellow
> Sent: Saturday, 11 August 2007 2:31 PM
> To: m...
> Subject: [msp430] Re: Anyone did decimal arithmatic on MSP430 in
runtime??
>
> This will only take a few minuets.
> Try the following code:
>
> int value;
> int dig1, dig2, dig3, dig4, dig5;
> void main( void )
> {
> value = 0x4141; /*** or any other number ***/
> dig1 = value/10000;
> dig2 = (value%10000)/1000;
> dig3 = (value%1000)/100;
> dig4 = (value%100)/10;
> dig5 = (value%10);
> }
>
> Open a Watch Window to show dig1, dig2, dig3, dig4, dig5.
> What do you see? (I see 1, 6, 7, 0, 5 respectively.)
>
> --- In m..., alex ander wrote:
> >
> > No no....see , the idea was to separate the 1's ,10's ,100's and
> 1000's digits in hex first
> > and then multiply each one weighted....i.e suppose let;s take
> 0X4141 example so ....
> > dig1= 4 ,dig2=1 ,dig3 =4 and dig4=1 so now when you multiply
> dig1 by 4096 (decimal)....then it will be 16384..i.e decimal
> equivalent of 0X4000.....then similar for 0X100...
> > then 0X40 and then 0X01.....so the addition should look like
> > 16384 + 256 + 64 + 1 = 16705......
> > But while performing the above task, the compiler hold
> just some random values which do not change. i.e 0X0000030CD..like
> wise. ...
> > I guess what i was not able to get was the compiler is
> handling things differently
> > than a normal GCC C compiler or a calculator....
> > But i guess i will try code at the bottom and let you
> know.....It looks simple enough to
> > work....Thanks for your help old_cow....
> >
> > old_cow_yellow wrote:
> > Let us assume a "value" of 0x4141. The decimal
> representation of this
> > "value" is 16705. Thus the first decimal digit is 1 (not 4). The
> > second decimal digit is 6 (not 1). The third is 7 (not 4). The fourth
> > is 0 (not 1). And there is a fifth decimal digit, which equals 5.
> >
> > The following code will produce the correct decimal digits.
> >
> > dig1 = value / 10000;
> > dig2 = (value % 10000) / 1000;
> > dig3 = (value % 1000) / 100;
> > dig4 = (value % 100) / 10;
> > dig5 = value % 10;
> >
> > --- In m..., alex ander wrote:
> > >
> > > Thanks a lot Bill, Infact i am doing something similar but not
> > able to get further than there
> > > in converting to either BCD or decimal . Here is how i separated
> > digits. Value is raw ADC
> > > count in Hex.
> > >
> > >
> > > dig1= (value & 0XF000)/0X1000; //isolate dig 1//
> > > dig2= (value & 0X0F00)/0X100; //isolate dig 2//
> > > dig3= (value & 0X00F0)/0X10; //isolate dig 3//
> > > dig4= (value & 0X000F); //isolate dig 4//
> > >
> > > In the 1st line , i can clearly see 4 for input of 0X4141
> > in watch window
> > > But then stuck on how to obtain 20480 , equivalent decimal
> > for 0X5000 + 2560 for 0X0A00. ...and so on and then mutually add
> > them or OR them to obtain 23245 (decimal equivalent of 0X5ACD).
> > >
> > >
> > > Bill Knight wrote:
> > > If is BCD to binary, how about this? (I just hacked it
> > together and
> > > have not run it through a compiler or anything, but it will
hopefully
> > > give you the idea.)
> > >
> > > Regards
> > > -Bill Knight
> > > R O SoftWare
> > >
> > > unsigned bcd2bin(unsigned bcd)
> > > {
> > > unsigned resut = 0;
> > > unsigned multiplier = 1;
> > >
> > > while (bcd)
> > > {
> > > result += ((bcd & 0x0F) * multiplier);
> > > bcd >>= 4;
> > > multiplier *= 10;
> > > }
> > >
> > > return result;
> > > }
> > >
> > > Jose I Quinones wrote:
> > > > Do you mean BCD?
> > > >
> > > > Data is always stored in binary. Hexadecimal is just a notation of
> > four bits
> > > > to make it easier to write it down.
> > > >
> > > > In a way, it really does not matter how the data is stored as
> > while the
> > > > program is running, why would we care? Unless of course you have
> > to display
> > > > it!
> > > >
> > > > What I have done before (this applies to all sorts of programming)
> > is deduct
> > > > the digits from the number at hand. Have in mind that this
> depends on
> > > > whether your hex is 8 bit *max of 255) or 16 bits (max of 65535).
> > Then you
> > > > can see how many times you can subtract a thousand, a hundred, a
> > ten, etc.
> > > > from the original number and then the respective reminder as you
> > go on. Per
> > > > example, for the number 234, you can subtract a 100 2 times and
> > now you have
> > > > 34, from which you can subtract ten three times and now you have
> > four. These
> > > > would be your three BCD digits. This of course does not sound too
> > clean.
> > > >
> > > > I am thinking there are a bunch of BCD functions out there to
> > manipulate the
> > > > hex numbers. Also make sure the hex number is not already BCD
> > coded in which
> > > > case each nibble represents a decimal digit.
> > > >
> > > > Hope the info helps. Best regards,
> > > > JIQ
> > > > _____
> > > >
> > > > From: m... [mailto:m...] On
> > Behalf Of
> > > > europus
> > > > Sent: Monday, July 30, 2007 12:01 PM
> > > > To: m...
> > > > Subject: [msp430] Anyone did decimal arithmatic on MSP430 in
> runtime??
> > > >
> > > >
> > > >
> > > > Hi there,
> > > > I want to know if anyone converted hex data into decimal
> > > > in C compiler ...If yes..how did you manage ? Thanks in
> > > > advance..
> > >
> > >
> > >
> > >
> > >
> > >
> > > ---------------------------------
> > > Ready for the edge of your seat? Check out tonight's top picks on
> > Yahoo! TV.
> > >
> > >
> > >
> >
> >
> >
> >
> >
> >
> > ---------------------------------
> > Looking for a deal? Find great prices on flights and hotels with
> Yahoo! FareChase.
> >
> >
> >
>
> Yahoo! Groups Links
>
Hi Lichen,

I hadn't followed the whole thread (I should have), but perhaps the OP did a "180" :-)
You're absolutely right - the subject after all is "decimal arithmetic", not "hexadecimal".
I think the OP's last post (that I replied to) threw me out. He said he wanted hex digits.
Of course I wasn't suggesting to first convert to Hex and then decimal, that would just
be dumb :-)

I don't understand though why you would prefer globals to make debugging easier ?

A decent debugger will display locals just as faithfully (when in scope of course).
I'm used to CrossWorks and surely that would be the Rolls Royce of debuggers when in debug mode.
Anyone on this group that still re-asserts DOS-ish use of tools should get real IMO...
(Have they even tried such a modern IDE, and how extremely powerful this debugger is ??
(or the whole IDE for that matter) ).

Best Regards,
Kris

-----Original Message-----
From: m... [mailto:m...] On Behalf Of old_cow_yellow
Sent: Sunday, 12 August 2007 1:50 AM
To: m...
Subject: [msp430] Re: Anyone did decimal arithmatic on MSP430 in runtime??

Jugging from Kris' comment,it is obvious that I did not express myself
clearly.

The eventual goal is to display a 16-bit integer on a 5-digit
7-segment LCD in "decimal", not in "hexadecimal". Am I incorrect?

In order to convert a 16-bit integer into 5 decimal-digits, you do not
need to first convert into 4 hexadecimal-digits and then convert those
hexadecimal-digits into decimal-digits. You can do it directly.

In the short code example I posted previously, please note that:

(a) I declared value, dig1, etc. as global variables. This way, the
compiler probably will allocate SRAM at 0x0200, 0x202, etc. This makes
Debugging, Watch Windows, etc. easier. After you debugged it, you can
move the globals to locals to save SRAM usage.
(b) I declared value, digi1, etc. as int. You may want to change them
to unsigned int. Both way work (in their respective domain).
(c) What I called "value" is what you called "full_val" in your
earlier code.
(d) What I called "dig1" etc. are what you called "disp_dig1" etc. in
your earlier code.

Anyway, my code is getting the final decimal-digits directly without
going through the intermediate steps you have in mind. Try it, and
look at the Watch Window to see if dig1, etc. are what you want the
7-segment LCD to display.

--- In m..., "Microbit" wrote:
>
> Yes, but Europus is doing a modulo 16, not 10 :
>
> value = 0x4141; /*** or any other number ***/
> // NOT NEEDED dig1 = value/0x10000;
> dig2 = (value%0x10000)/0x1000;
> dig3 = (value%0x1000)/0x100;
> dig4 = (value%0x100)/0x10;
> dig5 = (value%0x10);
>
> Of course it's much easier to just shift right by the proper number
of nibbles...
> It depends, the modulo lib function might use shift arithmetic when
the modulo is a power of 2.
>
> Best Regards,
> Kris
>
>
> -----Original Message-----
> From: m... [mailto:m...] On
Behalf Of old_cow_yellow
> Sent: Saturday, 11 August 2007 2:31 PM
> To: m...
> Subject: [msp430] Re: Anyone did decimal arithmatic on MSP430 in
runtime??
>
> This will only take a few minuets.
> Try the following code:
>
> int value;
> int dig1, dig2, dig3, dig4, dig5;
> void main( void )
> {
> value = 0x4141; /*** or any other number ***/
> dig1 = value/10000;
> dig2 = (value%10000)/1000;
> dig3 = (value%1000)/100;
> dig4 = (value%100)/10;
> dig5 = (value%10);
> }
>
> Open a Watch Window to show dig1, dig2, dig3, dig4, dig5.
> What do you see? (I see 1, 6, 7, 0, 5 respectively.)
>
> --- In m..., alex ander wrote:
> >
> > No no....see , the idea was to separate the 1's ,10's ,100's and
> 1000's digits in hex first
> > and then multiply each one weighted....i.e suppose let;s take
> 0X4141 example so ....
> > dig1= 4 ,dig2=1 ,dig3 =4 and dig4=1 so now when you multiply
> dig1 by 4096 (decimal)....then it will be 16384..i.e decimal
> equivalent of 0X4000.....then similar for 0X100...
> > then 0X40 and then 0X01.....so the addition should look like
> > 16384 + 256 + 64 + 1 = 16705......
> > But while performing the above task, the compiler hold
> just some random values which do not change. i.e 0X0000030CD..like
> wise. ...
> > I guess what i was not able to get was the compiler is
> handling things differently
> > than a normal GCC C compiler or a calculator....
> > But i guess i will try code at the bottom and let you
> know.....It looks simple enough to
> > work....Thanks for your help old_cow....
> >
> > old_cow_yellow wrote:
> > Let us assume a "value" of 0x4141. The decimal
> representation of this
> > "value" is 16705. Thus the first decimal digit is 1 (not 4). The
> > second decimal digit is 6 (not 1). The third is 7 (not 4). The fourth
> > is 0 (not 1). And there is a fifth decimal digit, which equals 5.
> >
> > The following code will produce the correct decimal digits.
> >
> > dig1 = value / 10000;
> > dig2 = (value % 10000) / 1000;
> > dig3 = (value % 1000) / 100;
> > dig4 = (value % 100) / 10;
> > dig5 = value % 10;
> >
> > --- In m..., alex ander wrote:
> > >
> > > Thanks a lot Bill, Infact i am doing something similar but not
> > able to get further than there
> > > in converting to either BCD or decimal . Here is how i separated
> > digits. Value is raw ADC
> > > count in Hex.
> > >
> > >
> > > dig1= (value & 0XF000)/0X1000; //isolate dig 1//
> > > dig2= (value & 0X0F00)/0X100; //isolate dig 2//
> > > dig3= (value & 0X00F0)/0X10; //isolate dig 3//
> > > dig4= (value & 0X000F); //isolate dig 4//
> > >
> > > In the 1st line , i can clearly see 4 for input of 0X4141
> > in watch window
> > > But then stuck on how to obtain 20480 , equivalent decimal
> > for 0X5000 + 2560 for 0X0A00. ...and so on and then mutually add
> > them or OR them to obtain 23245 (decimal equivalent of 0X5ACD).
> > >
> > >
> > > Bill Knight wrote:
> > > If is BCD to binary, how about this? (I just hacked it
> > together and
> > > have not run it through a compiler or anything, but it will
hopefully
> > > give you the idea.)
> > >
> > > Regards
> > > -Bill Knight
> > > R O SoftWare
> > >
> > > unsigned bcd2bin(unsigned bcd)
> > > {
> > > unsigned resut = 0;
> > > unsigned multiplier = 1;
> > >
> > > while (bcd)
> > > {
> > > result += ((bcd & 0x0F) * multiplier);
> > > bcd >>= 4;
> > > multiplier *= 10;
> > > }
> > >
> > > return result;
> > > }
> > >
> > > Jose I Quinones wrote:
> > > > Do you mean BCD?
> > > >
> > > > Data is always stored in binary. Hexadecimal is just a notation of
> > four bits
> > > > to make it easier to write it down.
> > > >
> > > > In a way, it really does not matter how the data is stored as
> > while the
> > > > program is running, why would we care? Unless of course you have
> > to display
> > > > it!
> > > >
> > > > What I have done before (this applies to all sorts of programming)
> > is deduct
> > > > the digits from the number at hand. Have in mind that this
> depends on
> > > > whether your hex is 8 bit *max of 255) or 16 bits (max of 65535).
> > Then you
> > > > can see how many times you can subtract a thousand, a hundred, a
> > ten, etc.
> > > > from the original number and then the respective reminder as you
> > go on. Per
> > > > example, for the number 234, you can subtract a 100 2 times and
> > now you have
> > > > 34, from which you can subtract ten three times and now you have
> > four. These
> > > > would be your three BCD digits. This of course does not sound too
> > clean.
> > > >
> > > > I am thinking there are a bunch of BCD functions out there to
> > manipulate the
> > > > hex numbers. Also make sure the hex number is not already BCD
> > coded in which
> > > > case each nibble represents a decimal digit.
> > > >
> > > > Hope the info helps. Best regards,
> > > > JIQ
> > > > _____
> > > >
> > > > From: m... [mailto:m...] On
> > Behalf Of
> > > > europus
> > > > Sent: Monday, July 30, 2007 12:01 PM
> > > > To: m...
> > > > Subject: [msp430] Anyone did decimal arithmatic on MSP430 in
> runtime??
> > > >
> > > >
> > > >
> > > > Hi there,
> > > > I want to know if anyone converted hex data into decimal
> > > > in C compiler ...If yes..how did you manage ? Thanks in
> > > > advance..
> > >
> > >
> > >
> > >
> > >
> > >
> > > ---------------------------------
> > > Ready for the edge of your seat? Check out tonight's top picks on
> > Yahoo! TV.
> > >
> > >
> > >
> >
> >
> >
> >
> >
> >
> > ---------------------------------
> > Looking for a deal? Find great prices on flights and hotels with
> Yahoo! FareChase.
> >
> >
> >
>
> Yahoo! Groups Links
>

Yahoo! Groups Links
>> Anyone on this group that still re-asserts DOS-ish use of tools should
>> get real

Command line tools are ideal for use with scripts and with Source Code
Control systems, especially with large multi-person multi-year projects.

Command line systems are great for running test harnesses.

Windows based IDEs are ideal for debugging small chunks of code and/or
developing one-man projects.

There is a place for both ways of working.