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

delay by timer

i want make delay funtion by timer?

in the other MCU like PIC, AVR, stm. i find register that hold value counter and i cant set this value for example: timer 8 bit will count to 256 and i set value =100 to every tick it increased to 256 overflow and make a period delay.

thanks.

  • Hello phanhust

    On the nRF51822 you have 3 timer modules which run on the HFCLK, and the RTC (real time counter) which runs on the LFCLK.

    When using the SoftDevice the first timer module, TIMER0 is controlled by the SoftDevice and is off limits for the application. Due to the timer modules using the HFCLK the current consumption while using them is considerable, and they should only be used for cases where high timing accuracy is needed. For use-cases with lower accuracy requirements, you can use the RTC which has much lower current consumption. Most examples in the SDK uses the timer library, which employs the RTC.

    You can read more about the Timer modules on page 33 and 62 of the nRF51822 product specification, and page 99 of the nRF51 series Reference manual.

    You can read about the RTC on page 33 and 62 of the product specification and page 103 of the reference manual.

    You can read about the timer library here.

    You can find examples for both the RTC and the timer modules in the examples\peripheral folder in the SDK, and you can find documentation for the examples on the infocenter (The provided link is for SDK12.3).

    Best regards

    Jørn Frøysa

  • thanks for comment. in my project i need accuracy delay timer so and don't worry about the power consumption so what i need to do is use module TIMER to creat delay funtion. but i tried do it like exmple :github.com/.../main.c

    but i saw all example IN SDK use interrup or event handle. in my case i just want period that was made by timer and do not anything else

  • Yes the example codes in the SDK typically use the drivers or libraries which come with it. The example you link to is the bare-metal implementation of the timer operation, so it's as direct and "simple" as you can get. I'm not sure I've fully understood what it is you want to do. What to do you mean by "delay function"? Do you mean you want to prevent the CPU from continuing in the program for a certain amount of time?

  • hi bro, 1/i tried implement RTC like this tutorials into example BLE_UART sdk11 devzone.nordicsemi.com/.../ but when i init funtion that config clock to use LFCLK. BLE is disconect.

    static void lfclk_request(void)
    {
        uint32_t err_code = nrf_drv_clock_init();
        APP_ERROR_CHECK(err_code);
        nrf_drv_clock_lfclk_request(NULL);
    }
    
    2/delay function that function use to delay by timer or counter, not use like nrf_delay library.
    example i creat delay function by RTC
    -init timer,creat timer, and start timer with f=1khz-> 1ms/ tick
    i use app_timer_cnt_get to get value current in RTC:
    
    void delay_timer( int timeout)
    {
    int TimerStart=0;
    TimerStart= app_timer_cnt_get();
    while(app_timer_cnt_get()-TimerStart <=timeout);
    TimerStart=0;
    }
    
  • The SoftDevice uses the LFCLK in its timings, so the it will already be initialized if you have enabled the SoftDevice which will lead to an error in the APP_ERROR_CHECK() macro.

Related