Real Time Clock Module with I2C Interface  0.3
⇒ back to Project Page
 All Data Structures Files Functions Variables Typedefs Macros Pages
dcf77.c
Go to the documentation of this file.
1 /****************************************************************************/
36 /******************************************
37  \internal
38  Include files
39 *******************************************/
40 
41 #include <avr/io.h>
42 
43 #include "dcf77.h"
44 
45 #if RTC_LCD_DEBUG == 1
46 #include "lcd.h"
47 #endif
48 
49 
50 
51 /****************************************************************************/
120 /****************************************************************************/
121 
122 uint8_t dcf77_sync(uint8_t dcf77_signal, uint8_t logic_type, volatile real_time_data *real_time,
123  uint8_t sample_time, uint8_t command) {
124 
125 
126  static uint8_t time_count = 0;
127  static uint8_t bit_counter = 0;
128  static uint8_t parity_counter = 0;
129  static uint8_t tmp_value = 0;
130  static uint8_t state = DCF77_STARTSYNC_SEARCH01;
131 
132  static uint8_t prev_dcf77_signal = DCF77_UNINIT_VALUE;
133 
134 
135  /***************************************************************************
136  \internal
137  reset all internal values at start (required before every start of a new
138  sync!)
139  ***************************************************************************/
140  if (command == DCF77_RESET_TIME) {
141 
142  time_count = 0;
143  bit_counter = 0;
144  parity_counter = 0;
145  tmp_value = 0;
146  state = DCF77_STARTSYNC_SEARCH01;
147  prev_dcf77_signal = DCF77_UNINIT_VALUE; //this initial value indicates an uninitialized previuos signal
148 
149  return state;
150 
151  }
152 
153 
154  /***************************************************************************
155  \internal
156  normal operation --> time synchronization --> function has to be called
157  cyclical until state (return value) is "DCF77_TIMESYNC_COMPLETED"
158  ***************************************************************************/
159 
160  //invert input signal if "active high"
161  if (logic_type == DCF77_ACTIVE_HIGH) {
162  dcf77_signal = dcf77_signal ^ 1; //XOR
163  }
164 
165  //set previous DCF77 signal to current value if uninitialized
166  if (prev_dcf77_signal == DCF77_UNINIT_VALUE) {
167  prev_dcf77_signal = dcf77_signal;
168  }
169 
170 
171  /***************************************************************************
172  \internal
173  state machine to get DCF77 time...
174  ***************************************************************************/
175  switch (state) {
176 
177  // synchronization from second 59 to 00/01:
178  //
179  // (1) (2)
180  //_____________ _______________________________ _____________
181  // | | | |
182  // | | | |
183  // | | | |
184  // |________| |_________|
185  // 2 sec
186  // |<------------------------------------>|
187 
188  case DCF77_STARTSYNC_SEARCH01:
189 
190  if ((prev_dcf77_signal == 1) && (dcf77_signal == 0)) { //wait for rising edge (1), signal is inverted!
191 
192 #if RTC_LCD_DEBUG == 1
193  lcd_set_cursor(15, 0);
194  lcd_send_string("#", FALSE); //for blinking cursor
195 #endif
196 
197  time_count = 0;
198  state = DCF77_STARTSYNC_SEARCH02;
199  }
200 
201  break;
202 
203 
204  // synchronization from second 59 to 00/01:
205  //
206  // (1) (2)
207  //_____________ _______________________________ _____________
208  // | | | |
209  // | | | |
210  // | | | |
211  // |________| |_________|
212  // 2 sec
213  // |<------------------------------------>|
214 
215 
216  case DCF77_STARTSYNC_SEARCH02:
217 
218  if ((prev_dcf77_signal == 1) && (dcf77_signal == 0)) { //wait for rising edge (2), signal is inverted!
219 
220 #if RTC_LCD_DEBUG == 1
221  lcd_set_cursor(15, 0);
222  lcd_send_string(" ", FALSE);
223 #endif
224 
225  if ((time_count >= DCF77_SYNC_START_MIN) && (time_count <= DCF77_SYNC_START_MAX)) { //arrived during correct time window?
226  time_count = 0;
227  bit_counter = 0;
228  state = DCF77_STARTSYNC_COMPLETED; //synchronized! --> read in bits to get minutes, hours...
229  } else {
230  state = DCF77_STARTSYNC_SEARCH01;
231  }
232  } else {
233  if (time_count < DCF77_SYNC_START_MAX) { //max time exceed? --> try it next time again...
234  time_count = time_count + sample_time;
235  } else {
236  state = DCF77_STARTSYNC_SEARCH01;
237  }
238  }
239 
240  break;
241 
242 
243  // reading bit (falling edge):
244  //
245  // (1) "1" (2) (1) "0" (2)
246  //_____________ _______________________ _____________
247  // | | | |
248  // | 200 msec | | 100 msec|
249  // |<---------->| |<------->|
250  // |____________| |_________|
251  // 1 sec
252  // |<-------------------------------->|
253 
254 
255  case DCF77_STARTSYNC_COMPLETED:
256  case DCF77_MINUTE_COMPLETED:
257  case DCF77_HOUR_COMPLETED:
258  case DCF77_DAY_COMPLETED:
259  case DCF77_MONTH_COMPLETED:
260  case DCF77_YEAR_COMPLETED:
261  case DCF77_WEEKDAY_COMPLETED:
262  case DCF77_TIMESYNC_SEARCH01:
263 
264  //set state to deactivate indicators (other COMPLETED-states)
265  state = DCF77_TIMESYNC_SEARCH01;
266 
267  //wait for (next) falling edge (signal is inverted!)
268  if ((prev_dcf77_signal == 1) && (dcf77_signal == 0)) {
269  if ((time_count >= DCF77_SYNC_MIN) && (time_count <= DCF77_SYNC_MAX)) { //arrived during correct time window?
270 
271  time_count = 0;
272  state = DCF77_TIMESYNC_SEARCH02; //falling edge found ---> wait for rising edge
273  } else {
274  state = DCF77_STARTSYNC_SEARCH01;
275  }
276  } else {
277  if (time_count < DCF77_SYNC_MAX) { //max time exceed? --> try it next time again...
278  time_count = time_count + sample_time;
279  } else {
280  state = DCF77_STARTSYNC_SEARCH01;
281  }
282  }
283 
284  break;
285 
286 
287  // reading bit (falling edge):
288  //
289  // (1) "1" (2) (1) "0" (2)
290  //_____________ _______________________ _____________
291  // | | | |
292  // | 200 msec | | 100 msec|
293  // |<---------->| |<------->|
294  // |____________| |_________|
295  // 1 sec
296  // |<-------------------------------->|
297 
298 
299  case DCF77_TIMESYNC_SEARCH02:
300 
301  //wait for (next) rising edge (signal is inverted!)
302  if ((prev_dcf77_signal == 0) && (dcf77_signal == 1)) {
303  if ((time_count >= DCF77_LOW_MIN) && (time_count <= DCF77_LOW_MAX)) { //arrived during correct time window?
304 
305  state = DCF77_TIMESYNC_SEARCH01; //rising edge arrived in specified time window
306 
307  /******************************************
308  \internal
309  decoding - logical "0"
310  *******************************************/
311 
312  //for minutes
313  if (bit_counter == 20) {
314  tmp_value = 0;
315  parity_counter = 0;
316  } else if (bit_counter == 26) {
317 
318  //check parity bit
319  } else if (bit_counter == 27) {
320 
321  if ((parity_counter % 2) == 0) { //even parity (parity bit == 0)
322  real_time->current_minute = tmp_value;
323  state = DCF77_MINUTE_COMPLETED;
324  } else {
325  state = DCF77_STARTSYNC_SEARCH01;
326  }
327 
328  }
329 
330  //for hours
331  if (bit_counter == 28) {
332  tmp_value = 0;
333  parity_counter = 0;
334  } else if (bit_counter == 34) {
335 
336  //check parity bit
337  if ((parity_counter % 2) == 0) { //even parity (parity bit == 0)
338  real_time->current_hour = tmp_value;
339  state = DCF77_HOUR_COMPLETED;
340  } else {
341  state = DCF77_STARTSYNC_SEARCH01;
342  }
343 
344  }
345 
346  //for day
347  if (bit_counter == 35) {
348  tmp_value = 0;
349  parity_counter = 0;
350  } else if (bit_counter == 40) { //do not reset the parity counter (will be done after year)
351  real_time->current_day = tmp_value;
352  state = DCF77_DAY_COMPLETED;
353  }
354 
355  //for weekday
356  if (bit_counter == 41) {
357  tmp_value = 0;
358  } else if (bit_counter == 43) { //do not reset the parity counter (will be done after year)
359  real_time->current_weekday = tmp_value;
360  state = DCF77_WEEKDAY_COMPLETED;
361  }
362 
363  //for month
364  if (bit_counter == 44) {
365  tmp_value = 0;
366  } else if (bit_counter == 48) { //do not reset the parity counter (will be done after year)
367  real_time->current_month = tmp_value;
368  state = DCF77_MONTH_COMPLETED;
369  }
370 
371  //for year
372  if (bit_counter == 49) {
373  tmp_value = 0;
374  } else if (bit_counter == 57) {
375 
376  //check parity bit
377  if ((parity_counter % 2) == 0) { //even parity (parity bit == 0)
378  real_time->current_year = tmp_value;
379  state = DCF77_YEAR_COMPLETED;
380  } else {
381  state = DCF77_STARTSYNC_SEARCH01;
382  }
383 
384  }
385 
386  bit_counter++;
387 
388  if (bit_counter > 57) {
389  state = DCF77_TIMESYNC_COMPLETED;
390  }
391 
392  } else if ((time_count >= DCF77_HIGH_MIN) && (time_count <= DCF77_HIGH_MAX)) { //arrived during correct time window?
393 
394  state = DCF77_TIMESYNC_SEARCH01; //rising edge arrived in specified time window
395 
396  /******************************************
397  \internal
398  decoding - logical "1"
399  *******************************************/
400 
401  //minutes
402  if (bit_counter == 20) {
403  tmp_value = 1;
404  parity_counter = 1;
405  } else if (bit_counter == 21) {
406  tmp_value = tmp_value + 2;
407  parity_counter++;
408  } else if (bit_counter == 22) {
409  tmp_value = tmp_value + 4;
410  parity_counter++;
411  } else if (bit_counter == 23) {
412  tmp_value = tmp_value + 8;
413  parity_counter++;
414  } else if (bit_counter == 24) {
415  tmp_value = tmp_value + 10;
416  parity_counter++;
417  } else if (bit_counter == 25) {
418  tmp_value = tmp_value + 20;
419  parity_counter++;
420  } else if (bit_counter == 26) {
421  tmp_value = tmp_value + 40;
422  parity_counter++;
423 
424 
425  //check parity bit
426  } else if (bit_counter == 27) {
427 
428  if ((parity_counter % 2) > 0) { //even parity (odd without parity bit)
429  real_time->current_minute = tmp_value;
430  state = DCF77_MINUTE_COMPLETED;
431  } else {
432  state = DCF77_STARTSYNC_SEARCH01;
433  }
434 
435  }
436 
437  //hours
438  if (bit_counter == 28) {
439  tmp_value = 1;
440  parity_counter = 1;
441  } else if (bit_counter == 29) {
442  tmp_value = tmp_value + 2;
443  parity_counter++;
444  } else if (bit_counter == 30) {
445  tmp_value = tmp_value + 4;
446  parity_counter++;
447  } else if (bit_counter == 31) {
448  tmp_value = tmp_value + 8;
449  parity_counter++;
450  } else if (bit_counter == 32) {
451  tmp_value = tmp_value + 10;
452  parity_counter++;
453  } else if (bit_counter == 33) {
454  tmp_value = tmp_value + 20;
455  parity_counter++;
456  } else if (bit_counter == 34) {
457 
458  if ((parity_counter % 2) > 0) { //even parity (odd without parity bit)
459  real_time->current_hour = tmp_value;
460  state = DCF77_HOUR_COMPLETED;
461  } else {
462  state = DCF77_STARTSYNC_SEARCH01;
463  }
464 
465  }
466 
467  //day of month
468  if (bit_counter == 35) {
469  tmp_value = 1;
470  parity_counter = 1;
471  } else if (bit_counter == 36) {
472  tmp_value = tmp_value + 2;
473  parity_counter++;
474  } else if (bit_counter == 37) {
475  tmp_value = tmp_value + 4;
476  parity_counter++;
477  } else if (bit_counter == 38) {
478  tmp_value = tmp_value + 8;
479  parity_counter++;
480  } else if (bit_counter == 39) {
481  tmp_value = tmp_value + 10;
482  parity_counter++;
483  } else if (bit_counter == 40) { //do not reset the parity counter (will be done after year)
484  tmp_value = tmp_value + 20;
485  parity_counter++;
486  real_time->current_day = tmp_value;
487  state = DCF77_DAY_COMPLETED;
488  }
489 
490  //weekday
491  if (bit_counter == 41) {
492  tmp_value = 1;
493  parity_counter++;
494  } else if (bit_counter == 42) {
495  tmp_value = tmp_value + 2;
496  parity_counter++;
497  } else if (bit_counter == 43) { //do not reset the parity counter (will be done after year)
498  tmp_value = tmp_value + 4;
499  parity_counter++;
500  real_time->current_weekday = tmp_value;
501  state = DCF77_WEEKDAY_COMPLETED;
502  }
503 
504  //month
505  if (bit_counter == 44) {
506  tmp_value = 1;
507  parity_counter++;
508  } else if (bit_counter == 45) {
509  tmp_value = tmp_value + 2;
510  parity_counter++;
511  } else if (bit_counter == 46) {
512  tmp_value = tmp_value + 4;
513  parity_counter++;
514  } else if (bit_counter == 47) {
515  tmp_value = tmp_value + 8;
516  parity_counter++;
517  } else if (bit_counter == 48) { //do not reset the parity counter (will be done after year)
518  tmp_value = tmp_value + 10;
519  parity_counter++;
520  real_time->current_month = tmp_value;
521  state = DCF77_MONTH_COMPLETED;
522  }
523 
524  //year
525  if (bit_counter == 49) {
526  tmp_value = 1;
527  parity_counter++;
528  } else if (bit_counter == 50) {
529  tmp_value = tmp_value + 2;
530  parity_counter++;
531  } else if (bit_counter == 51) {
532  tmp_value = tmp_value + 4;
533  parity_counter++;
534  } else if (bit_counter == 52) {
535  tmp_value = tmp_value + 8;
536  parity_counter++;
537  } else if (bit_counter == 53) {
538  tmp_value = tmp_value + 10;
539  parity_counter++;
540  } else if (bit_counter == 54) {
541  tmp_value = tmp_value + 20;
542  parity_counter++;
543  } else if (bit_counter == 55) {
544  tmp_value = tmp_value + 40;
545  parity_counter++;
546  } else if (bit_counter == 56) {
547  tmp_value = tmp_value + 80;
548  parity_counter++;
549  } else if (bit_counter == 57) {
550 
551  if ((parity_counter % 2) > 0) { //even parity (odd without parity bit)
552  real_time->current_year = tmp_value;
553  state = DCF77_YEAR_COMPLETED;
554  } else {
555  state = DCF77_STARTSYNC_SEARCH01;
556  }
557 
558  }
559 
560  bit_counter++;
561 
562  if (bit_counter > 57) {
563  state = DCF77_TIMESYNC_COMPLETED;
564  }
565 
566  } else {
567  state = DCF77_STARTSYNC_SEARCH01;
568  }
569  } else {
570  if (time_count < DCF77_HIGH_MAX) { //max time exceed? --> try it next time again...
571  time_count = time_count + sample_time;
572  } else {
573  state = DCF77_STARTSYNC_SEARCH01;
574  }
575  }
576 
577  break;
578 
579 
580 
581  default:
582  break;
583 
584  }
585 
586 
587  //save signal value
588  prev_dcf77_signal = dcf77_signal;
589 
590 
591  return state;
592 
593 }
594