NRF52840学习历程(二)按键03事件中断组件方式编程

时间在2021125,寒假放假在家好好学一学

 

开发板:初雪的100出头那块 NRF52840 EVAL KIT

下载工具:JINLK V11(最好是JLINK V9以上 也有人用JLINK OB也行,其他的下载器STLINK,DAP不建议用)

版本号: KEIL5编程环境,CMSIS为5.3.0, NRF52840的CMSIS为8.35.0

参考资料: NRF52840-Eval-Kit-Schematic.pdf(原理图)

nRF5_SDK_17.0.2_d674dde(官方例程)

nRF5_SDK_17.0.0_offline_doc(官方文档)

 

实现功能: 按键点灯

 

按键原理图

IO口为11和18,其中18为RESET按键,现在我还不会取消18的复位功能,暂时不用

配置IO为上啦输入,然后按键按下的时候为0电平,按键没有按或松开的时候为高电平

用到       nrf_gpio_cfg_input 配置为输入模式

代码

       nrf_gpio_cfg_input(11,NRF_GPIO_PIN_PULLUP );

 

因为按键只有1个,我再外置三个独立按键,IO为24,20,17,同样的另外一端接了地

然后按下面设置

如果没有

那么就自己手动添加(吧其他的工程含有这个的复制过来就行)

在头文件路劲设置中,添加下面没有包含的头文件目录

启动外部中断

nrf_drv_gpiote_init();//启动GPIOTE时钟,可以这么说

定义一个结构体,就像上面的那个CONFIG[0]一样,拿来配置中断内容的

nrf_drv_gpiote_in_config_t key_ex_config; //按键中断配置用

结构体内容如下

代码如下: 没用的不用管

key_ex_config.hi_accuracy=true; // 启用高精确度输入事件(高频时钟)
	key_ex_config.pull = NRF_GPIO_PIN_PULLUP ; //上啦

 

然后把结构体写到寄存器里面

Pin就是按键引脚,p_config就是我们设置的机构体,evt就是中断函数   

 nrf_drv_gpiote_in_init(KEY0, &key_ex_config, KEY0_Interrupt);

再启动中断

   nrf_drv_gpiote_in_event_enable(KEY0, true);//启动KEY0中断

    

其中中断代码如下

void KEY0_Interrupt(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
	nrf_gpio_pin_toggle(LED1);
}

完整代码如下:


#include <stdbool.h>
#include <stdint.h>
#include "nrf_delay.h"
#include "nrf_gpio.h"
#include "nrf_drv_gpiote.h"

uint32_t LED0,LED1,LED2,LED3;
uint32_t KEY0,KEY1,KEY2,KEY3;

void KEY0_Interrupt(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action);
void KEY1_Interrupt(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action);
void KEY2_Interrupt(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action);
void KEY3_Interrupt(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action);


/**
 * @brief Function for application main entry.
 */
int main(void)
{
	nrf_drv_gpiote_in_config_t key_ex_config; //按键中断配置用
	
	LED0 =  NRF_GPIO_PIN_MAP(0,13);
	LED1 =  NRF_GPIO_PIN_MAP(0,14);
	LED2 =  NRF_GPIO_PIN_MAP(1,9);
	LED3 =  NRF_GPIO_PIN_MAP(0,16);

	KEY0 =  NRF_GPIO_PIN_MAP(0,11);
	KEY1 =  NRF_GPIO_PIN_MAP(0,24);
	KEY2 =  NRF_GPIO_PIN_MAP(0,20);
	KEY3 =  NRF_GPIO_PIN_MAP(0,17);

	nrf_gpio_cfg_output(LED0);
	nrf_gpio_cfg_output(LED1);
	nrf_gpio_cfg_output(LED2);
	nrf_gpio_cfg_output(LED3);

	nrf_gpio_pin_set(LED0);
	nrf_gpio_pin_set(LED1);
	nrf_gpio_pin_set(LED2);
	nrf_gpio_pin_set(LED3);
	
	nrf_gpio_cfg_input(KEY0,NRF_GPIO_PIN_PULLUP );
	nrf_gpio_cfg_input(KEY1,NRF_GPIO_PIN_PULLUP );
	nrf_gpio_cfg_input(KEY2,NRF_GPIO_PIN_PULLUP );
	nrf_gpio_cfg_input(KEY3,NRF_GPIO_PIN_PULLUP );
	
	nrf_drv_gpiote_init();//启动GPIOTE时钟,可以这么说
	
	key_ex_config.hi_accuracy=true; // 启用高精确度输入事件
	key_ex_config.pull = NRF_GPIO_PIN_PULLUP ; //上啦
	key_ex_config.sense = NRF_GPIOTE_POLARITY_HITOLO ;//下降沿
	
	nrf_drv_gpiote_in_init(KEY0, &key_ex_config, KEY0_Interrupt);
	nrf_drv_gpiote_in_init(KEY1, &key_ex_config, KEY1_Interrupt);
	nrf_drv_gpiote_in_init(KEY2, &key_ex_config, KEY2_Interrupt);
	nrf_drv_gpiote_in_init(KEY3, &key_ex_config, KEY3_Interrupt);

	nrf_drv_gpiote_in_event_enable(KEY0, true);//启动KEY0中断
	nrf_drv_gpiote_in_event_enable(KEY1, true);//启动KEY1中断
	nrf_drv_gpiote_in_event_enable(KEY2, true);//启动KEY2中断
	nrf_drv_gpiote_in_event_enable(KEY3, true);//启动KEY3中断
	
	
	while(1)
	{				
	}
	
}

void KEY0_Interrupt(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
	nrf_gpio_pin_toggle(LED0);
}
void KEY1_Interrupt(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
	nrf_gpio_pin_toggle(LED1);
}
void KEY2_Interrupt(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
	nrf_gpio_pin_toggle(LED2);
}
void KEY3_Interrupt(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
	nrf_gpio_pin_toggle(LED3);
}

好了,用组件代替寄存器的中断, 下面还要继续讲按键,按键还没完

猜你喜欢

转载自blog.csdn.net/jwdeng1995/article/details/113108026