41 #include <util/delay.h>
42 #include <avr/interrupt.h>
43 #include <avr/pgmspace.h>
53 #if RTC_LCD_DEBUG == 1 || RTC_LCD_TIMEDEBUG == 1
57 #if RTC_SAA1064_OUTPUT == 1
59 #endif // RTC_SAA1064_OUTPUT
70 #warning "F_CPU was not defined! It will now be set to 20000000UL."
71 #define F_CPU 20000000UL
92 #if RTC_LCD_DEBUG == 1 || RTC_LCD_TIMEDEBUG == 1
98 static const char slave_mode_error_MSG[]
PROGMEM =
"INTERNAL ERROR (SLA)";
99 static const char clear_line_MSG[]
PROGMEM =
" ";
162 #if RTC_SAA1064_OUTPUT == 1
165 #endif // RTC_SAA1064_OUTPUT
169 static real_time_data real_time;
174 #if RTC_LCD_DEBUG == 1
175 uint8_t timer_value_start;
177 uint8_t i2c_retry_counter;
179 uint8_t prev_dcf77_state = DCF77_STARTSYNC_SEARCH01;
182 uint8_t return_value;
184 #if RTC_LCD_DEBUG == 1 || RTC_LCD_TIMEDEBUG == 1
185 static char tmp_char[40];
204 #if RTC_LCD_DEBUG == 1 || RTC_LCD_TIMEDEBUG == 1
220 ACSR = ACSR | (1 << ACBG);
221 ACSR = ACSR & ~(1 << ACD) & ~(1 << ACIE) & ~(1 << ACIC);
237 #if RTC_LCD_DEBUG == 1 || RTC_LCD_TIMEDEBUG == 1
248 TIMSK0 = TIMSK0 | (1 << OCIE0A);
266 TWCR = (1 << TWEN) | (1 << TWEA) | (1 << TWIE) | (1 << TWINT);
278 real_time.current_hour = 0;
279 real_time.current_minute = 0;
280 real_time.current_second = 0;
281 real_time.prev_second = 0;
283 real_time.current_year = 0;
284 real_time.current_month = 0;
285 real_time.current_day = 0;
286 real_time.current_weekday = 0;
309 #if RTC_LCD_DEBUG == 1 || RTC_LCD_TIMEDEBUG == 1
325 #if RTC_SAA1064_OUTPUT == 1
328 #endif // RTC_SAA1064_OUTPUT
347 #if RTC_LCD_DEBUG == 1 || RTC_LCD_TIMEDEBUG == 1
365 #if RTC_LCD_DEBUG == 1
367 timer_value_start = TCNT0;
383 real_time.current_second =
ctrl_data[2] & 0x3f;
384 real_time.current_minute = (
ctrl_data[2] >> 6) & 0x03;
385 real_time.current_minute = real_time.current_minute | ((
ctrl_data[3] & 0x0f) << 2);
386 real_time.current_month = (
ctrl_data[3] >> 4) & 0x0f;
387 real_time.current_hour =
ctrl_data[4] & 0x1f;
388 real_time.current_weekday = (
ctrl_data[4] >> 5) & 0x07;
389 real_time.current_day =
ctrl_data[5] & 0x1f;
390 real_time.current_year =
ctrl_data[6] & 0x7f;
395 rtc_data[2] = ((real_time.current_second / 10) << 4) & 0x7f;
396 rtc_data[2] = rtc_data[2] | ((real_time.current_second - ((real_time.current_second / 10) * 10)) & 0x0f);
397 rtc_data[3] = (real_time.current_minute / 10) << 4;
398 rtc_data[3] = rtc_data[3] | ((real_time.current_minute - ((real_time.current_minute / 10) * 10)) & 0x0f);
399 rtc_data[4] = (real_time.current_hour / 10) << 4;
400 rtc_data[4] = rtc_data[4] | ((real_time.current_hour - ((real_time.current_hour / 10) * 10)) & 0x0f);
401 rtc_data[5] = real_time.current_weekday & 0x0f;
402 rtc_data[6] = (real_time.current_day / 10) << 4;
403 rtc_data[6] = rtc_data[6] | ((real_time.current_day - ((real_time.current_day / 10) * 10)) & 0x0f);
404 rtc_data[7] = (real_time.current_month / 10) << 4;
405 rtc_data[7] = rtc_data[7] | ((real_time.current_month - ((real_time.current_month / 10) * 10)) & 0x0f);
406 rtc_data[8] = (real_time.current_year / 10) << 4;
407 rtc_data[8] = rtc_data[8] | ((real_time.current_year - ((real_time.current_year / 10) * 10)) & 0x0f);
412 while (return_value !=
I2C_IO_SUCC && i2c_retry_counter > 0) {
421 #if RTC_LCD_DEBUG == 1
469 if (((real_time.current_minute %
DCF77_UPDATE_CYCLE) == 0) && (real_time.current_second == 0)) {
476 if (dcf77_config.
dcf_state != DCF77_TIMESYNC_COMPLETED) {
481 if (dcf77_config.
dcf_state == DCF77_STARTSYNC_COMPLETED) {
494 if (dcf77_config.
dcf_state == DCF77_MINUTE_COMPLETED) {
510 rtc_data[2] = (
dcf77_time.current_minute / 10) << 4;
511 rtc_data[2] = rtc_data[2] | ((
dcf77_time.current_minute - ((
dcf77_time.current_minute / 10) * 10)) & 0x0f);
516 while (return_value !=
I2C_IO_SUCC && i2c_retry_counter > 0) {
530 if (dcf77_config.
dcf_state == DCF77_HOUR_COMPLETED) {
536 rtc_data[2] = (
dcf77_time.current_hour / 10) << 4;
537 rtc_data[2] = rtc_data[2] | ((
dcf77_time.current_hour - ((
dcf77_time.current_hour / 10) * 10)) & 0x0f);
542 while (return_value !=
I2C_IO_SUCC && i2c_retry_counter > 0) {
556 if (dcf77_config.
dcf_state == DCF77_DAY_COMPLETED) {
562 rtc_data[2] = (
dcf77_time.current_day / 10) << 4;
563 rtc_data[2] = rtc_data[2] | ((
dcf77_time.current_day - ((
dcf77_time.current_day / 10) * 10)) & 0x0f);
568 while (return_value !=
I2C_IO_SUCC && i2c_retry_counter > 0) {
582 if (dcf77_config.
dcf_state == DCF77_WEEKDAY_COMPLETED) {
588 rtc_data[2] =
dcf77_time.current_weekday & 0x0f;
593 while (return_value !=
I2C_IO_SUCC && i2c_retry_counter > 0) {
607 if (dcf77_config.
dcf_state == DCF77_MONTH_COMPLETED) {
613 rtc_data[2] = (
dcf77_time.current_month / 10) << 4;
614 rtc_data[2] = rtc_data[2] | ((
dcf77_time.current_month - ((
dcf77_time.current_month / 10) * 10)) & 0x0f);
619 while (return_value !=
I2C_IO_SUCC && i2c_retry_counter > 0) {
637 if ((prev_dcf77_state != DCF77_TIMESYNC_COMPLETED) && (dcf77_config.
dcf_state == DCF77_TIMESYNC_COMPLETED)) {
647 rtc_data[2] = ((
dcf77_time.current_second / 10) << 4) & 0x7f;
648 rtc_data[2] = rtc_data[2] | ((
dcf77_time.current_second - ((
dcf77_time.current_second / 10) * 10)) & 0x0f);
649 rtc_data[3] = (
dcf77_time.current_minute / 10) << 4;
650 rtc_data[3] = rtc_data[3] | ((
dcf77_time.current_minute - ((
dcf77_time.current_minute / 10) * 10)) & 0x0f);
651 rtc_data[4] = (
dcf77_time.current_hour / 10) << 4;
652 rtc_data[4] = rtc_data[4] | ((
dcf77_time.current_hour - ((
dcf77_time.current_hour / 10) * 10)) & 0x0f);
653 rtc_data[5] =
dcf77_time.current_weekday & 0x0f;
654 rtc_data[6] = (
dcf77_time.current_day / 10) << 4;
655 rtc_data[6] = rtc_data[6] | ((
dcf77_time.current_day - ((
dcf77_time.current_day / 10) * 10)) & 0x0f);
656 rtc_data[7] = (
dcf77_time.current_month / 10) << 4;
657 rtc_data[7] = rtc_data[7] | ((
dcf77_time.current_month - ((
dcf77_time.current_month / 10) * 10)) & 0x0f);
658 rtc_data[8] = (
dcf77_time.current_year / 10) << 4;
659 rtc_data[8] = rtc_data[8] | ((
dcf77_time.current_year - ((
dcf77_time.current_year / 10) * 10)) & 0x0f);
664 while (return_value !=
I2C_IO_SUCC && i2c_retry_counter > 0) {
676 #if RTC_LCD_DEBUG == 1
684 prev_dcf77_state = dcf77_config.
dcf_state;
707 while (return_value !=
I2C_IO_SUCC && i2c_retry_counter > 0) {
720 while (return_value !=
I2C_IO_SUCC && i2c_retry_counter > 0) {
730 real_time.current_second = (10 * ((rtc_data[1] >> 4) & 0x07)) + (rtc_data[1] & 0x0f);
731 real_time.current_minute = (10 * ((rtc_data[2] >> 4) & 0x07)) + (rtc_data[2] & 0x0f);
732 if ((rtc_data[3] & 0x10) == 1) {
733 real_time.current_hour = (10 * ((rtc_data[3] >> 4) & 0x01)) + (rtc_data[3] & 0x0f) +
734 (12 * ((rtc_data[3] >> 5) & 0x01));
736 real_time.current_hour = (10 * ((rtc_data[3] >> 4) & 0x03)) + (rtc_data[3] & 0x0f);
738 real_time.current_weekday = rtc_data[4] & 0x07;
739 real_time.current_day = (10 * ((rtc_data[5] >> 4) & 0x03)) + (rtc_data[5] & 0x0f);
740 real_time.current_month = (10 * ((rtc_data[6] >> 4) & 0x01)) + (rtc_data[6] & 0x0f);
741 real_time.current_year = (10 * ((rtc_data[7] >> 4) & 0x0f)) + (rtc_data[7] & 0x0f);
745 #if RTC_SAA1064_OUTPUT == 1
749 if ((real_time.current_minute == 0) || (real_time.current_minute == 10) || (real_time.current_minute == 20) ||
750 (real_time.current_minute == 30) || (real_time.current_minute == 40) || (real_time.current_minute == 50)) {
751 if (real_time.current_second == 19) {
752 rtc_show_date_saa1064(real_time.current_day, real_time.current_month, real_time.current_year, real_time.current_hour,
753 real_time.current_minute, real_time.current_second, 1);
754 }
else if ((real_time.current_second >= 20) && (real_time.current_second < 32)) {
755 rtc_show_date_saa1064(real_time.current_day, real_time.current_month, real_time.current_year, real_time.current_hour,
756 real_time.current_minute, real_time.current_second, 0);
757 }
else if (real_time.current_second == 32) {
770 #if RTC_LCD_TIMEDEBUG == 1
772 if (real_time.current_second != real_time.prev_second) {
774 real_time.prev_second = real_time.current_second;
779 if (real_time.current_hour < 10) {
782 itoa(real_time.current_hour, tmp_char, 10);
785 if (real_time.current_minute < 10) {
788 itoa(real_time.current_minute, tmp_char, 10);
791 if (real_time.current_second < 10) {
794 itoa(real_time.current_second, tmp_char, 10);
797 if (real_time.current_day < 10) {
800 itoa(real_time.current_day, tmp_char, 10);
802 if (dcf77_config.
dcf_state == DCF77_TIMESYNC_COMPLETED) {
811 if (real_time.current_month < 10) {
814 itoa(real_time.current_month, tmp_char, 10);
816 if (dcf77_config.
dcf_state == DCF77_TIMESYNC_COMPLETED) {
833 itoa(real_time.current_year, tmp_char, 10);
863 #if RTC_LCD_DEBUG == 1
867 timer_value_start = TCNT0 - timer_value_start;
868 itoa(timer_value_start, tmp_char, 10);
878 itoa(dcf77_config.
dcf_state, tmp_char, 10);
891 ctrl_data[2] = real_time.current_second & 0x3f;
893 ctrl_data[3] = (real_time.current_minute >> 2) & 0x0f;
895 ctrl_data[4] = real_time.current_hour & 0x1f;
897 ctrl_data[5] = real_time.current_day & 0x1f;
898 ctrl_data[6] = real_time.current_year & 0x7f;