temp
1.temp传感器的分辨率:0.25℃
2.温度传感器初始化函数:nrf_temp_init();
3.开启温度传感器:触发start任务
NRF_TEMP->TASKS_START = 1;
4.关闭温度传感器:触发stop任务
NRF_TEMP->TASKS_STOP = 1;
5.读取温度值函数:
nrf_temp_read()/4 (分辨率为0.25℃)
需要等待NRF_TEMP->EVENTS_DATARY事件触发
temp示例程序
#include <stdbool.h>
#include <stdint.h>
#include "nrf_delay.h"
#include "boards.h"
/* 包含log头文件 */
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
/* 包含uart头文件 */
#include "app_uart.h"
#if defined (UART_PRESENT)
#include "nrf_uart.h"
#endif
#if defined (UARTE_PRESENT)
#include "nrf_uarte.h"
#endif
#include "nrfx_temp.h"
/* 定义uart发送接收缓冲区 */
#define UART_TX_BUF_SIZE 256
#define UART_RX_BUF_SIZE 256
/**
* log初始化函数
*/
static void log_init(void)
{
ret_code_t err = NRF_LOG_INIT(NULL); //log初始化
APP_ERROR_CHECK(err); //检测log配置信息
NRF_LOG_DEFAULT_BACKENDS_INIT(); //开启log
}
/**
* uart事件回调函数
*/
void uart_event_handle(app_uart_evt_t* event)
{
//uart通讯出错事件
if(event->evt_type == APP_UART_COMMUNICATION_ERROR)
{
APP_ERROR_HANDLER(event->data.error_communication);
}
//fifo错误事件
else if(event->evt_type == APP_UART_FIFO_ERROR)
{
APP_ERROR_HANDLER(event->data.error_code);
}
}
/**
* uart初始化参数配置函数
*/
void uart_config(void)
{
uint32_t err;
/* 串口结构体参数 */
const app_uart_comm_params_t comm_params = {
RX_PIN_NUMBER, //接收引脚p0.08
TX_PIN_NUMBER, //发送引脚p0.06
RTS_PIN_NUMBER, //输出信号引脚p0.05
CTS_PIN_NUMBER, //输入信号引脚p0.07
APP_UART_FLOW_CONTROL_DISABLED, //使能软件控制流
false, //不校验
NRF_UART_BAUDRATE_115200 //串口波特率115200
};
/* 串口初始化 */
APP_UART_FIFO_INIT(&comm_params, //串口结构体
UART_RX_BUF_SIZE, //串口接收缓冲区大小
UART_TX_BUF_SIZE, //串口发送缓冲区大小
uart_event_handle, //串口事件回调函数
APP_IRQ_PRIORITY_LOWEST, //串口中断优先级最低
err); //配置信息
APP_ERROR_CHECK(err); //检测配置是否成功
}
/**
* uart初始化函数
*/
int main(void)
{
uint32_t volatile temp;
//log初始化
log_init();
//串口初始化
uart_config();
//打印串口信息
NRF_LOG_INFO("uart example start");
NRF_LOG_FLUSH();
nrf_temp_init(); //初始化温度传感器
while(1)
{
NRF_TEMP->TASKS_START = 1; //开启温度任务
while(NRF_TEMP->EVENTS_DATARDY == 0); //等待读取数据完成事件,
temp = (nrf_temp_read()/4); //以0.25度为计量单位
printf("temp = %u 度\r\n",temp);
NRF_TEMP->TASKS_STOP = 1; //关闭温度传感器
nrf_delay_ms(500);
}
}
random number
1.初始化随机发生器函数:
nrf_drv_rng_init(NULL);
2.查询缓冲池中随机数的数量:
nrf_drv_rng_bytes_avaliable(p_bytes)
3.读取随机数函数:
nrf_drv_rng_rand(p_buf,length)
随机数示例程序
#include <stdbool.h>
#include <stdint.h>
#include "nrf_delay.h"
#include "boards.h"
/* 包含log头文件 */
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
/* 包含uart头文件 */
#include "app_uart.h"
#if defined (UART_PRESENT)
#include "nrf_uart.h"
#endif
#if defined (UARTE_PRESENT)
#include "nrf_uarte.h" //带easydma的串口
#endif
/* 引入随机数头文件 */
#include "nrf_drv_rng.h"
/* 定义可用随机随机数数量 */
#define RANDOM_NUMBER_SIZE 16
/* 定义uart收接发缓冲区 */
#define UART_TX_BUF_SIZE 256
#define UART_RX_BUF_SIZE 256
/**
* log初始化函数
*/
static void log_init(void)
{
ret_code_t err = NRF_LOG_INIT(NULL); //log初始化
APP_ERROR_CHECK(err); //检测log配置信息
NRF_LOG_DEFAULT_BACKENDS_INIT(); //开启log
}
/**
* uart事件回调函数
*/
void uart_event_handle(app_uart_evt_t* event)
{
//uart通讯出错事件
if(event->evt_type == APP_UART_COMMUNICATION_ERROR)
{
APP_ERROR_HANDLER(event->data.error_communication);
}
//fifo错误事件
else if(event->evt_type == APP_UART_FIFO_ERROR)
{
APP_ERROR_HANDLER(event->data.error_code);
}
}
/**
* uart初始化参数配置函数
*/
void uart_config(void)
{
uint32_t err;
/* 串口结构体参数 */
const app_uart_comm_params_t comm_params = {
RX_PIN_NUMBER, //接收引脚p0.08
TX_PIN_NUMBER, //发送引脚p0.06
RTS_PIN_NUMBER, //输出信号引脚p0.05
CTS_PIN_NUMBER, //输入信号引脚p0.07
APP_UART_FLOW_CONTROL_DISABLED, //使能软件控制流
false, //不校验
NRF_UART_BAUDRATE_115200 //串口波特率115200
};
/* 串口初始化 */
APP_UART_FIFO_INIT(&comm_params, //串口结构体
UART_RX_BUF_SIZE, //串口接收缓冲区大小
UART_TX_BUF_SIZE, //串口发送缓冲区大小
uart_event_handle, //串口事件回调函数
APP_IRQ_PRIORITY_LOWEST, //串口中断优先级最低
err); //配置信息
APP_ERROR_CHECK(err); //检测配置是否成功
}
/**
* uart初始化函数
*/
int main(void)
{
uint32_t err;
uint8_t i=0;
uint8_t avaliable;
uint8_t length;
uint8_t rng_buf[RANDOM_NUMBER_SIZE];
log_init();
uart_config();
err = nrf_drv_rng_init(NULL); //初始化内部随机数发生器
APP_ERROR_CHECK(err);
NRF_LOG_INFO("uart example start");
NRF_LOG_FLUSH();
while(1)
{
nrf_drv_rng_bytes_available(&avaliable); //获取缓冲池中具有的随机数数量
printf("number of random numbers is %d\r\n",avaliable);
length = MIN(RANDOM_NUMBER_SIZE,avaliable); //比较随机数的数量,最多为16
err = nrf_drv_rng_rand(rng_buf,length); //读取随机数的值到rng_buf缓冲区中
APP_ERROR_CHECK(err);
printf("random number\r\n");
for(i=0;i<length; i++)
{
printf("%3d\t",(int)rng_buf[i]); //打印随机数
}
printf("\r\n");
nrf_delay_ms(1000);
}
}