From 63d9bfb5240196d2f76c4ba8e7a01cd2bb990def Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Sat, 16 Sep 2023 16:44:07 +0700 Subject: [PATCH] working RTC --- .../lib/ESPMegaPRO/ESPMegaPRO.cpp | 26 +++++++++++++++- Template Project/lib/ESPMegaPRO/ESPMegaPRO.h | 31 +++++++++++++++++-- Template Project/platformio.ini | 3 +- Template Project/src/rtc_demo.cpp | 6 +++- 4 files changed, 60 insertions(+), 6 deletions(-) diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp index 59cc5c6..6675142 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.cpp @@ -7,6 +7,7 @@ PCF8574 inputBankA(INPUT_BANK_A_ADDRESS); PCF8574 inputBankB(INPUT_BANK_B_ADDRESS); Adafruit_PWMServoDriver pwmBank = Adafruit_PWMServoDriver(PWM_BANK_ADDRESS); I2C_eeprom ESPMega_EEPROM(EEPROM_ADDRESS); + #ifdef ANALOG_CARD_ENABLE Adafruit_ADS1115 analogInputBankA; Adafruit_ADS1115 analogInputBankB; @@ -22,7 +23,6 @@ void ESPMega_begin() inputBankA.begin(); inputBankB.begin(); pwmBank.begin(); - ESPMega_RTC.begin(); ESPMega_EEPROM.begin(); // ESPMegaPRO v3 use the PWMBank to drive Half Bridge // Push Pull Output is required. @@ -100,6 +100,30 @@ void IRAM_ATTR refreshInputBankB() inputBufferB = inputBankB.read8(); } +rtctime_t ESPMega_getTime() { + tmElements_t timeElement; + RTC.read(timeElement); + rtctime_t time; + time.hours = timeElement.Hour; + time.minutes = timeElement.Minute; + time.seconds = timeElement.Second; + time.day = timeElement.Day; + time.month = timeElement.Month; + time.year = timeElement.Year+1970; + return time; +} + +void ESPMega_setTime(int hours,int minutes, int seconds, int day, int month, int year) { + tmElements_t timeElement; + timeElement.Hour = hours; + timeElement.Minute = minutes; + timeElement.Second = seconds; + timeElement.Day = day; + timeElement.Month = month; + timeElement.Year = year-1970; + RTC.write(timeElement); +} + #ifdef ANALOG_CARD_ENABLE int16_t ESPMega_analogRead(int id) { diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h index 07ec186..d6e67bc 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO.h @@ -9,7 +9,8 @@ #include #include #include -#include +#include +#include #endif #define INPUT_BANK_A_ADDRESS 0x21 @@ -24,12 +25,18 @@ #define DAC3_ADDRESS 0x63 #define EEPROM_ADDRESS 0x70 -#define ESPMega_RTC rtc - //#define USE_INTERRUPT #define INPUT_BANK_A_INTERRUPT 36 #define INPUT_BANK_B_INTERRUPT 39 extern I2C_eeprom ESPMega_EEPROM; +struct rtctime_t { + uint8_t hours; + uint8_t minutes; + uint8_t seconds; + uint8_t day; + uint8_t month; + uint16_t year; +}; /** * Initiate ESPMega PRO Internal Components @@ -74,6 +81,24 @@ void ESPMega_digitalWrite(int id, bool value); void IRAM_ATTR refreshInputBankA(); void IRAM_ATTR refreshInputBankB(); +/** + * Get time from the onboard RTC as a struct + * + * @return Time Element Struct +*/ +rtctime_t ESPMega_getTime(); +/** + * Set the onboard RTC's time + * + * @param hours + * @param minutes + * @param seconds + * @param day Day of the month + * @param month Month in numerical form + * @param year Years in AD +*/ +void ESPMega_setTime(int hours,int minutes, int seconds, int day, int month, int year); + #ifdef ANALOG_CARD_ENABLE /** * Read one of the ESPMega Analog Card's Analog Input pins (A0-A7) diff --git a/Template Project/platformio.ini b/Template Project/platformio.ini index d6420ce..d1ddcbe 100644 --- a/Template Project/platformio.ini +++ b/Template Project/platformio.ini @@ -21,5 +21,6 @@ lib_deps = adafruit/Adafruit PWM Servo Driver Library@^2.4.1 SPI@^2.0.0 robtillaart/MCP4725@^0.3.7 robtillaart/I2C_EEPROM@^1.7.3 - sparkfun/SparkFun DS1307 Real-Time Clock (RTC)@^1.0.1 + paulstoffregen/Time@^1.6.1 + paulstoffregen/DS1307RTC monitor_speed = 115200 \ No newline at end of file diff --git a/Template Project/src/rtc_demo.cpp b/Template Project/src/rtc_demo.cpp index 0fdadee..c0146a0 100644 --- a/Template Project/src/rtc_demo.cpp +++ b/Template Project/src/rtc_demo.cpp @@ -1,8 +1,12 @@ #include void setup() { ESPMega_begin(); + Serial.begin(115200); + ESPMega_setTime(16,35,0,16,9,2023); } void loop() { - delay(5000); + delay(1000); + rtctime_t tm = ESPMega_getTime(); + Serial.printf("%02d:%02d:%02d %02d/%02d/%04d\n", tm.hours,tm.minutes,tm.seconds,tm.day,tm.month,tm.year); } \ No newline at end of file