//**************************************************************//
// Name : 大号数码管时钟 //
// Author : HOROLD FINCH //
// Date : 25 ApRIL, 2006 //
// Version : 1.0 //
// Notes : 74HC595驱动 ULN2003驱动数码管 //
// : //
//****************************************************************
#include <Wire.h> // must be included here so that Arduino library object file references work
#include <RtcDS3231.h>
RtcDS3231<TwoWire> Rtc(Wire);
#define latchPin 8
#define clockPin 9
#define dataPin 7
#define siteselection LOW //for PNPBJT is HIHG,for NPNBJT is low
#define countof(a) (sizeof(a) / sizeof(a[0]))
#define colol 2
int DIG1 = 6;
int DIG2 = 5;
int DIG3 = 4;
int DIG4 = 3;
int displays[] = {0X77, 0X41, 0X6E, 0X6B, 0XD9, 0X3B, 0X3F, 0X61, 0X7F, 0X7B, 0XFF, 0X00, 0X3E, 0XC, 0X78, 0X36, 0x8};
//0 1 2 3 4 5 6 7 8 9 全部 全不 E r 口 C 负号
void setup() {
Serial.begin(9600);
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode (DIG1, OUTPUT);
pinMode (DIG2, OUTPUT);
pinMode (DIG3, OUTPUT);
pinMode (DIG4, OUTPUT);
pinMode (colol, OUTPUT);
for (int i = 15; i > 0; i--) {
SegPlayall(200 * (i / 5));
SegUnplayall(200 * (i / 5));
}
ds3231config();
}
void loop() {
checkds3231();
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
RtcTemperature temp = Rtc.GetTemperature();
//temp.Print(Serial);
// you may also get the temperature as a float and print it
// Serial.print(temp.AsFloatDegC());
// Serial.println("C");
playtemperature(temp.AsFloatDegC(), 1500);
}
/////////////////////////////////////////
//函数作用:用于四位数码管显示数字
//输入值:unsigned int 范围0-9999 //
//返回值: 无 //
////////////////////////////////////////
void playnum (unsigned int i, unsigned int playtime) {
for (int j = 0; j < playtime / 4; j++) {
SegDisplay(i / 1000, DIG1);
SegDisplay((i % 1000) / 100, DIG2);
SegDisplay((i % 100) / 10, DIG3);
SegDisplay((i % 10), DIG4);
}
}
/////////////////////////////////////////
//函数作用:用于四位数码管显示温度,后面两位是℃
//输入值:float值 temp.AsFloatDegC(),unsigned int 范围0-9999 //
//返回值: 无 //
////////////////////////////////////////
void playtemperature (float temp, unsigned int playtime) {
if (temp < 0) {//这里判断以下温度是不是负数,如果是负数,就显示负号。缺点是只能显示到-9℃。但是室温一般不会到-9℃,所以无妨。
for (int j = 0; j < playtime / 4; j++) {
int i = -temp; //需要转化类型,不然后不能取余
SegDisplay( 16, DIG1);//显示负号
SegDisplay( i , DIG2);//不用
SegDisplay(14, DIG3);
SegDisplay(15, DIG4);
}
}
else{
for (int j = 0; j < playtime / 4; j++) {
int i = temp; //需要转化类型,不然后不能取余
SegDisplay( i / 10, DIG1);
SegDisplay( i % 10, DIG2);
SegDisplay(14, DIG3);
SegDisplay(15, DIG4);
}
}
}
/////////////////////////////////////////
//函数作用:用于四位数码管显示温度,后面两位是℃
//输入值:float值 temp.AsFloatDegC(),unsigned int 范围0-9999 //
//返回值: 无 //
////////////////////////////////////////
void playerror (int i , unsigned int playtime) {
for (int j = 0; j < playtime / 4; j++) {
SegDisplay(12, DIG1);
SegDisplay(13, DIG2);
SegDisplay(13, DIG3);
SegDisplay(i, DIG4);
}
}
/////////////////////////////////////////
//函数作用:用于单位数码管显示数字
//输入值:i 范围0-11 //
// 显示位置 1-4 //
//返回值: 无 //
////////////////////////////////////////
void SegDisplay(int i, int DIG) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, displays[i]);
digitalWrite(latchPin, HIGH);
digitalWrite(DIG, ~siteselection);
delayMicroseconds(500);//调节这个和下面的参数可以调整显示亮度,建议这两个数加起来为860。这样是为了显示时间准确
digitalWrite(DIG, siteselection);
delayMicroseconds(360);
}
/////////////////////////////////////////
//函数作用:显示全部笔端
//输入值:显示时间 unsigned int //
//返回值: 无 //
////////////////////////////////////////
void SegPlayall(unsigned int playtime) {
for (int j = 0; j < playtime / 4; j++) {
SegDisplay(10, DIG1);
SegDisplay(10, DIG2);
SegDisplay(10, DIG3);
SegDisplay(10, DIG4);
digitalWrite(colol, HIGH);
}
}
/////////////////////////////////////////
//函数作用:不显示全部笔端
//输入值:显示时间 unsigned int //
//返回值: 无 //
////////////////////////////////////////
void SegUnplayall(unsigned int playtime) {
for (int j = 0; j < playtime / 4; j++) {
SegDisplay(11, DIG1);
SegDisplay(11, DIG2);
SegDisplay(11, DIG3);
SegDisplay(11, DIG4);
digitalWrite(colol, LOW);
}
}
void ds3231config() {
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
//--------RTC SETUP ------------
// if you are using ESP-01 then uncomment the line below to reset the pins to
// the available pins for SDA, SCL
// Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
//printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid())
{
if (Rtc.LastError() != 0)
{
// we have a communications error
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
// what the number means
//0: success
//1: data too long to fit in transmit buffer
//2: received NACK on transmit of address
//3: received NACK on transmit of data
//4: other error
playerror(Rtc.LastError(), 2000);
// Serial.print("RTC communications error = ");
// Serial.println(Rtc.LastError());
}
else
{
// Common Causes:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing
playerror(5, 2000);
//Serial.println("RTC lost confidence in the DateTime!");
// following line sets the RTC to the date & time this sketch was compiled
// it will also reset the valid flag internally unless the Rtc device is
// having an issue
Rtc.SetDateTime(compiled);
}
}
if (!Rtc.GetIsRunning())
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled)
{
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time. (this is expected)");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
// never assume the Rtc was last configured by you, so
// // just clear them to your needed state
// Rtc.Enable32kHzPin(false);
// Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
}
void checkds3231() {
if (!Rtc.IsDateTimeValid())
{
if (Rtc.LastError() != 0)
{
// we have a communications error
// see https://www.arduino.cc/en/Reference/WireEndTransmission for
// what the number means
//0: success
//1: data too long to fit in transmit buffer
//2: received NACK on transmit of address
//3: received NACK on transmit of data
//4: other error
playerror(Rtc.LastError(), 2000);
// Serial.print("RTC communications error = ");
// Serial.println(Rtc.LastError());
}
else
{
// Common Causes:
// 1) the battery on the device is low or even missing and the power line was disconnected
playerror(5, 2000);
// Serial.println("RTC lost confidence in the DateTime!");
}
}
}
void printDateTime(const RtcDateTime& dt)
{
char datestring[20];
String Timesrtring;
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.println(datestring);
Timesrtring = datestring[11]; //不能通过 Timesrtring=datestring[11]+datestring[12];这种运算得到一个字符串
Timesrtring = Timesrtring + datestring[12];
Timesrtring = Timesrtring + datestring[14];
Timesrtring = Timesrtring + datestring[15];
// Serial.println(Timesrtring);
// Serial.println(Timesrtring.toInt()); //toInt()函数将字符串转化为int
digitalWrite(colol, HIGH);
playnum(Timesrtring.toInt(), 1500);
digitalWrite(colol, LOW);
}
All reference designs on this site are sourced from major semiconductor manufacturers or collected online for learning and research. The copyright belongs to the semiconductor manufacturer or the original author. If you believe that the reference design of this site infringes upon your relevant rights and interests, please send us a rights notice. As a neutral platform service provider, we will take measures to delete the relevant content in accordance with relevant laws after receiving the relevant notice from the rights holder. Please send relevant notifications to email: bbs_service@eeworld.com.cn.
It is your responsibility to test the circuit yourself and determine its suitability for you. EEWorld will not be liable for direct, indirect, special, incidental, consequential or punitive damages arising from any cause or anything connected to any reference design used.
Supported by EEWorld Datasheet