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

RTC

Hai,

We are using RTC in one of our project (in nrf52832).

RTC is working fine. I am waking up by every 30 mins once. But I am not able to configure same RTC instance

into a different wake up time. (example 15 mins ,60 mins and 120 mins).

How i can solve the above issue. kindly let us know the solution / or any example code to configure rtc in different interval of wake up time.

Regards,

Meghana

  • Hi Meghana,

    Can you show your code so that I can suggest exactly what you need to change? Generally, as you use it for wakeup, I assume you wake up once the timer reaches a value in the compare register. So to change the time you either need to change the value of your CC register, or the Prescaler (or a combination). I would also like to mention that you could consider using the app timer library for this purpose.

    Einar

  • Hi,

    kindly look into the attached

     

    #define COMPARE_COUNTERTIME  60UL //60seconds once wakeup
    
    
    
    void rtc_handler(nrf_drv_rtc_int_type_t int_type)
    {
        if (int_type == NRF_DRV_RTC_INT_COMPARE0)
        {
          RTC_Flag=1;
         // nrf_gpio_pin_toggle(18);//RED_LED
           nrf_drv_rtc_counter_clear(&rtc); //nrf_gpio_pin_toggle(COMPARE_EVENT_OUTPUT);
          nrf_drv_rtc_int_enable(&rtc, NRF_RTC_INT_COMPARE0_MASK);
        }
        else if (int_type == NRF_DRV_RTC_INT_TICK)
        {
          // nrf_gpio_pin_toggle(18);
        }
    }
    
    
    void rtc_config(void)
    {
        uint32_t err_code;
    
        //Initialize RTC instance
        nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
        config.prescaler = 4095;
        err_code = nrf_drv_rtc_init(&rtc, &config, rtc_handler);
        APP_ERROR_CHECK(err_code);
    
        //Enable tick event & interrupt
        nrf_drv_rtc_int_enable(&rtc, NRF_RTC_INT_COMPARE0_MASK); 
        //nrf_drv_rtc_tick_enable(&rtc,true);
    
        //Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
        err_code = nrf_drv_rtc_cc_set(&rtc,0,COMPARE_COUNTERTIME * 8,true);
        APP_ERROR_CHECK(err_code);
    
        //Power on RTC instance
        nrf_drv_rtc_enable(&rtc);
    }
    
    int main()
    {
        // code...........
        //code....
       rtc_config();
       while(10
       {
           __WFE;
           // code...........
           //code....
       }
    }
    code.

    In the above code device is waking up for every 60 secs once. In same code and in same instance of rtc.

    i need to wake up at different interval....and i tried by changing COMPARE_COUNTERTIME..

    But its not working..

    Note: In our application, we want to use rtc as a delay for different interval.

    Regards,

    Meghana

  • Hi Meghana,

    I do not see any problems with your code that should cause this, and when I test your code (with some additions from the RTC example to make it work as you have stripped down the snippet in your previous post), it works as expected. This code gives a compare event every second:

    #include "nrf.h"
    #include "nrf_gpio.h"
    #include "nrf_drv_rtc.h"
    #include "nrf_drv_clock.h"
    #include "boards.h"
    #include "app_error.h"
    #include <stdint.h>
    #include <stdbool.h>
    
    #ifdef BSP_LED_0
        #define TICK_EVENT_OUTPUT     BSP_LED_0                                 /**< Pin number for indicating tick event. */
    #endif
    #ifndef TICK_EVENT_OUTPUT
        #error "Please indicate output pin"
    #endif
    #ifdef BSP_LED_1
        #define COMPARE_EVENT_OUTPUT   BSP_LED_1                                /**< Pin number for indicating compare event. */
    #endif
    #ifndef COMPARE_EVENT_OUTPUT
        #error "Please indicate output pin"
    #endif
    
    const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(0); /**< Declaring an instance of nrf_drv_rtc for RTC0. */
    
    #define COMPARE_COUNTERTIME  1UL //60seconds once wakeup
    
    static void leds_config(void)
    {
        bsp_board_init(BSP_INIT_LEDS);
    }
    
    
    /** @brief Function starting the internal LFCLK XTAL oscillator.
     */
    static void lfclk_config(void)
    {
        ret_code_t err_code = nrf_drv_clock_init();
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_clock_lfclk_request(NULL);
    }
    
    
    void rtc_handler(nrf_drv_rtc_int_type_t int_type)
    {
        if (int_type == NRF_DRV_RTC_INT_COMPARE0)
        {
          nrf_gpio_pin_toggle(TICK_EVENT_OUTPUT);//RED_LED
           nrf_drv_rtc_counter_clear(&rtc); //nrf_gpio_pin_toggle(COMPARE_EVENT_OUTPUT);
          nrf_drv_rtc_int_enable(&rtc, NRF_RTC_INT_COMPARE0_MASK);
        }
        else if (int_type == NRF_DRV_RTC_INT_TICK)
        {
          // nrf_gpio_pin_toggle(18);
          nrf_gpio_pin_toggle(COMPARE_EVENT_OUTPUT);
        }
    }
    
    
    void rtc_config(void)
    {
        uint32_t err_code;
    
        //Initialize RTC instance
        nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
        config.prescaler = 4095;
        err_code = nrf_drv_rtc_init(&rtc, &config, rtc_handler);
        APP_ERROR_CHECK(err_code);
    
        //Enable tick event & interrupt
        nrf_drv_rtc_int_enable(&rtc, NRF_RTC_INT_COMPARE0_MASK); 
        //nrf_drv_rtc_tick_enable(&rtc,true);
    
        //Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
        err_code = nrf_drv_rtc_cc_set(&rtc,0,COMPARE_COUNTERTIME * 8,true);
        APP_ERROR_CHECK(err_code);
    
        //Power on RTC instance
        nrf_drv_rtc_enable(&rtc);
    }
    
    int main()
    {
        // code...........
        //code....
       leds_config();
       lfclk_config();
       rtc_config();
       while(1)
       {
           __WFE();
           // code...........
           //code....
       }
    }

    Perhaps there is an issue in how you test this?

    Also, if you want to use the RTC for getting various delays you may want to look at the app timer library, which is used for this purpose all over the SDK. This use RTC1 under the hood but handles all the details for you.

Related