CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

shift left right
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kmp84



Joined: 02 Feb 2010
Posts: 345

View user's profile Send private message

PostPosted: Thu Aug 02, 2018 7:27 am     Reply with quote

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: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Aug 02, 2018 7:54 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Aug 02, 2018 8:31 am     Reply with quote

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: 19195

View user's profile Send private message

PostPosted: Thu Aug 02, 2018 8:35 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Aug 02, 2018 9:27 am     Reply with quote

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



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Aug 02, 2018 12:36 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Aug 03, 2018 12:19 pm     Reply with quote

Thanks mr.Ttelmah, Smile

Best Regards!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group