NRF52832学习笔记(2)——定时器接口使用

一、简介

在有BLE协议栈的工程中,软件定时器用的是实时时钟RTC1

二、使用步骤

2.1 头文件

#include "app_timer.h"

2.2 定义定时器

APP_TIMER_DEF(s_testTimer);    // 测试的定时器

2.3 定义定时时间

#define TEST_PERIOD    APP_TIMER_TICKS(200)	// 定时时间(200ms)

2.4 定义回调函数

/**
 @brief 测试定时器的回调函数
 @param 无
 @return 无
*/
static void timer_testCallback(void *arg)
{
    UNUSED_PARAMETER(arg);
    // 在这里加入自己的应用处理
}

2.5 创建定时器

定时器模式有两种:

/**@brief Timer modes. */
typedef enum
{
    APP_TIMER_MODE_SINGLE_SHOT,                 /**< The timer will expire only once. */
    APP_TIMER_MODE_REPEATED                     /**< The timer will restart each time it expires. */
} app_timer_mode_t;

APP_TIMER_MODE_SINGLE_SHOT 一次性定时器
APP_TIMER_MODE_REPEATED 循环定时器

/**
 @brief 创建测试的定时器
 @param 无
 @return 无
*/
void CreateTestTimer(void)
{
    app_timer_create(&s_testTimer, APP_TIMER_MODE_SINGLE_SHOT, timer_testCallback);
}

2.6 开启定时器

/**
 @brief 开启测试的定时器
 @param 无
 @return 无
*/
void StartTestTimer(void)
{
    app_timer_start(s_testTimer, TEST_PERIOD, NULL);                           
}

2.7 关闭定时器

/**
 @brief 关闭测试的定时器
 @param 无
 @return 无
*/
void StopTestTimer(void)
{
    app_timer_stop(s_testTimer);                            		
}

三、加入文件

3.1 user_timer.h

#ifndef _USER_TIMER_H_
#define _USER_TIMER_H_

/*********************************************************************
 * DEFINITIONS
 */
#define TEST_PERIOD    APP_TIMER_TICKS(200)	// 定时时间(200ms)

/*********************************************************************
* API FUNCTIONS
*/
void CreateTestTimer(void);
void StartTestTimer(void);
void StopTestTimer(void);

#endif /* _USER_TIMER_H_ */

3.2 user_timer.c

/*********************************************************************
 * INCLUDES
 */
#include "app_timer.h"
#include "user_timer.h"

static void timer_testCallback(void *arg);

/*********************************************************************
 * LOCAL VARIABLES
 */
APP_TIMER_DEF(s_testTimer);    // LED闪烁开的定时器

/*********************************************************************
 * PUBLIC FUNCTIONS
 */
/**
 @brief 创建测试的定时器
 @param 无
 @return 无
*/
void CreateTestTimer(void)
{
    app_timer_create(&s_testTimer, APP_TIMER_MODE_SINGLE_SHOT, timer_testCallback);
}

/**
 @brief 开启测试的定时器
 @param 无
 @return 无
*/
void StartTestTimer(void)
{
    app_timer_start(s_testTimer, TEST_PERIOD, NULL);                           
}

/**
 @brief 关闭测试的定时器
 @param 无
 @return 无
*/
void StopTestTimer(void)
{
    app_timer_stop(s_testTimer);                            		
}


/*********************************************************************
 * LOCAL FUNCTIONS
 */
/**
 @brief 测试定时器的回调函数
 @param 无
 @return 无
*/
static void timer_testCallback(void *arg)
{
    UNUSED_PARAMETER(arg);
    // 在这里加入自己的应用处理
}

四、使用例子

如SDK15.3 中 ble_peripheral 的 ble_app_template 工程
main.c 中的 main 函数中有两个函数:timers_init()用于初始化定时器,application_timers_start()用于启动定时器应用。

4.1 timers_init()中

加入创建定时器

/**@brief Function for the Timer initialization.
 *
 * @details Initializes the timer module. This creates and starts application timers.
 */
static void timers_init(void)
{
    // Initialize timer module.
    ret_code_t err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);

    // Create timers.
	
	/* YOUR_JOB: Create any timers to be used by the application.
				 Below is an example of how to create a timer.
				 For every new timer needed, increase the value of the macro APP_TIMER_MAX_TIMERS by
				 one.
		ret_code_t err_code;
		err_code = app_timer_create(&m_app_timer_id, APP_TIMER_MODE_REPEATED, timer_timeout_handler);
		APP_ERROR_CHECK(err_code); */
	CreateTestTimer();
}

4.2 application_timers_start()中

启动定时器应用

/**@brief Function for starting timers.
 */
static void application_timers_start(void)
{
    /* YOUR_JOB: Start your timers. below is an example of how to start a timer.
       ret_code_t err_code;
       err_code = app_timer_start(m_app_timer_id, TIMER_INTERVAL, NULL);
       APP_ERROR_CHECK(err_code); */
	StartTestTimer();
}

• 由 Leung 写于 2019 年 12 月 17 日

• 参考:青风电子社区

发布了86 篇原创文章 · 获赞 131 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/qq_36347513/article/details/103584388
今日推荐