Real Time Clock Module with I2C Interface  0.3
⇒ back to Project Page
 All Data Structures Files Functions Variables Typedefs Macros Pages
i2c_rtc_control_operation.c
Go to the documentation of this file.
1 /****************************************************************************/
35 /******************************************
36  \internal
37  Include files
38 *******************************************/
39 
40 #include <stdlib.h>
41 #include <util/delay.h>
42 #include <avr/pgmspace.h>
43 
44 #include "basic_definitions.h"
45 #include "rtc_control.h"
46 #include "i2c.h"
47 #include "dcf77.h"
48 #include "saa1064.h"
50 
51 #if RTC_LCD_DEBUG == 1 || RTC_LCD_TIMEDEBUG == 1
52 #include "lcd.h"
53 #endif
54 
55 
56 
57 
58 
59 
60 /******************************************
61  \internal
62  Pre-defined Strings
63  (saved in program memory)
64 *******************************************/
65 
66 #if RTC_LCD_DEBUG == 1 || RTC_LCD_TIMEDEBUG == 1
67 static const char mode_12h_MSG[] PROGMEM = "12-h-Modus";
68 static const char mode_24h_MSG[] PROGMEM = "24-h-Modus";
69 static const char no_battery_MSG[] PROGMEM = "Batterie-Fehler!";
70 static const char battery_MSG[] PROGMEM = "Batterie ok.";
71 static const char SAA1064_Err1_MSG[] PROGMEM = "SAA1064 Err1";
72 static const char SAA1064_Err2_MSG[] PROGMEM = "SAA1064 Err2";
73 static const char SAA1064_Err3_MSG[] PROGMEM = "SAA1064 Err3";
74 #endif
75 
76 
77 
78 
79 
80 
81 /****************************************************************************/
104 /****************************************************************************/
105 
106 void rtc_setup_ds1307(uint8_t *rtc_data)
107 {
108 
109  //for I2C communication
110  uint8_t i2c_retry_counter;
111 
112  //other
113  uint8_t return_value;
114 
115 
116  //check for acivated clock oscillator which indicates an battery-powered state
117  //set register address
118  rtc_data[0] = DS1307_I2C_ADRESS; //slave address+Write-Bit
119  rtc_data[1] = 0x00;
120 
121  //I2C communication
122  i2c_retry_counter = I2C_MAX_RETRYS;
123  return_value = I2C_IO_ERROR;
124  while (return_value != I2C_IO_SUCC && i2c_retry_counter > 0) {
125  return_value = i2c_master_data_io(rtc_data, 2, FALSE);
126  i2c_retry_counter--;
127  _delay_us(I2C_RETRY_WAIT_US);
128  }
129 
130 
131  if (return_value == I2C_IO_SUCC) {
132 
133  rtc_data[0] = DS1307_I2C_ADRESS | 0x01; //slave address+Read-Bit
134 
135  //I2C communication
136  i2c_retry_counter = I2C_MAX_RETRYS;
137  return_value = I2C_IO_ERROR;
138  while (return_value != I2C_IO_SUCC && i2c_retry_counter > 0) {
139  return_value = i2c_master_data_io(rtc_data, 4, FALSE);
140  i2c_retry_counter--;
141  _delay_us(I2C_RETRY_WAIT_US);
142  }
143 
144  if (return_value == I2C_IO_SUCC) {
145 #if RTC_LCD_DEBUG == 1 || RTC_LCD_TIMEDEBUG == 1
146  lcd_clear();
147  if ((rtc_data[3] & 0x10) == 1) { //12/24 hours mode
148  lcd_send_string(mode_12h_MSG, TRUE);
149  } else {
150  lcd_send_string(mode_24h_MSG, TRUE);
151  }
152  lcd_set_cursor(0, 1);
153 #endif
154  if ((rtc_data[1] & 0x80) == 0x80) { //if set, oscillator is switched off --> no battery
155 #if RTC_LCD_DEBUG == 1 || RTC_LCD_TIMEDEBUG == 1
156  lcd_send_string(no_battery_MSG, TRUE);
157 #endif
158 
159  //setup register data
160  rtc_data[0] = DS1307_I2C_ADRESS; //slave address + Write-Bit
161  rtc_data[1] = 0x00; //DS1307 register address
162  rtc_data[2] = 0x00; //second register
163  rtc_data[3] = 0x00; //minute register
164  rtc_data[4] = 0x00; //hour register
165  rtc_data[5] = 0x01; //day register
166  rtc_data[6] = 0x01; //date register
167  rtc_data[7] = 0x01; //month register
168  rtc_data[8] = 0x00; //year register
169  rtc_data[9] = 0x10; //control register
170 
171  //I2C communication
172  i2c_retry_counter = I2C_MAX_RETRYS;
173  return_value = I2C_IO_ERROR;
174  while (return_value != I2C_IO_SUCC && i2c_retry_counter > 0) {
175  return_value = i2c_master_data_io(rtc_data, 10, FALSE);
176  i2c_retry_counter--;
177  _delay_us(I2C_RETRY_WAIT_US);
178  }
179 
180  } else {
181  //setup register data
182  rtc_data[0] = DS1307_I2C_ADRESS; //slave address + Write-Bit
183  rtc_data[1] = 0x07; //DS1307 register address
184  rtc_data[2] = 0x10; //control register
185 
186  //I2C communication
187  i2c_retry_counter = I2C_MAX_RETRYS;
188  return_value = I2C_IO_ERROR;
189  while (return_value != I2C_IO_SUCC && i2c_retry_counter > 0) {
190  return_value = i2c_master_data_io(rtc_data, 3, FALSE);
191  i2c_retry_counter--;
192  _delay_us(I2C_RETRY_WAIT_US);
193  }
194 
195 #if RTC_LCD_DEBUG == 1 || RTC_LCD_TIMEDEBUG == 1
196  lcd_send_string(battery_MSG, TRUE);
197 #endif
198  }
199  _delay_ms(2000); //wait for x ms
200  }
201  }
202 
203 }
204 
205 
206 
207 
208 /****************************************************************************/
228 /****************************************************************************/
229 
230 void rtc_setup_saa1064(uint8_t *saa1064_data)
231 {
232 
233 
234 #if RTC_LCD_DEBUG == 1
235 
236  uint8_t return_value;
237 
239 
240  saa1064_data[0] = 0x00;
241  saa1064_data[1] = 0x00;
242  saa1064_data[2] = 0x00;
243  saa1064_data[3] = 0x00;
244  saa1064_data[4] = 0x00;
245  saa1064_data[5] = 0x00;
246  saa1064_data[6] = 0x00;
247  return_value = saa1064_read_status(I2C_SAA1064_ADDRESS1, saa1064_data);
248 
249  if (return_value != SAA1064_IO_SUCCESS) {
250  lcd_clear();
251  lcd_send_string(SAA1064_Err1_MSG, TRUE);
252  _delay_ms(500);
253  _delay_ms(500);
254  _delay_ms(500);
255  _delay_ms(500);
256  }
257 
258  saa1064_data[0] = 0x00;
259  saa1064_data[1] = 0x00;
260  saa1064_data[2] = 0x00;
261  saa1064_data[3] = 0x00;
262  saa1064_data[4] = 0x00;
263  saa1064_data[5] = 0x00;
264  saa1064_data[6] = 0x00;
265  return_value = saa1064_read_status(I2C_SAA1064_ADDRESS2, saa1064_data);
266 
267  if (return_value != SAA1064_IO_SUCCESS) {
268  lcd_clear();
269  lcd_send_string(SAA1064_Err2_MSG, TRUE);
270  _delay_ms(500);
271  _delay_ms(500);
272  _delay_ms(500);
273  _delay_ms(500);
274  }
275 
276  saa1064_data[0] = 0x00;
277  saa1064_data[1] = 0x00;
278  saa1064_data[2] = 0x00;
279  saa1064_data[3] = 0x00;
280  saa1064_data[4] = 0x00;
281  saa1064_data[5] = 0x00;
282  saa1064_data[6] = 0x00;
283  return_value = saa1064_read_status(I2C_SAA1064_ADDRESS3, saa1064_data);
284 
285  if (return_value != SAA1064_IO_SUCCESS) {
286  lcd_clear();
287  lcd_send_string(SAA1064_Err3_MSG, TRUE);
288  _delay_ms(500);
289  _delay_ms(500);
290  _delay_ms(500);
291  _delay_ms(500);
292  }
293 
294 #else //RTC_LCD_DEBUG == 1
295 
296  UNUSED(saa1064_data);
297 
298 #endif //RTC_LCD_DEBUG == 1
299 
300 
301 
303 
307 
308 
309 
310 
311 }
312 
313 
314 
315 /****************************************************************************/
343 /****************************************************************************/
344 
345 void rtc_show_time_saa1064(uint8_t current_hour, uint8_t current_minute, uint8_t current_second,
346  uint8_t dcf_status, uint8_t update_all)
347 {
348 
349 
350  static uint8_t prev_second = 0; // saves the previous second value to detect changes
351  static uint8_t prev_minute = 0; // saves the previous minute value to detect changes
352  static uint8_t prev_hour = 0; // saves the previous hour value to detect changes
353  static uint8_t startup = TRUE; // indicates the first call
354 
355  static uint8_t ff_mem = 0; //memory for flipflop
356  static uint32_t tmp_time = 0; //temporary value for calculating the current configuration
357 
358 
359  //reset startup flag
360  if (update_all > 0) {
361  startup = TRUE;
362  }
363 
364 
365  //determine the LED current - compare time with configuration
366  tmp_time = ((uint32_t) current_hour * (uint32_t) 10000) +
367  ((uint32_t) current_minute * (uint32_t) 100) +
368  (uint32_t) current_second;
369 
370 
371  //update all segments at the same time
372  if ((tmp_time == (uint32_t) LED_LOW_CURRENT_START_TIME) || (tmp_time == (uint32_t) LED_HIGH_CURRENT_START_TIME)) {
373  startup = TRUE;
374  }
375 
376  //use different modes for night and day --> night mode
377  if ((tmp_time >= (uint32_t) LED_LOW_CURRENT_START_TIME) || (tmp_time < (uint32_t) LED_HIGH_CURRENT_START_TIME)) {
378 
379  //do only updates for hours and minutesand seconds, if these changed
380  if ((current_hour != prev_hour) || (startup == TRUE)) {
382  _delay_ms(400.0); //creates a delayed update of the later digits
383  }
384 
385  if ((current_minute != prev_minute) || (startup == TRUE)) {
387  //_delay_ms(400.0); //creates a delayed update of the later digits
388  }
389 
390  if ((current_second != prev_second) || (startup == TRUE)) {
391  if (dcf_status == DCF77_TIMESYNC_COMPLETED) {
393  } else {
395  }
396 
397  }
398 
399  //use different modes for night and day --> day mode
400  } else {
401 
402  //do only updates for hours and minutes and seconds, if these changed
403  if ((current_hour != prev_hour) || (startup == TRUE)) {
405  _delay_ms(400.0); //creates a delayed update of the later digits
406  }
407 
408  if ((current_minute != prev_minute) || (startup == TRUE)) {
410  _delay_ms(400.0); //creates a delayed update of the later digits
411  }
412 
413  if ((current_second != prev_second) || (startup == TRUE)) {
414  if (dcf_status != DCF77_TIMESYNC_COMPLETED) {
415  if (ff_mem > 0) {
417  ff_mem = 0;
418  } else {
420  ff_mem = 1;
421  }
422  } else {
424  }
425 
426  }
427 
428  }
429 
430  //save the current value to detect changes
431  prev_second = current_second;
432  prev_minute = current_minute;
433  prev_hour = current_hour;
434  startup = FALSE;
435 
436 }
437 
438 
439 
440 
441 /****************************************************************************/
467 /****************************************************************************/
468 
469 void rtc_show_date_saa1064(uint8_t current_day, uint8_t current_month, uint16_t current_year, uint8_t current_hour,
470  uint8_t current_minute, uint8_t current_second, uint8_t reset)
471 {
472 
473  static uint8_t prev_second = 0; // saves the previous second value to detect changes
474  static uint8_t startup = TRUE; // indicates the first call
475  static uint8_t mode = 0;
476 
477  static uint32_t tmp_time = 0; //temporary value for calculating the current configuration
478 
479 
480  //determine the LED current - compare time with configuration
481  tmp_time = ((uint32_t) current_hour * (uint32_t) 10000) +
482  ((uint32_t) current_minute * (uint32_t) 100) +
483  (uint32_t) current_second;
484 
485  //update all segments at the same time
486  if ((tmp_time == (uint32_t) LED_LOW_CURRENT_START_TIME) || (tmp_time == (uint32_t) LED_HIGH_CURRENT_START_TIME)) {
487  mode = 0;
488  startup = TRUE;
489  }
490 
491  // reset the mode memory, if requested
492  if (reset > 0) {
493 
494  mode = 0;
498 
499  } else {
500 
501 
502  if ((current_second != prev_second) || (startup == TRUE)) {
503 
504  //use different modes for night and day --> night mode
505  if ((tmp_time >= (uint32_t) LED_LOW_CURRENT_START_TIME) || (tmp_time < (uint32_t) LED_HIGH_CURRENT_START_TIME)) {
506 
507  if (mode == 0) {
508 
512  _delay_ms(800.0); //creates a delayed update of the later digits
513 
514  } else if (mode == 1) {
515 
519  _delay_ms(800.0); //creates a delayed update of the later digits
520 
521  } else if (mode == 2) {
522 
526 
527  } else {
528 
532  _delay_ms(800.0); //creates a delayed update of the later digits
533 
534  }
535 
536  if (mode < 3) {
537  mode++;
538  } else {
539  mode = 0;
540  }
541 
542 
543  //use different modes for night and day --> day mode
544  } else {
545 
546  if (mode == 0) {
547 
549  _delay_ms(400.0); //creates a delayed update of the later digits
551  _delay_ms(400.0); //creates a delayed update of the later digits
553 
554  } else if (mode == 1) {
555 
557  _delay_ms(400.0); //creates a delayed update of the later digits
559  _delay_ms(400.0); //creates a delayed update of the later digits
561 
562  } else if (mode == 2) {
563 
565  // _delay_ms(400.0); //creates a delayed update of the later digits
567  _delay_ms(400.0); //creates a delayed update of the later digits
569 
570  } else {
571 
573  //_delay_ms(400.0); //creates a delayed update of the later digits
575  _delay_ms(400.0); //creates a delayed update of the later digits
577 
578  }
579 
580  if (mode < 3) {
581  mode++;
582  } else {
583  mode = 0;
584  }
585 
586  }
587 
588 
589  }
590 
591  }
592 
593  prev_second = current_second;
594  startup = FALSE;
595 
596 
597 
598 }