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 :)

  • Hi Nils,

    as you say, there are 3 Timers available on the nRF51822. In addition there are 2 RTC's. The Timers use the 16 MHz clock (HFCLK) , while the RTC's use the 32 kHz clock (LFCLK).

    When the softdevice is enabled it reserves one Timer and one RTC (in addition to other HW resources). That leaves your application with 2 Timers and 1 RTC. For PWMs you would typically use a Timer, while timekeeping is usually done with an RTC.

    If you have multiple tasks that require millisecond-resolution timing, you can look into the SDK component app_timer. App_timer uses the RTC and facilitates timed events. For example: you want to read a sensor every 10 ms, do some processing every 100 ms, and then run a 3 second timeout for something.

    To summarize: the Timers are quite generic and can be connected to different peripherals to perform different functions.It's up to your application to make sure the Timers and RTCs are allocated (and multiplexed if needed) to the different tasks you want to perform.

  • 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.

  • Hi,

    thank you for your answers, you helped me pretty much with understanding how to use the timers. Philip do you have any sample Code, i think i will use one timer like you did it.

    I think my biggest problem is understanding how the chips work. I read what gpiote, ppi, tasks and events mean. But i didn't understand how to use them or how they work together.

    And when i want to programm something like a button i use gpiote but why?

    I think this is preety important for programming the chip, knowing how this works. Maybe you can help me or know some good stuff that i can read.

    best regards Nils :)

  • Hi Nils,

    I don't have any sample code of the round robin scheduler that I can share.

    But I have attached my code for setting up RTC1 for a 1ms interrupt, with two 32 bit counters for milliseconds and seconds since the "beginning of time" .

    To build the scheduler, you would add the timer_variables and bool stuff after line 73.

    You can use either of the two 32 bit counters to do some other neat things in your code, like this:

    [b] temp = RTC1_Milliseconds; while(RTC1_Milliseconds < (temp+37)) {} // wait here for 37 ms

    temp = RTC1_Seconds;
    {
        // lots of stuff goes in here that takes a long time to execute
    }
    printf("Execution time in seconds:  %6d\n", RTC1_Seconds - temp);[/b]
    

    The example code supplied refers to one of my header files, which must also be included in routines that use this stuff. the only relevant lines to this example in the header are:

    [b]extern uint32_t RTC1_Milliseconds; extern uint32_t RTC1_Seconds; [/b]

    Enjoy.

    RTC1_ms_timer.c

Related