Continue to Site

# Expalanation Needed about this code

Status
Not open for further replies.

#### internetuser2k12

##### Banned
Hello!

I have a code for BCD to Decimal conversion of date and time

Code:
        *sec  =  ((*sec & 0x70) >> 4)*10 + (*sec & 0x0F);
*min  =  ((*min & 0xF0) >> 4)*10 + (*min & 0x0F);
*hr   =  ((*hr & 0x30) >> 4)*10 + (*hr & 0x0F);
*week_day =(*week_day & 0x07);
*day  =  ((*day & 0xF0) >> 4)*10 + (*day & 0x0F);
*mn   =  ((*mn & 0x10) >> 4)*10 + (*mn & 0x0F);
*year =  ((*year & 0xF0)>>4)*10+(*year & 0x0F);

sec and minutes both vary from decimal 0 to 59 and bcd 0x00 to 0x59. Then why is the formula for sec and time bcd to decimal transformation is different?

For seconds it is *sec = ((*sec & 0x70 ) >> 4)*10 + (*sec & 0x0F);

For minutes it is *min = ((*min & 0xF0 ) >> 4)*10 + (*min & 0x0F);

1. Don't start multiple therads on the same topic.
2. The necessity of masking out some RTC register bits (e.g. seconds.bit7) becomes obvious if you read the datasheet.

OK. I will read the data sheet.

Status
Not open for further replies.