Page 1 of 1

STM32F1xx RTC Time Reset

Posted: Wed Nov 25, 2020 12:39 pm
by saeed144
Hi
I'm using STM32RTC lib to get time, but when the board reset time reset too, I know I should work with RTC backup register, but how on Arduino IDE?

Re: STM32F1xx RTC Time Reset

Posted: Wed Nov 25, 2020 1:01 pm
by GonzoG
Have you changed clock source to external ??

Code: Select all

setClockSource(STM32RTC::LSE_CLOCK)

Re: STM32F1xx RTC Time Reset

Posted: Wed Nov 25, 2020 3:49 pm
by saeed144
GonzoG wrote: Wed Nov 25, 2020 1:01 pm Have you changed clock source to external ??

Code: Select all

setClockSource(STM32RTC::LSE_CLOCK)
when I change clock source, the program stop after change.

Code: Select all

/*
  SimpleRTC

  This sketch shows how to configure the RTC and to display
  the date and time periodically

  Creation 12 Dec 2017
  by Wi6Labs
  Modified 03 Jul 2020
  by Frederic Pillon for STMicroelectronics

  This example code is in the public domain.

  https://github.com/stm32duino/STM32RTC
*/

#include <STM32RTC.h>

/* Get the rtc object */
STM32RTC& rtc = STM32RTC::getInstance();

/* Change these values to set the current initial time */
const byte seconds = 20;
const byte minutes = 14;
const byte hours = 19;

/* Change these values to set the current initial date */
/* Monday 15th June 2015 */
const byte weekDay = 1;
const byte day = 15;
const byte month = 6;
const byte year = 15;

void setup()
{
  Serial.begin(9600);
  delay(1000);
  // Select RTC clock source: LSI_CLOCK, LSE_CLOCK or HSE_CLOCK.
  // By default the LSI is selected as source.
  Serial.print("RTC Test");
  rtc.setClockSource(STM32RTC::LSE_CLOCK);
  Serial.print("Clcok Set");
  rtc.begin(); // initialize RTC 24H format

  // Set the time
  rtc.setHours(hours);
  rtc.setMinutes(minutes);
  rtc.setSeconds(seconds);

  // Set the date
  rtc.setWeekDay(weekDay);
  rtc.setDay(day);
  rtc.setMonth(month);
  rtc.setYear(year);

  // you can use also
  //rtc.setTime(hours, minutes, seconds);
  //rtc.setDate(weekDay, day, month, year);
}

void loop()
{
  // Print date...
  Serial.printf("%02d/%02d/%02d ", rtc.getDay(), rtc.getMonth(), rtc.getYear());

  // ...and time
  Serial.printf("%02d:%02d:%02d.%03d\n", rtc.getHours(), rtc.getMinutes(), rtc.getSeconds(), rtc.getSubSeconds());

  delay(1000);
}
Output is: RTC Test
not more

Re: STM32F1xx RTC Time Reset

Posted: Thu Nov 26, 2020 3:37 pm
by saeed144
:roll: :cry:

Re: STM32F1xx RTC Time Reset

Posted: Thu Nov 26, 2020 4:25 pm
by fpiSTM
Which board you used ? Is there an LSE ?

Re: STM32F1xx RTC Time Reset

Posted: Thu Nov 26, 2020 5:26 pm
by saeed144
fpiSTM wrote: Thu Nov 26, 2020 4:25 pm Which board you used ? Is there an LSE ?
I'm using STM32F103C8T6 with my own designed PCB, using 8MHz for main core and 32.768Khz for RTC and 3v 2032 battery.
Yes LSE is enabled, I attached the code above.

Re: STM32F1xx RTC Time Reset

Posted: Fri Nov 27, 2020 9:47 am
by GonzoG
saeed144 wrote: Wed Nov 25, 2020 3:49 pm
when I change clock source, the program stop after change.

Code: Select all

void setup(){
 ...
  // Set the time
  rtc.setHours(hours);
  rtc.setMinutes(minutes);
  rtc.setSeconds(seconds);
...
}
This part resets your time with every boot.
You need to add a condition:

Code: Select all


 if (!rtc.isTimeSet()) {
    // Set the time
    rtc.setHours(hours);
    rtc.setMinutes(minutes);
    rtc.setSeconds(seconds);
  }
  

saeed144 wrote: Wed Nov 25, 2020 3:49 pm
when I change clock source, the program stop after change.
Then there might be something wrong with your board design or variant setup.
It works without any problems with F103 blue pill.