NRF51822之GPIOTE使用

本文介绍如何使用nRF51822的GPIOTE模块实现按钮中断功能。通过代码示例展示了如何配置GPIO引脚并设置中断处理函数。需要注意的是,机械按键的抖动可能导致多次触发中断。

---恢复内容开始---

在上篇介绍nrf51822的GPIOTE http://www.cnblogs.com/libra13179/p/5336580.html

 

我们现在开始下水游泳。

/** @file
* @brief Example GPIOTE project.
* @defgroup nrf_app_gpiote_example Example Template
*
*/

#include <stdbool.h>
#include "SEGGER_RTT.h"
#include "nrf_gpio.h"
#include "app_gpiote.h"
#include "boards.h"

#define APP_GPIOTE_MAX_USERS    1 /** Change this to match amount ot GPIOTE users, app_button uses one */

app_gpiote_user_id_t m_app_gpiote_my_id;

void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name)
{
    NVIC_SystemReset();
}

void gpiote_event_handler(uint32_t event_pins_low_to_high, uint32_t event_pins_high_to_low)
{
    if (event_pins_high_to_low & (1 << BUTTON_1))
    {
        SEGGER_RTT_WriteString(0, "nrf_gpio_pin_set.\n\n");//这边可以更据自己的情况进行替换
    }
    if (event_pins_low_to_high & (1 << BUTTON_1))
    {
        SEGGER_RTT_WriteString(0, "nrf_gpio_pin_clear.\n\n");//这边可以更据自己的情况进行替换
    }
}

/**
 * Initialize the button GPIO pins to generate interrupts on push.
 */
static void buttons_init()
{
    uint32_t err_code;
    

    uint32_t   low_to_high_bitmask = 0x00000000;
    uint32_t   high_to_low_bitmask = (1 << BUTTON_1);
    
    // Configure BUTTON1 with SENSE enabled
    nrf_gpio_cfg_sense_input(BUTTON_1, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW);
    
    APP_GPIOTE_INIT(APP_GPIOTE_MAX_USERS);   
    
    err_code = app_gpiote_user_register(&m_app_gpiote_my_id, 
                                        low_to_high_bitmask, 
                                        high_to_low_bitmask, 
                                        gpiote_event_handler);
    APP_ERROR_CHECK(err_code);
    
    err_code = app_gpiote_user_enable(m_app_gpiote_my_id);
    APP_ERROR_CHECK(err_code);
}


/**@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();
}

/**
 * @brief Function for application main entry.
 */
int main(void)
{   
    SEGGER_RTT_WriteString(0, "\033[2JBY libra .\n\n");
    buttons_init();
    while(true)
    {
        power_manage();
    }
}


/** @} */

当我们按下 BUTTON_1可以看到显示多个nrf_gpio_pin_set,而多个nrf_gpio_pin_set是由于机械按键的抖动造成的。

需要添加的工程中文件

 

..\..\..\..\..\components\libraries\gpiote\app_gpiote.c

..\..\..\..\..\components\drivers_nrf\common\nrf_drv_common.c

..\..\..\..\..\components\drivers_nrf\gpiote\nrf_drv_gpiote.c

添加文件技巧建立一个空文件到工程中将上面的路径直接复制保存即可。

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值