博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NRF51822之app_button使用
阅读量:5320 次
发布时间:2019-06-14

本文共 4171 字,大约阅读时间需要 13 分钟。

我们现在开始使用app_button,为什么要使用这个来替代直接使用GPIOTE呢?

因为我们在手册中可以看到如果一直开启GPIOTE in模式的需要需要很大电流。所以我们需要使用RTC来“周期”的查询。

 

马上上代码

/** @file* @brief Example app_button project.**/#include 
#include
#include "app_button.h"#include "boards.h"#include "app_gpiote.h"#include "app_timer.h"#define APP_TIMER_PRESCALER 0 /**< Value of the RTC1 PRESCALER register. */#define APP_TIMER_OP_QUEUE_SIZE 4 /**< Size of timer operation queues. */#define BUTTON_DEBOUNCE_DELAY 50 // Delay from a GPIOTE event until a button is reported as pushed. #define APP_GPIOTE_MAX_USERS 1 // Maximum number of users of the GPIOTE handler. /* * Handler to be called when button is pushed. * param[in] pin_no The pin number where the event is genereated * param[in] button_action Is the button pushed or released */static void button_handler(uint8_t pin_no, uint8_t button_action){ if(button_action == APP_BUTTON_PUSH) { switch(pin_no) { case BUTTON_1: nrf_gpio_pin_toggle(LED_1); break; case BUTTON_2: nrf_gpio_pin_toggle(LED_2); break; case BUTTON_3: nrf_gpio_pin_toggle(LED_3); break; case BUTTON_4: nrf_gpio_pin_toggle(LED_4); break; default: break; } }  //if(button_action == APP_BUTTON_RELEASE)//键释}/** * Initialize the clock. */void init_clock(){ NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos); NRF_CLOCK->EVENTS_LFCLKSTARTED = 0; NRF_CLOCK->TASKS_LFCLKSTART = 1; while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0); // Wait for clock to start} /** * Initialize all four LEDs on the nRF51 DK. */void init_leds(){ nrf_gpio_cfg_output(LED_1); nrf_gpio_cfg_output(LED_2); nrf_gpio_cfg_output(LED_3); nrf_gpio_cfg_output(LED_4); nrf_gpio_pin_set(LED_1); nrf_gpio_pin_set(LED_2); nrf_gpio_pin_set(LED_3); nrf_gpio_pin_set(LED_4);} /**@brief Function for the Power Management. */static void power_manage(void){ // Use directly __WFE and __SEV macros since the SoftDevice is not available. // Wait for event. __WFE(); // Clear Event Register. __SEV(); __WFE();}/** * Function for application main entry. */int main(void){ init_leds(); init_clock(); uint32_t err_code; // Button configuration structure. static app_button_cfg_t p_button[] = { {BUTTON_1, APP_BUTTON_ACTIVE_LOW, NRF_GPIO_PIN_PULLUP, button_handler}, {BUTTON_2, APP_BUTTON_ACTIVE_LOW, NRF_GPIO_PIN_PULLUP, button_handler}, {BUTTON_3, APP_BUTTON_ACTIVE_LOW, NRF_GPIO_PIN_PULLUP, button_handler}, {BUTTON_4, APP_BUTTON_ACTIVE_LOW, NRF_GPIO_PIN_PULLUP, button_handler}}; // Macro for initializing the application timer module. // It will handle dimensioning and allocation of the memory buffer required by the timer, making sure that the buffer is correctly aligned. It will also connect the timer module to the scheduler (if specified). APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, NULL); // Macro for initializing the GPIOTE module. // It will handle dimensioning and allocation of the memory buffer required by the module, making sure that the buffer is correctly aligned. APP_GPIOTE_INIT(APP_GPIOTE_MAX_USERS); // Initializing the buttons. err_code = app_button_init(p_button, sizeof(p_button) / sizeof(p_button[0]), BUTTON_DEBOUNCE_DELAY); APP_ERROR_CHECK(err_code); // Enabling the buttons. err_code = app_button_enable(); APP_ERROR_CHECK(err_code); while(true) { power_manage(); }}

 

 

需要注意的是app_button底层调用app_time和app_gpiote。而在裸机下需要我们配置好32khz时钟,即在代码中init_clock

转载于:https://www.cnblogs.com/libra13179/p/5362292.html

你可能感兴趣的文章
mysql数据库学习记录1
查看>>
nodejs-mysql模块
查看>>
HDU--2546 饭卡
查看>>
2019杭电多校一 K. Function (数论)
查看>>
[IOT] - 在树莓派的 Raspbian 系统中安装 .Net Core 3.0 运行环境
查看>>
Proxifier安装与使用
查看>>
[译]Google官方关于Android架构中MVP模式的示例
查看>>
Python学习路线
查看>>
C# 3.0语言新特性(语言规范):7 查询表达式from XX in ss where xx.s=e select xx
查看>>
lov的建立
查看>>
demo 基于html css 实现小米官网部分内容搭建
查看>>
【BJOI2006】狼抓兔子
查看>>
二叉搜索树的第k个结点
查看>>
bzoj2815 ZJOI2012 灾难
查看>>
制作web安装程序
查看>>
1-3*交换变量
查看>>
一个.NET通用JSON解析/构建类的实现(c#)
查看>>
Windows Phone开发(31):画刷 转:http://blog.csdn.net/tcjiaan/article/details/7460226
查看>>
Windows Phone开发(5):室内装修 转:http://blog.csdn.net/tcjiaan/article/details/7269014
查看>>
Hibernate初探之单表映射——创建Hibernate的配置文件
查看>>