working RTC

This commit is contained in:
Siwat Sirichai 2023-09-16 16:44:07 +07:00
parent a927c74393
commit 63d9bfb524
4 changed files with 60 additions and 6 deletions

View File

@ -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)
{

View File

@ -9,7 +9,8 @@
#include <Adafruit_ADS1X15.h>
#include <MCP4725.h>
#include <I2C_eeprom.h>
#include <SparkFunDS1307RTC.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#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)

View File

@ -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

View File

@ -1,8 +1,12 @@
#include <ESPMegaPRO.h>
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);
}