STM32F1xx RTC Time Reset

Post here first, or if you can't find a relevant section!
Post Reply
saeed144
Posts: 36
Joined: Mon Sep 21, 2020 10:11 am

STM32F1xx RTC Time Reset

Post 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?
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: STM32F1xx RTC Time Reset

Post by GonzoG »

Have you changed clock source to external ??

Code: Select all

setClockSource(STM32RTC::LSE_CLOCK)
saeed144
Posts: 36
Joined: Mon Sep 21, 2020 10:11 am

Re: STM32F1xx RTC Time Reset

Post 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
saeed144
Posts: 36
Joined: Mon Sep 21, 2020 10:11 am

Re: STM32F1xx RTC Time Reset

Post by saeed144 »

:roll: :cry:
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: STM32F1xx RTC Time Reset

Post by fpiSTM »

Which board you used ? Is there an LSE ?
saeed144
Posts: 36
Joined: Mon Sep 21, 2020 10:11 am

Re: STM32F1xx RTC Time Reset

Post 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.
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: STM32F1xx RTC Time Reset

Post 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.
Post Reply

Return to “General discussion”