I am trying to convert a decimal value to the corresponding BCD code. However, this seems not to be working at all, because only gives the 'tens' part and the 'unit' is lost. Such as, 27 only return 2.
I want the result into a 8-bit array (the result will not be greater than 99)...
This is kinda crude, but could be a way to do it given the assumption above:
x = 0
stop = 0
while (x<10 and stop = 0){
value = value - x*10 // reduce by 10, keep count, stop when value is <10
if (value < 10){
upperbyte = x
stop = 1}
else {x=x+1}
}
lowerbyte = (value, HEX)
lowerbyte = value & 0x0F // AND forces upper bits low, leaves hex value in lower nibble
upperbyte = x<<4 // moves the found nibble into the upper bits
newbyte = upperbyte | lowerbyte // OR them together
CrossRoads:
This is kinda crude, but could be a way to do it given the assumption above...
I added that code, but for some reason is not showing anything on the Serial Monitor?
int x = 0;
int sstop = 0;
byte upperbyte;
byte lowerbyte;
byte newbyte;
while (x<10 and sstop == 0)
{
DecResult = DecResult - x*10; // reduce by 10, keep count, stop when value is <10
if (DecResult < 10)
{
upperbyte = x;
sstop = 1;
}
else
{
x=x+1;
}
}
lowerbyte = DecResult & 0x0F; // AND forces upper bits low, leaves hex value in lower nibble
upperbyte = x<<4; // moves the found nibble into the upper bits
newbyte = upperbyte | lowerbyte; // OR them together
Serial.print("Result: ");
Serial.println(newbyte, BYTE);
CrossRoads:
I gotta get to bed.
Try putting some ()s in the while test:
while ((x<10) && (sstop == 0)){
Concept seems straight forward tho, yes? Keep playing with the code, you'll get there.
G'night.
You give me the idea. I made this way and works perfect. I will improve it tomorrow.
Not sure I understand the question. Do you mean your decimal interpretation of a value in a register? ie does "15" =0x0F? isn't the example "27" stored internally as 0x1B? if so then what you want is Binary to BCD. Here is my take on using the ADD3 algorithm for converting binary to BCD. I only needed two BCD digits but it can be easily expanded.
//-------------- binary to BCD using add3 algorithm -----------
byte binaryToBcd(byte binSource){
byte bcdResult=0;
byte tempA=0;
byte tempB=0;
byte tempC=0;
for (int y=0;y<8;y++){
tempC=binSource & B10000000;
binSource=binSource<<1;
bcdResult=bcdResult<<1;
if(tempC>0){
bcdResult=bcdResult | B00000001;
}
if (y<7){ //don't check or do add 3 on last shift
tempA=bcdResult & B00001111; //check lo nibble
if (tempA >= 5) {
bcdResult=bcdResult+3;
}
tempB=bcdResult&B11110000;
tempB=tempB>>4; // check hi nibble
if(tempB>=5) {
tempB=tempB+3;
}
tempB=tempB<<4;
tempA=bcdResult & B00001111;
bcdResult= tempB | tempA ; // reassemble nibbles
}
}
return bcdResult;
/* Encode the input number into BCD into the output buffer, of
* the specified length. The BCD encoded number is right-justified
* in the field. Return the number of digits converted, or -1 if the
* buffer was not big enough for the whole conversion.
*/
int bcd_encode(unsigned long number, unsigned char *buffer, int length)
{
unsigned char *p;
unsigned char n, m, bval, digit;
n = 0; /* nibble count */
m = 0; /* converted digit count */
bval = 0; /* the bcd encoded value */
/* Start from the righthand end of the buffer and work
* backwards
*/
p = buffer + length - 1;
while (p >= buffer) {
if (number != 0) {
digit = number % 10;
number = number / 10;
m++;
} else
digit = 0;
/* If we have an odd-numbered digit position
* then save the byte and move to the next buffer
* position. Otherwise go convert another digit
*/
if (n & 1) {
bval |= digit << 4;
*p-- = bval;
bval = 0;
} else
bval = digit;
n++;
}
/* If number is not zero, then we have run out of room
* and the conversion didn't fit. Return -1;
*/
if (number)
return(-1);
/* return the number of converted digits
*/
return(m);
}
Not guaranteed to be super fast -- it does do long division. But it should work.