Hi I want to toggle LED with timing as follows 100ms ON1, 250ms Off1 1250ms ON2, 1500ms off2 and this cycle gets repeated (Both ON1 off1 and ON2 off2 pair repeats) For this I have planned to utilize hardware timer with elapsing timings as 100,250,1250 and 1500 and this will repeat. I am pretty new to the embedded field, My questions are as follows How to trigger this using a hardware timer? (How to enable and alternate the timings dynamically?) How to set a call back function that toggles LED based on the timer elapse ? Note : This is not a standalone code but an application will Be running .From OS the callback will be triggered in the background so that other normal application is not affected during this process
Call back function to be called whenever hardware timer elapses a specified elapse time in STM32F101
-
1Please only ask one question per question. Essentially you are now asking "how to write the whole program", which is too broad a question. – Lundin Jan 16 '18 at 12:28
-
the call back is essentially an interrupt handler. Since you have different time steps, youc an for example have an intterupt every 50ms (least common denominator), every other interrupt call a function to handle the 100ms case, every 5 the 250 and so on. Or you can use 5 timers or some combination in between. – old_timer Jan 16 '18 at 15:35
-
If you have an operating system then it is a different story, see answer below, read the OS documentation and examples to understand how to request such services. – old_timer Jan 16 '18 at 15:35
2 Answers
Use the OS's software timer service. Create four software timers with the specified periods. The timers should be configured to count once and then stop each time they are started (i.e., they should be "one-shot", not "auto-reloading" nor "continuous" or whatever terminology your OS uses). Each software timer will have a unique callback function that you specify when you create the software timer.
From main
, or wherever is appropriate, start the first timer once to get things started. Then start the second timer from the first timer's callback function. And start the third timer from the second timer's callback function. And so on until the last timer's callback function restarts the first timer and the circle of timers repeats.

- 6,643
- 1
- 17
- 30
-
-
2@PeterJ_01 The op's question says "From the OS", which implies there is. – Colin Jan 16 '18 at 16:04
-
Yeah on f101. Maybe nanoWindows? OP uses very strange (non technical) language anyway – 0___________ Jan 16 '18 at 16:06
-
2
Use timer interrupt for it.
You have the ready example here: https://www.diymat.co.uk/arm-blinking-led-driver/
It does what you need and a bit more as well :)

- 60,014
- 4
- 34
- 74