Use STM32 RTC Internal Arduino IDE

Posted on

STM32 RTC Internal Without Date Reset

STM32 RTC Internal How to Use It Using Arduino – This article will provide you with how Access STM32 RTC Data and display it to Arduino IDE Serial Monitor without reset.

This chip is now beginning to be favored by the Arduino programmers. How not, with the same chip size with Atmega328, the Blue Pill board STM32 chip has several advantages over Atmega328, one of which is the Internal RTC.

This is what I like the most, this chip is precisely the STM32F103C8T6 series has an Internal RTC, so this will make simple of the circuit that we will make, just add the clock source, the crystal oscillator.

Previously I have written a tutorial how to access the Internal RTC STM32F103 Blue Pill data, and it worked.

But when the power is disconnected from the BLue Pill, the data stored properly is the Hour, Minute and Seconds data, while the Date, Month and Year will reset to 1-1-2000.

A little annoying indeed, it turns out that Date, Month and Year data are stored in SRAM.

Date data needs to be saved to the backup register in the STM32F103C8T6 Blue Pill.

Our friend from Gorontalo with the name “Zulns” has made the RTC STM32F1 library with data updates to the backup register.

So even if the power is disconnected from STM32 or Blue Pill, the Date, Month and Year will not reset. Zulns, thanks for the library!

This article is an improvement article from the STM32F103 Bluepill Internal RTC article How to manage and display RTC data on the Arduino IDE Serial Monitor.

The board that I use is BluePill STM32F103.

The library that I use is Arduino_Core_STM32. You can download via this link, or click the button below.

Download Library

Zulns has made very good documentation from the STM32F1 RTC library on github, you can also read the stm32f1_rtc.h library download and the guide here:

Download RTC STM32 Library

Code

#include <stm32f1_rtc.h>

#define FLAGS_REGISTER  1
#define HIJRI_MODE_BIT  1
#define HIJRI_MODE_FLAG  (1 << HIJRI_MODE_BIT)
#define IS_HIJRI_MODE(x)      ((x.getBackupRegister(FLAGS_REGISTER) & HIJRI_MODE_FLAG)  == HIJRI_MODE_FLAG)

const char * weekdayName[] = { "Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jum'at", "Sabtu" };
const char * arabicWeekdayName[] = { "Ahad", "Ithnayn", "Thulatha", "Arba'a", "Khamis", "Jumu'ah", "Sabt" };
const char * monthName[] = { "Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Augustus", "September", "Oktober", "November", "Desember" };
const char * hijriMonthName[] = { "Muharram", "Safar", "Rabi'al-Ula", "Rabi'ath-Thani", "Jumadal-Ula", "Jumadath-Thani", "Rajab", "Sha'ban", "Ramadan", "Shawwal", "Dhul-Qa'dah", "Dhul-Hijjah" };

DateVar date;
TimeVar time;
STM32F1_RTC rtc;

uint32_t epochTime;
bool     isHijriMode = true;

void setup() {
  Serial.begin(115200);
  rtc.begin();
  isHijriMode = IS_HIJRI_MODE(rtc);

  //Hilangkan komen baris dibawah jika ingin atur waktu
  //  setWaktu();
  //  delay(100);

  epochTime = rtc.getTime();
  rtc.epochToTime(epochTime, time);
}

void loop() {
  if (rtc.isCounterUpdated()) {
    rtc.clearSecondFlag();
    epochTime = rtc.getTime();
    rtc.epochToTime(epochTime, time);
    printTime();
    printDate();
  }
void printTime() {
}



  uint8_t h = time.hours;

  print2Digits(h);
  Serial.print(":");
  print2Digits(time.minutes);
  Serial.print(":");
  print2Digits(time.seconds);
  Serial.print("  |  ");


void print2Digits(uint8_t n) {
}

  if (n < 10)
    Serial.print(0);
  Serial.print(n);
}

void setWaktu() {
  // Tentukan waktu yang ingin di atur
  time.hours = 11;
  time.minutes = 35;
  time.seconds = 0;
  date.day = 15;
  date.month = 5;
  date.year = 2020;

  epochTime = rtc.dateTimeToEpoch(date, time);
  rtc.setTime(epochTime);
}

void printDate() {
  //Kalender Hijriah
  rtc.epochToHijriDate(epochTime, date);
  Serial.print(arabicWeekdayName[date.weekday]);
  Serial.print(", ");
  Serial.print(date.day);
  Serial.print(" ");
  Serial.print(hijriMonthName[date.month - 1]);
  Serial.print(" ");
  Serial.print(date.year);
  Serial.print(" H");
  Serial.print("  |  ");

  // Kalender Masehi
  rtc.epochToDate(epochTime, date);
  Serial.print(weekdayName[date.weekday]);
  Serial.print(", ");
  Serial.print(date.day);
  Serial.print(" ");
  Serial.print(monthName[date.month - 1]);
  Serial.print(" ");
  Serial.println(date.year);
}


Program using ST Link V2

To upload the program to stm32f103c8t6, ​​I use ST Link V2 STM32 Programmer.

Meanwhile, to see the results through the serial monitor on Arduino IDE, I use FTDI.

Use STM32 RTC Internal Arduino IDE

If you feel that your RTC is late in counting of seconds, please do not connect anything to PC14 and PC15 pins.

If the header pin is still soldered on this pin, please remove it. This pin is connected directly to the RTC crystal.

This will disturb the oscillation of the crystal oscillator. Notice in the picture below, I cut on PCB path on pin PC14 and PC15. And now it runs normally.

STM32 RTC Internal Without Date Reset

I hope this can help you in developing STM32 RTC based electronic devices that require RTC. That’s all.