-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDS-1307RTC.TXT
142 lines (111 loc) · 3.82 KB
/
DS-1307RTC.TXT
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
\ Language: ESP32Forth
\ Simplified use of the DS1307 Real Time Clock I2C chip.
\ Read the documentation for DS1307 for more details:
\ https://datasheets.maximintegrated.com/en/ds/DS1307.pdf
\ Author: Jan Langevad - february 2022 - free software.
\ Not claiming that this is optimal, but an introduction to RTC an I2C
\ Good ideas are welcome 🙂
base @
decimal
0 value TimeBuff
INTERNALS \ <**********
: GetBuffMemory
10 malloc ( --- 0/Addr allocate byte buffer for Time RTC data RAM )
is TimeBuff
;
GetBuffMemory
FORTH
hex \ <---------- OBS - Practical with BCD numbers too
\ 15 constant GPIO21 ( SDA ) \ = 21
\ 16 CONSTANT GPIO22 ( SCL )
\ : INIT-PINS GPIO21 OUTPUT PINMODE LOW GPIO21 PIN
\ GPIO22 OUTPUT PINMODE LOW GPIO22 PIN ;
\ INIT-PINS
variable second
variable minute
variable hour
variable dayofweek
variable dayofmonth
variable month
variable year
variable nullbyte
variable WireOK? \ save wire prep stack result here
0 WireOK? !
( OBS: BCD - Binary Coded Decimal! TESTDATA: )
22 year !
02 month !
28 dayofmonth !
02 dayofweek ! \ d# 1..7 Sunday=1
12 hour ! \ Default i RTC is 24 Hour format
34 minute !
00 second !
0 nullbyte !
: setuptimeBCD \ yy mm dd d# hh mm ss --- \ to entered with HEX BASE
second C!
minute C!
hour C!
dayofweek C! \ 1...7 1 = sunday
dayofmonth C!
month C!
year C!
0 TimeBuff c!
0 TimeBuff 1+ c!
second c@ TimeBuff 2 + c!
minute c@ TimeBuff 3 + c!
hour c@ TimeBuff 4 + c!
dayofweek c@ TimeBuff 5 + c! \ 1...7 1 = sunday
dayofmonth c@ TimeBuff 6 + c!
month c@ TimeBuff 7 + c!
year c@ TimeBuff 8 + c!
;
22 02 28 2 12 34 00 setuptimeBCD \ test yy mm dd d# hh mm ss ---
WIRE \ change vocabulary
: Wire.Prep 15 16 Wire.begin WireOK? ! ; \ initialize IC2 pins SDA SCL ---
Wire.Prep \ 15 16 hex = 21 22 decimal GPIO pin #
cr ." WireOK?=1 is OK: " WireOK? @ . cr
: settime \ $68 is the I2C address used by DS1307 RTC chip
68 Wire.beginTransmission
\ unfortunately Wire.write in our Forth requires adresses and not values on stack
\ Therefore use of TimeBuff
TimeBuff 1 Wire.write drop \ send register address
TimeBuff 1 Wire.write drop \ reset seconds!! and start RTC
TimeBuff 3 + 1 Wire.write drop \ minute
TimeBuff 4 + 1 Wire.write drop \ hour
TimeBuff 5 + 1 Wire.write drop \ dayofweek
TimeBuff 6 + 1 Wire.write drop \ dayofmonth
TimeBuff 7 + 1 Wire.write drop \ month
TimeBuff 8 + 1 Wire.write drop \ year
1 Wire.endTransmission drop \ --- 1 = stop option, drop return 0= ok 1..4 see doc!
;
: mmssAdjustTime \ mm ss --- Just setting Minute and Second in RTC:
TimeBuff 2 + c!
TimeBuff 3 + c!
68 Wire.beginTransmission
TimeBuff 1 Wire.write drop \ send register address
TimeBuff 2 + 1 Wire.write drop \ reset seconds!! and start RTC
TimeBuff 3 + 1 Wire.write drop \ minute
1 Wire.endTransmission drop \ --- 1 = stop option, drop return 0= ok 1..4 see doc!
;
: gettime ( --- SS MM HH D# DD MM YY )
68 Wire.beginTransmission
nullbyte 1 Wire.write drop \ send register address
0 Wire.endTransmission drop \ --- 0 = cont. option, drop return 0= ok 1..4 see d
68 7 -1 Wire.requestFrom drop
Wire.Available drop \ --- # bytes to read
Wire.Read \ second !
Wire.Read \ minute !
Wire.Read \ hour !
Wire.Read \ dayofweek !
Wire.Read \ dayofmonth !
Wire.Read \ month !
Wire.Read \ year !
\ Apparently ot needed(!/?):
\ 1 Wire.endTransmission drop \ --- 1 = stop option, drop return 0= ok 1..4 see d
;
: gettime. gettime . . . . . . . ; \ Print time on terminal
: gettime! gettime \ Store current time in memory
year C! month C! dayofmonth C! dayofweek C! hour C! minute C! second C!
;
FORTH
BASE !
\ EOF