-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClock 2.spin
executable file
·375 lines (238 loc) · 8.9 KB
/
Clock 2.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
CON
_CLKMODE = XTAL1 + PLL16X
_XINFREQ = 5_000_000
JClock = 24
JReset = 25
sSCL = 28 'RTC
sSDT = 29
Latch = 0
Clock = 1
DataL = 2
VAR
long Smem[7] 'Screen memory, one long per row, times 7 rows
long Sbuffer[7] 'Buffer to build image before copy
long NumTemp[7] 'Buffer for shifting numeric characters
long parameter1
long Blinker
byte Time[7]
long LEDtime
byte DayOfWeek
byte colontime
byte LastDial
PUB Main | g
dira[0..2]~~
dira[3..7]~
dira[JClock]~~
dira[JReset]~~
Blinker := cnt
'SetTheTime(8, 23, 3, 9, 20, 11, 1) 'hour, minute, day of week, day of month, month, year, 0 = AM, 1 = PM
repeat
colontime := 0
DisplayTime
waitcnt(Blinker += 80_000_000)
colontime := 1
DisplayTime
waitcnt(Blinker += 80_000_000)
PUB Dial | g, newdial, output
NewDial := ina[4..3]
if (LastDial <> NewDial) 'Did it change?
repeat g from 1 to 4
if (NewDial == 0) 'A match?
if (LastDial == 1)
output := 2
if (LastDial == 2)
output := 1
if (NewDial == 2) 'A match?
if (LastDial == 0)
output := 2
if (LastDial == 3)
output := 1
if (NewDial == 3) 'A match?
if (LastDial == 2)
output := 2
if (LastDial == 1)
output := 1
if (NewDial == 1) 'A match?
if (LastDial == 3)
output := 2
if (LastDial == 0)
output := 1
LastDial := NewDial
return output
else
return 0
PUB LoadNumber(num, position) | g, temp
temp := number[num] 'Load number
temp <<= ((3 - position) * 8)
LEDtime |= temp
PUB Clear
LEDtime := 0
DayOfWeek := 0
PUB DisplayTime | gg, temp
LoadTime
Clear
'Convert that crap to a date and time
Temp := Time[2] 'Hours tens
Temp >>= 4
Temp &= %00000001
LoadNumber(temp, 0)
Temp := Time[2] 'Hours ones
Temp &= %0000_1111
LoadNumber(temp, 1)
LoadNumber(10, 10) 'Colon
Temp := Time[1] 'Minutes tens
Temp >>= 4
LoadNumber(temp, 2)
Temp := Time[1] 'Minutes ones
Temp &= %0000_1111
LoadNumber(temp, 3)
DayofWeek := 1 << Time[3] 'Get day of week
if (ColonTime)
LEDtime |= %00000000_00000000_00000001_00000000
Temp := Time[2]
if (Temp & %00100000)
LEDtime += %00000000_00000001_00000000_00000000 'PM light
else
LEDtime += %00000001_00000000_00000000_00000000 'PM light
PollIO
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, dayweek, 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 := dayweek '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
Write(%00010011) 'Control register
Stop
PUB PollIO | temp0
OUTA[Latch]~ 'Set latch...
OUTA[Clock]~ '... and clock to LOW to get started.
OUTA[Latch]~~ 'Set latches HIGH to start. No change for the lights (registers should still be same as last cycle), brings up first bit of Sense
OUTA[Latch]~ 'Reset latch for next time
repeat 3
temp0 := number[8]
repeat 8
OUTA[DataL] := temp0 'Set next LSB bit for Light0 OUT
OUTA[Clock]~~ 'CLK input and output shift registers
OUTA[Clock]~ 'Sends OUT light data, brings IN sense data, which we now check
temp0 >>= 1 'Shift light bits RIGHT to put next one in LSB
temp0 := DayOfWeek
repeat 8
OUTA[DataL] := temp0 'Set next LSB bit for Light0 OUT
OUTA[Clock]~~ 'CLK input and output shift registers
OUTA[Clock]~ 'Sends OUT light data, brings IN sense data, which we now check
temp0 >>= 1 'Shift light bits RIGHT to put next one in LSB
temp0 := !LEDtime
repeat 32
OUTA[DataL] := temp0 'Set next LSB bit for Light0 OUT
OUTA[Clock]~~ 'CLK input and output shift registers
OUTA[Clock]~ 'Sends OUT light data, brings IN sense data, which we now check
temp0 >>= 1 'Shift light bits RIGHT to put next one in LSB
OUTA[Latch]~~
OUTA[Latch]~ 'Set latches to output light data (also re-latches input data but who cares?)
{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}
DAT
number
byte %11101110 'Zero
byte %00101000 'One
byte %01110110 'Two
byte %01111100 'Three
byte %10111000 'Four
byte %11011100 'Five
byte %11011110 'Six
byte %01101000 'Seven
byte %11111110 'Eight
byte %11111100 'Nine
colon
long %00000001_00000001_00000001_00000001
rotary
byte 1
byte 0
byte 2
byte 3
byte 1
byte 0