We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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.