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 CCS Technical Support

Conversion fromBCD(x)

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








Conversion fromBCD(x)
PostPosted: Tue Mar 22, 2005 9:27 am     Reply with quote

Hello !

Could somone explain me in details what these lines means, please ?
It is supposed to be useful for a conversion on a DS1306.
I works very well, but i would like to understand it.

Many Thanks !


Code:

const unsigned int conv[16] = {
0,10,20,30,40,50,60,70,80,90,90,90,90,90,90,90 };

#define fromBCD(x) ((x & 0xf)+conv[(x>>4)])
unsigned int toBCD(unsigned int val)
{
int ctr;
for (ctr=0;ctr<10;ctr++)
{
if (val == conv[ctr])
return(ctr<<4);
else if (val < conv[ctr]) break;
}
--ctr;
return((val-conv[ctr])+(ctr<<4));
}
Ttelmah
Guest







PostPosted: Tue Mar 22, 2005 10:44 am     Reply with quote

I recognise that code. :-)
Two seperate functions. One to convert a binary number to BCD, and one to convert a BCD number to normal binary.
BCD, uses the top 'nibble' of the binary value to represent the tens, and the bottom nibble to represent the units. Hence to convert 'from' BCD, you want the top nibble (val & 0xF0) divided by 16, times ten, plus the value of the bottom nibble.
So the number '0x89', has to become '89'.
Now fortunately the shift right operation, (>>4), gets rid of the bottom bits as it shifts, and shifts '0' into the top bit (so you don't need to mask with 0xF0). So the line 'fromBCD', takes the value shifted right four times (which is the same as val & 0xF0 divided by 16), and multiplies this by ten, by using the value from the array. This is slightly quicker than performing the actual multiplication, and allows 'illegal' values (greater than 9), to be clipped, and then adds the low nibble (val & 0xF).
Hence with the '0x89' example, you get the total of '80', plus '9', giving the required '89'.
toBCD, has to do the opposite, It does this by comparing the value with 10, 20, 30, etc., from the array, then if it matches, simply returning the index in the array*16 as the BCD number. If the value is below the compared value (so for instance, it is 89, and the comparison gets to 90), then it reduces the index by 1 (we are in the eighties), subtracts the array value from the source (giving us '9'), and returns the array index (8) * 16, plus the remainder (9), giving '0x89'.
There are literally dozens of ways of coding this, the particular version has the advantage of being quick on the PIC 16 chips.

Best Wishes
Guest








PostPosted: Tue Mar 29, 2005 9:28 am     Reply with quote

Thanks a lot that really helps me !
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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