1 nordic定时器的使用(定时器闪灯)
1.1 加入头文件
#include "app_timer.h"
1.2 定义定时器名称
//创建定时器
APP_TIMER_DEF(led_on);
APP_TIMER_DEF(led_off);
1.3 创建定时器
void Createled_offtimer(void)
{
NRF_LOG_INFO("create");
app_timer_create(&led_off,APP_TIMER_MODE_SINGLE_SHOT,timer_led_offCallback);
}
void Createled_ontimer(void)
{
NRF_LOG_INFO("create");
app_timer_create(&led_on,APP_TIMER_MODE_SINGLE_SHOT,timer_led_onCallback);
}
app_timer_create()这里面三个参数分别是:定时器名称,定时器的模式,回调函数;
1.3.1模式选择
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;
单次和循环;
1.3.2回调函数
这里是定时器最重要的地方了,你要做的事情就是放在这里
/LED_ON定时器
static void timer_led_onCallback(void *arg)
{
UNUSED_PARAMETER(arg);
// 在这里加入自己的应用处理
NRF_LOG_INFO("on");
LED_INIT();
nrf_gpio_pin_write(BOARD_LED2, 1);
nrf_gpio_pin_write(BOARD_LED1, 1);
nrf_gpio_pin_write(LED9, 1);
nrf_gpio_pin_write(LED10, 1);
nrf_gpio_pin_write(LED, 0);
Startled_offtimer();
}
static void timer_led_offCallback(void *arg)
{
UNUSED_PARAMETER(arg);
// 在这里加入自己的应用处理
NRF_LOG_INFO("off");
LED_INIT();
nrf_gpio_pin_write(BOARD_LED2, 0);
nrf_gpio_pin_write(BOARD_LED1, 0);
nrf_gpio_pin_write(LED9, 0);
nrf_gpio_pin_write(LED10, 0);
nrf_gpio_pin_write(LED, 1);
Startled_ontimer();
}
1.4 开启定时器
void Startled_ontimer(void)
{
NRF_LOG_INFO("start");
app_timer_start(led_on,TESTon_PERIOD,NULL);
}
void Startled_offtimer(void)
{
NRF_LOG_INFO("start");
app_timer_start(led_off,TESToff_PERIOD,NULL);
}
app_timer_start()里面三个参数分别是:定时器名称,定时器的定时时间,返回值(没有就为NULL);
1.4.1 定时时间
#define TESTon_PERIOD APP_TIMER_TICKS(500) //定时时间0.5S,结束后开灯
#define TESToff_PERIOD APP_TIMER_TICKS(3000) //定时时间3S,结束后关灯
1.5 在工程中加入自己的定时器
如SDK15.3 中 ble_peripheral 的 ble_app_template 工程
在 main.c 中的 main 函数中有两个函数:
timers_init() 用于初始化定时器 ,
application_timers_start() 用于启动定时器应用。
static void timers_init(void)
{
ret_code_t err_code = app_timer_init();
APP_ERROR_CHECK(err_code);
CreateLedFlashTimer();
CreateSyncTimer();
CreateBatteryMeasureTimer();
CreateLockWorkingTimer();
CreateBeepWorkingTimer();
CreateSpiTimer();
//灯的定时器
Createled_ontimer();
Createled_offtimer();
}
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); */
StartLedSlowFlashTimer();
StartSyncTimer();
//开启灯亮定时器
Startled_ontimer();
}
如此以来,定时器便可以控制灯光闪烁。
2 nordic按键点灯
2.1初始化
//按键初始化
void button_irqinit(void)
{
ret_code_t errCode = nrf_drv_gpiote_init(); //驱动初始化
APP_ERROR_CHECK(errCode);
nrf_drv_gpiote_in_config_t inConfig = GPIOTE_CONFIG_IN_SENSE_TOGGLE(false);
inConfig.pull = NRF_GPIO_PIN_PULLUP; //上拉
inConfig.sense = NRF_GPIOTE_POLARITY_TOGGLE;
errCode = nrf_drv_gpiote_in_init(BUTTON, &inConfig, buttonirqCallback);
APP_ERROR_CHECK(errCode);
nrf_drv_gpiote_in_event_enable(BUTTON, true);
NRF_LOG_INFO("button_irqinit");
app_timer_create(&s_button, APP_TIMER_MODE_SINGLE_SHOT, timer_buttonCallback);
}
这里面有很多API和库函数;前期还是先拿来直接用吧,但是有时间还是有必要仔细看看,内部寄存器的配置。只会调用函数,是不能学到精华的。
调用 API 函数 nrf_drv_gpiote_init,对 GPIOE 进行初始化,进入 nrf_drv_gpiote_init 函数内部, 发现其函数主要功能是设置中断类型为 PORT 中断,也就是端口事件。
ok,言归正传。这里我们把初始化直接拿来用。改了两个回调函数
nrf_drv_gpiote_in_init(BUTTON, &inConfig, buttonirqCallback);
pp_timer_create(&s_button, APP_TIMER_MODE_SINGLE_SHOT, timer_buttonCallback);
2.2 获取中断值
nrf_drv_gpiote_in_init(BUTTON, &inConfig, buttonirqCallback);
这个回调里面
static void buttonirqCallback(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
s_irqValue = 0;
NRF_LOG_INFO("buttonirqCallback");
if(nrf_gpio_pin_read(BUTTON) == 0)
{
s_irqValue|= BUTTON_ON;
}
else if(nrf_gpio_pin_read(BUTTON) == 1)
{
s_irqValue|= BUTTON_OFF;
}
app_timer_start(s_button, BUTTON_PERIOD, NULL);
NRF_LOG_INFO("app_timer_start");
}
1 获取中断值;这个是用来判断是哪里产生的中断。
2 开启了定时器消抖;
#define BUTTON_PERIOD APP_TIMER_TICKS(30) // 30ms 按键消抖定时
30MS消抖
消抖定时器的开启后执行app_timer_create(&s_button, APP_TIMER_MODE_SINGLE_SHOT, timer_buttonCallback);的回调
2.3中断处理
static void timer_buttonCallback(void *arg)
{
UNUSED_PARAMETER(arg);
NRF_LOG_INFO("timer_buttonCallback");
if(nrf_gpio_pin_read(BUTTON) == 0)
{
s_irqValue|= BUTTON_ON;
}
else if(nrf_gpio_pin_read(BUTTON) == 1)
{
s_irqValue|= BUTTON_OFF;
}
ButtonLED_HandleIrq(s_irqValue);
}
进入中断处理函数:
//按键点灯
void ButtonLED_HandleIrq(uint8 irqValue)
{
LED_INIT();
if(irqValue &BUTTON_ON)
{
NRF_LOG_INFO("LED ON");
All_led_on();
}
if(irqValue & BUTTON_OFF)
{
All_led_off();
}
}
void All_led_on(void)
{
nrf_gpio_pin_write(BOARD_LED2, 1);
nrf_gpio_pin_write(BOARD_LED1, 1);
nrf_gpio_pin_write(LED9, 1);
nrf_gpio_pin_write(LED10, 1);
nrf_gpio_pin_write(LED, 0);
}
执行相应的操作。