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 Philip,

    thanks for this great answer you helped me pretty much with the understanding how to use the rtc or the timer.

    Thanks for this great example, perfect for me but i have some question.

    • Why is the prescaler 31, you want to devide the timervalue by 32 is 31 right?
    • Why restarts the rtc_counter if it is bigger then 42 ? why this value?

    Thank you for helping, i am a newbie who trys to understand all this stuff. It is a lot to learn but with such examples it works fine :)

  • Yes Philip great Comment. With Tutorial it would be much easier to understand how the things work and to use them.

    Just for newbies like me it is pretty hard to understand the coherences so a turorial would be pretty helpful.

    Nils :)

  • I think this Topic here is pretty interesting for ather perople and newbies how starts programming the chip. To understand what ervy part of the chip does and how it can be programmed is essential for the users.

    I think i understand how to programm th rtc the HFCLK Timer is to programm similar? How to use this compare [] things in the pwm example?

    But i didn't understand how to use schedluing and what it is good for, its a little bit to abstract for me. When i have to use it and when not and what is the main task of the scheduler?

    When i have tu use the PPI and the GPIOTE ? Are they good for relieve the cpu? Or have i to use it also when i just to make a calculation with the cpu and then make an reaction at the output?

    Which timers ar use by the softdevice? Timer 0 and rtc 0?

    I hope you have some indulgence with me when i ask to much questions. It like i tell before i am a newbie. Its pretty much to lern but i hope i will understand it.

    Best regards, Nils :)

  • Hi Philip,

    Thanks. This is valuable feedback for us. I'll make sure it's passed on internally.

    Regarding item 3, tutorials. There's been some discussions about including tutorials as step-by-step code examples in the SDK, showing the 'why' as well as the 'how'. Do you think this is a usable format, or should it be a completely standalone document like for example an Application Note?

    There's also been discussed including a complete set of peripheral drivers with a uniform API in the SDK. Using a driver API can be an alternative to learning registers and details regarding peripheral operation. Hopefully this can make development quicker and easier for at least the more common peripheral uses. As you say however, the PPI is not a common interface intuitively understood. A driver by itself won't give any rationale or insights into how the PPI can be best used.

  • Hi Nils,

    I am delighted that my comments and example code are helping you.

    The PRESCALER register is set to 31 because the actual divide operation is the register value + 1. This is shown in the first formula in 18.1.2

    So if you want to divide by 32, you load PRESCALER with 31. Although 2 examples are given they are not clear enough about what is going on. Here is what I would add to the first example:

    round(32768/100) - 1 = round(327.68) - 1 = 328 - 1 = 327 So PRESCALER is set to 327, and the LFCLK is divided by 328 giving 99.9 Hz

    You wrote "Why restarts the rtc_counter if it is bigger then 42?" In my example code, I do not reset the rtc_counter if it is bigger than 42. The rtc_counter is never reset, it just keeps counting at 1024 Hz, due to the divide by 32 of the LFCLK (because PRESCALER is 31). In fact my code doesn't even care about the value in COUNTER. But every time COUNTER is incremented (every 976.5625 us), a TICK interrupt also occurs, and that is what I use for the rest of my code.

    So the RTC1 causes an interrupt at slightly faster than every millisecond (976.5625 us) and so on each tick, the "millisecond clock gains 23.4375 us". After 1 second of real time, the RTC1_Milliseconds value would be 1024 (which is no surprise, since the RTC1 is running at 1024 Hz).

    So how do you make it increase by 1000 every second, rather than 1024. We could just subtract 24 once per second, but that would be a big jump. Instead I decided to not do the increment (skip it) on 24 occurrences during the 1 second period, and spread these skips out evenly during the 1 second. So if you increment RTC1_Milliseconds every tick (at 1024 Hz) but after 41 of these, on the next tick we don't do the increment, then after 1024 interrupts (ticks) the RTC1_Milliseconds will have increased by 1000. RTC1_skip_increment_counter is what controls this.

Related