first commit
This commit is contained in:
93
libraries/Ds1302/examples/01/GetDateTime.cpp
Normal file
93
libraries/Ds1302/examples/01/GetDateTime.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
/** GetDateTime.cpp
|
||||
*
|
||||
* Example of getting the date and time from the RTC.
|
||||
*
|
||||
* @version 1.0.1
|
||||
* @author Rafa Couto <caligari@treboada.net>
|
||||
* @license GNU Affero General Public License v3.0
|
||||
* @see https://github.com/Treboada/Ds1302
|
||||
*
|
||||
*/
|
||||
#include <Arduino.h>
|
||||
#include <Ds1302.h>
|
||||
|
||||
|
||||
// DS1302 RTC instance
|
||||
Ds1302 rtc(PIN_ENA, PIN_CLK, PIN_DAT);
|
||||
|
||||
|
||||
const static char* WeekDays[] =
|
||||
{
|
||||
"Monday",
|
||||
"Tuesday",
|
||||
"Wednesday",
|
||||
"Thursday",
|
||||
"Friday",
|
||||
"Saturday",
|
||||
"Sunday"
|
||||
};
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
|
||||
// initialize the RTC
|
||||
rtc.init();
|
||||
|
||||
// test if clock is halted and set a date-time (see example 2) to start it
|
||||
if (rtc.isHalted())
|
||||
{
|
||||
Serial.println("RTC is halted. Setting time...");
|
||||
|
||||
Ds1302::DateTime dt = {
|
||||
.year = 17,
|
||||
.month = Ds1302::MONTH_OCT,
|
||||
.day = 3,
|
||||
.hour = 4,
|
||||
.minute = 51,
|
||||
.second = 30,
|
||||
.dow = Ds1302::DOW_TUE
|
||||
};
|
||||
|
||||
rtc.setDateTime(&dt);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
// get the current time
|
||||
Ds1302::DateTime now;
|
||||
rtc.getDateTime(&now);
|
||||
|
||||
static uint8_t last_second = 0;
|
||||
if (last_second != now.second)
|
||||
{
|
||||
last_second = now.second;
|
||||
|
||||
Serial.print("20");
|
||||
Serial.print(now.year); // 00-99
|
||||
Serial.print('-');
|
||||
if (now.month < 10) Serial.print('0');
|
||||
Serial.print(now.month); // 01-12
|
||||
Serial.print('-');
|
||||
if (now.day < 10) Serial.print('0');
|
||||
Serial.print(now.day); // 01-31
|
||||
Serial.print(' ');
|
||||
Serial.print(WeekDays[now.dow - 1]); // 1-7
|
||||
Serial.print(' ');
|
||||
if (now.hour < 10) Serial.print('0');
|
||||
Serial.print(now.hour); // 00-23
|
||||
Serial.print(':');
|
||||
if (now.minute < 10) Serial.print('0');
|
||||
Serial.print(now.minute); // 00-59
|
||||
Serial.print(':');
|
||||
if (now.second < 10) Serial.print('0');
|
||||
Serial.print(now.second); // 00-59
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
delay(100);
|
||||
}
|
||||
|
||||
33
libraries/Ds1302/examples/01/platformio.ini
Normal file
33
libraries/Ds1302/examples/01/platformio.ini
Normal file
@@ -0,0 +1,33 @@
|
||||
; PlatformIO Project Configuration File
|
||||
|
||||
[platformio]
|
||||
src_dir = .
|
||||
lib_dir = ../..
|
||||
|
||||
[env:arduino]
|
||||
platform = atmelavr
|
||||
board = nanoatmega328
|
||||
framework = arduino
|
||||
build_flags =
|
||||
-D PIN_ENA=2
|
||||
-D PIN_DAT=3
|
||||
-D PIN_CLK=4
|
||||
|
||||
[env:esp8266]
|
||||
platform = espressif8266
|
||||
board = d1_mini
|
||||
framework = arduino
|
||||
build_flags =
|
||||
-D PIN_ENA=15
|
||||
-D PIN_DAT=13
|
||||
-D PIN_CLK=12
|
||||
|
||||
[env:esp32]
|
||||
platform = espressif32
|
||||
board = lolin32
|
||||
framework = arduino
|
||||
build_flags =
|
||||
-D PIN_ENA=25
|
||||
-D PIN_DAT=26
|
||||
-D PIN_CLK=27
|
||||
|
||||
Reference in New Issue
Block a user