View previous topic :: View next topic |
Author |
Message |
Fabri
Joined: 22 Aug 2005 Posts: 275
|
bcd Conversion of 16bit number |
Posted: Wed Feb 01, 2006 4:17 am |
|
|
Hi Everibody,
I need to convert 16 bit hex number into BCD 5 digit.
Is there any function in CCS compiler to do it ?
Thanks for support,
Regards, |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed Feb 01, 2006 5:00 am |
|
|
A 16bit number, will only correspond to 4BCD characters, not five. The 'point' about BCD, is that it uses four bits to represent a single digit.
There have been library routines published here in the past (I have posted some, and have seen others). The DS1302.c driver in the standard library, also contains such a routine. These handle single bytes, but a call to 'make8', will allow a 16bitvalue to be handled as well.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Fabri
Joined: 22 Aug 2005 Posts: 275
|
|
Posted: Wed Feb 01, 2006 6:46 am |
|
|
Hi,
I need to convert an 16 bit hex number , for example 0xffff, to decimal 5 digit as 6 5 5 3 5.
In past I wrote an assembler routine for motorola HC11. Have you got any suggestion to do that with CCS compiler ?
Regards, |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Wed Feb 01, 2006 4:28 pm |
|
|
Just print it!....
printf("%5LD",val);
Will output the five ascii text digits to the current com device. Using sprintf instead, will allow these to be sent to a string. This is base 'C', nothing special to CCS.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed Feb 01, 2006 11:40 pm |
|
|
Quote: |
I need to convert 16 bit hex number into BCD 5 digit.
|
The question itself has a conceptual mistake as well pointed by RJ. Binary Coded Decimal (BCD)
is a coding system in which each decimal digit from 0 to 9 is represented by a 4-digit binary number.
Also called packed decimal, this is the representation of a number by using four binary bits
numbers.
So the number 29 would be encoded as 0010 1001 instead of its binary equivalent 0001 1101
Guest wrote:
Quote: |
Just print it!....
printf("%5LD",val);
|
The bin/dec convertion is made ONLY while outputing a char to console, it is not available for further use.
Fabri wrote:
Quote: |
Is there any function in CCS compiler to do it ?
|
There is not any CCS function to do this convertion.
Fabri wrote:
Quote: |
I need to convert an 16 bit hex number , for example 0xffff, to decimal 5 digit as 6 5 5 3 5.
|
I wrote the following code to use in a display:
Code: |
long Dig_0, Dig_1, Dig_2, Dig_3, Dig_4;
void bin_to_Dig(long bin_inp) // 0x1234
{
long temp;
temp = bin_inp;
Dig_4 = temp/10000;
temp = temp - (Dig_4 * 10000);
Dig_3 = temp/1000;
temp = temp - (Dig_3 * 1000);
Dig_2 = temp/100;
temp = temp - (Dig_2 * 100);
Dig_1 = temp/10;
temp = temp - (Dig_1 * 10);
Dig_0 = temp;
}
|
Output results: Code: |
Binary Inp Display Out
0x0064 00100
0x00AA 00170
0x0100 00256
0x1000 04096
0x3FFF 16383
0xFFFF 65535
|
Humberto |
|
![](templates/subSilver/images/spacer.gif) |
Fabri
Joined: 22 Aug 2005 Posts: 275
|
|
Posted: Thu Feb 02, 2006 1:28 am |
|
|
Hi,
I decided to use the way of sprintf to a string and then I convert all character to number.
Thanks for help,
Regards |
|
![](templates/subSilver/images/spacer.gif) |
Eugeneo
Joined: 30 Aug 2005 Posts: 155 Location: Calgary, AB
|
Re: bcd Conversion of 16bit number |
Posted: Thu Feb 02, 2006 1:38 am |
|
|
Fabri wrote: | Hi Everibody,
I need to convert 16 bit hex number into BCD 5 digit.
Regards, |
I suspect your looking for a 5 digit non packed(4 digit) bcd
try this
char bcd_string[5];
sprintf(bcd_string,"%lu",16_bit_number)
now each element of bcd_string contains the 10^0 to 10^4 |
|
![](templates/subSilver/images/spacer.gif) |
Fabri
Joined: 22 Aug 2005 Posts: 275
|
|
Posted: Thu Feb 02, 2006 1:51 am |
|
|
Hi Eugeneo,
I did so,
Regards, |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Thu Feb 02, 2006 3:44 am |
|
|
One caveat.
The string storage area, should be _six_ characters long, not the five being shown above. Remember in C, 'strings' are just arrays of bytes, with a '0' terminator (numerical zero, not ASCII '0'). To store a five character string, you must have six available spaces, or the terminator will overwrite something else....
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Thu Feb 02, 2006 3:44 am |
|
|
One caveat.
The string storage area, should be _six_ characters long, not the five being shown above. Remember in C, 'strings' are just arrays of bytes, with a '0' terminator (numerical zero, not ASCII '0'). To store a five character string, you must have six available spaces, or the terminator will overwrite something else....
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Fabri
Joined: 22 Aug 2005 Posts: 275
|
|
Posted: Thu Feb 02, 2006 3:52 am |
|
|
Hi,
Of course, I use 5 digit with a string of 6 char.
Thanks, |
|
![](templates/subSilver/images/spacer.gif) |
Eugeneo
Joined: 30 Aug 2005 Posts: 155 Location: Calgary, AB
|
|
Posted: Thu Feb 02, 2006 11:23 pm |
|
|
Thats right. I would have had the screwup fairy visit me on that one. Most string operations including sprintf terminate with a null.
Just for argument sake, would the null be inserted in next compiler assigned memory location? or would sprintf be linked to the upper bound of the char definition and null terminate premature? |
|
![](templates/subSilver/images/spacer.gif) |
|