18 Timer/counter (TIMER)

The timer task is used to create a time delay between the executions of two tasks. You can specify the time in the timer task to wait before the process triggers the next task in the workflow.

Timer/counter (TIMER) user guide link.

/**@brief Initialize the application timer module.
 * @details This macro handles dimensioning and allocation of the memory buffer required by the timer,
 *          making sure that the buffer is correctly aligned. It will also connect the timer module
 *          to the scheduler (if specified).
 * @note    This module assumes that the LFCLK is already running. If it is not, the module will
 *          be non-functional, since the RTC will not run. If you do not use a SoftDevice, you
 *          must start the LFCLK manually. See the rtc_example's lfclk_config() function
 *          for an example of how to do this. If you use a SoftDevice, the LFCLK is started on
 *          SoftDevice init.
 * @param[in]  PRESCALER        Value of the RTC1 PRESCALER register. This will decide the
 *                              timer tick rate. Set to 0 for no prescaling.
 * @param[in]  OP_QUEUE_SIZE    Size of the queue holding timer operations that are pending execution.
 * @param[in]  SCHEDULER_FUNC   Pointer to scheduler event handler
 * @note Since this macro allocates a buffer, it must only be called once (it is OK to call it
 *       several times as long as it is from the same location, for example, to do a re-initialization).
/*lint -emacro(506, APP_TIMER_INIT) */ /* Suppress "Constant value Boolean */
    do                                                                            \
    {                                                                             \
                                               sizeof(uint32_t))];                \
        uint32_t ERR_CODE = app_timer_init((PRESCALER),                           \
                                           (OP_QUEUE_SIZE) + 1,                   \
                                           APP_TIMER_BUF,                         \
                                           SCHEDULER_FUNC);                       \
        APP_ERROR_CHECK(ERR_CODE);                                                \
    } while (0)			

PRESCALER determines the time resolution of the timer. It sets the amount of time it can count before it wrap around. On the nRF52 the RTC is a 24-bit counter with a 12 bit prescaler that run on the 32.768 LFCLK.

The counter increment frequency (tick rate) fRTC (kHz) = 32.768/(PRESCALER+1).


32.768 kHz * 1/(15+1) = 2.048 kHz

The timer will wrap around every (224) * 1/2.048 kHz = 8192 s.

Repeated timer

An application timer in the repeated mode will restart each time it expires. Toggling an LED is a good example of a repeated timer.

uint32_t app_timer_create(app_timer_id_t const * p_timer_id, app_timer_mode_t mode, app_timer_timeout_handler_t timeout_handler)

uint32_t app_timer_init (uint32_t prescaler, uint8_t op_queues_size, void * p_buffer, app_timer_evt_schedule_func_t evt_schedule_func)