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

Timer - nRF52833

Hello everyone,

I'm a fresher of programming nRF52833. I'm working with Timer and I have some questions like this:

  1. Can I use several timer in an application? I mean I want to use 4 timer for 4 GPIOTE. Is that possible to do??
  2. How can I measure the time difference between 2 separate 2 events?? I want to calculate the time between event A and event B and event B happen after event.

I hope to see the answer as soon as possible . Thanks.

Best regards!!

  • Hi,

    1. Yes, the nRF52833 has 5 TIMER instances available. Instance 0 is used by the softdevice, if you are using that. The others are available for use by the application.

    2. This depends a bit on which events you are referring to, and what the goal you are trying to achieve is. You can use a TIMER to measure the time, by connecting the two events to the timer's START and CAPTURE tasks, then read out the count from the CC register in an interrupt handler, etc., and convert this to elapsed time from the frequency that the timer was running at.

    Best regards,
    Jørgen

  • Hi, thanks for your reply

    1. So if I want to use other TImer, what should I declare?

    2. Are there any example code about this? I need some references for this stuff!!!

    Thanks,

    Best regards,

  • 1. That depends on how you are interacting with the timer. If you are writing directly to the registers, you can use NRF_TIMER1->/NRF_TIMER2-> and so on. If you are using the timer driver, you can pass 1/2/3/4 to the NRF_DRV_TIMER_INSTANCE() macro when defining the timer.

    2. I created an example for something similar some time ago, see this post. If you are not using pin inputs to generate the events, you can remove the gpiote_init() function. Then you should replace the following events in ppi_init() with the events you want to measure time between:

    uint32_t gpiote_evt_addr_1          = nrf_drv_gpiote_in_event_addr_get(PIN_IN_1);
    uint32_t gpiote_evt_addr_2          = nrf_drv_gpiote_in_event_addr_get(PIN_IN_2);

    nRF52 series ICs also has a fork feature for PPI, allowing you to trigger two separate tasks from a single event over the same PPI channel. You can replace the following:

    err_code = nrf_drv_ppi_channel_alloc(&ppi_channel_2);
    APP_ERROR_CHECK(err_code);
    err_code = nrf_drv_ppi_channel_alloc(&ppi_channel_3);
    APP_ERROR_CHECK(err_code);
    
    uint32_t timer_stop_task_addr       = nrf_drv_timer_task_address_get(&m_timer, NRF_TIMER_TASK_STOP);
    uint32_t timer_compare_task_addr    = nrf_drv_timer_task_address_get(&m_timer, NRF_TIMER_TASK_CAPTURE0);
    
    err_code = nrf_drv_ppi_channel_assign(ppi_channel_2, gpiote_evt_addr_2, timer_compare_task_addr);
    APP_ERROR_CHECK(err_code);
    err_code = nrf_drv_ppi_channel_assign(ppi_channel_3, gpiote_evt_addr_2, timer_stop_task_addr);
    APP_ERROR_CHECK(err_code);
    
    err_code = nrf_drv_ppi_channel_enable(ppi_channel_2);
    APP_ERROR_CHECK(err_code);
    
    err_code = nrf_drv_ppi_channel_enable(ppi_channel_3);
    APP_ERROR_CHECK(err_code);

    With this, if you want to save a PPI channel:

    err_code = nrf_drv_ppi_channel_alloc(&ppi_channel_2);
    APP_ERROR_CHECK(err_code);
    
    uint32_t timer_stop_task_addr       = nrf_drv_timer_task_address_get(&m_timer, NRF_TIMER_TASK_STOP);
    uint32_t timer_compare_task_addr    = nrf_drv_timer_task_address_get(&m_timer, NRF_TIMER_TASK_CAPTURE0);
    
    err_code = nrf_drv_ppi_channel_assign(ppi_channel_2, gpiote_evt_addr_2, timer_compare_task_addr);
    APP_ERROR_CHECK(err_code);
    err_code = nrf_drv_ppi_channel_fork_assign(ppi_channel_2, timer_stop_task_addr);
    APP_ERROR_CHECK(err_code);
    
    err_code = nrf_drv_ppi_channel_enable(ppi_channel_2);
    APP_ERROR_CHECK(err_code);

    Best regards,
    Jørgen

  • thanks for your reference and your reply. I'm very appreciate about it. Thanks for your support

    Best regards.

Related