NRF52840学习历程(二)按键01CPU扫描版本

时间在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按键,不知道能不能用做普通按键,我试试

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

用到       nrf_gpio_cfg_input 配置为输入模式

代码      

nrf_gpio_cfg_input(11,NRF_GPIO_PIN_PULLUP );

       nrf_gpio_cfg_input(18,NRF_GPIO_PIN_PULLUP );

 

现在IO扫描方式读取按键

代码如下      

while(1)
	{
		if(0 == nrf_gpio_pin_read(11))
		{
			nrf_delay_ms(10); //xiaodou
			if(0 == nrf_gpio_pin_read(11))
			{
				nrf_gpio_pin_toggle(LED0);
				
				while(0 == nrf_gpio_pin_read(11));
			}
		}
		
	}

 

现象: 按一下按键,LED状态取反

 

我试了18脚,没有办法控制,应该是复位引脚,可能需要取消其复位功能才能用作普通IO口,我现在还不会,后面如果学会了也不会告诉你们

 

整体代码如下:

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

/**
 * @brief Function for application main entry.
 */
int main(void)
{
	uint32_t LED0,LED1,LED2,LED3;
	uint32_t KEY0,KEY1;
	
	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,18);

	nrf_gpio_cfg_output(13);
	nrf_gpio_cfg_output(14);
	nrf_gpio_cfg_output(41);
	nrf_gpio_cfg_output(16);

	nrf_gpio_pin_set(13);
	nrf_gpio_pin_set(14);
	nrf_gpio_pin_set(41);
	nrf_gpio_pin_set(16);
	
	nrf_gpio_cfg_input(11,NRF_GPIO_PIN_PULLUP );
	nrf_gpio_cfg_input(18,NRF_GPIO_PIN_PULLUP );
	
	
	while(1)
	{
		if(0 == nrf_gpio_pin_read(KEY0))
		{
			nrf_delay_ms(10); //xiaodou
			if(0 == nrf_gpio_pin_read(KEY0))
			{
				nrf_gpio_pin_toggle(LED0);
				
				while(0 == nrf_gpio_pin_read(KEY0)); //songshou
			}
		}
				
	}
	
}

下面一节继续聊按键,还没玩的

猜你喜欢

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