標(biāo)題:
STM32利用ADC噪聲產(chǎn)生隨機(jī)數(shù)源程序
[打印本頁]
作者:
Laumincy
時(shí)間:
2019-8-26 18:49
標(biāo)題:
STM32利用ADC噪聲產(chǎn)生隨機(jī)數(shù)源程序
rand_number.c
#include "rand_number.h"
#include "adc.h"
#include "stdlib.h"
/***************/
//使用ADC噪聲產(chǎn)生隨機(jī)數(shù)種子
//min,max------------>隨機(jī)數(shù)范圍
//Rand_Value ADC的電壓值
int32_t get_rand(int16_t min ,int16_t max)
{
uint16_t Rand_Value[4];
int16_t rand_temp;
uint8_t i=0;
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&Rand_Value, 4);
while(1)if(i++>20)break;
rand_temp = (Rand_Value[0] & 0xf)<<12 | (Rand_Value[1] & 0xf)<<8 | (Rand_Value[2] & 0xf)<<4 | (Rand_Value[3] & 0xf);
srand(rand_temp);
rand_temp = rand()% max + min;
HAL_ADC_Stop_DMA(&hadc1);
return rand_temp;
}
rand_number.h
#ifndef __RAND_NUMBER_H
#define __RAND_NUMBER_H
#define USED_103 1
#if USED_103
#include "stm32f1xx_hal.h"
#else
#include "stm32f4xx_hal.h"
#endif
int32_t get_rand(int16_t min ,int16_t max);
#endif
ADC噪聲隨機(jī)數(shù).PNG
(208.13 KB, 下載次數(shù): 47)
下載附件
2019-8-26 18:49 上傳
作者:
Sea_
時(shí)間:
2020-4-2 15:35
樓主,返回值應(yīng)該寫成 return ((rand() % (max - min))+min),你那樣還是會(huì)有超范圍的可能。
作者:
Sea_
時(shí)間:
2020-4-2 15:37
樓主,返回值應(yīng)該是 return ((rand() % (max - min)) + min);
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1