Am I wrong in thinking that the RTC should keep time on battery power alone? From other posts on this forum, I realize that I can't expect a very accurate clock but I had assumed that it would keep running when unplugged.
I believe the connection between the battery and the microcontroller is good: without a battery, rtc.isTimeSet() returns false after unplugging the usb cable and plugging it back in. With the battery and the LSI clock source, rtc.isTimeSet() returns true after unplugging the usb cable. The code I used, based on cutting and pasting from Frederic Pillon's RTC_Tests program, is below.
Also, with a battery in place, the time when power was removed is preserved. When plugged back in, it picks up where it left off. With no battery, the time reverts to the time when the microcontroller was programmed.
This board is the one with the built in SD card reader and battery. In other tests, it has behaved as expected. I've been able to get the SD card working along with several I2C sensors (SHT35, BME280) and a one wire temperature sensor (ds18b20).
My appreciation to the developers who have made it possible to use the Arduino ide with STM32 boards. I'm new to the world of microcontrollers and the Arduino environment has made it much more accessible than it would have been otherwise.
Any suggestions would be appreciated.
Jan Erik
Code: Select all
/*
Cut and pasted from RTC_Tests
by Frederic Pillon for STMicroelectronics
*/
#include <STM32RTC.h>
STM32RTC& rtc = STM32RTC::getInstance();
static const char* mydate = __DATE__;
static const char* mytime = __TIME__;
static byte seconds = 0;
static byte minutes = 0;
static byte hours = 0;
static byte weekDay = 1;
static byte day = 0;
static byte month = 0;
static byte year = 0;
static STM32RTC::Hour_Format hourFormat = STM32RTC::HOUR_24;
static STM32RTC::AM_PM period = STM32RTC::AM;
void setup()
{
int forceClockSet = 0;
while(!Serial) {}
Serial.println("With battery, LSE_Clock, flashed without removing power");
STM32RTC::Source_Clock clkSource = rtc.LSE_CLOCK;
Serial.print("clkSource: ");
Serial.println(clkSource);
rtc.begin(hourFormat);
if (forceClockSet){
Serial.println("unconditional rtc_config");
rtc_config(clkSource, rtc.HOUR_24, mydate, mytime);
}
if (rtc.isTimeSet()) {
Serial.println("clock is set");
} else {
Serial.println("setting clock");
rtc_config(clkSource, rtc.HOUR_24, mydate, mytime);
}
printDateTime(5, 1000, false);
}
void loop() {
}
Code: Select all
// Testing reboot with no battery
No battery, LSI_Clock, flashed without removing power
clkSource: 0
setting clock
03/21/21 12:24:17
03/21/21 12:24:18
03/21/21 12:24:19
03/21/21 12:24:20
03/21/21 12:24:21
// Reboot without removing power, rtc.isTimeSet returns true
No battery, LSI_Clock, flashed without removing power
clkSource: 0
clock is set
03/21/21 12:24:31
03/21/21 12:24:32
03/21/21 12:24:33
03/21/21 12:24:34
03/21/21 12:24:35
// Unplug usb and plug back in, rtc.isTimeSet returns false, clock goes back to time at compile
No battery, LSI_Clock, flashed without removing power
clkSource: 0
setting clock
03/21/21 12:24:17
03/21/21 12:24:18
03/21/21 12:24:19
03/21/21 12:24:20
03/21/21 12:24:21
/* Tests with battery */
With battery, LSI_Clock, flashed after removing power
clkSource: 0
setting clock
03/21/21 12:39:59
03/21/21 12:40:00
03/21/21 12:40:01
03/21/21 12:40:02
03/21/21 12:40:03
// BT0 -> Gnd, reset without removing usb cable
With battery, LSI_Clock, flashed after removing power
clkSource: 0
clock is set
03/21/21 12:41:47
03/21/21 12:41:48
03/21/21 12:41:49
03/21/21 12:41:50
03/21/21 12:41:51
// Unplug usb, wait 5 minutes, plug usb back in, system time is now 12:45:30
// Time when unplugged was stored but clock didn't advance
With battery, LSI_Clock, flashed after removing power
clkSource: 0
clock is set
03/21/21 12:42:33
03/21/21 12:42:34
03/21/21 12:42:35
03/21/21 12:42:36
03/21/21 12:42:37
/[code]