-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNixieClock.ino
487 lines (431 loc) · 11.5 KB
/
NixieClock.ino
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
/*
Author : MeetinaXD
(meetinaxd@ltiex.com)
Last Edit : Octo 27,2019. 20:09 (UTC + 08)
Program : NixieClock Control Unit (ATMega 328p-u in Arduino)
Modify Logs :
* Octo 27,20:09, 增加防中毒和看门狗功能
* Octo 31,14:28, 修改isOverTime中的错误;更改喂狗次序,修复一开机为功能3时导致重启的问题
* Nove 2 ,18:15, 修改了开始动画的逻辑,必须选择功能后才出现,修改了防中毒逻辑。
<<<<<<< HEAD
<<<<<<< HEAD
* Nove 3 ,--:--, 修复当显示的纪念日正计时不为三位数时,小数点可能不会正确显示的问题。
现在纪念日,日数小数点的位置不再被固定在第三位,根据数字长度自动显示在后面。
<<<<<<< HEAD
* Nove 5 ,21:06, 删除了refresh方法,合并到sendData中
buildDate不再指定,使用__DATE__宏定义
不再使用bits数组储存数字,使用NixieTube结构体,方便控制灯的开关
规范数字赋值,统一使用setNixie()方法设置数字
新增getNixie获取当前辉光管的数字,不允许直接访问变量
修复BUG:灯关闭时不进行辉光管刷新
=======
>>>>>>> parent of b228e38... 补充说明
=======
>>>>>>> parent of b228e38... 补充说明
=======
>>>>>>> parent of db6f504... 一堆更改
WARNING:
THIS PROGRAM IS NOT A FREE SOFTWARE, YOU ARE NOT
ALLOW TO REDISTRIBUTE IT AND/OR MODIFY IT FOR
PROFIT WITHOUT AUTHOR'S PERMISSION.
IF YOU ARE PERSONAL USE,NOT FOR PARTICULAR PURPOSE,
YOU CAN USE BUT YOU SHOULD ACCEPT THE GNU LESSER
GENERAL PUBLIC LICENSE,AND COPY THIS LICENSE ALONG
WITH THIS PROGRAM.
************** ALL RIGHTS RESERVED. **************
*/
#include "./NixieClock.h"
//看门狗 https://blog.csdn.net/sdlgq/article/details/50396518
RTC_DS1307 RTC; //时钟模块
byte bits[6] = {0,0,0,0,0,0};//当前数字位
byte old_bits[6] = {0,0,0,0,0,0};//上一个数字位
byte Data[3] = {69,69,69};//45 45 45
byte Date[6] = {19,7,30,15,26,0};//日期时间储存变量
// const byte LoveDateTime[6] = {19,3,14,17,0,0};//日期时间储存变量
DateTime NextRefreshTime;
byte state = 0; //开关功能选择
byte old_state = 0; //上一个开关状态
bool change = true;//世界线是否产生变动
/* 0.息屏 1.时间 2.计时 3.世界线 */
void (*funcList[4])(void) = {showNothing,showTime,showCounter,showWorldChange};
void setup() {
DDRC = 0xFF;
PORTC = 0xF;//前四位控制小数点
Wire.begin();
RTC.begin();
pinMode(BUTTONA,INPUT_PULLUP);
pinMode(BUTTONB,INPUT_PULLUP);
for (byte i = 0;i < 3;i++){
pinMode(DS[i],OUTPUT);
digitalWrite(DS[i],LOW);
pinMode(MR[i],OUTPUT);
digitalWrite(MR[i],LOW);
digitalWrite(MR[i],HIGH);
pinMode(SHCP[i],OUTPUT);
}
pinMode(STCP,OUTPUT);
digitalWrite(STCP,LOW);
digitalWrite(SHCPA,HIGH);
digitalWrite(SHCPB,HIGH);
digitalWrite(SHCPC,HIGH);
/* isrunnning方法存在问题,每次编译后首次运行都返回true,已经弃用。 */
// if (!RTC.isrunning()){//如果DS1302模块没有在运行
// /* 设置为编译时的时间 */
// RTC.adjust(DateTime(__DATE__, __TIME__));
// }
/* 开启看门狗,8秒无响应后重启机器 */
do{
state = (digitalRead(BUTTONA) << 1) | digitalRead(BUTTONB);
delay(100);
}while(state == 0);
wdt_enable(WDTO_8S);
randomSeed(RTC.now().unixtime());
startingEffect(); //展示开机效果
NextRefreshTime = RTC.now() + TimeSpan(__OVER_TIME__);
}
void loop(){
// state = 1;
//要先喂狗,否则世界线变动会重启
wdt_reset();//记得喂狗
state = (digitalRead(BUTTONA) << 1) | digitalRead(BUTTONB);
funcList[state]();
if(isOverTime() && state != 0) //是否已经到刷新时间
refreshNixie();
}
/* 取位操作,最多八位 */
byte getBit(byte number,byte pos){
return ((number & movList[pos - 1]) > 0);
}
/* 设置辉光管显示的数字 */
void setNixie(byte i,byte number){
if(bits[i] != 0xFF)
bits[i] = number & 0xF;
}
/* 开启辉光管显示,参数:辉光管编号,注意开启后辉光管仍然无显示 */
void lightUpNixie(byte i){
if (bits[i] == 0xF)
bits[i] = 0xE;
}
/* 关闭辉光管显示,参数:辉光管编号 */
void closeDownNixie(byte i){
bits[i] = 0xB;
}
/* 点亮小数点 */
void lightUpPoint(byte i){
byte p = i;
if(i == 4) p = 5;
if(i == 5) p = 4;
PORTC &= 0xF0;
PORTC |= (p & 0xF);
}
/* 熄灭小数点*/
void closeDownPoint(){
PORTC |= 0xF;
}
/* 74HC595N译码器输出引脚使能 */
void refresh(){
digitalWrite(STCP,LOW);
digitalWrite(STCP,HIGH);//上升沿
}
/* 发送数据到74HC595N译码器 */
void sendData(){
Data[0] = bits[0] << 4 | bits[1];
Data[1] = bits[2] << 4 | bits[3];
Data[2] = bits[4] << 4 | bits[5];
byte s = 69;
for (byte k = 0;k < 3;k++){
byte v = 0;
for (byte i = 8;i > 0;i--){
v = getBit(Data[k],i);
digitalWrite(DS[k],v);
digitalWrite(SHCP[k],LOW);
digitalWrite(SHCP[k],HIGH);
}
}
refresh();
}
/* 是否已经超时 */
bool isOverTime(){
TimeSpan gap = NextRefreshTime - RTC.now();
if (gap.totalseconds() < 0){ //已经超时
return true;
}
return false;
}
/* 辉光管防中毒,刷新辉光管 */
void refreshNixie(){
/*
1.闪烁所有的辉光管
2.同时轮转所有的辉光管
*/
simulateFlash(0x3F,0); //闪烁所有辉光管
roundNixie(0x3F,6); //轮转所有的辉光管,6圈。
//更新下次刷新时间
NextRefreshTime = RTC.now() + TimeSpan(__OVER_TIME__);
}
/* 显示世界线变动 */
void showWorldChange(){
/*
逻辑实现: 1.世界线未变动时,小数点0闪烁(慢闪)
2.世界线即将产生变动时,小数点0快闪,然后变化
3.世界线会不稳定,表现为突然没有征兆变化为另外的数字,然后过一会儿恢复为原来的数字
4.随机有荧光管频闪的效果
*/
if (state != old_state){//状态被改变
old_state = state;
for (byte i = 0; i < 6; i++){//关闭所有辉光管
closeDownNixie(i);
}
sendData();
refresh();
change = true;
}
//世界线变动
if (change){
simulateFlash(1,0);
lightUpNixie(0);
setNixie(0,random(0,1));
roundNixie(1,2);
for (int i = 0; i < 10; i++){ //小数点快闪
lightUpPoint(0);
delay(100);
closeDownPoint();
delay(100);
}
byte NixieTube = 0;
for (byte i = 1; i < 6; i++){ //点亮辉光管,设置随机数后开始轮转
NixieTube = 1 << i;
lightUpNixie(i);
setNixie(i,random(0,9)&0xF);
roundNixie(NixieTube,2);
delay(35);
}
change = false;
}
/* 随机频闪 */
if (random(1,64) == 4){ //世界线变动
change = true;
return;
}
if (random(1,32) == 5)
simulateFlash(0x3F,0); //模拟闪烁效果(所有辉光管)
if (random(1,64) == 6){//世界线不稳定
for (int i = 0; i < 6; i++){
old_bits[i] = bits[i];
bits[i] = random(0,9)&0xF;
}
lightUpPoint(0);
sendData();
refresh();
delay(random(800,3000));
for (int i = 0; i < 6; i++)
bits[i] = old_bits[i];
sendData();
refresh();
}
{//什么都没有发生的时候
lightUpPoint(0);
delay(500);
closeDownPoint();
delay(500);
}
// delay(35);
}
/* 黑屏,什么都不显示 */
void showNothing(){
if (state!=old_state){
old_state = state;
for (int i = 0; i < 6; i++)
closeDownNixie(i);
sendData();
refresh();
}
delay(40);
}
/* 显示当前时间 */
void showTime(){
old_state = state;
byte NixieTube = 0;
readFromRTC_Module();
bits[0] = Date[3] / 10;
bits[1] = Date[3] % 10;
bits[2] = Date[4] / 10;
bits[3] = Date[4] % 10;
bits[4] = Date[5] / 10;
bits[5] = Date[5] % 10;
for (byte i = 0; i < 6; i++){
if(old_bits[i] != bits[i]){
NixieTube |= 1 << i;
old_bits[i] = bits[i];
}
}
sendData();
refresh();
if(NixieTube != 0){//时间发生改变
roundNixie(NixieTube,1);
sendData(); //发送各辉光管数据到74HC595N译码器中
refresh(); //译码器使能
}
delay(40);//下次检测时间变动的间隔
}
/* 显示计时 */
void showCounter(){
/*1.先显示日,并固定五秒 2.显示时分秒*/
/*期间如果发生了日的变换,则重复以上步骤*/
/*
1.逻辑:全灭->左对齐only点亮有的灯
*/
TimeSpan gap = RTC.now() - LoveDateTime;
uint32_t days = gap.days();
byte days_len = 0;
byte NixieTube = 0;
byte pointPos = 0;
if (state != old_state){//状态被改变,需要先显示日
for (uint32_t i = days; i > 0;i /= 10,days_len++);
pointPos = pointList[days_len - 1];
old_state = state;
for (byte i = 0; i < 6; i++){//关闭所有辉光管
closeDownNixie(i);
}
sendData();
refresh();
for (byte i = 0; i < days_len; i++){
NixieTube = 1 << i;
lightUpNixie(i);
setNixie(i,getNumber(days,i + 1,days_len));
roundNixie(NixieTube,2);
delay(35);
}
for (byte i = 0; i < 6; i++){//打开所有辉光管
lightUpNixie(i);
}
for (int i = 0; i < 10; i++){
lightUpPoint(4);
delay(100);
closeDownPoint();
delay(100);
}
}
setNixie(0,gap.hours() / 10);
setNixie(1,gap.hours() % 10);
setNixie(2,gap.minutes() / 10);
setNixie(3,gap.minutes() % 10);
setNixie(4,gap.seconds() / 10);
setNixie(5,gap.seconds() % 10);
for (byte i = 0; i < 6; i++){
if(old_bits[i] != bits[i]){
NixieTube |= 1 << i;
old_bits[i] = bits[i];
}
}
if(NixieTube != 0){//时间还没有发生改变
roundNixie(NixieTube,1);
sendData(); //发送各辉光管数据到74HC595N译码器中
refresh(); //译码器使能
}
delay(40);//下次检测时间变动的间隔
}
/* 从RTC时间模块中读取时间 */
void readFromRTC_Module(){
DateTime now = RTC.now();
Date[0] = now.year() - 2000;
Date[1] = now.month();
Date[2] = now.day();
Date[3] = now.hour();
Date[4] = now.minute();
Date[5] = now.second();
}
byte oneInByte(byte x){
byte sum = 0;
for (byte i = 0; i < 8; i++)
sum += (x & (1<<i)) > 0 ? 1 : 0;
return sum;
}
byte getNumber(uint32_t number,byte i,byte length){
if(i < 0 || i > length) return -1;
number /= powList[length - i];
number %= 10;
return number;
}
/* 通电后效果 */
void startingEffect(){
/* 首先全黑,然后全部一个个跑马灯 */
byte NixieTube = 0;
for (byte i = 0; i < 6; i++){
closeDownNixie(i);
}
sendData();
refresh();
/* 模拟荧光灯闪烁 */
for (byte i = 0; i < 6; i++){
// lightUpNixie(i);
setNixie(i,buildDate[i]);
simulateFlash(1<<i,0);
}
delay(1000);
/* 展示loveTime */
for (byte i = 0; i < 6; i++){
NixieTube = 1 << i;
lightUpNixie(i);
setNixie(i,LoveDate[i]);
roundNixie(NixieTube,2);
delay(35);
}
delay(500);
}
void roundNixie(byte NixieTube,byte round){
for (byte i = 0; i < round * 10; i++){
for (byte j = 0; j < 6; j++){
if((NixieTube & (1<<j)) == 0) continue;
if(++bits[j] == 10) bits[j] = 0;
}
sendData();
refresh();
delay(5);
}
}
/* 模拟荧光管闪烁效果,参数:辉光管编号 */
void simulateFlash(byte NixieTube,byte level){
for (int i = 0; i < 6; i++)
old_bits[i] = bits[i];
for (byte i = 0; i < 5; i++){
for (byte j = 0; j < 6; j++){
if((NixieTube & (1<<j)) == 0) continue;
closeDownNixie(j);
}
sendData();
refresh();
delay(simulateTime[level][i*2]);
for (byte j = 0; j < 6; j++){
if((NixieTube & (1<<j)) == 0) continue;
lightUpNixie(j);
setNixie(j,old_bits[j]);
}
sendData();
refresh();
delay(simulateTime[level][i*2 + 1]);
}
for (byte j = 0; j < 8; j++){
if((NixieTube & (1<<j)) == 0) continue;
lightUpNixie(j);
setNixie(j,old_bits[j]);
}
sendData();
refresh();
}
/*
跑马灯
*/
// void showTime(){
// bits[5]++;
// for (int i = 4;i > 0;i--){
// if(bits[i + 1] == 10){
// bits[i + 1] = 0;
// bits[i]++;
// }
// }
// if(bits[0] == 10){
// for(int i = 0;i < 6;i++)
// bits[i] = 0;
// }
// }