STM32RTC: How to trigger an interrupt every second

Post here first, or if you can't find a relevant section!
Post Reply
forfrends
Posts: 14
Joined: Mon Jan 20, 2020 10:49 am

STM32RTC: How to trigger an interrupt every second

Post by forfrends »

Hello everyone!
I am using the STM32F103C8T6 board (blue pill) and kernel: "STM32 Cores by STMicroelectronycs" https://github.com/stm32duino/wiki/wiki/Getting-Started
I am using the STM32RTC library: https://github.com/stm32duino/STM32RTC
I need to call an interrupt 1 time per second. Can you give an example of how to do this? In the library there are only examples for interruption for a specific time (for example: 07:25:00). I need interruption to happen every second.
as an example, there is an RTClock library, but this library does not work on this kernel. There is a command in this library:

Code: Select all

#include <RTClock.h>

RTClock rt (RTCSEL_LSE); // initialise
uint32 tt; 

void SecondsInterrupt()       // This function is called in the attachSecondsInterrpt
{
    // some code
}

void setup() 
{
    rt.attachSecondsInterrupt(SecondsInterrupt);// Call SecondsInterrupt
}
Is there something similar in the STM32RTC library?
Please help me with an example.
by stas2z » Tue Apr 21, 2020 7:17 pm
forfrends wrote: Tue Apr 21, 2020 7:09 pmAn example using a timer interrupt causes a lot of errors: https://github.com/stm32duino/STM32Exam ... llback.ino
you need example shipped with core or use specific commit compatible with your core version
https://github.com/stm32duino/STM32Exam ... llback.ino

latest commits are for upcoming 1.9.0 core
Go to full post
stas2z
Posts: 131
Joined: Mon Feb 24, 2020 8:17 pm
Answers: 8

Re: STM32RTC: How to trigger an interrupt every second

Post by stas2z »

If you need just an every second interrupt, use HardwareTimer library
fredbox
Posts: 125
Joined: Thu Dec 19, 2019 3:05 am
Answers: 2

Re: STM32RTC: How to trigger an interrupt every second

Post by fredbox »

I am using the STM32F103C8T6 board (blue pill) and kernel: "STM32 Cores by STMicroelectronycs" https://github.com/stm32duino/wiki/wiki/Getting-Started
That is the right place to start. Select API References, Hardware Timer. There are examples there.
You can also use systick which happens 1000 times per second. See viewtopic.php?p=644#p644
forfrends
Posts: 14
Joined: Mon Jan 20, 2020 10:49 am

Re: STM32RTC: How to trigger an interrupt every second

Post by forfrends »

How accurate will the HardwareTimer be? If I count every second, what will be the difference with RTC?
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: STM32RTC: How to trigger an interrupt every second

Post by fpiSTM »

It will be more accurate than the STM32RTC usage.
Currently, with the current STM32RTC, you could set an Alarm to match the second but you will have to reload the alarm at each interrupt.
It currently does not implement the subsecond.
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: STM32RTC: How to trigger an interrupt every second

Post by mrburnette »

forfrends wrote: Tue Apr 21, 2020 1:56 pm How accurate will the HardwareTimer be? If I count every second, what will be the difference with RTC?
The real question is, "How accurate are your needs?" Then a comparison can be made to reality.
Generic 8MHz crystals on a $2 Blue Pill are not going to be the very best quality, but will be adequate for most purposes. Were you to order a crystal, you would see numerous choices that an engineer would be presented:
Screenshot from 2020-04-21 10-43-27.png
Screenshot from 2020-04-21 10-43-27.png (40.92 KiB) Viewed 12442 times
As the digital clock module on the microcontroller is based on the accuracy of the crystal, the better the crystal the better the clock long-term accuracy.
stas2z
Posts: 131
Joined: Mon Feb 24, 2020 8:17 pm
Answers: 8

Re: STM32RTC: How to trigger an interrupt every second

Post by stas2z »

mrburnette wrote: Tue Apr 21, 2020 2:47 pm
forfrends wrote: Tue Apr 21, 2020 1:56 pm How accurate will the HardwareTimer be? If I count every second, what will be the difference with RTC?
The real question is, "How accurate are your needs?" Then a comparison can be made to reality.
Generic 8MHz crystals on a $2 Blue Pill are not going to be the very best quality, but will be adequate for most purposes. Were you to order a crystal, you would see numerous choices that an engineer would be presented:

Screenshot from 2020-04-21 10-43-27.png

As the digital clock module on the microcontroller is based on the accuracy of the crystal, the better the crystal the better the clock long-term accuracy.
same can be said about rtc
pill's 32.768kHz crystal is not a very precise part also
forfrends
Posts: 14
Joined: Mon Jan 20, 2020 10:49 am

Re: STM32RTC: How to trigger an interrupt every second

Post by forfrends »

I do not need super high accuracy. + - 1-3 minutes a day - for me it is permissible.
I was just stuck with choosing a timing source. I need an interrupt every second. The RTClock library has such a function, but does not work on the kernel from STMicroelectronycs. The STM32RTC library runs on this core, but does not have the necessary function. An example using a timer interrupt causes a lot of errors: https://github.com/stm32duino/STM32Exam ... llback.ino

Code: Select all

Blink:45:26: error: invalid conversion from 'void (*)()' to 'void (*)(HardwareTimer*)' [-fpermissive]
   45 |   MyTim->attachInterrupt(Update_IT_callback);
      |                          ^~~~~~~~~~~~~~~~~~
      |                          |
      |                          void (*)()
In file included from C:\Users\�����\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\cores\arduino/stm32/analog.h:45,
                 from C:\Users\�����\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\cores\arduino/board.h:8,
                 from C:\Users\�����\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\cores\arduino/wiring.h:41,
                 from C:\Users\�����\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\cores\arduino/Arduino.h:32,
                 from sketch\Blink.ino.cpp:1:
C:\Users\�����\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\cores\arduino/HardwareTimer.h:129:33: note:   initializing argument 1 of 'void HardwareTimer::attachInterrupt(void (*)(HardwareTimer*))'
  129 |     void attachInterrupt(void (*handler)(HardwareTimer *)); // Attach interrupt callback which will be called upon update event (timer rollover)
      |                          ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
exit status 1
invalid conversion from 'void (*)()' to 'void (*)(HardwareTimer*)' [-fpermissive]

Blink:2:2: error: #error "Due to API change, this sketch is compatible with STM32_CORE_VERSION  >= 0x01090000"
    2 | #error "Due to API change, this sketch is compatible with STM32_CORE_VERSION  >= 0x01090000"
      |  ^~~~~
exit status 1
#error "Due to API change, this sketch is compatible with STM32_CORE_VERSION  >= 0x01090000"
stas2z
Posts: 131
Joined: Mon Feb 24, 2020 8:17 pm
Answers: 8

Re: STM32RTC: How to trigger an interrupt every second

Post by stas2z »

forfrends wrote: Tue Apr 21, 2020 7:09 pmAn example using a timer interrupt causes a lot of errors: https://github.com/stm32duino/STM32Exam ... llback.ino
you need example shipped with core or use specific commit compatible with your core version
https://github.com/stm32duino/STM32Exam ... llback.ino

latest commits are for upcoming 1.9.0 core
forfrends
Posts: 14
Joined: Mon Jan 20, 2020 10:49 am

Re: STM32RTC: How to trigger an interrupt every second

Post by forfrends »

Thanks!
I did a little test. I made a timer interrupt every second, and counted seconds, minutes, and hours. Along with starting the program, I also started a timer on the phone. After 12 hours, he verified the testimony. The timer on STM32 showed a value of 3 seconds more than on the phone. This is perfect for my device!
Thanks for the help!
Post Reply

Return to “General discussion”