Super Simple RTC

Post your cool example code here.
User avatar
neo2121
Posts: 15
Joined: Tue Aug 02, 2016 2:35 pm
Location: Bucharest, Romania
Contact:

Super Simple RTC

Post by neo2121 » Mon Aug 22, 2016 9:28 pm

I'm building a smart watch based on the STM32F1 and I got to the RTC part. For this I need to use the RTC built into the STM. Having read the threads here, I've decided to put together a super simplified example that (I hope) anyone will understand, even relative beginners in Arduino. Hope it helps someone. The library's example is simple enough, but I felt another approach couldn't hurt.

Code: Select all

#include <RTClock.h>
//this is where we will store the unix time
uint32_t tt = 0;
//these will store your hours, minutes, seconds for display
//we will update these variables whenever we read the RTC so we can use them over and over wherever we need
byte currentHours = 0;
byte currentMinutes = 0;
byte currentSeconds = 0;

// initialise the RTC library. 
// NOTE: 'RTCSEL_LSE' assumes you have a 32.768Mhz crystal setup physically attached to the appropriate pins 
// on the microcontroller. Pins PC15 and PC14 are the RTC crystal pins.
RTClock rtc (RTCSEL_LSE); 

void setup() {
  Serial.begin(9600);
  //you can also SET the rtc time with rtc.setTime(unixTimestamp) where unixTimestamp is a unix timestamp
}

void loop() {
  //get the RTC time (returns unix time, a 32bit integer representing seconds elapsed from Jan 01, 1970)
  tt = rtc.getTime();
  breakTime(tt);
  if(currentHours < 10) { Serial.print(0); } //leading zeroes to look nice
  Serial.print( currentHours );
  Serial.print( ":" );
  if(currentMinutes < 10) { Serial.print(0); } //leading zeroes to look nice
  Serial.print( currentMinutes );
  Serial.print(":");
  if(currentSeconds < 10) { Serial.print(0); } //leading zeroes to look nice
  Serial.println( currentSeconds );
  
  delay(1000);//or whatever
}

//stolen and simplified from the time library by Paul Stoffregen
//just derives the hours, minutes, seconds from the unix timestamp and sets the global variables we use for display to those values
void breakTime(uint32_t timeInput){
  currentSeconds = timeInput % 60;
  timeInput /= 60; // now it is minutes
  currentMinutes = timeInput % 60;
  timeInput /= 60; // now it is hours
  currentHours = timeInput % 24;
}
I've tried to not use more advanced stuff like interrupts etc. This code just gets the time, stores it in variables for hour/minute/second and serial prints a string with them. No extra libraries are required, just the RTClock library that comes with stm32duino.

Hardware:
Image
Last edited by neo2121 on Tue Aug 23, 2016 6:13 am, edited 2 times in total.

User avatar
Pito
Posts: 1502
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: Super Simple RTC

Post by Pito » Mon Aug 22, 2016 10:11 pm

The 'blue pill' does not have it, but you can add it yourself on the breadboard.
It does :)
Btw errata recommends for the LSE external oscillator:
It is recommended to mount an additional parallel feedback resistor (from 16 MΩ to 22 MΩ) on board to help the oscillation start-up in all cases (see Figure 1). For more details on compatible crystals and hardware techniques on PCB, refer to AN2867 application note.
Pukao Hats Cleaning Services Ltd.

User avatar
neo2121
Posts: 15
Joined: Tue Aug 02, 2016 2:35 pm
Location: Bucharest, Romania
Contact:

Re: Super Simple RTC

Post by neo2121 » Tue Aug 23, 2016 5:58 am

So THAT is what that package next to the 8mhz crystal is? I couldn't find the code on it anywhere and had no idea crystals came in non-shiny packages too :)) I edited my post, thanks for the heads up.

Also, the datasheet for the STM32F103x8 says there is a 5M feedback resistor built into the chip itself (pg. 54), so that's why I said it's optional.

User avatar
Kurt_R
Posts: 15
Joined: Thu Nov 05, 2015 1:13 am

Re: Super Simple RTC

Post by Kurt_R » Sun Sep 18, 2016 12:46 am

Thanks for this, do you happen to know which RTC units this should work with?
I have DS1302, DS1307 and DS3231, hopefully one of those fit into this build.

Kurt

User avatar
RogerClark
Posts: 6711
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Super Simple RTC

Post by RogerClark » Sun Sep 18, 2016 3:34 am

@neo2121

Slightly off topic, but if you want a smart watch, it may be easier to re-purpose an nRF51822 based smart watch / fitness tracker.

You can get them for less than $20, with an nRF51822 with 256k flash and 32k ram, 64 x 32 pixel OLD display, 3 axis accelerometer, 1 button, 1 vibration motor and also a heart rate sensor

I have one on my bench at the moment that I'm getting to grips with
ID100HR_front_smart_watch1.jpg
ID100HR_front_smart_watch1.jpg (112.52 KiB) Viewed 1626 times
Finally got the display to work correctly last night
Picture 7.jpg
Picture 7.jpg (69.51 KiB) Viewed 1626 times

User avatar
Just4Fun
Posts: 55
Joined: Tue Jan 05, 2016 9:48 pm
Contact:

Re: Super Simple RTC

Post by Just4Fun » Sun Sep 18, 2016 11:32 am

Pito wrote: Btw errata recommends for the LSE external oscillator:
It is recommended to mount an additional parallel feedback resistor (from 16 MΩ to 22 MΩ) on board to help the oscillation start-up in all cases (see Figure 1). For more details on compatible crystals and hardware techniques on PCB, refer to AN2867 application note.
Thanks Pito, I completely missed that! :oops:

Now understand some "strange" behavior I had with LSE... (and some people at STM forum too...)

Cheers

User avatar
ahull
Posts: 1578
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: Super Simple RTC

Post by ahull » Sun Sep 18, 2016 11:44 am

Kurt_R wrote:Thanks for this, do you happen to know which RTC units this should work with?
I have DS1302, DS1307 and DS3231, hopefully one of those fit into this build.

Kurt
The STM32FXXX series has a built in RTC. You don't need any additional RTC, but your board will need the 32,768 Hz crystal, 2 x caps and a suitable 3v3 battery attached to VBat.
- Andy Hull -

User avatar
Kurt_R
Posts: 15
Joined: Thu Nov 05, 2015 1:13 am

Re: Super Simple RTC

Post by Kurt_R » Mon Sep 19, 2016 12:01 am

Ohh, okay. So that's similar to the Teensy, when they included a crystal and a battery.

User avatar
Pito
Posts: 1502
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: Super Simple RTC

Post by Pito » Tue Sep 20, 2016 4:51 pm

The lowest energy takes the PCF8563 - 200nA. Needs an resonator and 1 capacitor. Precision depends on the resonator and capacitor (and T and Vcc), and usually several seconds a month when capacitor set properly.

The mid current is with DS1307, needs resonator and capacitor, the precision same as w/ the PCF.

The highest current is with the DS3231 - 1.5uA. It does not require external components, it is temp compensated and possess aging tuning, and precision is a second per month (mine did 2secs/6Months, at room temp, after messing with aging tuning).

The RTCs inside MCUs are sometimes even more power hungry, need an resonator and capacitors, have sometimes a simple tuning via manipulating the seconds register. Precision similar to DS1307..

PS: my ETA2824-2-elabore inside my watch does -9sec/day and I am happy.. :P
Pukao Hats Cleaning Services Ltd.

User avatar
neo2121
Posts: 15
Joined: Tue Aug 02, 2016 2:35 pm
Location: Bucharest, Romania
Contact:

Re: Super Simple RTC

Post by neo2121 » Mon Sep 26, 2016 4:13 pm

@Roger

I saw you posted that eBay link on some other thread. But which exact model is it? I'd love to get one and play around, but the link you posted had various products at various prices, been meaning to ask which one it was.

Regarding my smart-watch, I have the schematic done and the core of it is working on my breadboard. I want to make a custom PCB and all that jazz, not because it's easy but because it's hard :)) I'll post a thread in the projects section when it looks a bit more interesting. The plan is to have a watch with a gpio female header on one side that you can poke stuff into and an oscilloscope.

Post Reply