|
这是我做辉光钟总结的一点经验
- #include <DS1302.h>
- //clock config
- const int kCePin = 5; // 对应rst引脚,Chip Enable
- const int kIoPin = 6; // 对应data引脚,Input/Output
- const int kSclkPin = 7; // 对应clck引脚,Serial Clock
- //建立一个ds1302 对象 rtc
- DS1302 rtc(kCePin, kIoPin, kSclkPin);
- //595 config
- int datapin = 2; //DS 14脚 用来串行输入数据
- int latchpin = 3; //st_cp 12脚 注:latch 有门闩的意思,可以理解成闸门,控制并行输出的时间
- int clockpin = 4; //sh_cp 11脚 //595会在clock的上升沿读取 1bit data, 对它的操控封装在 shiftOut()函数里了, 不需要大家自己操作。
- //clock data
- int data_sec = 0; //秒数数据
- int data_min = 0; //分钟数据
- int data_hour = 0; //小时数据
- void setup() {
- Serial.begin(9600);
- nixie_tube_init();
- }
- void loop() {
- //将当前时间存储到 t 这个对象中
- Time t = rtc.time();
- int hour_gewei = t.hr % 10; //取得个位数字 模10
- int hour_shiwei = t.hr / 10;//取得十位数字 处以十
- int min_gewei = t.min % 10;
- int min_shiwei = t.min / 10;
- int sec_gewei = t.sec % 10;
- int sec_shiwei = t.sec / 10;
- //晚上不显示
- if( t.hr>=0 and t.hr<=6 ) {
- digitalWrite(latchpin, LOW);
- shiftOut(datapin, clockpin, LSBFIRST, 255);
- shiftOut(datapin, clockpin, LSBFIRST, 255);
- digitalWrite(latchpin, HIGH);
- delay(100000);
- return;
- }
- //分钟数变化时来个随机显示
- if ((min_shiwei * POW(2,4) + min_gewei) != data_min) {
- random_display(30);
- }
-
- // (5 -> 0101 1-> 0001) => 0101 0001 = 0101 * 2^4 + 0001
- // 10 01 => 1001 = 10 * 10^2 + 1
- // data_sec = sec_shiwei * POW(2,4) + sec_gewei;
- data_min = min_shiwei * POW(2,4) + min_gewei;
- data_hour = hour_shiwei * POW(2,4) + hour_gewei;
- digitalWrite(latchpin, LOW);
- shiftOut(datapin, clockpin, LSBFIRST, data_min);
- shiftOut(datapin, clockpin, LSBFIRST, data_hour);
- digitalWrite(latchpin, HIGH);
- delay(100);
- }
- int POW(int i, int j) {
- int result = 1;
- if(j == 0) return result;
- for(int k = 0; k < j; k++) {
- result *= i;
- }
- return result;
- }
- void random_display(int times) {
- for(int i=0; i<times; i++) {
- digitalWrite(latchpin, LOW);
- shiftOut(datapin, clockpin, LSBFIRST, random(1,10) * POW(2,4) + random(1,10));
- shiftOut(datapin, clockpin, LSBFIRST, random(1,10) * POW(2,4) + random(1,10));
- digitalWrite(latchpin, HIGH);
- delay(20);
- }
- }
- void nixie_tube_init() {
- pinMode(clockpin, OUTPUT);
- pinMode(datapin, OUTPUT);
- pinMode(latchpin, OUTPUT);
- //show something random
- random_display(40);
- }
复制代码
|
|