View previous topic :: View next topic |
Author |
Message |
kmp84
Joined: 02 Feb 2010 Posts: 345
|
|
Posted: Thu Aug 02, 2018 7:27 am |
|
|
Ttelmah wrote: |
Have a table of all the values corresponding to BCD digits 1000, 100, 10, and starting with the largest just perform repeated subtractions, if the value tests as bigger than the digiit
|
Any example mr.TT? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9121 Location: Greensville,Ontario
|
|
Posted: Thu Aug 02, 2018 7:54 am |
|
|
You should just use Google ! There are 1000s, of 'hits' about it and it's easy to convert into 'C'. Motorola has/had a great app note about doing it, course that was using a 6800 processor about 40 year ago.
It's probably a 1st year college software assignment, maybe check those kinds of websites ? |
|
|
kmp84
Joined: 02 Feb 2010 Posts: 345
|
|
Posted: Thu Aug 02, 2018 8:31 am |
|
|
temtronic wrote: |
It's probably a 1st year college software assignment, maybe check those kinds of websites ? |
Mr."tt" Can you post 2nd year college example?
Best Regards! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19238
|
|
Posted: Thu Aug 02, 2018 8:35 am |
|
|
It's the most basic conversion method. I'm not sure what order you actually want the digits, so putting MSB at the end:
Code: |
const unsigned int32 digits[9] = {1000000000, 100000000, 10000000, \
1000000, 100000, 10000, 1000, 100, 10 };
void topackedBCD(unsigned int32 value)
{
signed int8 position;
unsigned int8 result[5] = {0,0,0,0,0};
unsigned int8 mask = 0x10;
unsigned int32 temp;
for(position=0;position<9;position++) //9 digits
{
temp=digits[position];
while (value>=temp)
{
result[(unsigned int8)(9-position)/2]+=mask;//output
value-=temp;
}
if (mask==0x10) //swap nibble for output
mask=1;
else
mask=0x10;
}
result[0] |=value; //last digit.
memcpy(OutBcdBuff, result, 5);
}
|
This will work on just about any processor.
Last edited by Ttelmah on Fri Aug 03, 2018 1:32 am; edited 1 time in total |
|
|
kmp84
Joined: 02 Feb 2010 Posts: 345
|
|
Posted: Thu Aug 02, 2018 9:27 am |
|
|
Hi mr. Ttelmah,
Bytes order that I have to use is the same :
12345UL = {0x45,0x23, 0x01}; It's OK! But this example does't work correct with value 10,100,1000 etc.
Maybe I have to rename topic to bin/bcd conversion.. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19238
|
|
Posted: Thu Aug 02, 2018 12:36 pm |
|
|
A one character mistake typing it:
while (value>temp)
needs to be
while (value>=temp)
(Have updated this in the original file)
You problem is not normal bin to BCD, since a 'binary coded decimal', for ASCII or similar output will use a byte for each digit. You want 'packed' BCD, where the digits have been put into nibbles. This is what is used by things like clock chips. These though normally only need 8bit or 16bit values, not 32bits. |
|
|
kmp84
Joined: 02 Feb 2010 Posts: 345
|
|
Posted: Fri Aug 03, 2018 12:19 pm |
|
|
Thanks mr.Ttelmah,
Best Regards! |
|
|
|