Assuming it's packed BCD in an integer then the following should work,
Code:
unsigned int BCDtoI(unsigned int BCD){
unsigned int result;
result=(BCD>>12)*1000;
result+=((BCD>>8)&0x0f)*100;
result+=((BCD>>4)&0x0f)*10;
result+=((BCD)&0x0f);
return result;
}
Assuming it's packed BCD in an integer then the following should work,
Code:
unsigned int BCDtoI(unsigned int BCD){
unsigned int result;
result=(BCD>>12)*1000;
result+=((BCD>>8)&0x0f)*100;
result+=((BCD>>4)&0x0f)*10;
result+=((BCD)&0x0f);
return result;
}
All the code is doing is getting the relevant digit and multiplying by the decimal value. So, BCD>>12 will get the thousands digit which is then multiplied by 1000.
The way to convert a BCD number to it's binary equivalent is to divide the high digit by 16 and then multiply by 10. Another way to do the same thing is to subtract the high digit * 6 (16-10). To do this in code you do BCD>>4 to get the high digit and then multiply by 6. As 6 is 3*2 then you can reduce the number of shifts by 1 and multiply by 3. This is just the same as saying that BCD/16*6 is the same as BCD/8*3.
The less efficient (but more logical) way to write the same thing is,
bcd-=((bcd&0xf0)>>4)*6;
Ha, was just looking at the 18F data sheet instructions. The assembler instruction, DAW, looks custom fit for the BCD to BIN decode. Would this do the trick?
Code:
;Decode say a DS1307 minutes from BCD to binary
;minutes = 0x27 BCD
call FN_READ8BIT
movff READ8BIT,MIN
movf MIN,W,BANKED
daw
movwf MIN,BANKED
;MIN is now 27 minutes in binary?