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

How Can I Create Own timers_init

Hi folks,

I try change blinky code for nRF52810 SoC. I must create own timer. I dont know app_timer example help for me?! I want to do when power on leds on 2 seconds. I dont want to use nrf_delay_ms(). My codes are under the topic. 

thanks...

....
standart blinky code block
....
void lfclk_request()
{
    uint32_t err_code = nrf_drv_clock_init();
    APP_ERROR_CHECK(err_code);
    nrf_drv_clock_lfclk_request(NULL);   
}
void timer_a_handler(void * p_context)
{
    nrf_drv_gpiote_out_set(Kontrol_1);
}
void create_timers()
{   
    uint32_t err_code;
    // Create timers
    err_code = app_timer_create(&m_led_a_timer_id,
                                APP_TIMER_MODE_REPEATED,
                                timer_a_handler);
    APP_ERROR_CHECK(err_code);
}
void start_timer()
{
    uint32_t err_code;

    // Start timer
    err_code = app_timer_start(m_led_a_timer_id,
                                APP_TIMER_TICKS(1000),
                                NULL);
    APP_ERROR_CHECK(err_code);
}
 
/**@brief Function for application main entry.
 */
int main(void)
{
    // Initialize.
    log_init();
    leds_init();
    timers_init();
    buttons_init();
    power_management_init();
    lfclk_request();  --->
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
    create_timers();  ---> 
    start_timer();    --->
    ble_stack_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();
    sistem_baslangici();
    // Start execution.
    NRF_LOG_INFO("Blinky example started.");
    advertising_start();

    // Enter main loop.
    for (;;)
    {
        
         sutun();
       // idle_state_handle();
    }
}

sistem_baslangici()

....
some #define and header files
....
void sistem_baslangici(void)
{
    nrf_gpio_pin_clear(Kontrol_1);
    nrf_gpio_pin_set(Kontrol_2);
    nrf_gpio_pin_clear(Motor_GERI);
    nrf_delay_ms(2000);
    nrf_gpio_pin_set(Kontrol_1);
    nrf_gpio_pin_clear(Kontrol_2);
    nrf_gpio_pin_clear(Motor_GERI);
    nrf_delay_ms(2000);
}

Parents
  • I think you want to turn an LED off for 2 seconds, then off for 2 seconds.

    You can use a repeated timer of 4 seconds to turn the LED on. Then start a oneshot timer to turn it off after 2 seconds.

    You can also use a repeated timer of 2 seconds and toggle the LED. I think this option is better due to using less resources.

    Option 1:

    #define LED_ON_INTERVAL 2000
    #define LED_OFF_INTERVAL    2000
    #define MY_LED   BSP_BOARD_LED_1
    static uint32_t led_idx = MY_LED;
    
    static void timers_init(void)
    {
        ret_code_t err_code = app_timer_init();           //Initializes the library
        APP_ERROR_CHECK(err_code);
    
        err_code = app_timer_create(&led_on_timer_id, 
                                    APP_TIMER_MODE_REPEATED,
                                    led_on_timeout_handler);
        APP_ERROR_CHECK(err_code);
        err_code = app_timer_create(&led_off_timer_id, 
                                    APP_TIMER_MODE_SINGLE_SHOT,
                                    led_off_timeout_handler);
    }
    
    void led_on_timeout_handler(void *led_idx)
    {
        uint32_t *p_led_idx = led_idx;
        bsp_board_led_on(*p_led_idx);         //Or however you want to turn it on
        ret_code_t err_code = app_timer_start(led_off_timer_id, LED_OFF_INTERVAL, led_idx);    //oneshot timer to turn off LED
        APP_ERROR_CHECK(err_code);
    }
    
    void led_off_timeout_handler(void *ptr_led)
    {
      uint32_t *p_led_idx = ptr_led;
      bsp_board_led_off(*p_led_idx);
    }
    
    int main (void)
    {
      ret_code_t err_code;
      err_code = app_timer_start(led_on_timer_id, LED_ON_INTERVAL, led_idx);
      APP_ERROR_CHECK(err_code);
    }

    Option 2:

    #define LED_TOGGLE_INTERVAL 2000
    #define MY_LED   BSP_BOARD_LED_1
    static uint32_t led_idx = MY_LED;
    
    static void timers_init(void)
    {
        ret_code_t err_code = app_timer_init();           //Initializes the library
        APP_ERROR_CHECK(err_code);
    
        err_code = app_timer_create(&led_toggle_timer_id, 
                                    APP_TIMER_MODE_REPEATED,
                                    led_toggle_timeout_handler);
    }
    
    void led_toggle_timeout_handler(void *led_idx)
    {
        uint8_t *p_led_idx = led_idx;
        bsp_board_led_invert(*p_led_idx);         //Or however you want to turn it on/off
        APP_ERROR_CHECK(err_code);
    }
    
    int main (void)
    {
      ret_code_t err_code;
      err_code = app_timer_start(led_toggle_timer_id, LED_TOGGLE_INTERVAL, &led_idx);
      APP_ERROR_CHECK(err_code);
    }

    Notice that I have used another method to turn LEDs on/off, but you can use your method too. These examples are based on my own code for nRF52840 with SDK15.0.0

    Also note that I have not compiled these examples.

Reply
  • I think you want to turn an LED off for 2 seconds, then off for 2 seconds.

    You can use a repeated timer of 4 seconds to turn the LED on. Then start a oneshot timer to turn it off after 2 seconds.

    You can also use a repeated timer of 2 seconds and toggle the LED. I think this option is better due to using less resources.

    Option 1:

    #define LED_ON_INTERVAL 2000
    #define LED_OFF_INTERVAL    2000
    #define MY_LED   BSP_BOARD_LED_1
    static uint32_t led_idx = MY_LED;
    
    static void timers_init(void)
    {
        ret_code_t err_code = app_timer_init();           //Initializes the library
        APP_ERROR_CHECK(err_code);
    
        err_code = app_timer_create(&led_on_timer_id, 
                                    APP_TIMER_MODE_REPEATED,
                                    led_on_timeout_handler);
        APP_ERROR_CHECK(err_code);
        err_code = app_timer_create(&led_off_timer_id, 
                                    APP_TIMER_MODE_SINGLE_SHOT,
                                    led_off_timeout_handler);
    }
    
    void led_on_timeout_handler(void *led_idx)
    {
        uint32_t *p_led_idx = led_idx;
        bsp_board_led_on(*p_led_idx);         //Or however you want to turn it on
        ret_code_t err_code = app_timer_start(led_off_timer_id, LED_OFF_INTERVAL, led_idx);    //oneshot timer to turn off LED
        APP_ERROR_CHECK(err_code);
    }
    
    void led_off_timeout_handler(void *ptr_led)
    {
      uint32_t *p_led_idx = ptr_led;
      bsp_board_led_off(*p_led_idx);
    }
    
    int main (void)
    {
      ret_code_t err_code;
      err_code = app_timer_start(led_on_timer_id, LED_ON_INTERVAL, led_idx);
      APP_ERROR_CHECK(err_code);
    }

    Option 2:

    #define LED_TOGGLE_INTERVAL 2000
    #define MY_LED   BSP_BOARD_LED_1
    static uint32_t led_idx = MY_LED;
    
    static void timers_init(void)
    {
        ret_code_t err_code = app_timer_init();           //Initializes the library
        APP_ERROR_CHECK(err_code);
    
        err_code = app_timer_create(&led_toggle_timer_id, 
                                    APP_TIMER_MODE_REPEATED,
                                    led_toggle_timeout_handler);
    }
    
    void led_toggle_timeout_handler(void *led_idx)
    {
        uint8_t *p_led_idx = led_idx;
        bsp_board_led_invert(*p_led_idx);         //Or however you want to turn it on/off
        APP_ERROR_CHECK(err_code);
    }
    
    int main (void)
    {
      ret_code_t err_code;
      err_code = app_timer_start(led_toggle_timer_id, LED_TOGGLE_INTERVAL, &led_idx);
      APP_ERROR_CHECK(err_code);
    }

    Notice that I have used another method to turn LEDs on/off, but you can use your method too. These examples are based on my own code for nRF52840 with SDK15.0.0

    Also note that I have not compiled these examples.

Children
No Data
Related