Real Time Clock Module with I2C Interface  0.3
⇒ back to Project Page
 All Data Structures Files Functions Variables Typedefs Macros Pages
main.c
Go to the documentation of this file.
1 /****************************************************************************/
34 /******************************************
35  \internal
36  Include files
37 *******************************************/
38 
39 #include <avr/io.h>
40 #include <stdlib.h>
41 #include <util/delay.h>
42 #include <avr/interrupt.h>
43 #include <avr/pgmspace.h>
44 
45 #include <math.h>
46 
47 #include "rtc_control.h"
48 #include "port_definitions.h"
49 #include "i2c.h"
50 #include "dcf77.h"
52 
53 #if RTC_LCD_DEBUG == 1 || RTC_LCD_TIMEDEBUG == 1
54 #include "lcd.h"
55 #endif
56 
57 #if RTC_SAA1064_OUTPUT == 1
58 #include "saa1064.h"
59 #endif // RTC_SAA1064_OUTPUT
60 
61 
62 
63 /******************************************
64  \internal
65  Definitions
66 *******************************************/
67 
68 //Definition for Frequency Setting
69 #ifndef F_CPU
70 #warning "F_CPU was not defined! It will now be set to 20000000UL."
71 #define F_CPU 20000000UL
72 #endif
73 
74 
75 /******************************************
76  \internal
77  Global variables
78 *******************************************/
79 
80 volatile uint8_t time_scale;
81 volatile uint8_t prev_time_scale;
82 
83 volatile uint8_t i2c_slave_data_arrived;
84 volatile uint8_t slave_number_bytes;
85 volatile uint8_t i2c_slave_mode_blocked;
87 
89 volatile real_time_data dcf77_time;
90 
91 
92 #if RTC_LCD_DEBUG == 1 || RTC_LCD_TIMEDEBUG == 1
93 /******************************************
94  \internal
95  Pre-defined Strings
96  (saved in program memory)
97 *******************************************/
98 static const char slave_mode_error_MSG[] PROGMEM = "INTERNAL ERROR (SLA)";
99 static const char clear_line_MSG[] PROGMEM = " ";
100 #endif
101 
102 
103 
104 
105 /****************************************************************************/
156 /****************************************************************************/
157 
158 int main(void)
159 {
160 
161 
162 #if RTC_SAA1064_OUTPUT == 1
163  //for LED output (SAA1064)
164  static uint8_t saa1064_data[I2C_SAA1064_NUMBER_REG];
165 #endif // RTC_SAA1064_OUTPUT
166 
167  //for real time clock
168  static uint8_t rtc_data[I2C_MASTER_BUFFER_LENGTH]; // I2C data buffer for data transmission from or to DS1307 RTC chip
169  static real_time_data real_time; // contains the real time data from DS1307 RTC chip
170 
171 
172 
173  //timer and bus-related
174 #if RTC_LCD_DEBUG == 1
175  uint8_t timer_value_start;
176 #endif
177  uint8_t i2c_retry_counter;
178 
179  uint8_t prev_dcf77_state = DCF77_STARTSYNC_SEARCH01;
180 
181  //other
182  uint8_t return_value;
183 
184 #if RTC_LCD_DEBUG == 1 || RTC_LCD_TIMEDEBUG == 1
185  static char tmp_char[40];
186  uint8_t ff_mem = 0;
187 #endif
188 
189 
190 
191  /***************************************************************************
192  \internal
193  port and pin configuration
194  ***************************************************************************/
195 
196  //enable pull-ups for all pins an set them to input port (default state)
197  PORTB = 0xff;
198  DDRB = 0x00;
199  PORTC = 0xff;
200  DDRC = 0x00;
201  PORTD = 0xff;
202  DDRD = 0x00;
203 
204 #if RTC_LCD_DEBUG == 1 || RTC_LCD_TIMEDEBUG == 1
205  //pin initialization for LCD module
206  lcd_pin_init();
207  lcd_blpin_init();
208 #endif
209 
210  //pin initialization for DCF77 I/O
211  COMP_PORT = COMP_PORT & ~(1 << COMP_DATA);
212  COMP_DDR = COMP_DDR & ~(1 << COMP_DATA);
213  PON_PORT = PON_PORT & ~(1 << PON_ADDR);
214  PON_DDR = PON_DDR | (1 << PON_ADDR);
215 
216  //Comparator configuration for COMP_DATA
217  ADMUX = 0xC2; //ADC input Pin 2
218  ADCSRA = 0x00; //switch ADC off
219  ADCSRB = 0x40; //use ADC-Pin as input (multiplex)
220  ACSR = ACSR | (1 << ACBG); //enable internal bandgap reference
221  ACSR = ACSR & ~(1 << ACD) & ~(1 << ACIE) & ~(1 << ACIC); //switch on Comparator, disable interrupt, disable input capture
222 
223  //pin initialization for LED
226 
227 
228  //pin initialization for I2C
229  i2c_pin_init();
230 
231 
232  /***************************************************************************
233  \internal
234  Initialization of sub-modules and interrupts
235  ***************************************************************************/
236 
237 #if RTC_LCD_DEBUG == 1 || RTC_LCD_TIMEDEBUG == 1
238  //initialize LCD
239  lcd_init();
240 #endif
241 
242 
243  //set up Timer ISR
244  TCCR0A = 0x02; //CTC mode
245  //TCCR0B = 0x0d; //WGM02 set, Prescaler: 1024
246  TCCR0B = 0x05; //WGM02 set, Prescaler: 1024
247  OCR0A = RTC_OCR0A; //value for compare match (156 = 10 ms for 16 MHz clock, 195 = 10 ms for 20 MHz clock)
248  TIMSK0 = TIMSK0 | (1 << OCIE0A); //enable interrupt for Timer A
249  time_scale = 0; //reset time scale (basis for some operations) [*10 ms]
250  prev_time_scale = 0; //previous time scale to detect changes
251 
252 
253  //initialize I2C
254 
255  //set clock speed
257 
258  //set slave address
259  TWAR = I2C_SLAVE_ADDRESS;
260 
261  //preset I2C slave data flag
264 
265  //enable I2C slave operation mode
266  TWCR = (1 << TWEN) | (1 << TWEA) | (1 << TWIE) | (1 << TWINT);
267 
268 
269  //activate global Interrupts
270  sei();
271 
272 
273  /***************************************************************************
274  \internal
275  Initialization of data structures
276  ***************************************************************************/
277 
278  real_time.current_hour = 0;
279  real_time.current_minute = 0;
280  real_time.current_second = 0;
281  real_time.prev_second = 0;
282 
283  real_time.current_year = 0;
284  real_time.current_month = 0;
285  real_time.current_day = 0;
286  real_time.current_weekday = 0;
287 
288  dcf77_time.current_hour = 0;
289  dcf77_time.current_minute = 0;
290  dcf77_time.current_second = 0;
291  dcf77_time.prev_second = 0;
292 
293  dcf77_time.current_year = 0;
294  dcf77_time.current_month = 0;
295  dcf77_time.current_day = 0;
296  dcf77_time.current_weekday = 0;
297 
298 
299  //setup DCF signal memory and initial configuration
300  dcf77_config.current_dcf_signal = 0;
301  dcf77_config.dcf_enabled = DCF77_ENABLED_DEFAULT;
302 
303 
304  /***************************************************************************
305  \internal
306  Setup RTC if not battery powered
307  ***************************************************************************/
308 
309 #if RTC_LCD_DEBUG == 1 || RTC_LCD_TIMEDEBUG == 1
310  lcd_clear();
311  lcd_send_string("RTC V0.3", FALSE);
312 #endif
313 
314  _delay_ms(500); //wait for some time (maybe RTC chip needs some startup time...)
315 
316  //call DS1307 setup function
317  rtc_setup_ds1307(rtc_data);
318 
319 
320  /***************************************************************************
321  \internal
322  Setup SAA1064
323  ***************************************************************************/
324 
325 #if RTC_SAA1064_OUTPUT == 1
326  //call SAA1064 setup function
327  rtc_setup_saa1064(saa1064_data);
328 #endif // RTC_SAA1064_OUTPUT
329 
330 
331  /***************************************************************************
332  \internal
333  Start DCF module and sync function
334  ***************************************************************************/
335 
336  //start DCF module --> switch PON from 1 to 0 (inverted logic)
337  if (dcf77_config.dcf_enabled > 0) {
338  PON_PORT = PON_PORT | (1 << PON_ADDR);
339  }
340 
341 
342  //initialize the time sync function
343  dcf77_config.dcf_state = dcf77_sync(dcf77_config.current_dcf_signal, DCF77_ACTIVE_LOW,
344  &real_time, DCF77_SAMPLE_TIME, DCF77_RESET_TIME);
345 
346 
347 #if RTC_LCD_DEBUG == 1 || RTC_LCD_TIMEDEBUG == 1
348  //clear LCD
349  lcd_clear();
350 #endif
351 
352 
353 
354 
355  /***************************************************************************
356  \internal
357  Main Loop
358  ***************************************************************************/
359 
360  while (1) {
361 
362 
363  if (prev_time_scale != time_scale) {
364 
365 #if RTC_LCD_DEBUG == 1
366  //get timer value to calculate computing time
367  timer_value_start = TCNT0;
368 #endif
369 
370  /***************************************************************************
371  \internal
372  Check for I2C data from master
373  ***************************************************************************/
374 
375  if (i2c_slave_data_arrived == TRUE) {
376 
377  //get data from register
378  dcf77_config.dcf_enabled = (ctrl_data[1] >> 1) & 0x01;
379 
380  //set time command from master?
381  if ((ctrl_data[1] & 0x80) == 0x80) {
382 
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;
391 
392  //write to RTC
393  rtc_data[0] = DS1307_I2C_ADRESS; //slave address + Write-Bit
394  rtc_data[1] = 0x00;
395  rtc_data[2] = ((real_time.current_second / 10) << 4) & 0x7f; //*10 seconds
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; //*10 minutes
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; //*10 minutes
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; // *10 minutes
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; // *10 minutes
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; // *10 minutes
407  rtc_data[8] = rtc_data[8] | ((real_time.current_year - ((real_time.current_year / 10) * 10)) & 0x0f);
408 
409  //I2C communication
410  i2c_retry_counter = I2C_MAX_RETRYS;
411  return_value = I2C_IO_ERROR;
412  while (return_value != I2C_IO_SUCC && i2c_retry_counter > 0) {
413  return_value = i2c_master_data_io(rtc_data, 9, FALSE);
414  i2c_retry_counter--;
415  _delay_us(I2C_RETRY_WAIT_US);
416  }
417 
418  } //if ((ctrl_data[1] & 0x80) == 0x80) {...
419 
420 
421 #if RTC_LCD_DEBUG == 1
422  lcd_set_cursor(0, 1);
423  lcd_send_string(" ", FALSE);
424  lcd_set_cursor(0, 1);
425  itoa(ctrl_data[0], tmp_char, 10);
426  lcd_send_string(tmp_char, FALSE);
427  lcd_send_string(" ", FALSE);
428  itoa(ctrl_data[1], tmp_char, 10);
429  lcd_send_string(tmp_char, FALSE);
430  lcd_send_string(" ", FALSE);
431  itoa(ctrl_data[2], tmp_char, 10);
432  lcd_send_string(tmp_char, FALSE);
433  lcd_send_string(" ", FALSE);
434  itoa(ctrl_data[3], tmp_char, 10);
435  lcd_send_string(tmp_char, FALSE);
436  lcd_send_string(" ", FALSE);
437  itoa(ctrl_data[4], tmp_char, 10);
438  lcd_send_string(tmp_char, FALSE);
439  lcd_send_string(" ", FALSE);
440  itoa(ctrl_data[5], tmp_char, 10);
441  lcd_send_string(tmp_char, FALSE);
442  lcd_send_string(" ", FALSE);
443  itoa(ctrl_data[6], tmp_char, 10);
444  lcd_send_string(tmp_char, FALSE);
445 #endif
446 
448  }
449 
450 
451 
452  /***************************************************************************
453  \internal
454  DCF Module Input
455  ***************************************************************************/
456 
457  if (dcf77_config.dcf_enabled > 0) {
458 
459  /* dcf77_config.current_dcf_signal = (ACSR >> ACO) & 0x01;
460 
461  if (dcf77_config.current_dcf_signal == 1) { //comparator input below bandgap reference
462  DCF_LED_PORT = DCF_LED_PORT & ~(1 << DCF_LED_ADDR);
463  } else {
464  DCF_LED_PORT = DCF_LED_PORT | (1 << DCF_LED_ADDR);
465  }*/
466 
467 
468  //try synchronization every DCF_UPDATE_CYCLE
469  if (((real_time.current_minute % DCF77_UPDATE_CYCLE) == 0) && (real_time.current_second == 0)) {
470  dcf77_config.dcf_state = dcf77_sync(dcf77_config.current_dcf_signal, DCF77_ACTIVE_LOW,
471  &dcf77_time, DCF77_SAMPLE_TIME, DCF77_RESET_TIME);
472  }
473 
474 
475  //sync only if sync is not completed
476  if (dcf77_config.dcf_state != DCF77_TIMESYNC_COMPLETED) {
477  // dcf77_config.dcf_state = dcf77_sync(dcf77_config.current_dcf_signal, DCF77_ACTIVE_LOW,
478  // &dcf77_time, DCF77_SAMPLE_TIME, DCF77_GET_TIME);
479 
480  //seconds are synchronized
481  if (dcf77_config.dcf_state == DCF77_STARTSYNC_COMPLETED) {
482 
483  //do NOT set the second value here to avoid
484  //time errors by wrong detected starts
485  //seconds will be written only when the complete
486  //data (until year information) was received
487 
488  //preset the second value to the value after a completed cycle
489  dcf77_time.current_second = DCF77_CYCLE_SECONDS;
490 
491  }
492 
493  //got minutes...
494  if (dcf77_config.dcf_state == DCF77_MINUTE_COMPLETED) {
495  if (dcf77_time.current_minute < 60) { //check for plausibility
496 
497  //time data is always valid for the next minute
498  //---> decrement minute and avoid updating if minute == 59
499  if (dcf77_time.current_minute == 0) {
500  dcf77_time.current_minute = 59;
501  dcf77_config.dcf_state = dcf77_sync(dcf77_config.current_dcf_signal, DCF77_ACTIVE_LOW,
502  &dcf77_time, DCF77_SAMPLE_TIME, DCF77_RESET_TIME);
503  } else {
504  dcf77_time.current_minute--;
505  }
506 
507  //write new data directly to DS1307
508  rtc_data[0] = DS1307_I2C_ADRESS; //slave address+Write-Bit
509  rtc_data[1] = 0x01;
510  rtc_data[2] = (dcf77_time.current_minute / 10) << 4; //*10 minutes
511  rtc_data[2] = rtc_data[2] | ((dcf77_time.current_minute - ((dcf77_time.current_minute / 10) * 10)) & 0x0f);
512 
513  //I2C communication
514  i2c_retry_counter = I2C_MAX_RETRYS;
515  return_value = I2C_IO_ERROR;
516  while (return_value != I2C_IO_SUCC && i2c_retry_counter > 0) {
517  return_value = i2c_master_data_io(rtc_data, 3, FALSE);
518  i2c_retry_counter--;
519  _delay_us(I2C_RETRY_WAIT_US);
520  }
521 
522  //value not plausible --> restart time aquisition
523  } else {
524  dcf77_config.dcf_state = dcf77_sync(dcf77_config.current_dcf_signal, DCF77_ACTIVE_LOW,
525  &dcf77_time, DCF77_SAMPLE_TIME, DCF77_RESET_TIME);
526  }
527  }
528 
529  //got hours...
530  if (dcf77_config.dcf_state == DCF77_HOUR_COMPLETED) {
531  if (dcf77_time.current_hour < 24) { //check for plausibility
532 
533  //write new data directly to DS1307
534  rtc_data[0] = DS1307_I2C_ADRESS; //slave address+Write-Bit
535  rtc_data[1] = 0x02;
536  rtc_data[2] = (dcf77_time.current_hour / 10) << 4; //*10 minutes
537  rtc_data[2] = rtc_data[2] | ((dcf77_time.current_hour - ((dcf77_time.current_hour / 10) * 10)) & 0x0f);
538 
539  //I2C communication
540  i2c_retry_counter = I2C_MAX_RETRYS;
541  return_value = I2C_IO_ERROR;
542  while (return_value != I2C_IO_SUCC && i2c_retry_counter > 0) {
543  return_value = i2c_master_data_io(rtc_data, 3, FALSE);
544  i2c_retry_counter--;
545  _delay_us(I2C_RETRY_WAIT_US);
546  }
547 
548  //value not plausible --> restart time aquisition
549  } else {
550  dcf77_config.dcf_state = dcf77_sync(dcf77_config.current_dcf_signal, DCF77_ACTIVE_LOW,
551  &dcf77_time, DCF77_SAMPLE_TIME, DCF77_RESET_TIME);
552  }
553  }
554 
555  //got day
556  if (dcf77_config.dcf_state == DCF77_DAY_COMPLETED) {
557  if (dcf77_time.current_day < 32) { //check for plausibility
558 
559  //write new data directly to DS1307
560  rtc_data[0] = DS1307_I2C_ADRESS; //slave address+Write-Bit
561  rtc_data[1] = 0x04;
562  rtc_data[2] = (dcf77_time.current_day / 10) << 4; // *10 minutes
563  rtc_data[2] = rtc_data[2] | ((dcf77_time.current_day - ((dcf77_time.current_day / 10) * 10)) & 0x0f);
564 
565  //I2C communication
566  i2c_retry_counter = I2C_MAX_RETRYS;
567  return_value = I2C_IO_ERROR;
568  while (return_value != I2C_IO_SUCC && i2c_retry_counter > 0) {
569  return_value = i2c_master_data_io(rtc_data, 3, FALSE);
570  i2c_retry_counter--;
571  _delay_us(I2C_RETRY_WAIT_US);
572  }
573 
574  //value not plausible --> restart time aquisition
575  } else {
576  dcf77_config.dcf_state = dcf77_sync(dcf77_config.current_dcf_signal, DCF77_ACTIVE_LOW,
577  &dcf77_time, DCF77_SAMPLE_TIME, DCF77_RESET_TIME);
578  }
579  }
580 
581  //got weekday
582  if (dcf77_config.dcf_state == DCF77_WEEKDAY_COMPLETED) {
583  if ((dcf77_time.current_weekday > 0) && (dcf77_time.current_weekday < 8)) { //check for plausibility
584 
585  //write new data directly to DS1307
586  rtc_data[0] = DS1307_I2C_ADRESS; //slave address+Write-Bit
587  rtc_data[1] = 0x03;
588  rtc_data[2] = dcf77_time.current_weekday & 0x0f;
589 
590  //I2C communication
591  i2c_retry_counter = I2C_MAX_RETRYS;
592  return_value = I2C_IO_ERROR;
593  while (return_value != I2C_IO_SUCC && i2c_retry_counter > 0) {
594  return_value = i2c_master_data_io(rtc_data, 3, FALSE);
595  i2c_retry_counter--;
596  _delay_us(I2C_RETRY_WAIT_US);
597  }
598 
599  //value not plausible --> restart time aquisition
600  } else {
601  dcf77_config.dcf_state = dcf77_sync(dcf77_config.current_dcf_signal, DCF77_ACTIVE_LOW,
602  &dcf77_time, DCF77_SAMPLE_TIME, DCF77_RESET_TIME);
603  }
604  }
605 
606  //got month
607  if (dcf77_config.dcf_state == DCF77_MONTH_COMPLETED) {
608  if (dcf77_time.current_month < 13) { //check for plausibility
609 
610  //write new data directly to DS1307
611  rtc_data[0] = DS1307_I2C_ADRESS; //slave address+Write-Bit
612  rtc_data[1] = 0x05;
613  rtc_data[2] = (dcf77_time.current_month / 10) << 4; // *10 minutes
614  rtc_data[2] = rtc_data[2] | ((dcf77_time.current_month - ((dcf77_time.current_month / 10) * 10)) & 0x0f);
615 
616  //I2C communication
617  i2c_retry_counter = I2C_MAX_RETRYS;
618  return_value = I2C_IO_ERROR;
619  while (return_value != I2C_IO_SUCC && i2c_retry_counter > 0) {
620  return_value = i2c_master_data_io(rtc_data, 3, FALSE);
621  i2c_retry_counter--;
622  _delay_us(I2C_RETRY_WAIT_US);
623  }
624 
625  //value not plausible --> restart time aquisition
626  } else {
627  dcf77_config.dcf_state = dcf77_sync(dcf77_config.current_dcf_signal, DCF77_ACTIVE_LOW,
628  &dcf77_time, DCF77_SAMPLE_TIME, DCF77_RESET_TIME);
629  }
630  }
631 
632  } //if (dcf77_config.dcf_state != DCF77_TIMESYNC_COMPLETED) {...
633 
634 
635 
636  // state change to DCF77_TIMESYNC_COMPLETED detected
637  if ((prev_dcf77_state != DCF77_TIMESYNC_COMPLETED) && (dcf77_config.dcf_state == DCF77_TIMESYNC_COMPLETED)) {
638  if (dcf77_time.current_year < 100) { //check for plausibility
639 
640  //preset the second value to the value after a completed cycle
641  dcf77_time.current_second = DCF77_CYCLE_SECONDS;
642 
643  //cycle completed - write the complete data again
644  //to avoid errors, if a new minute has started
645  rtc_data[0] = DS1307_I2C_ADRESS; //slave address + Write-Bit
646  rtc_data[1] = 0x00;
647  rtc_data[2] = ((dcf77_time.current_second / 10) << 4) & 0x7f; //*10 seconds
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; //*10 minutes
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; //*10 minutes
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; // *10 minutes
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; // *10 minutes
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; // *10 minutes
659  rtc_data[8] = rtc_data[8] | ((dcf77_time.current_year - ((dcf77_time.current_year / 10) * 10)) & 0x0f);
660 
661  //I2C communication
662  i2c_retry_counter = I2C_MAX_RETRYS;
663  return_value = I2C_IO_ERROR;
664  while (return_value != I2C_IO_SUCC && i2c_retry_counter > 0) {
665  return_value = i2c_master_data_io(rtc_data, 9, FALSE);
666  i2c_retry_counter--;
667  _delay_us(I2C_RETRY_WAIT_US);
668  }
669 
670  //value not plausible --> restart time aquisition
671  } else {
672  dcf77_config.dcf_state = dcf77_sync(dcf77_config.current_dcf_signal, DCF77_ACTIVE_LOW,
673  &dcf77_time, DCF77_SAMPLE_TIME, DCF77_RESET_TIME);
674  }
675 
676 #if RTC_LCD_DEBUG == 1
677  lcd_set_cursor(7, 1);
678  lcd_send_string("*", FALSE);
679 #endif
680  }
681 
682 
683  //save previous DCF state
684  prev_dcf77_state = dcf77_config.dcf_state;
685 
686 
687 
688  } //if (dcf77_config.dcf_enabled > 0) {...
689 
690 
691  /***************************************************************************
692  \internal
693  Read RTC data from DS1307
694  ***************************************************************************/
695 
696  if (time_scale == 25 || time_scale == 50 || time_scale == 75 || time_scale == 100 ||
697  time_scale == 125 || time_scale == 150 || time_scale == 175 || time_scale == 200 ||
698  time_scale == 225 || time_scale == 250) {
699 
700  //set address
701  rtc_data[0] = DS1307_I2C_ADRESS; //slave address+Write-Bit
702  rtc_data[1] = 0x00;
703 
704  //I2C communication
705  i2c_retry_counter = I2C_MAX_RETRYS;
706  return_value = I2C_IO_ERROR;
707  while (return_value != I2C_IO_SUCC && i2c_retry_counter > 0) {
708  return_value = i2c_master_data_io(rtc_data, 2, FALSE);
709  i2c_retry_counter--;
710  _delay_us(I2C_RETRY_WAIT_US);
711  }
712 
713  if (return_value == I2C_IO_SUCC) {
714 
715  rtc_data[0] = DS1307_I2C_ADRESS | 0x01; //slave address+Read-Bit
716 
717  //I2C communication
718  i2c_retry_counter = I2C_MAX_RETRYS;
719  return_value = I2C_IO_ERROR;
720  while (return_value != I2C_IO_SUCC && i2c_retry_counter > 0) {
721  return_value = i2c_master_data_io(rtc_data, 8, FALSE);
722  i2c_retry_counter--;
723  _delay_us(I2C_RETRY_WAIT_US);
724  }
725 
726 
727  if (return_value == I2C_IO_SUCC) {
728 
729  //update current time data
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) { //AM/PM Mode
733  real_time.current_hour = (10 * ((rtc_data[3] >> 4) & 0x01)) + (rtc_data[3] & 0x0f) +
734  (12 * ((rtc_data[3] >> 5) & 0x01));
735  } else { //24h Mode
736  real_time.current_hour = (10 * ((rtc_data[3] >> 4) & 0x03)) + (rtc_data[3] & 0x0f);
737  }
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);
742 
743 
744 
745 #if RTC_SAA1064_OUTPUT == 1
746 
747  //write time data to LED display
748  //show date info after 10 minutes again
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) {
758  rtc_show_time_saa1064(real_time.current_hour, real_time.current_minute, real_time.current_second, dcf77_config.dcf_state, 1);
759  } else {
760  rtc_show_time_saa1064(real_time.current_hour, real_time.current_minute, real_time.current_second, dcf77_config.dcf_state, 0);
761  }
762  } else {
763  rtc_show_time_saa1064(real_time.current_hour, real_time.current_minute, real_time.current_second, dcf77_config.dcf_state, 0);
764  }
765 
766 #endif
767 
768 
769 
770 #if RTC_LCD_TIMEDEBUG == 1
771  //write data to LCD
772  if (real_time.current_second != real_time.prev_second) {
773 
774  real_time.prev_second = real_time.current_second;
775 
776  //print time information to display
777  lcd_cursor_home();
778  //lcd_send_string("Zeit: ", FALSE);
779  if (real_time.current_hour < 10) {
780  lcd_send_string("0", FALSE);
781  }
782  itoa(real_time.current_hour, tmp_char, 10);
783  lcd_send_string(tmp_char, FALSE);
784  lcd_send_string(":", FALSE);
785  if (real_time.current_minute < 10) {
786  lcd_send_string("0", FALSE);
787  }
788  itoa(real_time.current_minute, tmp_char, 10);
789  lcd_send_string(tmp_char, FALSE);
790  lcd_send_string(":", FALSE);
791  if (real_time.current_second < 10) {
792  lcd_send_string("0", FALSE);
793  }
794  itoa(real_time.current_second, tmp_char, 10);
795  lcd_send_string(tmp_char, FALSE);
796  lcd_set_cursor(0, 1);
797  if (real_time.current_day < 10) {
798  lcd_send_string("0", FALSE);
799  }
800  itoa(real_time.current_day, tmp_char, 10);
801  lcd_send_string(tmp_char, FALSE);
802  if (dcf77_config.dcf_state == DCF77_TIMESYNC_COMPLETED) {
803  lcd_send_string(".", FALSE);
804  } else {
805  if (ff_mem == 0) {
806  lcd_send_string(".", FALSE);
807  } else {
808  lcd_send_string(" ", FALSE);
809  }
810  }
811  if (real_time.current_month < 10) {
812  lcd_send_string("0", FALSE);
813  }
814  itoa(real_time.current_month, tmp_char, 10);
815  lcd_send_string(tmp_char, FALSE);
816  if (dcf77_config.dcf_state == DCF77_TIMESYNC_COMPLETED) {
817  lcd_send_string(".", FALSE);
818  } else {
819  if (ff_mem == 0) {
820  lcd_send_string(" ", FALSE);
821  ff_mem = 1;
822  } else {
823  lcd_send_string(".", FALSE);
824  ff_mem = 0;
825  }
826  }
827  if ((real_time.current_year + RTC_BASE_YEAR) < 10) {
828  lcd_send_string("0", FALSE);
829  }
830  if (DISPLAY_NUMBER_COLUMNS > 9) {
831  itoa(real_time.current_year + RTC_BASE_YEAR, tmp_char, 10);
832  } else {
833  itoa(real_time.current_year, tmp_char, 10);
834  }
835  lcd_send_string(tmp_char, FALSE);/*
836 
837  if (real_time.current_weekday == DCF77_MONDAY) {
838  lcd_send_string(" Mo", FALSE);
839  } else if (real_time.current_weekday == DCF77_TUESDAY) {
840  lcd_send_string(" Di", FALSE);
841  } else if (real_time.current_weekday == DCF77_WEDNESDAY) {
842  lcd_send_string(" Mi", FALSE);
843  } else if (real_time.current_weekday == DCF77_THURSDAY) {
844  lcd_send_string(" Do", FALSE);
845  } else if (real_time.current_weekday == DCF77_FRIDAY) {
846  lcd_send_string(" Fr", FALSE);
847  } else if (real_time.current_weekday == DCF77_SATURDAY) {
848  lcd_send_string(" Sa", FALSE);
849  } else if (real_time.current_weekday == DCF77_SUNDAY) {
850  lcd_send_string(" So", FALSE);
851  }*/
852 
853  }
854 
855 #endif
856  }
857 
858  }
859 
860  } //if (time_scale == 25 || time_scale == 50 || time_scale == 75...
861 
862 
863 #if RTC_LCD_DEBUG == 1
864  lcd_set_cursor(0, 3);
865  lcd_send_string(" ", FALSE);
866  lcd_set_cursor(0, 3);
867  timer_value_start = TCNT0 - timer_value_start;
868  itoa(timer_value_start, tmp_char, 10);
869  lcd_send_string(tmp_char, FALSE);
870  if ((time_scale != 0) && (prev_time_scale != MAX_TIME_SCALE)) {
871  lcd_set_cursor(4, 3);
872  lcd_send_string(" ", FALSE);
873  lcd_set_cursor(4, 3);
874  itoa(time_scale - prev_time_scale, tmp_char, 10);
875  lcd_send_string(tmp_char, FALSE);
876  }
877  lcd_set_cursor(7, 3);
878  itoa(dcf77_config.dcf_state, tmp_char, 10);
879  lcd_send_string(tmp_char, FALSE);
880 #endif
881 
882 
883  /***************************************************************************
884  \internal
885  Write values to control registers and switch on any I2C slave functionality
886  ***************************************************************************/
887 
888  ctrl_data[1] = dcf77_config.current_dcf_signal & 0x01;
889  ctrl_data[1] = ctrl_data[1] | ((dcf77_config.dcf_enabled & 0x01) << 1);
890  ctrl_data[1] = ctrl_data[1] | ((dcf77_config.dcf_state & 0x0f) << 2);
891  ctrl_data[2] = real_time.current_second & 0x3f;
892  ctrl_data[2] = ctrl_data[2] | ((real_time.current_minute & 0x03) << 6);
893  ctrl_data[3] = (real_time.current_minute >> 2) & 0x0f;
894  ctrl_data[3] = ctrl_data[3] | ((real_time.current_month & 0x0f) << 4);
895  ctrl_data[4] = real_time.current_hour & 0x1f;
896  ctrl_data[4] = ctrl_data[4] | ((real_time.current_weekday & 0x07) << 5);
897  ctrl_data[5] = real_time.current_day & 0x1f;
898  ctrl_data[6] = real_time.current_year & 0x7f;
899 
900 
901  //save timescale value
903 
904  } //if (prev_time_scale != time_scale) {...
905 
906 
907  } //while(1)
908 
909 
910 }
911 
912