-
Notifications
You must be signed in to change notification settings - Fork 0
/
DS1307 RTC control.spin
executable file
·529 lines (346 loc) · 11 KB
/
DS1307 RTC control.spin
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
' Datalogger Test
' 2-21-11 BJH
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000 '80 MHz
SCL = 28 'I2C display
SDT = 29
sSCL = 28 'RTC
sSDT = 29
CON
buffSize = 100
Chime = 8
Light = 9
SecondReminder = 10
SetTime = 15
DayStart = 6 'When "daytime" starts
NightStart = 20 'When "nighttime" starts
VAR long parameter1 'to pass @buff1 to ASM
long parameter2 'to pass @buff2 to ASM
long parameter3 'to pass sample rate to ASM
long parameter4 'to pass #samples to ASM
long buff1[buffSize]
long buff2[buffSize]
long j
byte Header[44]
long CogStack[20]
VAR
byte col
byte line
byte X
byte Scrmem[81]
byte DATABYTE
byte Time[7]
PUB Startup | g, gg, temp
bytefill(@scrmem,32,80)
Contrast(25)
CursorOFF
Bright(8)
CLR
'SetTheTime(10, 01, 5, 9, 15, 11, 0) 'hour, minute, day of week, day of month, month, year
repeat
DisplayTime
waitcnt(40_000_000 + cnt)
PUB DisplayTime | gg, temp
LoadTime
'Convert that crap to a date and time
POS(1,4)
Temp := Time[2] 'Hours tens (24 hour format)
Temp >>= 4
Temp &= %00000001
dbdec(temp, 0)
Temp := Time[2] 'Hours ones
Temp &= %0000_1111
dbdec(temp, 0)
dbtext(string(":"), 0)
Temp := Time[1] 'Minutes tens
Temp >>= 4
dbdec(temp, 0)
Temp := Time[1] 'Minutes ones
Temp &= %0000_1111
dbdec(temp, 0)
dbtext(string(":"), 0)
Temp := Time[0] 'Seconds tens
Temp >>= 4
dbdec(temp, 0)
Temp := Time[0] 'Seconds ones
Temp &= %0000_1111
dbdec(temp, 0)
Temp := Time[2]
if Temp & %00100000
dbtext(string(" PM "), 0)
else
dbtext(string(" AM "), 0)
Temp := Time[5] 'Month tens
Temp >>= 4
dbdec(temp, 0)
Temp := Time[5] 'Month ones
Temp &= %0000_1111
dbdec(temp, 0)
dbtext(string("-"), 0)
Temp := Time[4] 'Date tens
Temp >>= 4
dbdec(temp, 0)
Temp := Time[4] 'Date ones
Temp &= %0000_1111
dbdec(temp, 0)
dbtext(string("-"), 0)
Temp := Time[6] 'Year tens
Temp >>= 4
dbdec(temp, 0)
Temp := Time[6] 'Year ones
Temp &= %0000_1111
dbdec(temp, 1)
PUB LoadTime | gg
INIT
Start
Write(%11010000)
Write(0)
Stop
Start
Write(%11010001)
repeat gg from 0 to 6
Time[gg] := Read(0)
Stop
PUB SetTheTime(hours, minutes, dayofweek, month, day, year, ampm) | g, temp
INIT
Start
Write(%11010000)
Write(0) 'Start at address 0
Write(%0) 'Seconds, who gives a damn?
temp := minutes / 10 'Get minutes 10's place
temp <<= 4 'Shift 4
temp |= minutes - (minutes / 10 * 10) 'Get minutes one's place
Write(temp) 'Minutes
temp := hours / 10 'Get hours 10's place
temp <<= 4 'Shift 4
temp |= hours - (hours / 10 * 10) 'Get hours ones's place
temp |= %01000000 'Set 12 hour mode
temp |= ampm << 5 'Add the am (0) or pm (1) bit
Write(temp) 'Hours (12 hour format)
temp := dayofweek 'Get day of the week
Write(temp) 'Day of week
temp := day / 10 'Get day 10's place
temp <<= 4 'Shift 4
temp += day - (day / 10 * 10) 'Get day 1's place
Write(temp) 'Day
temp := month / 10 'Get months 10's place.
temp <<= 4 'Shift 4
temp += month - (month / 10 * 10) 'Get months ones's place
Write(temp) 'Month
temp := year / 10 'Get years 10's place. (nope, it's not Y2K2 compliant!)
temp <<= 4 'Shift 4
temp += year - (year / 10 * 10) 'Get years ones's place
Write(temp) 'Year
Stop
{LCD DRIVER CODE BEGIN}
PUB Alert | g 'Pulses brightness of LCD to get attention
repeat 3
repeat g from 8 to 1
Bright(g)
waitcnt((CLKFREQ / 1000)* 30 + cnt)
repeat g from 1 to 8
Bright(g)
waitcnt((CLKFREQ / 1000)* 30 + cnt)
Bright(8)
PUB CursorON
dbSTART
dbWrite($FE)
dbWrite($4B)
dbSTOP
PUB CursorOFF
dbSTART
dbWrite($FE)
dbWrite($4C)
dbSTOP
PUB Contrast(temp)
dbSTART
dbWrite($FE)
dbWrite($52)
dbWrite(temp) 'Set contrast 1-50 (low to high)
dbSTOP
PUB Bright(temp)
dbSTART
dbWrite($FE)
dbWrite($53)
dbWrite(temp) 'Set brightness 1-8
dbSTOP
PUB POS(curx,cury) | realline 'Position the cursor, using old Atari BASIC language :)
if cury == 1
realline := 0
if cury == 2
realline := 64
if cury == 3
realline := 20
if cury == 4
realline := 84
dbSTART
dbWrite($FE)
dbWrite($45)
dbWrite(realline+ (curx - 1))
dbSTOP
PUB CLR 'Clears screen, sets cursor/line to 1/1
dbSTART
dbWrite($FE)
dbWrite($51) 'Clear screen
col := 1
line := 1
dbSTOP
PUB CR 'Perform carriage-return (like there's even a mechanical carriage anymore)
'Only call from within other operations, since it does not contain its own START-STOP condition
col := 1
line := line + 1
if line == 5
line := 1
POS(col,line)
PUB Writescreen(temp)
Scrmem[x] := temp
PUB Message(str_addr)
POS(1,4)
dbText(string(" "),0)
POS(1,4)
dbText(str_addr,0)
PUB dbTEXT(stringptr,crbit) | temp, temp1, g, gg, count 'Prints text. 0 = no carriage return, 1= carriage return
'Includes word-wrap for fun & profit.
dbSTART
g := 0
count := 0
repeat strsize(stringptr)
temp := byte[stringptr + g]
dbWrite(temp)
col := col + 1
g := g + 1
if temp == 32 'Check if next word will fit on current line.
gg := g
count := col
repeat
if byte[stringptr + gg] == 32 or byte[stringptr + gg] == 0
quit
gg := gg + 1
count := count + 1
if count > 21 'If not, put cursor past limit to initiate carriage return.
col := 21
if col == 21
col := 1
line := line + 1
dbSTOP
POS(col,line)
dbSTART
if byte[stringptr + g] == 32
g := g + 1
if crbit
CR
PUB dbDEC(value,crbit) | i
'' Print a decimal number
dbSTART
if value < 0
-value
dbWrite("-")
i := 1_000_000_000
repeat 10
if value => i
dbWrite(value / i + "0")
value //= i
result~~
elseif result or i == 1
dbWrite("0")
i /= 10
if crbit
CR
dbSTOP
PUB dbBIN(value, digits)
'' Print a binary number
dbSTART
value <<= 32 - digits
repeat digits
dbWrite((value <-= 1) & 1 + "0")
CR
dbSTOP
PUB dbINIT ' An I2C device may be left in an
' invalid state and may need to be
outa[SCL] := 1 ' reinitialized. Drive SCL high.
dira[SCL] := 1
dira[SDT] := 0 ' Set SDA as input
repeat 9
outa[SCL] := 0 ' Put out up to 9 clock pulses
outa[SDT] := 1
if ina[SDT] ' Repeat if SDA not driven high
quit ' by the EEPROM
PUB dbSTART ' SDA goes HIGH to LOW with SCL HIGH
outa[SCL]~~ ' Initially drive SCL HIGH
dira[SCL]~~
outa[SDT]~~ ' Initially drive SDA HIGH
dira[SDT]~~
outa[SDT]~ ' Now drive SDA LOW
outa[SCL]~ ' Leave SCL LOW
dbWrite(254) 'Call address of I2C device, LCD screen
PUB dbSTOP ' SDA goes LOW to HIGH with SCL High
outa[SCL]~~ ' Drive SCL HIGH
outa[SDT]~~ ' then SDA HIGH
dira[SCL]~ ' Now let them float
dira[SDT]~ ' If pullups present, they'll stay HIGH
PUB dbWRITE(data1) : ackbit
ackbit := 0
data1 <<= 24
repeat 8 ' Output data to SDA
outa[SDT] := (data1 <-= 1) & 1
outa[SCL]~~ ' Toggle SCL from LOW to HIGH to LOW
outa[SCL]~
dira[SDT]~ ' Set SDA to input for ACK/NAK
outa[SCL]~~
ackbit := ina[SDT] ' Sample SDA when SCL is HIGH
outa[SCL]~
outa[SDT]~ ' Leave SDA driven LOW
dira[SDT]~~
{DEBUG LCD CODE END}
{I2C DRIVER FOR REAL TIME CLOCK BEGIN}
PUB INIT ' An I2C device may be left in an
' invalid state and may need to be
outa[sSCL] := 1 ' reinitialized. Drive SCL high.
dira[sSCL] := 1
dira[sSDT] := 0 ' Set SDA as input
repeat 9
outa[sSCL] := 0 ' Put out up to 9 clock pulses
outa[sSDT] := 1
if ina[sSDT] ' Repeat if SDA not driven high
quit ' by the EEPROM
PUB START ' SDA goes HIGH to LOW with SCL HIGH
outa[sSCL]~~ ' Initially drive SCL HIGH
dira[sSCL]~~
outa[sSDT]~~ ' Initially drive SDA HIGH
dira[sSDT]~~
outa[sSDT]~ ' Now drive SDA LOW
outa[sSCL]~ ' Leave SCL LOW
PUB STOP ' SDA goes LOW to HIGH with SCL High
outa[sSCL]~~ ' Drive SCL HIGH
outa[sSDT]~~ ' then SDA HIGH
dira[sSCL]~ ' Now let them float
dira[sSDT]~ ' If pullups present, they'll stay HIGH
PUB WRITE(data1) : ackbit
ackbit := 0
data1 <<= 24
repeat 8 ' Output data to SDA
outa[sSDT] := (data1 <-= 1) & 1
outa[sSCL]~~ ' Toggle SCL from LOW to HIGH to LOW
outa[sSCL]~
dira[sSDT]~ ' Set SDA to input for ACK/NAK
outa[sSCL]~~
ackbit := ina[sSDT] ' Sample SDA when SCL is HIGH
outa[sSCL]~
outa[sSDT]~ ' Leave SDA driven LOW
dira[sSDT]~~
PUB Read(ackbit): data
'' Read in i2c data, Data byte is output MSB first, SDA data line is
'' valid only while the SCL line is HIGH. SCL and SDA left in LOW state.
data := 0
dira[sSDT]~ ' Make SDA an input
repeat 8 ' Receive data from SDA
outa[sSCL]~~ ' Sample SDA when SCL is HIGH
data := (data << 1) | ina[sSDT]
outa[sSCL]~
outa[sSDT] := ackbit ' Output ACK/NAK to SDA
dira[sSDT]~~
outa[sSCL]~~ ' Toggle SCL from LOW to HIGH to LOW
outa[sSCL]~
outa[sSDT]~ ' Leave SDA driven LOW
{I2C DRIVER FOR REAL TIME CLOCK END}