This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

comprehension question Timer

Hi,

maybe i have a silly question, but i didn't programm timers before. I know how they work. But how is it possible that there are only three Timers in the 51822 and so many parts of the programm part (PWM,BLE,GPIOTE...) which need a timer? How can i organize my timers?

and is it possible to run one timer and write a function like "get_actual_time" which gives me the time in the moment i call the function?

Best reagards, Nils :)

Parents
  • Hi Nils,

    I often use the following approach as a very simple round-robin scheduler, with tasks that have to run at different rates.

    I do the following for things that need timers of greater than 1ms. I set up a timer to generate an interrupt every 1 ms. I have a uint32_t timer_variable for each of my repetitive tasks. I have a flag (bool) for each task.

    In the interrupt routine, I increment all the timer_variables, and test each one to see if it is at its max value, and if it is I set the bool for that timer_variable (task), and reset the timer_variable. So I can have some thing happening every 10 ms or 10000ms or any other repeat duration. The interrupt routine then exits.

    In the main code is a loop forever block that checks the bool values, and if any are set, the appropriate task is run and the bool is reset. Obviously, you need to be careful how long these routines run, and what the total execution time is.

    One of these timer_variable could be setup to count to 1000 and the task then runs once per second. The task could implement a clock & calendar.

Reply
  • Hi Nils,

    I often use the following approach as a very simple round-robin scheduler, with tasks that have to run at different rates.

    I do the following for things that need timers of greater than 1ms. I set up a timer to generate an interrupt every 1 ms. I have a uint32_t timer_variable for each of my repetitive tasks. I have a flag (bool) for each task.

    In the interrupt routine, I increment all the timer_variables, and test each one to see if it is at its max value, and if it is I set the bool for that timer_variable (task), and reset the timer_variable. So I can have some thing happening every 10 ms or 10000ms or any other repeat duration. The interrupt routine then exits.

    In the main code is a loop forever block that checks the bool values, and if any are set, the appropriate task is run and the bool is reset. Obviously, you need to be careful how long these routines run, and what the total execution time is.

    One of these timer_variable could be setup to count to 1000 and the task then runs once per second. The task could implement a clock & calendar.

Children
No Data
Related