-
Notifications
You must be signed in to change notification settings - Fork 5
/
wordclock_mega8.PAS
549 lines (494 loc) · 13.1 KB
/
wordclock_mega8.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
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
program wordclock;
// Version 1.02 mit Abschaltung nachts für DCF-Resync
// Wordclock-Version für c't Wordclock-Platine (Segor-Bausatz)
// Q&D Version basiert auf WordClock für PONG, aber verbesserte Buchstaben-Kodierung
// C. Meyer 7/2013
{$NOSHADOW}
{ $WG} {global Warnings off}
Device = mega8, VCC=5;
{ $BOOTRST $00C00} {Reset Jump to $00C00}
Define_Fuses
// Override_Fuses;
NoteBook = A;
COMport = USB;
LockBits0 = [];
FuseBits0 = [CKSEL0, CKSEL1, CKSEL3, SUT0, BODEN];
FuseBits1 = [];
ProgMode = SPI;
ProgFuses = true;
ProgLock = false;
ProgFlash = true;
ProgEEprom = false;
Import SysTick, TWImaster, RTClock, TickTimer;
Import DCFclock;
From System Import LongWord, Random;
Define
ProcClock = 8000000; {Hertz}
SysTick = 10; {msec}
StackSize = $0032, iData;
FrameSize = $0032, iData;
TWIpresc = TWI_BR100;
TickTimer = Timer1;
RTClock = iData, Time;
RTCsource = SysTick;
DCFclock = iData;
DCFport = PinB, 2, positive; {Port, bitnummer, Polarität}
DCFfieldMode = decrement; // decrement, optional "reset"
Implementation
{$IDATA}
{--------------------------------------------------------------}
{ Type Declarations }
type
{--------------------------------------------------------------}
{ Const Declarations }
const
sr_on: Boolean = false;
sr_off: Boolean = true;
rtc_adr: byte = $68;
// Buchstaben-Pattern, 12 Bit wie angezeigt, plus Zeile im obersten Nibble
c_es_war: word = $0000 or %110111000000;
c_es_ist: word = $0000 or %110000111000;
c_gleich: word = $1000 or %111111000000;
c_genau: word = $1000 or %000000011111;
c_gerade: word = $2000 or %111111000000;
c_fuenfmin: word = $2000 or %000000011110;
c_viertel: word = $3000 or %000011111110;
c_dreiviertel: word = $3000 or %111111111110;
c_zehnmin: word = $4000 or %111100000000;
c_zwanzig: word = $4000 or %000001111111;
c_nach: word = $5000 or %111100000000;
c_vor: word = $5000 or %000011100000;
c_halb: word = $5000 or %000000001111;
c_drei: word = $6000 or %111100000000;
c_ein: word = $6000 or %001110000000;
c_eins: word = $6000 or %001111000000;
c_sieben: word = $6000 or %000001111110;
c_elf: word = $7000 or %111000000000;
c_fuenf: word = $7000 or %001111000000;
c_zwoelf: word = $7000 or %000000011111;
c_vier: word = $8000 or %111100000000;
c_zwei: word = $8000 or %000011110000;
c_acht: word = $8000 or %000000001111;
c_neun: word = $9000 or %011110000000;
c_sechs: word = $9000 or %000000111110;
c_zehn: word = $A000 or %111100000000;
c_uhr: word = $A000 or %000001110000;
c_vor12: word = $A000 or %000000000111;
c_nach12: word = $B000 or %111100000000;
c_nachts: word = $B000 or %111111000000;
c_mittag: word = $B000 or %000000111111;
c_test: word = $0FFF;
{--------------------------------------------------------------}
{ Var Declarations }
var
{$PDATA}
SR_CLK[@PortB,5]: Bit;
SR_DATA[@PortB,3]: Bit;
HOURBTN[@PinB,1]: Bit;
MINUTEBTN[@PinB,0]: Bit;
TESTPIN[@PinB,4]: Bit;
DCFPIN[@PinB,2]: Bit;
{$DATA}
b, i, j, led_row: byte;
LED_word: word;
{$IDATA}
DCFtoDS1307done, TickSema, Blinkenlights,
SecondSema, MinuteSema, DCFpresent: Boolean;
LED_word_array: Array[0..11] of word;
// Einzel-Zeilen, evt. einmal gebraucht
LED_word_line0[@LED_word_array+0]: word;
LED_word_line1[@LED_word_array+24]: word;
LED_word_line2[@LED_word_array+48]: word;
LED_word_line3[@LED_word_array+72]: word;
LED_word_line4[@LED_word_array+96]: word;
LED_word_line5[@LED_word_array+120]: word;
LED_word_line6[@LED_word_array+144]: word;
LED_word_line7[@LED_word_array+168]: word;
LED_word_line8[@LED_word_array+192]: word;
LED_word_line9[@LED_word_array+216]: word;
LED_word_line10[@LED_word_array+240]: word;
LED_word_line11[@LED_word_array+264]: word;
UpdateTimer: SysTimer8;
BlinkTimer, LED_select,
hour, minute: Byte;
NightMode: Boolean;
{--------------------------------------------------------------}
{ functions }
procedure onTickTimer;
begin
TickSema:= true;
end;
procedure RTCtickMinute;
begin
MinuteSema:= true;
end;
procedure RTCtickSecond;
begin
SecondSema:= true;
end;
function DS1307getSecond: byte;
var my_byte: byte;
begin
TWIout(rtc_adr,0); // Sekunden-Register
TWIinp(rtc_adr,my_byte);
return(BCDtoByte(my_byte));
end;
function DS1307getMinute: byte;
var my_byte: byte;
begin
TWIout(rtc_adr,1); // Minuten-Register
TWIinp(rtc_adr,my_byte);
return(BCDtoByte(my_byte));
end;
function DS1307getHour: byte;
var my_byte: byte;
begin
TWIout(rtc_adr,2); // Stunden-Register
TWIinp(rtc_adr,my_byte);
return(BCDtoByte(my_byte and $3F));
end;
procedure DS1307setSecond(my_second: byte);
var my_byte: byte;
begin
my_Byte:= ByteToBCD(my_second) and $7F; // Clock HALT Bit immer löschen
TWIout(rtc_adr,0,my_Byte); // Sekunden-Register
end;
procedure DS1307setMinute(my_minute: byte);
var my_byte: byte;
begin
my_Byte:= ByteToBCD(my_Minute);
TWIout(rtc_adr,1,my_Byte); // Minuten-Register
end;
procedure DS1307setHour(my_hour: byte);
var my_byte: byte;
begin
my_Byte:= ByteToBCD(my_hour) and $3F;
TWIout(rtc_adr,2,my_Byte); // Stunden-Register
end;
//##############################################################################
procedure InitPorts;
begin
PortB:= %00010111; // Pin B0 = Set Minute, B1 = Set Hous, B2 = DCF77 in
DDRB:= %00101000; // Pin B4 = Test
PortC:= %00000000;
DDRC:= %00001111;
PortD:= %00000000;
DDRD:= %11111111;
end InitPorts;
procedure LED_out;
begin
PortC:=hi(LED_word) and $0F;
PortD:=lo(LED_word);
end;
procedure LED_off;
begin
PortC:=(PortC and $F0);
PortD:=0;
end;
procedure LED_SR_next;
begin
LED_off;
if NightMode then
// nachts Pause für DCF-Resync
return;
endif;
if not inctolim(led_row, 11) then
led_row:= 0;
SR_DATA:= sr_on;
SR_CLK:= true;
nop; nop; nop;
SR_CLK:= false;
SR_DATA:= sr_off;
LED_word:= LED_word_array[0];
else
SR_CLK:= true;
nop; nop; nop;
SR_CLK:= false;
endif;
LED_word:= LED_word_array[led_row];
LED_out;
end;
//##############################################################################
procedure ptn2led(my_pattern: word);
var my_idx: byte;
begin
my_idx:= hi(my_pattern) shr 4; // Zeilennummer im obersten Nibble
LED_word_array[my_idx]:= LED_word_array[my_idx] or my_pattern;
end;
procedure time_to_letters(my_hour, my_minute: byte; my_adjust: boolean);
var my_div, my_mod, my_addhour: byte; my_disp_uhr: Boolean;
begin
for i:= 0 to 11 do
LED_word_array[i]:= 0;
endfor;
my_disp_uhr:= false;
my_mod:= my_minute mod 5;
// Minute words
my_addhour:= 0;
my_div:= my_minute div 5;
case my_mod of // Einzel-Minuten
0:
ptn2led(c_es_ist);
ptn2led(c_genau);
|
1,2:
ptn2led(c_es_war);
ptn2led(c_gerade);
|
3,4:
ptn2led(c_es_ist);
ptn2led(c_gleich);
inc(my_div);
if my_minute > 57 then
my_addhour:= 1;
endif;
|
endcase;
case my_div of // 5-Minuten-Schritte
0,12:
my_disp_uhr:= true;
|
1:
ptn2led(c_fuenfmin);
ptn2led(c_nach);
|
2:
ptn2led(c_zehnmin);
ptn2led(c_nach);
|
3:
ptn2led(c_viertel);
ptn2led(c_nach);
|
4:
ptn2led(c_zwanzig);
ptn2led(c_nach);
|
5:
ptn2led(c_fuenfmin);
ptn2led(c_vor);
ptn2led(c_halb);
my_addhour:= 1;
|
6:
ptn2led(c_halb);
my_addhour:= 1;
|
7:
ptn2led(c_fuenfmin);
ptn2led(c_nach);
ptn2led(c_halb);
my_addhour:= 1;
|
8:
ptn2led(c_zwanzig);
ptn2led(c_vor);
my_addhour:= 1;
|
9:
ptn2led(c_dreiviertel);
my_addhour:= 1;
|
10:
ptn2led(c_zehnmin);
ptn2led(c_vor);
my_addhour:= 1;
|
11:
ptn2led(c_fuenfmin);
ptn2led(c_vor);
my_addhour:= 1;
|
endcase;
my_hour:= my_hour + my_addhour;
case my_hour of // Stunden-Schritte
0,12,24:
ptn2led(c_zwoelf);
|
1,13:
if my_disp_uhr then
// Sonderfall ein"s"
ptn2led(c_ein);
else
ptn2led(c_eins);
endif;
|
2,14:
ptn2led(c_zwei);
|
3,15:
ptn2led(c_drei);
|
4,16:
ptn2led(c_vier);
|
5,17:
ptn2led(c_fuenf);
|
6,18:
ptn2led(c_sechs);
|
7,19:
ptn2led(c_sieben);
|
8,20:
ptn2led(c_acht);
|
9,21:
ptn2led(c_neun);
|
10,22:
ptn2led(c_zehn);
|
11,23:
ptn2led(c_elf);
|
endcase;
if my_disp_uhr then
ptn2led(c_uhr);
endif;
case my_hour of
0..5,24:
ptn2led(c_nachts);
|
9..11:
ptn2led(c_vor12);
ptn2led(c_mittag);
|
12..13:
ptn2led(c_mittag);
|
14..16:
ptn2led(c_nach12);
ptn2led(c_mittag);
|
endcase;
end;
{--------------------------------------------------------------}
{ Main Program }
{$IDATA}
begin
InitPorts;
TickTimerTime(600); // Wert in us
TickTimerStart;
Blinkenlights:= true;
BlinkTimer:= 20;
EnableInts;
MinuteSema:= true;
i:= DS1307getSecond; // First Power up, Clock HALT Bit löschen
DS1307setSecond(i);
DCFtoDS1307done:= false;
TESTPIN:= false;
led_row:= 0;
j:= 0;
DCFpresent:= false;
// Testmodus. Wenn Pin 1 mit Pin 6 des ISP verbunden,
// alle Zeilen einmal komplett beleuchten
repeat
for i:= 0 to 11 do
if i = j then
LED_word_array[i]:= $0555;
else
LED_word_array[i]:= $0AAA;
endif;
endfor;
if TickSema then
TickSema:=false;
LED_SR_next;
if not DCFPIN then // ist beim Einschalten kurz low
DCFpresent:= true;
endif;
endif;
if SecondSema then
inc(j);
SecondSema:= false;
endif;
until j=12;
loop
if TickSema then
TickSema:=false;
LED_SR_next;
endif;
if (not DCFready) then
// Solange kein gültiges DCF-Telegramm empfangen wurde oder Empfang schlecht,
// Uhrzeit aus DS1307 holen
if MinuteSema then
minute:= DS1307getMinute;
hour:= DS1307getHour;
RTCsetMinute(minute);
RTCsetHour(hour);
//Blinkenlights:= true;
BlinkTimer:= 10;
MinuteSema:= false;
time_to_letters(hour, minute,false);
endif;
else
// DCF ist OK, Uhrzeit aus synchronisierter AVR-RTC holen
if MinuteSema then
minute:= RTCgetMinute;
hour:= RTCgetHour;
//Blinkenlights:= true;
BlinkTimer:= 10;
MinuteSema:= false;
time_to_letters(hour, minute,false);
if (not DCFtoDS1307done) then
// bei einwandfreiem Empfang Uhrenchip einmal auf DCF-Zeit setzen
BlinkTimer:= 50;
Blinkenlights:= true;
DS1307setHour(hour);
DS1307setMinute(minute);
DS1307setSecond(RTCgetSecond);
DCFtoDS1307done:= true;
endif;
endif;
endif;
if MinuteSema then
//Blinkenlights:= true;
BlinkTimer:= 10;
MinuteSema:= false;
time_to_letters(hour, minute,false);
endif;
if SecondSema then
NightMode:= valueinrange(hour,1,4);
SecondSema:= false;
if not MINUTEBTN then // neg. Logik!
if inctolimwrap(minute,59,0) then
inctolimwrap(hour,23,0);
endif;
DS1307setSecond(0);
DS1307setMinute(minute);
DS1307setHour(hour);
RTCsetSecond(0);
RTCsetMinute(minute);
RTCsetHour(hour);
time_to_letters(hour, minute, true);
Blinkenlights:= false;
NightMode:= false;
endif;
if not HOURBTN then // neg. Logik!
inctolimwrap(hour,23,0);
DS1307setSecond(0);
DS1307setMinute(minute);
DS1307setHour(hour);
RTCsetSecond(0);
RTCsetMinute(minute);
RTCsetHour(hour);
time_to_letters(hour, minute, true);
Blinkenlights:= false;
NightMode:= false;
endif;
endif;
if Blinkenlights and DCFpresent then
if isSysTimerZero(UpdateTimer) and Blinkenlights then
setSysTimer(UpdateTimer, lo(randomrange(5,15)));
for i:= 0 to 11 do
LED_word_array[i]:= (LED_word_array[i] + random) and $FFF;
endfor;
if not dectolim(BlinkTimer, 0) then
Blinkenlights:= false;
time_to_letters(hour, minute,false);
endif;
endif;
endif;
endloop;
end wordclock.