-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcf77.pas
167 lines (140 loc) · 4.01 KB
/
dcf77.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
{
/***************************************************************************
dcf77.pas
---------
***************************************************************************/
*****************************************************************************
This file is part of the Lazarus packages by Andreas Jakobsche
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
}
unit DCF77;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, GPIO,
ExtCtrls;
type
TClockState = (
csSyncSecStart {möglichen Anfang einer Sekunde suchen},
csCheckSecStart {Prüfung, ob tatsächlich ein Sekundenstart gefunden wurde},
csSyncMin {Minutenanfang suchen});
TTimeRec = record
Sec, Min, Hour: Byte;
Change_CET_CEST: Boolean; {Zeitumstellung am Ende der Stunde}
CEST: Boolean; {Sommerzeit}
LeapSec: Boolean; {Am Ende der Stunde wird eine Schaltsekunde eingefügt}
end;
type
{ TDCF77Clock }
TDCF77Clock = class(TComponent)
private
FGPIO: TGPIOPort;
FTimer: TTimer;
function GetInput: TGPIOPort;
function GetTimer: TTimer;
private
FBit, Check0, Check1: Boolean;
ClockState: TClockState;
FSecond: Byte;
Prepare, Current: TTimeRec;
Pulse: Byte;
procedure CountPulse;
function GetActiveLow: Boolean;
function GetInputAddr: TGPIOAddress;
procedure CheckInput(Sender: TObject);
procedure SetActiveLow(const AValue: Boolean);
procedure SetInputAddr(const AValue: TGPIOAddress);
{ Private declarations }
property Input: TGPIOPort read GetInput;
property Timer: TTimer read GetTimer;
protected
{ Protected declarations }
public
{ Public declarations }
property Bit: Boolean read FBit;
property Second: Byte read FSecond;
published
{ Published declarations }
property ActiveLow: Boolean read GetActiveLow write SetActiveLow;
property InputAddr: TGPIOAddress read GetInputAddr write SetInputAddr;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Hardware',[TDCF77Clock]);
end;
{ TDCF77Clock }
function TDCF77Clock.GetInput: TGPIOPort;
begin
if not Assigned(FGPIO) then begin
FGPIO := TGPIOPort.Create(Self);
FGPIO.Direction := pdInput;
end;
Result := FGPIO
end;
function TDCF77Clock.GetTimer: TTimer;
begin
if not Assigned(FTimer) then begin
FTimer := TTimer.Create(Self);
FTimer.Interval := 50;
FTimer.OnTimer := @CheckInput;
end;
Result := FTimer
end;
procedure TDCF77Clock.CountPulse;
begin
if Pulse = 19 then Pulse := 0 else Inc(Pulse)
end;
function TDCF77Clock.GetActiveLow: Boolean;
begin
Result := Input.ActiveLow;
end;
function TDCF77Clock.GetInputAddr: TGPIOAddress;
begin
Result := Input.Address;
end;
procedure TDCF77Clock.CheckInput(Sender: TObject);
begin
CountPulse;
FBit := Input.Value;
case ClockState of
csSyncSecStart: begin
if Bit then Pulse := 0;
ClockState := csCheckSecStart
end;
csCheckSecStart: begin
if Bit and (Pulse in [0, 1, 2, 3]) then ClockState := csSyncMin
else ClockState := csSyncSecStart
end;
csSyncMin: begin
if not Bit and (Pulse in [0, 1]) then Sec := 59;
ClockState := csCountSecond;
end;
csCountSecond: begin
if Bit and (Pulse in [0, 1]) then begin
IncSec;
ClockState := csPrepBit
end
end;
csPrepBit: begin
case Current.Sec of
20: if not Bit and (Pulse in [2, 3]) then ClockState := csSyncSecStart;
end
end;
csSetTime: begin
if Bit and (Pulse in [0, 1]) then CountSec;
end;
end;
end;
procedure TDCF77Clock.SetActiveLow(const AValue: Boolean);
begin
Input.ActiveLow := AValue
end;
procedure TDCF77Clock.SetInputAddr(const AValue: TGPIOAddress);
begin
Input.Address := AValue
end;
end.