rtc_time read_rtc() { u8 second; u8 minute; u8 hour; u8 day; u8 month; u32 year; u32 century = 0; u8 last_second; u8 last_minute; u8 last_hour; u8 last_day; u8 last_month; u32 last_year; u32 last_century = 0; u8 registerB; // Note: This uses the "read registers until you get the same values twice in a row" technique // to avoid getting dodgy/inconsistent values due to RTC updates while (get_update_in_progress_flag()); // Make sure an update isn't in progress second = get_RTC_register(0x00); minute = get_RTC_register(0x02); hour = get_RTC_register(0x04); day = get_RTC_register(0x07); month = get_RTC_register(0x08); year = get_RTC_register(0x09); if(century_register != 0) { century = get_RTC_register(century_register); } do { last_second = second; last_minute = minute; last_hour = hour; last_day = day; last_month = month; last_year = year; last_century = century; while (get_update_in_progress_flag()); // Make sure an update isn't in progress second = get_RTC_register(0x00); minute = get_RTC_register(0x02); hour = get_RTC_register(0x04); day = get_RTC_register(0x07); month = get_RTC_register(0x08); year = get_RTC_register(0x09); if(century_register != 0) { century = get_RTC_register(century_register); } } while( (last_second != second) || (last_minute != minute) || (last_hour != hour) || (last_day != day) || (last_month != month) || (last_year != year) || (last_century != century) ); registerB = get_RTC_register(0x0B); // Convert BCD to binary values if necessary kputs("year = "); printHex(year); kputs("\n"); kputs("century = "); printHex(century); kputs("\n"); kputs("century_register = "); printHex(century_register); kputs("\n"); kputs("registerB = "); printHex(registerB); kputs("\n"); if (!(registerB & 0x04)) { second = (second & 0x0F) + ((second / 16) * 10); minute = (minute & 0x0F) + ((minute / 16) * 10); hour = ( (hour & 0x0F) + (((hour & 0x70) / 16) * 10) ) | (hour & 0x80); day = (day & 0x0F) + ((day / 16) * 10); month = (month & 0x0F) + ((month / 16) * 10); // 0x16 -> 0x06 + 10 year = (year & 0x0F) + ((year / 16) * 10); if(century_register != 0) { century = (century & 0x0F) + ((century / 16) * 10); } } // Convert 12 hour clock to 24 hour clock if necessary kputs("hour(before 24hr check) = "); printInt(hour); kputs("\n"); if (!(registerB & 0x02) && (hour & 0x80)) { hour = ((hour & 0x7F) + 12) % 24; } // Calculate the full (4-digit) year if(century_register != 0) { year += century * 100; kputs("century_register != ZERO!"); } else { year += (kCurrentYear / 100) * 100; if(year < kCurrentYear) year += 100; } rtc_time ret; ret.year = year; ret.month = month; ret.day = day; ret.hour = hour; ret.minute = minute; ret.second = second; return ret; }
void read_rtc() { unsigned char century=21; unsigned char last_second; unsigned char last_minute; unsigned char last_hour; unsigned char last_day; unsigned char last_month; unsigned char last_year; unsigned char last_century; unsigned char registerB; while (get_update_in_progress_flag()); second = get_RTC_register(0x00); minute = get_RTC_register(0x02); hour = get_RTC_register(0x04); day = get_RTC_register(0x07); month = get_RTC_register(0x08); year = get_RTC_register(0x09); if(century_register != 0) { century = get_RTC_register(century_register); } do { last_second = second; last_minute = minute; last_hour = hour; last_day = day; last_month = month; last_year = year; last_century = century; while (get_update_in_progress_flag()); second = get_RTC_register(0x00); minute = get_RTC_register(0x02); hour = get_RTC_register(0x04); day = get_RTC_register(0x07); month = get_RTC_register(0x08); year = get_RTC_register(0x09); if(century_register != 0) { century = get_RTC_register(century_register); } } while( (last_second != second) || (last_minute != minute) || (last_hour != hour) || (last_day != day) || (last_month != month) || (last_year != year) || (last_century != century) ); registerB = get_RTC_register(0x0B); if (!(registerB & 0x04)) { second = (second & 0x0F) + ((second / 16) * 10); minute = (minute & 0x0F) + ((minute / 16) * 10); hour = ( (hour & 0x0F) + (((hour & 0x70) / 16) * 10) ) | (hour & 0x80); day = (day & 0x0F) + ((day / 16) * 10); month = (month & 0x0F) + ((month / 16) * 10); year = (year & 0x0F) + ((year / 16) * 10); if(century_register != 0) { century = (century & 0x0F) + ((century / 16) * 10); } } if (!(registerB & 0x02) && (hour & 0x80)) { hour = ((hour & 0x7F) + 12) % 24; } if(century_register != 0) { year += century * 100; } else { year += (CURRENT_CENTURY-1) * 100; } }
void read_rtc(unsigned char *h, unsigned char *m, unsigned char *s, unsigned char *dy, unsigned char *mo, unsigned char *yr, unsigned char *cr) { unsigned char century; unsigned char last_second; unsigned char last_minute; unsigned char last_hour; unsigned char last_day; unsigned char last_month; unsigned char last_year; unsigned char last_century; unsigned char registerB; // Note: This uses the "read registers until you get the same values twice in a row" technique // to avoid getting dodgy/inconsistent values due to RTC updates while (get_update_in_progress_flag()); // Make sure an update isn't in progress second = get_RTC_register(0x00); minute = get_RTC_register(0x02); hour = get_RTC_register(0x04); day = get_RTC_register(0x07); month = get_RTC_register(0x08); year = get_RTC_register(0x09); if(century_register != 0) { century = get_RTC_register(century_register); } do { last_second = second; last_minute = minute; last_hour = hour; last_day = day; last_month = month; last_year = year; last_century = century; while (get_update_in_progress_flag()); // Make sure an update isn't in progress second = get_RTC_register(0x00); minute = get_RTC_register(0x02); hour = get_RTC_register(0x04); day = get_RTC_register(0x07); month = get_RTC_register(0x08); year = get_RTC_register(0x09); if(century_register != 0) { century = get_RTC_register(century_register); } } while( (last_second != second) || (last_minute != minute) || (last_hour != hour) || (last_day != day) || (last_month != month) || (last_year != year) || (last_century != century) ); registerB = get_RTC_register(0x0B); // Convert BCD to binary values if necessary if (!(registerB & 0x04)) { second = (second & 0x0F) + ((second / 16) * 10); minute = (minute & 0x0F) + ((minute / 16) * 10); hour = ( (hour & 0x0F) + (((hour & 0x70) / 16) * 10) ) | (hour & 0x80); day = (day & 0x0F) + ((day / 16) * 10); month = (month & 0x0F) + ((month / 16) * 10); year = (year & 0x0F) + ((year / 16) * 10); if(century_register != 0) { century = (century & 0x0F) + ((century / 16) * 10); century_r = century; // guardo el valor del century } } // Convert 12 hour clock to 24 hour clock if necessary if (!(registerB & 0x02) && (hour & 0x80)) { hour = ((hour & 0x7F) + 12) % 24; } // Calculate the full (4-digit) year // if(century_register != 0) { // year += century * 100; // } else { // year += (CURRENT_YEAR / 100) * 100; // if(year < CURRENT_YEAR) year += 100; // } *h = hour; *m = minute; *s = second; *mo = month; *dy = day; *yr = year; *cr = century_r; }