LevelMeter-Display  2.0
An ultrasonic Level Meter for Stormwater Cisterns
dcf77.h
Go to the documentation of this file.
1 /*
2  * dcf77.h
3  *
4  * Created: 15.10.2012 15:30:24
5  * Author: rolo
6  */
7 
8 #ifndef DCF77_H_
9 #define DCF77_H_
10 
11 #include <stdbool.h>
12 #include <stdint.h>
13 #include <avr/io.h>
14 #include <avr/pgmspace.h>
15 #include <stdlib.h>
16 #include "Config.h"
17 #include "led.h"
18 
19 #define DCF77_PIN PINA
20 #define DCF77_PORT PORTA
21 #define DCF77_DDR DDRA
22 #define DCF77_PINMASK (1<<1)
23 
24 extern bool DCFInSync;
25 
26 extern void DCF77int(void);
27 extern int32_t GetDCFdiff();
28 extern void Invalidate();
29 extern uint8_t min,hour;
30 extern int32_t DCFdiff;
31 
36 static inline void InitDCF77(void)
37 {
38  DCF77_DDR &= ~DCF77_PINMASK; // DCF77-pin is input
39  DCF77_PORT |= DCF77_PINMASK; // Enable pull up
40  Invalidate();
41 }
42 
45 static inline void RTCsync(void)
46 {
47  static int32_t lastDiff=0; // Keeps Track of Time Difference for hard Sync
48 
49  if (min==0)
50  {
51  return; // It saves us trouble not syncing at an hour border
52  }
53  DCFdiff=hour*3600l+min*60l-hrCnt*3600l-minCnt*60l-secCnt;
54  if (DCFdiff<-43200)
55  {
56  DCFdiff+=86400l;
57  }
58  if (DCFdiff>=43200)
59  {
60  DCFdiff-=86400l;
61  }
62  if (DCFdiff>60 || DCFdiff<-60) // more than a minute, step time
63  {
64  if (abs(DCFdiff-lastDiff)<=1) // nearly same difference received twice, step!
65  {
66  if (dFlags&DFLAG_DCF77)
67  {
68  hprintf_P(PSTR("Step time "),DCFdiff,RTCAdj);
69  }
70  hrCnt=hour;
71  minCnt=min;
72  secCnt=0;
73  DCFdiff=0;
75  }
76  else
77  {
78  lastDiff=DCFdiff;
79  }
80  } else if (DCFdiff>0)
81  {
83  } else if (DCFdiff<0)
84  {
86  } else
87  {
89  }
90  if (dFlags&DFLAG_DCF77)
91  {
92  hprintf_P(PSTR("Diff:%ld Tick: %d Adj:%s\n"),DCFdiff,tickCnt,RTCAdjTxt[RTCAdj]);
93  }
94  DCFInSync=true;
95 }
96 
97 
98 
99 #endif /* DCF77_H_ */
const char * RTCAdjTxt[]
Human readable interpretation of RTCAdj.
Definition: display.c:66
int32_t DCFdiff
Difference to RTC in Seconds, positive means RTC is late.
Definition: dcf77.c:29
volatile uint8_t minCnt
Minutes of the RTC.
Definition: display.c:63
RTC is running faster by 1%.
Definition: display.h:70
#define DCF77_PINMASK
Definition: dcf77.h:22
Structures and Constants for Configuration Handling.
void Invalidate()
Invalidates Reception Variables, so Reception is started from scratch.
Definition: dcf77.c:75
volatile uint8_t secCnt
Seconds of the RTC.
Definition: display.c:62
int32_t GetDCFdiff()
Gets the difference between DCF and RTC in seconds. Positive means RTC is late.
Definition: dcf77.c:48
static void InitDCF77(void)
Initializes the DCF77-Variables.
Definition: dcf77.h:36
volatile uint8_t dFlags
Flags Controlling Debug Output.
Definition: display.c:83
RTC is running at normal speed (realtime)
Definition: display.h:69
bool DCFInSync
True when a valid Telegram has been received.
Definition: dcf77.c:28
uint8_t hour
The Hour Value received.
Definition: dcf77.c:43
volatile uint8_t hrCnt
Hours of the RTC.
Definition: display.c:64
#define DCF77_DDR
Definition: dcf77.h:21
int hprintf_P(const char *fmt,...)
printf_P for Communication with the Host
Definition: uart.c:375
volatile uint8_t tickCnt
counter for timer0 interrupts
Definition: display.c:61
Provides inline Functions for controlling the LEDs.
void DCF77int(void)
Interrupt Handler for DCF77.
Definition: dcf77.c:153
static const uint8_t DFLAG_DCF77
Gives information about DCF77 and RTC.
Definition: display.h:119
RTC is running slower by 1%.
Definition: display.h:71
static void RTCsync(void)
Synchronizes the RTC with DCF77.
Definition: dcf77.h:45
volatile RTCAdj_t RTCAdj
Soft Adjustment of the RTC (+-1%)
Definition: display.c:65
uint8_t min
The Minute Value received.
Definition: dcf77.c:42
#define DCF77_PORT
Definition: dcf77.h:20