1.在sdk_config.h中加入宏
// <e> RNG_ENABLED - nrf_drv_rng - RNG peripheral driver - legacy layer
//==========================================================
#ifndef RNG_ENABLED
#define RNG_ENABLED 1
#endif

#ifndef RNG_CONFIG_ERROR_CORRECTION
#define RNG_CONFIG_ERROR_CORRECTION 1
#endif

// <o> RNG_CONFIG_POOL_SIZE - Pool size 
#ifndef RNG_CONFIG_POOL_SIZE
#define RNG_CONFIG_POOL_SIZE 64
#endif

// <o> RNG_CONFIG_IRQ_PRIORITY  - Interrupt priority

// <i> Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice
// <0=> 0 (highest) 
// <1=> 1 
// <2=> 2 
// <3=> 3 
// <4=> 4 
// <5=> 5 
// <6=> 6 
// <7=> 7

#ifndef RNG_CONFIG_IRQ_PRIORITY
#define RNG_CONFIG_IRQ_PRIORITY 6
#endif
// <e> NRFX_RNG_ENABLED - nrfx_rng - RNG peripheral driver
//==========================================================
#ifndef NRFX_RNG_ENABLED
#define NRFX_RNG_ENABLED 1
#endif
// <q> NRFX_RNG_CONFIG_ERROR_CORRECTION  - Error correction

#ifndef NRFX_RNG_CONFIG_ERROR_CORRECTION
#define NRFX_RNG_CONFIG_ERROR_CORRECTION 1
#endif

// <o> NRFX_RNG_CONFIG_IRQ_PRIORITY  - Interrupt priority
 
// <0=> 0 (highest) 
// <1=> 1 
// <2=> 2 
// <3=> 3 
// <4=> 4 
// <5=> 5 
// <6=> 6 
// <7=> 7

#ifndef NRFX_RNG_CONFIG_IRQ_PRIORITY
#define NRFX_RNG_CONFIG_IRQ_PRIORITY 6
#endif

// <e> NRFX_RNG_CONFIG_LOG_ENABLED - Enables logging in the module.
//==========================================================
#ifndef NRFX_RNG_CONFIG_LOG_ENABLED
#define NRFX_RNG_CONFIG_LOG_ENABLED 0
#endif
// <o> NRFX_RNG_CONFIG_LOG_LEVEL  - Default Severity level
 
// <0=> Off 
// <1=> Error 
// <2=> Warning 
// <3=> Info 
// <4=> Debug

#ifndef NRFX_RNG_CONFIG_LOG_LEVEL
#define NRFX_RNG_CONFIG_LOG_LEVEL 3
#endif

// <o> NRFX_RNG_CONFIG_INFO_COLOR  - ANSI escape code prefix.
 
// <0=> Default 
// <1=> Black 
// <2=> Red 
// <3=> Green 
// <4=> Yellow 
// <5=> Blue 
// <6=> Magenta 
// <7=> Cyan 
// <8=> White

#ifndef NRFX_RNG_CONFIG_INFO_COLOR
#define NRFX_RNG_CONFIG_INFO_COLOR 0
#endif

// <o> NRFX_RNG_CONFIG_DEBUG_COLOR  - ANSI escape code prefix.
 
// <0=> Default 
// <1=> Black 
// <2=> Red 
// <3=> Green 
// <4=> Yellow 
// <5=> Blue 
// <6=> Magenta 
// <7=> Cyan 
// <8=> White

#ifndef NRFX_RNG_CONFIG_DEBUG_COLOR
#define NRFX_RNG_CONFIG_DEBUG_COLOR 0
#endif
// <e> NRF_QUEUE_ENABLED - nrf_queue - Queue module
//==========================================================
#ifndef NRF_QUEUE_ENABLED
#define NRF_QUEUE_ENABLED 1
#endif
// <q> NRF_QUEUE_CLI_CMDS  - Enable CLI commands specific to the module

#ifndef NRF_QUEUE_CLI_CMDS
#define NRF_QUEUE_CLI_CMDS 0
#endif

2.导入nrfx_rng.c,nrf_drv_rng.c,nrf_ringbuf.c,nrf_queue.c到工程

3.引入头文件

#include "nrf_drv_rng.h"

4.定义一个随机产生函数

#define RANDOM_BUFF_SIZE    16      /**< Random numbers buffer size. */

/** @brief Function for getting vector of random numbers.
 *
 * @param[out] p_buff       Pointer to unit8_t buffer for storing the bytes.
 * @param[in]  length       Number of bytes to take from pool and place in p_buff.
 *
 * @retval     Number of bytes actually placed in p_buff.
 */
static uint8_t random_vector_generate(uint8_t * p_buff, uint8_t size)
{
    uint32_t err_code;
    uint8_t  available;

nrf_drv_rng_bytes_available(&available); // nction for getting the number of currently available random bytes.
                                             // Parameters
                                            //[out]    p_bytes_available    The number of bytes currently available in the pool.
    uint8_t length = MIN(size, available);  //得到最小长度

err_code = nrf_drv_rng_rand(p_buff, length); // Function for getting the vector of random numbers.
                                                 //   Parameters
                                                //  [out]    p_buff    Pointer to uint8_t buffer for storing the bytes.
                                                //  [in]    length    Number of bytes to take from the pool and place in p_buff.
                                                //  Return values
                                                //  NRF_SUCCESS    If the requested bytes were written to p_buff.
                                                //  NRF_ERROR_NOT_FOUND    If no bytes were written to the buffer because there were not enough bytes available in the pool.
    APP_ERROR_CHECK(err_code);

return length;
}

5.在主函数中处理

/**
 * @brief Function for main application entry.
 */
int main(void)
{
    uint32_t err_code;

bsp_board_init(BSP_INIT_LEDS);

const app_uart_comm_params_t comm_params =
      {
          RX_PIN_NUMBER,
          TX_PIN_NUMBER,
          RTS_PIN_NUMBER,
          CTS_PIN_NUMBER,
          UART_HWFC,
          false,
#if defined (UART_PRESENT)
          NRF_UART_BAUDRATE_115200
#else
          NRF_UARTE_BAUDRATE_115200
#endif
      };

APP_UART_FIFO_INIT(&comm_params,
                         UART_RX_BUF_SIZE,
                         UART_TX_BUF_SIZE,
                         uart_error_handle,
                         APP_IRQ_PRIORITY_LOWEST,
                         err_code);

APP_ERROR_CHECK(err_code);

err_code = nrf_drv_rng_init(NULL);  // Function for initializing the nrf_drv_rng module.
                                         // Parameters
                                         // [in]    p_config    Initial configuration.
                                         // Return values
                                         // NRF_SUCCESS    Driver was successfully initialized.
                                        //  NRF_ERROR_MODULE_ALREADY_INITIALIZED    Driver was already initialized.

APP_ERROR_CHECK(err_code);

while (true)
    {
       uint8_t p_buff[RANDOM_BUFF_SIZE];
        uint8_t length = random_vector_generate(p_buff,RANDOM_BUFF_SIZE);
       printf("Random Vector:");
        for(int i=0;i<length;i++)
        {
              printf("%x,",p_buff[i]);  
        }
        printf("length=%d\n",length);
        nrf_delay_ms(1000);
              
    }

}

NRF52832 RNG相关推荐

  1. NRF52832 RNG随机数产生器

    NFC52832内部具有随机数产生器,使用起来非常方便,只有几个寄存器,可以看下图: 每次需要使用随机数的时候只需要启动一次产生器就行了,直接上代码吧: uint8_t GET_RNG(void) { ...

  2. nRF52832的硬件资源小结

    nRF52832是NRODIC公司推出的一款带有低功耗蓝牙功能的ARM Cortex-M4 32-bit 浮点单元处理器,64MHz时钟, 存储器有两种配置:512kB flash/64kB RAM和 ...

  3. OpenCV 【十六】RNG随机数发生器putText绘制文字

    1 目的 使用 随机数发生器类 (RNG) 并得到均匀分布的随机数. 通过使用函数 putText 显示文字. 第一步是实例化一个 Random Number Generator(随机数发生器对象) ...

  4. copyTo函数、随机数产生器 RNG、轮廓的特征矩 Moment、cvGet2D函数

    C++ byte 类型 在头文件 windows.h 中 OpenCV 中,IplImage 所在头文件为 #include<highgui/highgui_c.h> 文章目录 1.cop ...

  5. NRF52832与W25Q80通信

    1 NRF52832SPI主机的功能描述 nRF52832SPIM的主要特征 3个SPI实例 支持SPI的模式0到模式3 支持DMA Individual selection of IO pin fo ...

  6. 使用matlab做可重复性实验结果——rng

    MATLAB中的rng函数 rng:控制随机数生成语法: rng(seed) rng('shuffle') rng(seed, generator) rng('shuffle', generator) ...

  7. NRF52810能不能替代NRF52832

    NRF52810能不能替代NRF52832?答案是可以的. 主要是FALSH和RAM有点不一样. 先给大家说说他的特征 关键特性 64 MHz的手臂®Cortex-M4 192kb Flash + 2 ...

  8. 一起学nRF51xx 10 -  rng

    前言 随机数产生器(RNG)的结构: 随机数发生器(RNG)根据内部热产生真实的非确定性随机数噪音.RNG通过触发START任务启动,并通过触发STOP任务停止.当随机数已经生成,它会产生一个VALR ...

  9. STM32 基础系列教程 46 – RNG

    前言 随机数发生器(RNG)是一个以连续模拟噪声为基础的随机数发生器,在主机读数时提供一个 32 位的随机数.STM32F407自带RNG ,本节将给大演示STM32F4系列MCU中的RNG的使用.用 ...

最新文章

  1. Python会消亡吗?
  2. 网站重启服务器有啥好处,常规服务器重启的好处和障碍
  3. 1.7 编程基础之字符串 31 字符串p型编码 python
  4. 50道编程小题目之【质数的个数】
  5. Java VS Go,微服务究竟谁更快?
  6. 启动SQL Server 时自动执行存储过程
  7. unity 常见的置灰处理
  8. java后台实现CKFinder2.3版本+阿里OSS存储
  9. 比尔·盖茨退休 留给全球商界意味深长的遗产
  10. Java实现蜗牛爬井的问题
  11. htpp proxy
  12. Oracle core读书笔记
  13. SpringBoot 实现 QQ邮箱注册和登录
  14. SQL Server Management Studio (SSMS)
  15. 负数二进制表示的方式及原因
  16. 【解决Maven项目module不显示蓝色小方块/java文件显示灰色橙色/module already exists问题】
  17. 预测算法——指数平滑法
  18. python找出素数_python找素数
  19. 白杨SEO:从5118站长工具备案内参发现百度SEO批量建站优化玩法
  20. 考博英语题型及难度分析

热门文章

  1. [业界资讯]Ubuntu 2010“雪地猞猁”最新进展
  2. java 视频录制软件有哪些_有哪些好用的视频录制工具?
  3. Celery+django+redis异步执行任务
  4. 深度学习系列25:注意力机制
  5. vue实现文字翻转效果
  6. 传统与现代可视化 PK:再生水厂二维工艺组态系统
  7. 2022腾讯云双十一服务器价格出炉来看看便宜不
  8. 一文了解互联网运营核心指标(产品、运营人员必知)
  9. 4、Gantt 任务节点部分
  10. Collections中Counter函数,namedtuple函数,defaultdict函数的使用