1 Star 0 Fork 0

redpower/mc2302d_esp8266_clock

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
mc2302d_esp8266_clock.ino 15.92 KB
一键复制 编辑 原始数据 按行查看 历史
redpower1998 提交于 2022-09-26 08:44 . first commit
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
// 使用BSD许可协议
// 部分代码使用了文章中的内容:https://blog.csdn.net/qq_22848695/article/details/118547611
// 写着玩的, 没有注意代码规范和整理,凑合看吧。
// 发布于:https://gitee.com/redpower/mc2302d_esp8266_clock
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <DNSServer.h>
#include <time.h>
#ifndef uchar
#define uchar unsigned char
#endif
//SSID and Password of your WiFi router
const char* ssid = "XXXXX";
const char* password = "XXXXXX";
const int timezone = 8;
int DIN = 12; //D6;
int CLK = 14; //D5;
int STB_1 = 5; //D1
int STB_2 = 4; //D2
uchar f_dimming_1 = 0; //亮度可调 1-7级 0默认 最亮
uchar f_dimming_2 = 0; //亮度可调 1-7级 0默认 最亮
uchar Font_Data[10] = { 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F }; //数码管
uchar Font_point[4] = { 0x00, 0x03, 0x01, 0x02 }; //数码管 点
uchar seg_data_1[14] = { 0 };//缓存数组
uchar seg_data_2[14] = { 0 };//缓存数组
//置位 点亮
//中间杂点
#define DIS1_SET_1 seg_data_1[10]=(seg_data_1[10]|0X01)
#define DIS1_SET_2 seg_data_1[10]=(seg_data_1[10]|0X02)
#define DIS1_SET_3 seg_data_1[7] =(seg_data_1[7] |0X04)
#define DIS1_SET_4 seg_data_1[10]=(seg_data_1[10]|0X04)
#define DIS1_SET_5 seg_data_1[10]=(seg_data_1[10]|0X08)
#define DIS1_SET_6 seg_data_1[9] =(seg_data_1[9] |0X04)
#define DIS1_SET_7 seg_data_1[0] =(seg_data_1[0] |0X80)
#define DIS1_SET_8 seg_data_1[2] =(seg_data_1[2] |0X80)
#define DIS1_SET_9 seg_data_1[4] =(seg_data_1[4] |0X80)
#define DIS1_SET_10 seg_data_1[5] =(seg_data_1[5] |0X01)
#define DIS1_SET_11 seg_data_1[11]=(seg_data_1[11]|0X04)
#define DIS1_SET_12 seg_data_1[5] =(seg_data_1[5] |0X02)
#define DIS1_SET_13 seg_data_1[10]=(seg_data_1[10]|0X10)
#define DIS1_SET_14 seg_data_1[10]=(seg_data_1[10]|0X20)
#define DIS1_SET_15 seg_data_1[10]=(seg_data_1[10]|0X40)
#define DIS1_SET_16 seg_data_1[10]=(seg_data_1[10]|0X80)
#define DIS1_SET_17 seg_data_1[11]=(seg_data_1[11]|0X01)
#define DIS1_SET_18 seg_data_1[11]=(seg_data_1[11]|0X02)
//左右点
#define DIS2_SET_1 seg_data_1[6]=(seg_data_1[6] |0X01)
#define DIS2_SET_2 seg_data_1[6]=(seg_data_1[6] |0X02)
#define DIS2_SET_3 seg_data_1[6]=(seg_data_1[6] |0X04)
#define DIS2_SET_4 seg_data_1[6]=(seg_data_1[6] |0X08)
#define DIS2_SET_5 seg_data_1[6]=(seg_data_1[6] |0X10)
#define DIS2_SET_6 seg_data_1[6]=(seg_data_1[6] |0X20)
#define DIS2_SET_7 seg_data_1[6]=(seg_data_1[6] |0X40)
#define DIS2_SET_8 seg_data_1[6]=(seg_data_1[6] |0X80)
#define DIS2_SET_9 seg_data_1[7]=(seg_data_1[7] |0X01)
#define DIS2_SET_10 seg_data_1[7]=(seg_data_1[7] |0X02)
#define DIS2_SET_11 seg_data_1[8]=(seg_data_1[8] |0X01)
#define DIS2_SET_12 seg_data_1[8]=(seg_data_1[8] |0X02)
#define DIS2_SET_13 seg_data_1[8]=(seg_data_1[8] |0X04)
#define DIS2_SET_14 seg_data_1[8]=(seg_data_1[8] |0X08)
#define DIS2_SET_15 seg_data_1[8]=(seg_data_1[8] |0X10)
#define DIS2_SET_16 seg_data_1[8]=(seg_data_1[8] |0X20)
#define DIS2_SET_17 seg_data_1[8]=(seg_data_1[8] |0X40)
#define DIS2_SET_18 seg_data_1[8]=(seg_data_1[8] |0X80)
#define DIS2_SET_19 seg_data_1[9]=(seg_data_1[9] |0X01)
#define DIS2_SET_20 seg_data_1[9]=(seg_data_1[9] |0X02)
//置0 熄灭
//中间杂点
#define DIS1_CLR_1 seg_data_1[10]=(seg_data_1[10]&(~0X01))
#define DIS1_CLR_2 seg_data_1[10]=(seg_data_1[10]&(~0X02))
#define DIS1_CLR_3 seg_data_1[7] =(seg_data_1[7] &(~0X04))
#define DIS1_CLR_4 seg_data_1[10]=(seg_data_1[10]&(~0X04))
#define DIS1_CLR_5 seg_data_1[10]=(seg_data_1[10]&(~0X08))
#define DIS1_CLR_6 seg_data_1[9] =(seg_data_1[9] &(~0X04))
#define DIS1_CLR_7 seg_data_1[0] =(seg_data_1[0] &(~0X80))
#define DIS1_CLR_8 seg_data_1[2] =(seg_data_1[2] &(~0X80))
#define DIS1_CLR_9 seg_data_1[4] =(seg_data_1[4] &(~0X80))
#define DIS1_CLR_10 seg_data_1[5] =(seg_data_1[5] &(~0X01))
#define DIS1_CLR_11 seg_data_1[11]=(seg_data_1[11]&(~0X04))
#define DIS1_CLR_12 seg_data_1[5] =(seg_data_1[5] &(~0X02))
#define DIS1_CLR_13 seg_data_1[10]=(seg_data_1[10]&(~0X10))
#define DIS1_CLR_14 seg_data_1[10]=(seg_data_1[10]&(~0X20))
#define DIS1_CLR_15 seg_data_1[10]=(seg_data_1[10]&(~0X40))
#define DIS1_CLR_16 seg_data_1[10]=(seg_data_1[10]&(~0X80))
#define DIS1_CLR_17 seg_data_1[11]=(seg_data_1[11]&(~0X01))
#define DIS1_CLR_18 seg_data_1[11]=(seg_data_1[11]&(~0X02))
//左右点
#define DIS2_CLR_1 seg_data_1[6]=(seg_data_1[6] &(~0X01))
#define DIS2_CLR_2 seg_data_1[6]=(seg_data_1[6] &(~0X02))
#define DIS2_CLR_3 seg_data_1[6]=(seg_data_1[6] &(~0X04))
#define DIS2_CLR_4 seg_data_1[6]=(seg_data_1[6] &(~0X08))
#define DIS2_CLR_5 seg_data_1[6]=(seg_data_1[6] &(~0X10))
#define DIS2_CLR_6 seg_data_1[6]=(seg_data_1[6] &(~0X20))
#define DIS2_CLR_7 seg_data_1[6]=(seg_data_1[6] &(~0X40))
#define DIS2_CLR_8 seg_data_1[6]=(seg_data_1[6] &(~0X80))
#define DIS2_CLR_9 seg_data_1[7]=(seg_data_1[7] &(~0X01))
#define DIS2_CLR_10 seg_data_1[7]=(seg_data_1[7] &(~0X02))
#define DIS2_CLR_11 seg_data_1[8]=(seg_data_1[8] &(~0X01))
#define DIS2_CLR_12 seg_data_1[8]=(seg_data_1[8] &(~0X02))
#define DIS2_CLR_13 seg_data_1[8]=(seg_data_1[8] &(~0X04))
#define DIS2_CLR_14 seg_data_1[8]=(seg_data_1[8] &(~0X08))
#define DIS2_CLR_15 seg_data_1[8]=(seg_data_1[8] &(~0X10))
#define DIS2_CLR_16 seg_data_1[8]=(seg_data_1[8] &(~0X20))
#define DIS2_CLR_17 seg_data_1[8]=(seg_data_1[8] &(~0X40))
#define DIS2_CLR_18 seg_data_1[8]=(seg_data_1[8] &(~0X80))
#define DIS2_CLR_19 seg_data_1[9]=(seg_data_1[9] &(~0X01))
#define DIS2_CLR_20 seg_data_1[9]=(seg_data_1[9] &(~0X02))
uchar RGY_DIS[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; //红1绿1黄1 红2绿2黄2
uchar table0[] = { 0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f }; //LED从低位往高位移
uchar table1[] = { 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0xfe }; //LED从高位往低位移
uchar table2[] = { 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00 }; //LED从1个亮到8个都点亮(从低位往高位)
uchar table3[] = { 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01, 0x00 }; //LED从1个亮到8个都点亮(从高位往低位)
uchar table4[] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff }; //LED从8个全亮到一个都不亮(从低位往高位)
uchar table5[] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff }; //LED从8个全亮到一个都不亮(从高位往低位)
uchar table6[] = { 0xfe, 0xfc, 0xfa, 0xf6, 0xee, 0xde, 0xbe, 0x7e }; //LED从低位往高位移(最低位始终为0)
uchar table7[] = { 0x7f, 0x3f, 0x5f, 0x6f, 0x77, 0x7b, 0x7d, 0x7e }; //LED从高位往低位移(最高位始终为0)
uchar i, j; //定义循环变量
int tt = 70; //定义时间指数
void Make_Signal_2()
{
Make_Signal(STB_2);
}
void Make_Signal_1()
{
Make_Signal(STB_1);
}
void delay_tim(int time)
{
Make_Signal_2();
delay(time);
seg_data_2[2] = seg_data_2[0];
seg_data_2[8] = seg_data_2[6];
Make_Signal_2();
delay(time);
seg_data_2[4] = seg_data_2[0];
seg_data_2[10] = seg_data_2[6];
Make_Signal_2();
delay(time);
}
void disp0() //状态0 所有LED闪烁3次
{
for (i = 0; i < 3; i++)
{
seg_data_2[0] = 0x00; seg_data_2[6] = 0x00;
delay_tim(300);
seg_data_2[0] = 0xff; seg_data_2[6] = 0xff;
delay_tim(300);
}
}
void disp1() //状态1 LED顺时针转一圈
{
for (i = 0; i < 8; i++)
{
seg_data_2[0] = table1[i];
delay_tim(100);
}
seg_data_2[0] = 0xff;
for (i = 0; i < 8; i++)
{
seg_data_2[6] = table1[i];
delay_tim(100);
}
seg_data_2[6] = 0xff;
}
void disp2() //状态2 LED逆时针转一圈
{
for (i = 0; i < 8; i++)
{
seg_data_2[6] = table0[i];
delay_tim(100);
}
seg_data_2[6] = 0xff;
for (i = 0; i < 8; i++)
{
seg_data_2[0] = table0[i];
delay_tim(100);
}
seg_data_2[0] = 0xff;
}
void disp3() //状态3 4个LED同时顺时、逆时针移动1/4圈
{
for (i = 0; i < 8; i++)
{
seg_data_2[0] = table1[i];
seg_data_2[6] = table0[i];
delay_tim(100);
}
for (i = 0; i < 8; i++)
{
seg_data_2[0] = table0[i];
seg_data_2[6] = table1[i];
delay_tim(100);
}
seg_data_2[6] = 0xff;
}
void disp4() //状态4 2个LED同时顺时针移动半圈
{
for (i = 0; i < 8; i++)
{
seg_data_2[0] = table1[i];
delay_tim(100);
}
seg_data_2[0] = 0xff;
for (i = 0; i < 8; i++)
{
seg_data_2[6] = table1[i];
delay_tim(100);
}
seg_data_2[6] = 0xff;
}
void disp5() //状态5 2个LED同时逆时针移动半圈
{
for (i = 0; i < 8; i++)
{
seg_data_2[6] = table0[i];
delay_tim(100);
}
seg_data_2[6] = 0xff;
for (i = 0; i < 8; i++)
{
seg_data_2[0] = table0[i];
delay_tim(100);
}
seg_data_2[0] = 0xff;
}
void disp6() //状态6 LED自上而下逐渐点亮(一半点亮一半不亮)
{
for (i = 0; i < 8; i++)
{
seg_data_2[0] = table3[i];
delay_tim(100);
}
seg_data_2[0] = 0xff;
for (i = 0; i < 8; i++)
{
seg_data_2[6] = table3[i];
delay_tim(100);
}
seg_data_2[6] = 0xff;
}
void disp7() //状态7 LED自下而上逐渐点亮(直到全部点亮)
{
for (i = 0; i < 8; i++)
{
seg_data_2[6] = table2[i];
delay_tim(100);
}
for (i = 0; i < 8; i++)
{
seg_data_2[0] = table2[i];
delay_tim(100);
}
}
void disp8() //状态8 间断8格的4个LED亮并逆时针旋转
{
for (j = 0; j < 2; j++)
{
for (i = 0; i < 8; i++)
{
seg_data_2[0] = table0[i];
seg_data_2[6] = table0[i];
delay_tim(100);
}
seg_data_2[0] = 0xff; seg_data_2[6] = 0xff;
for (i = 0; i < 8; i++)
{
seg_data_2[0] = table0[i];
seg_data_2[6] = table0[i];
delay_tim(100);
}
seg_data_2[0] = 0xff; seg_data_2[6] = 0xff;
}
}
void disp9() //状态9 间断8格的4个LED亮,然后逆时针逐渐点亮(直到全部点亮)
{
for (i = 0; i < 8; i++)
{
seg_data_2[0] = table2[i];
seg_data_2[6] = table2[i];
delay_tim(100);
}
delay_tim(200);
}
void Make_Signal(int index)
{
uchar i, j;
uchar dis_data = 0;
digitalWrite(index, HIGH);
digitalWrite(CLK, HIGH);
for (i = 0; i < 8; i++) //command 1 (#0000_0011B)
{
//STB_1=0;
digitalWrite(index, LOW);
//CLK=0;
digitalWrite(CLK, LOW);
if (i == 1 || i == 0) {
digitalWrite(DIN, HIGH);
}
else {
digitalWrite(DIN, LOW);
}
digitalWrite(CLK, HIGH);
}
digitalWrite(index, HIGH);
for (i = 0; i < 8; i++) //command 2 (#0100_0000B)
{
digitalWrite(index, LOW);
digitalWrite(CLK, LOW);
if (i == 6) {
digitalWrite(DIN, HIGH);
}
else {
digitalWrite(DIN, LOW);
}
digitalWrite(CLK, HIGH);
}
digitalWrite(index, HIGH);
for (i = 0; i < 8; i++) //command 3 (#1100_0000B)
{
digitalWrite(index, LOW);
digitalWrite(CLK, LOW);
if (i == 6 || i == 7) {
digitalWrite(DIN, HIGH);
}
else {
digitalWrite(DIN, LOW);
}
digitalWrite(CLK, HIGH);
}
for (i = 0; i < 14; i++)
{
if (index == STB_1)
dis_data = seg_data_1[i];
else
dis_data = seg_data_2[i];
for (j = 0; j < 8; j++)
{
digitalWrite(index, LOW);
digitalWrite(CLK, LOW);
digitalWrite(DIN, dis_data & 0x01);
digitalWrite(CLK, HIGH);
dis_data = dis_data >> 1;//右移一位
}
}
digitalWrite(index, HIGH);
for (i = 0; i < 8; i++) //command 4 (#1000_0100B)
{
digitalWrite(index, LOW);
digitalWrite(CLK, LOW);
switch (f_dimming_1) //显示亮度模式
{
case(0):
if (i == 3 || i == 7 || i == 2 || i == 1 || i == 0)
{
digitalWrite(DIN, HIGH);
}
else
{
digitalWrite(DIN, LOW);
}
break;
case(1):
if (i == 0 || i == 3 || i == 7)
{
digitalWrite(DIN, HIGH);
}
else
{
digitalWrite(DIN, LOW);
}
break;
case(2):
if (i == 1 || i == 3 || i == 7)
{
digitalWrite(DIN, HIGH);
}
else
{
digitalWrite(DIN, LOW);
}
break;
case(3):
if (i == 1 || i == 0 || i == 3 || i == 7)
{
digitalWrite(DIN, HIGH);
}
else
{
digitalWrite(DIN, LOW);
}
break;
case(4):
if (i == 2 || i == 3 || i == 7)
{
digitalWrite(DIN, HIGH);
}
else
{
digitalWrite(DIN, LOW);
}
break;
case(5):
if (i == 2 || i == 0 || i == 3 || i == 7)
{
digitalWrite(DIN, HIGH);
}
else
{
digitalWrite(DIN, LOW);
}
break;
case(6):
if (i == 2 || i == 1 || i == 3 || i == 7)
{
digitalWrite(DIN, HIGH);
}
else
{
digitalWrite(DIN, LOW);
}
break;
case(7):
if (i == 2 || i == 1 || i == 0 || i == 7)
{
digitalWrite(DIN, HIGH);
}
else
{
digitalWrite(DIN, LOW);
}
break;
}
digitalWrite(CLK, HIGH);
}
digitalWrite(index, HIGH);
}
void display_clock(struct tm * curr)
{
memset(seg_data_1, 0, 14);
seg_data_1[3] = Font_point[1];
int hour = curr->tm_hour;
int hour12 = hour;
bool is_morning = true;
if( hour > 12 )
{
hour12 = hour % 12;
is_morning = false;
}
seg_data_1[1] = Font_point[hour12 / 10]; //时的十位
seg_data_1[0] = Font_Data[hour12 % 10]; //时的个位
seg_data_1[2] = Font_Data[curr->tm_min / 10]; //分的十位
seg_data_1[4] = Font_Data[curr->tm_min % 10]; //分的个位
if (is_morning) //上午下午
{
DIS1_SET_1;
DIS1_SET_2;
DIS1_SET_3;
}
else
{
DIS1_SET_4;
DIS1_SET_5;
DIS1_SET_6;
}
switch (curr->tm_wday)
{
case 1:
DIS2_SET_1;
DIS2_SET_11;
break;
case 2:
DIS2_SET_2;
DIS2_SET_12;
break;
case 3:
DIS2_SET_3;
DIS2_SET_13;
break;
case 4:
DIS2_SET_4;
DIS2_SET_14;
break;
case 5:
DIS2_SET_5;
DIS2_SET_15;
break;
case 6:
DIS2_SET_6;
DIS2_SET_16;
break;
case 0:
DIS2_SET_7;
DIS2_SET_17;
break;
}
Make_Signal(STB_1);
}
void setup()
{
pinMode(DIN, OUTPUT);
pinMode(CLK, OUTPUT);
pinMode(STB_1, OUTPUT);
pinMode(STB_2, OUTPUT);
seg_data_1[1] = Font_point[0]; //时的十位
seg_data_1[0] = Font_Data[0]; //时的个位
seg_data_1[2] = Font_Data[0]; //分的十位
seg_data_1[4] = Font_Data[0]; //分的个位
Make_Signal(STB_1);
Make_Signal(STB_2);
Serial.begin(115200);
WiFi.begin(ssid, password); //Connect to your WiFi router
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(1000);
}
configTime(timezone * 3600, 0, "pool.ntp.org"); //19800 (utc/gmt offset, daylight saving offset, ntp server)
Serial.println("\nWaiting for time sync");
while (!time(nullptr))
{
Serial.print(".");
delay(1000);
}
}
void loop()
{
//GetDateTime函数获取RTC模块时间
time_t now = time(nullptr);
struct tm * timeinfo;
timeinfo = localtime (&now);
printDateTime(timeinfo);
Serial.println();
display_clock(timeinfo);
disp9(); //延迟一秒
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(struct tm *dt)
{
char datestring[20];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt->tm_mon,
dt->tm_mday,
dt->tm_year,
dt->tm_hour,
dt->tm_min,
dt->tm_sec);
Serial.print(datestring);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Arduino
1
https://gitee.com/redpower/mc2302d_esp8266_clock.git
git@gitee.com:redpower/mc2302d_esp8266_clock.git
redpower
mc2302d_esp8266_clock
mc2302d_esp8266_clock
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385