본문 바로가기

Firmware/Nordic nRF52

[Nordic nRF52] 타이머 추가하기

728x90
반응형

 

 

nRF5 SDK Application Timer Tutorial

2018-11-23: Updated tutorial to cover SDK version 15.2. 2019-02-08: Improved example project (SDK 15.2) and updated instructions accordingly. Introduction Scope The following topics will be included in this tutorial: Configuration of the application...

devzone.nordicsemi.com

위의 링크를 참조하여 싱글샷 or repeated 타이머를 만들 수 있습니다.

 

Include및 sdk 설정

#include "app_timer.h"
#include "nrf_drv_clock.h"

 

헤더파일 include하고, sdk_config.h 에서도 ENABLED 해줍니다.

// <e> APP_TIMER_ENABLED - app_timer - Application timer functionality
//==========================================================
#ifndef APP_TIMER_ENABLED
#define APP_TIMER_ENABLED 1
#endif


// <e> NRF_CLOCK_ENABLED - nrf_drv_clock - CLOCK peripheral driver - legacy layer
//==========================================================
#ifndef NRF_CLOCK_ENABLED
#define NRF_CLOCK_ENABLED 1
#endif

자세한 설정은 링크를 참조해서 하드웨어 맞게 설정하면 됩니다.

 

Initialization

만약 SoftDevice가 enabled되지 않았다면, LFCLK가 필요하므로 다음 함수를 추가하고 main에서 init해줍니다.

/**@brief Function starting the internal LFCLK oscillator.
 *
 * @details This is needed by RTC1 which is used by the Application Timer
 *          (When SoftDevice is enabled the LFCLK is always running and this is not needed).
 */
static void lfclk_request(void)
{
    ret_code_t err_code = nrf_drv_clock_init();
    APP_ERROR_CHECK(err_code);
    nrf_drv_clock_lfclk_request(NULL);
}

SoftDevice가 enabled된 상태라면 필요 없다고 합니다.

따라서, main()에서 다음 두줄을 추가 하면 됩니다.

lfclk_request();
app_timer_init();

 

 

Repeated Timer

  • timer id pointer 정의
APP_TIMER_DEF(m_repeated_timer_id);

 

  • event handler 함수
/**@brief Timeout handler for the repeated timer.
 */
static void repeated_timer_handler(void * p_context)
{
   // 반복해서 수행할 코드
}

 

  • 타이머 생성
/**@brief Create timers.
 */
static void create_timers()
{
    ret_code_t err_code;

    // Create timers
    err_code = app_timer_create(&m_repeated_timer_id,
                                APP_TIMER_MODE_REPEATED,
                                repeated_timer_handler);
    APP_ERROR_CHECK(err_code);
}

타이머 생성 함수를 main loop에서 호출합니다.

create_timers();

 

  • 타이머 시작하기
ret_code_t      err_code;
// Start repeated timer (start blinking LED).
err_code = app_timer_start(m_repeated_timer_id, APP_TIMER_TICKS(200), NULL);
APP_ERROR_CHECK(err_code);

APP_TIMER_TICKS 매크로를 사용해 틱을 조정하여 호출 간격을 조절할 수 있습니다.

 

  • 타이머 멈추기
// Stop the repeated timer (stop blinking LED).
err_code = app_timer_stop(m_repeated_timer_id);
APP_ERROR_CHECK(err_code);

 

Single shot Timer

  • timer pointer id 정의
APP_TIMER_DEF(m_single_shot_timer_id);

 

  • event handler 함수
/**@brief Timeout handler for the single shot timer.
 */
static void single_shot_timer_handler(void * p_context)
{
    // 호출 시 실행할 코드
}

 

  • 타이머 생성

create_timers()에 repeated timer처럼 아래 코드를 추가해 줍니다.

err_code = app_timer_create(&m_single_shot_timer_id,
                                APP_TIMER_MODE_SINGLE_SHOT,
                                single_shot_timer_handler);
APP_ERROR_CHECK(err_code);

 

  • 타이머 시작
ret_code_t      err_code;
err_code = app_timer_start(m_single_shot_timer_id, APP_TIMER_TICKS(1000), NULL);
APP_ERROR_CHECK(err_code);

시간은 TICK을 통해 조절해주면 됩니다.

 

-끝-

728x90
반응형