随机数的产生

工程下载地址

int main(void)
{
    char buf[30];

    simple_uart_config(0xff,9,0xff,11,false);
    uart_exit();
    
    while(1)
    {
        sprintf(buf,"rng:%02x\r\n",get_rng());
        simple_uart_putstring((const uint8_t *)buf);
        nrf_delay_ms(1000);
    }
}



uint8_t get_rng(void)
{
    uint8_t result = 0;
    NRF_RNG->TASKS_START = 1; // start the RNG peripheral.

    // Clear the VALRDY EVENT.
    NRF_RNG->EVENTS_VALRDY = 0;

    // Wait until the value ready event is generated.
    while (NRF_RNG->EVENTS_VALRDY == 0)
    {
        // Do nothing.
    }
    result = (uint8_t)NRF_RNG->VALUE;
    
    NRF_RNG->TASKS_STOP = 1;

    return result;
}



效果:
rng:c1
rng:da
rng:51
rng:68
rng:86
rng:ef
rng:db
rng:cc
rng:be
rng:f1
rng:70
rng:93
rng:a1
rng:d0
rng:ef
rng:af
rng:a3
rng:ef
rng:98
rng:68
rng:a5
rng:ec
rng:19
rng:42
rng:5f
rng:1d

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值