蓝牙芯片nRF51822开发系列(一):环境搭建和GPIO使用(点亮LED)

前言(文章末尾获取工程源码)

nRF51822是一款性价比较高的低功耗蓝牙(BLE)SoC,搭载ARM Cotex-M0内核,拥有丰富的外设:UART、I2C、SPI、8位可配置的ADC等等,可以满足大多数近距离无线通信的需求。但唯一不太好的地方是开发难度相对过高,对新手入门很不友好。


一、开发必备

需要准备以下工作:

  • ARM MDK Version 5,并从官网(https://www.keil.com/dd2/pack/)下载芯片支持包和安装;
  • nRFgo Studio,用于下载程序;
  • nRF51系列SDK开发工具,可从Nordic官网下载,如下图

在这里插入图片描述

二、开发环境搭建

1.创建文件


创建新的文件夹,其中component是SDK压缩包解压后的文件。config文件里面是sdk_config.h文件,是从SDK中examples复制过来(可以搜索一下查找)。Users是MDK工程存放的文件夹。

2.新建工程

选择芯片型号。
在这里插入图片描述

如图所示,选中CORE内核文件和Startup启动文件。
在这里插入图片描述
最简单的工程如下图所示。
在这里插入图片描述

三、编写代码

GPIO详解

nrf_gpio.h库文件内有对GPIO的详细介绍,下面列举几个主要的函数。

//输入输出
typedef enum
{
    NRF_GPIO_PIN_DIR_INPUT  = GPIO_PIN_CNF_DIR_Input, ///< Input.
    NRF_GPIO_PIN_DIR_OUTPUT = GPIO_PIN_CNF_DIR_Output ///< Output.
} nrf_gpio_pin_dir_t;

//上拉下拉
typedef enum
{
    NRF_GPIO_PIN_NOPULL   = GPIO_PIN_CNF_PULL_Disabled, ///<  Pin pull-up resistor disabled.
    NRF_GPIO_PIN_PULLDOWN = GPIO_PIN_CNF_PULL_Pulldown, ///<  Pin pull-down resistor enabled.
    NRF_GPIO_PIN_PULLUP   = GPIO_PIN_CNF_PULL_Pullup,   ///<  Pin pull-up resistor enabled.
} nrf_gpio_pin_pull_t;

//输入输出初始化配置
void nrf_gpio_cfg_output(uint32_t pin_number);
void nrf_gpio_cfg_input(uint32_t pin_number, nrf_gpio_pin_pull_t pull_config);

//输出高低/翻转电平
void nrf_gpio_pin_set(uint32_t pin_number);
void nrf_gpio_pin_clear(uint32_t pin_number);
void nrf_gpio_pin_toggle(uint32_t pin_number);

//读输入输出电平
uint32_t nrf_gpio_pin_read(uint32_t pin_number);
uint32_t nrf_gpio_pin_out_read(uint32_t pin_number);

使用的开发板介绍

本次使用的是某宝上带LIS3DH三轴加速度传感器的最小系统板,原理图大概如下(真正的原理图并没有提供)
在这里插入图片描述

在这里插入图片描述

编写主函数

由上面的原理图可知,P0.07管脚连接LED。

#include "nrf_delay.h"
#include "nrf_gpio.h"

#define LED 7

int main(){
	
	nrf_gpio_cfg_output(LED);  
	
	nrf_gpio_pin_clear(LED); //输出低电平
	
	while(1){
		

			
		nrf_gpio_pin_toggle(LED);  //翻转电平
		
		nrf_delay_ms(1000);
		
		
	}
}

引入头文件路径

编写完主函数后,会发现编译报错,是因为没有引入头文件路径。将头文件路径引入,如下图。
在这里插入图片描述
并设置宏定义,如下图
在这里插入图片描述
在这里插入图片描述
最后编译通过,创建了HEX文件。

四、下载程序

J-Link的SW方式连接开发板和PC,打开nRFgo Studio。

  1. 点击nRF5x Programming;
  2. 点击Erase all清空存储器;
  3. 选择hex文件;
  4. 下载

在这里插入图片描述
下载成功!
在这里插入图片描述

五、源码获取

关注下方公众号,查看相关文章底部获取源码;若有疑问,请在公众号回复“交流群”,进群一起讨论分享!
在这里插入图片描述

  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nrf51822中文参考手册,nRF51822 是一款集成nRF51x系列无线收发器的超低功耗的片上系统 (Soc) , 包含一个32位ARM Cortex-M0 CPU , flash 存储器和模拟、数字外设。NORDIC SEMICONDUCTOR nRF51822 Product Specification v1.3 Liability disclaimer Nordic Semiconductor ASa reserves the right to make changes without further notice to the product to improve reliability, function or design. Nordic Semiconductor asa does not assume any liability arising out of the application or use of any product or circuits described herein ife support applications Nordic Semiconductor's products are not designed for use in life support appliances, devices, or systems where malfunction of these products can reasonably be expected to result in personal injury. Nordic Semiconductor ASa customers using or selling these products for use in such applications do so at their own risk and agree to fully indemnify Nordic Semiconductor ASA for any damages resulting from such improper use or sale Contact details Foryournearestdistributorpleasevisitwww.nordicsemi.com Information regarding product updates, downloads, and technical support can be accessed through your My Page account on our home page Main office: Otto Nielsens veg 12 Mailing address: Nordic Semiconductor 7052 Trondheim P.O. Box 2336 Norway 7004 Trondhe Phone:+4772898900 Norway 4772898989 画N远 NS-EN ISO 9001 CERTIFICATEDFIRM RoHS and reach statement Nordic semiconductor's products meet the requirements of Directive 2002/95/EC of the European Parliament and of the Council on the restriction of Hazardous Substances(roHS)and the requirements of the reach regulation(EC 1907/2006)on Registration, Evaluation, Authorization and Restriction of Chemicals. The SvHC(Substances of Very High Concern) candidate list is continually being updated Complete hazardous substance reports material composition reports and latest version of nordics reach statementcanbefoundonourwebsitewww.nordicsemicom Page 2 of 67 NORDIC SEMICONDUCTOR nRF51822 Product Specification v1.3 Datasheet status Status Description Objective Pro

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值