ASCII to BCD conversion

I am trying to use the following code to convert 2 digit ASCII numbers to BCD number, for setting RTC Clock DS1307.


unsigned char ASCII_TO_BCD(unsigned char ascii_text[2])
{
        unsigned char bcd_value;
        ascii_text[0] &= 0x0F;     // set high nibble(3) to 0 (may not be needed)
        ascii_text[0] <<= 4;       // shift lower nibble to higher

        ascii_text[1] &= 0x0F;     // set high nibble(3) to 0 (may not be needed)
        bcd_value = ascii_text[0] | ascii_text[1];  // mix both to get BCD
        return bcd_value;
}

When I pass '12' to the function, 0x33 is returned instead of 0x12. Is something wrong in this code? Thanks.

Parents
  • try this.

    unsigned char ASCII_TO_BCD(unsigned char ascii_text[2])
            {
            unsigned char bcd_value;
    
            // left side
            if(ascii_text[0] >= '0' && ascii_text[0] <= '9')  // 0-9 range
                    {
                    bcd_value = ( ascii_text[0] - 48)  << 4 ; // 48 for '0' ASCII offset
                    }
            else if (ascii_text[0] >= 'A' && ascii_text[0] <= 'F') // A-F range
                    {
                    bcd_value = ( 10 + ascii_text[0] - 65 )  << 4 ; // 65 for 'A' ASCII offset
                    }
            else if (ascii_text[0] >= 'a' && ascii_text[0] <= 'f') // a-f range
                    {
                    bcd_value = ( 10 + ascii_text[0] - 97)  << 4 ; // 97 for 'a'  ASCII offset
                    }
    
            // right side
            if(ascii_text[1] >= '0' && ascii_text[1] <= '9')  // 0-9 range
                    {
                    bcd_value |= ( ascii_text[1] - 48); // 48 for '0' ASCII offset
                    }
            else if (ascii_text[1] >= 'A' && ascii_text[1] <= 'F') // A-F range
                    {
                    bcd_value |= ( 10 + ascii_text[1] - 65)   ; // 65 for 'A' ASCII offset
                    }
            else if (ascii_text[1] >= 'a' && ascii_text[1] <= 'f') // a-f range
                    {
                    bcd_value |= ( 10 + ascii_text[1] - 97 ) ; // 97 for 'a' ASCII offset
                    }
    
            return bcd_value;
            }
    

Reply
  • try this.

    unsigned char ASCII_TO_BCD(unsigned char ascii_text[2])
            {
            unsigned char bcd_value;
    
            // left side
            if(ascii_text[0] >= '0' && ascii_text[0] <= '9')  // 0-9 range
                    {
                    bcd_value = ( ascii_text[0] - 48)  << 4 ; // 48 for '0' ASCII offset
                    }
            else if (ascii_text[0] >= 'A' && ascii_text[0] <= 'F') // A-F range
                    {
                    bcd_value = ( 10 + ascii_text[0] - 65 )  << 4 ; // 65 for 'A' ASCII offset
                    }
            else if (ascii_text[0] >= 'a' && ascii_text[0] <= 'f') // a-f range
                    {
                    bcd_value = ( 10 + ascii_text[0] - 97)  << 4 ; // 97 for 'a'  ASCII offset
                    }
    
            // right side
            if(ascii_text[1] >= '0' && ascii_text[1] <= '9')  // 0-9 range
                    {
                    bcd_value |= ( ascii_text[1] - 48); // 48 for '0' ASCII offset
                    }
            else if (ascii_text[1] >= 'A' && ascii_text[1] <= 'F') // A-F range
                    {
                    bcd_value |= ( 10 + ascii_text[1] - 65)   ; // 65 for 'A' ASCII offset
                    }
            else if (ascii_text[1] >= 'a' && ascii_text[1] <= 'f') // a-f range
                    {
                    bcd_value |= ( 10 + ascii_text[1] - 97 ) ; // 97 for 'a' ASCII offset
                    }
    
            return bcd_value;
            }
    

Children
More questions in this forum