EmbeddedRelated.com
Forums

Anyone did decimal arithmatic on MSP430 in runtime??

Started by europus July 30, 2007
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

> I want to know if anyone converted hex data into decimal
> in C compiler ...If yes..how did you manage ? Thanks in
> advance..

Most modern C tools will have the classic stdio string conversion
functions that I believe you'll find useful: sscanf and sprintf.

For example, to convert a hex string to a decimal string, do this:

int n;
sscanf(hexString,"%x",&n);
sprintf(decString,"%d",n);

(You fill in the error checking and string storage).

If my answer doesn't satisfy you, then maybe you're wondering about
how a binary value is stored in memory, for example. If that's what
you're asking about, the answer is "It's stored in binary form and
hex and decimal doesn't apply".

JJS
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..
Hi,

I don't want to be fussy or unfriendly ... but ...

1. Didn't someone give you the answer to this in an earlier post?

2. This isn't a "Beginners Guide to C Forum" ... it's specially for the
MSP430 microprocessor.
Maybe you need to buy some C text books and/or find a C training course
and/or a "Learning C" forum?

Also don't forget that Google is your friend!
Google "hex decimal C conversion source code" for a load of helpful web
pages ...

Finally: the BEST way forward is possibly to work with an experienced C
programmer for a few days so he/she can show you the basics.
Your employer / college should be able to provide this support. If not ...
you're working/studying in the wrong place!
----- Original Message -----
From: europus
To: m...
Sent: Monday, July 30, 2007 6:00 PM
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..
Hi Richard,
I am sure now that you are not able to get the depth of the question. If you don;t
like my posting or if you do not have potential to answer them, please look
else where in the forum. This is a public forum consisting of members from
all different types.Juniors, seniors, experienced , unexperienced , new starters
and so on . This is also about mutual help. I do see that you do not have correct
attitude for helping others "politely" and "gently"...I hope i spelled them right...
For last 2 replies , i don;t understand why would you keep beating
around the bush. If you know the answer, please answer or else get to church
Dude !! Like you said, this is MSP430 Forum and not a place for preaching....

"Richard (UK)." wrote:
Hi,

I don't want to be fussy or unfriendly ... but ...

1. Didn't someone give you the answer to this in an earlier post?

2. This isn't a "Beginners Guide to C Forum" ... it's specially for the
MSP430 microprocessor.
Maybe you need to buy some C text books and/or find a C training course
and/or a "Learning C" forum?

Also don't forget that Google is your friend!
Google "hex decimal C conversion source code" for a load of helpful web
pages ...

Finally: the BEST way forward is possibly to work with an experienced C
programmer for a few days so he/she can show you the basics.
Your employer / college should be able to provide this support. If not ...
you're working/studying in the wrong place!

----- Original Message -----
From: europus
To: m...
Sent: Monday, July 30, 2007 6:00 PM
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..

---------------------------------
Got a little couch potato?
Check out fun summer activities for kids.
On 2007-08-03, alex ander wrote:

> Hi Richard,

> I am sure now that you are not able to get the depth of the
> question. If you don;t like my posting or if you do not have
> potential to answer them, please look else where in the forum.
> This is a public forum consisting of members from all
> different types.Juniors, seniors, experienced , unexperienced
> , new starters and so on .

True. However, it is an MSP430 forum, not an into to C
programming forum. Questions about C programming that are not
specific to the MSP430 belong elsewhere.

> This is also about mutual help. I do see that you do not have
> correct attitude for helping others "politely" and
> "gently"...I hope i spelled them right... For last 2 replies ,
> i don;t understand why would you keep beating around the bush.
> If you know the answer, please answer or else get to church
> Dude !! Like you said, this is MSP430 Forum

And Richard's point was that your question had nothing to do
with the MSP430.

--
Grant Edwards grante Yow! NEWARK has been
at REZONED!! DES MOINES has
visi.com been REZONED!!
Thanks Jose, That's i meant. I have another logic which does not work due to compiler
that i am using (Image Craft). What i tried to do is, i tried to AND a 16 bit word in four
steps to extract each hex digit.( ( 0X5ACD & 0XF000)/0x1000) = 5 , .( ( 0X5ACD & 0X0F00)/0x100) = A...and so on. Then i would try to convert these numbers into their respective decimal which does not work with long variables some how :( and compiler
is anyway going to store them in hex . So i was trying to get equivalent BCD digits , store it in Hex and then i.e 5x4096 = 20480....store as 0x20 .This is the part i am not able to get . MSP430 registers are 16 bits so i have to separately store them in 5 variables and then just output them on 7 segment display . I could have used sscanf() but still the long variable shows same values....There is on command in assembly instructions that handles decimal
addition which i am going to try finally....

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

Check out my Robotics , Electronics
, Music ,
Writings and Comic Strip
at www.avayan.com!!!

_____

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..



---------------------------------
Yahoo! oneSearch: Finally, mobile search that gives answers, not web links.
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.
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.
>
>
>
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..
>