Skip to main content
Indois
Associate III
May 25, 2021
Question

Long timer interrupt periods

  • May 25, 2021
  • 6 replies
  • 4231 views

Hello,

I would like to have timer interrupts every 10 minutes for temperature readings. Also, I would like to have opportunity to change this period in the code. How could such long period be implemented? Are there any examples? I am using STM32L011F4 MCU and STM32CubeIDE environment.

Thank you.

This topic has been closed for replies.

6 replies

waclawek.jan
Super User
May 25, 2021

Set the timer's prescaler and ARR so that resulting time is 10 minutes.

If it's not possible with chosen timer, set it to a suitable long period, and in the interrupt count up until the required time elapsed.

Alternatively, chain timers using master-slave connection, use the slave in external clock mode, and set both timers' PSC and ARR to match the required period.

JW

Indois
IndoisAuthor
Associate III
May 25, 2021

STM32CubeMX clock configuration settings (MCU works at maximum 32 MHz):

0693W00000APbnnQAD.png 

Generated Timer init function:

0693W00000APbbBQAT.png 

The maximum period I can enter is 65 536 ms (2^16) which is 65.536 seconds. If I enter for example 70 000 then timer interrupts every 5 seconds.

How could I set period to 10 minutes (600 000 ms)?

waclawek.jan
Super User
May 25, 2021

Set it to 1 minute (60 seconds), and in the interrupt service routine (ISR) count to 10.

JW

Indois
IndoisAuthor
Associate III
May 25, 2021

But if I use counting, it will act like delay which results in blocking the program. In such case period could not be changed during operation.

Ons KOOLI
Associate
May 25, 2021

Hi @Indois​ ,

Please refer to the AN3964. This application note describes a method of implementing a simple application for temperature measurement using the STM32L-DISCOVERY and 32L152CDISCOVERY boards. You can start from here and port it to your preferred board. When powered up, the temperature sensor application example first displays a welcome message before immediately displaying the current temperature in degrees Celsius with a 2-second refresh rate.

Best Regards,

Ons.

Indois
IndoisAuthor
Associate III
May 25, 2021

I am reading temperature from external sensor and would like to have long periods (like 10 minutes) between readings.

Indois
IndoisAuthor
Associate III
May 25, 2021

Maybe there are other methods to code interrupts for at least every 10 minutes (without timers)? I would like to have opportunity to change period during operation, so delay function could not be used in this case (it blocks the program operation).

waclawek.jan
Super User
May 25, 2021

> But if I use counting, it will act like delay which results in blocking the program.

No.

Simply setup up the timer for the 1 minute period, enable it, enable interrupt, and set a global variable to 10. In the ISR, decrement that global variable, and once it reaches 0, you do your action (temperature measurement). Counting happens only once in a minute in the ISR, the rest of program runs as usually.

JW

Tesla DeLorean
Guru
May 25, 2021

For long duration presumably you could use the RTC Alarm?

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..