Question
Random using ADC
Posted on October 12, 2012 at 23:42
Hi, i try to do an elettronic dice with stm32vldiscovery.
I want use 2 value read from adc i try to use this to process my datam_w = <
choose-initializer
>; /* must not be zero */
m_z = <
choose-initializer
>; /* must not be zero */
uint get_random()
{
m_z = 36969 * (m_z & 65535) + (m_z >> 16);
m_w = 18000 * (m_w & 65535) + (m_w >> 16);
return (m_z << 16) + m_w; /* 32-bit result */
} where m_z and m_w are 2 random value read from adc.
i used this code for adc
#include ''stm32f10x.h''
#include ''random.h''
#include ''stm32f10x_adc.h''
#include ''stm32f10x_dma.h''
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ADC1_DR_Address ((uint32_t)0x4001244C)
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
__IO uint16_t ADCConvertedValue;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
/* Private functions ---------------------------------------------------------*/
void prova (void)
{
/*!<
At
this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f10x_xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f10x.c file
*/
/* System clocks configuration ---------------------------------------------*/
RCC_Configuration();
/* GPIO configuration ------------------------------------------------------*/
GPIO_Configuration();
/* DMA1 channel1 configuration ----------------------------------------------*/
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr
=
ADC1_DR_Address
;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADCConvertedValue;
DMA_InitStructure.DMA_DIR
=
DMA_DIR_PeripheralSRC
;
DMA_InitStructure.DMA_BufferSize
=
1
;
DMA_InitStructure.DMA_PeripheralInc
=
DMA_PeripheralInc_Disable
;
DMA_InitStructure.DMA_MemoryInc
=
DMA_MemoryInc_Enable
;
DMA_InitStructure.DMA_PeripheralDataSize
=
DMA_PeripheralDataSize_HalfWord
;
DMA_InitStructure.DMA_MemoryDataSize
=
DMA_MemoryDataSize_HalfWord
;
DMA_InitStructure.DMA_Mode
=
DMA_Mode_Circular
;
DMA_InitStructure.DMA_Priority
=
DMA_Priority_High
;
DMA_InitStructure.DMA_M2M
=
DMA_M2M_Disable
;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
/* Enable DMA1 channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE);
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode
=
ADC_Mode_Independent
;
ADC_InitStructure.ADC_ScanConvMode
=
ENABLE
;
ADC_InitStructure.ADC_ContinuousConvMode
=
ENABLE
;
ADC_InitStructure.ADC_ExternalTrigConv
=
ADC_ExternalTrigConv_None
;
ADC_InitStructure.ADC_DataAlign
=
ADC_DataAlign_Right
;
ADC_InitStructure.ADC_NbrOfChannel
=
1
;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel14 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_10,1, ADC_SampleTime_55Cycles5);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
uint32_t
m_z
=
ADCConvertedValue
; /* must not be zero */
uint32_t
m_w
=
ADCConvertedValue
; /* must not be zero */
m_z
=
36969
* (m_z & 65535) + (m_z >> 16);
m_w = 18000 * (m_w & 65535) + (m_w >> 16);
int caso = ((m_z << 16) + m_w) << 31;
}
/**
* @brief Configures the different system clocks.
* @param None
* @retval None
*/
void RCC_Configuration(void)
{
#if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
/* ADCCLK = PCLK2/2 */
RCC_ADCCLKConfig(RCC_PCLK2_Div2);
#else
/* ADCCLK = PCLK2/4 */
RCC_ADCCLKConfig(RCC_PCLK2_Div4);
#endif
/* Enable peripheral clocks ------------------------------------------------*/
/* Enable DMA1 clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* Enable ADC1 and GPIOC clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);
}
/**
* @brief Configures the different GPIO ports.
* @param None
* @retval None
*/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure PC.04 (ADC Channel14) as analog input -------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
Adc work good cause in debug i can see how change ADCConvertedValue in realtime, but m_w and m_z give me an error and no value comes back.
I think i can't assign good the value from adc to my variable, someone can help me? thx.