cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f103c8 adc result varies a lot

giolenas
Associate II
Posted on May 23, 2016 at 12:46

Hi I am new on stm arm 32 bits microcontrollers.

I wanted to try the adc module and print the result on PA.00-09 that i have connected some leds. The thing is that I get the values form adc but vary a lot. What I mean? All led are blinking all the time and they are not stable. I am using an 10 turn 5k potentiometer. Because I have worked with 8 bit microcontrollers I understand that the cause is that I am running the adc module too fast so I divided the adc clock by 9 and increase the sample time to 5. And after that again my result varies a lot for about 4-5 lsb. So I am stuck :(

#include ''stm32f10x.h''
#include ''stm32f10x_rcc.h''
#include ''stm32f10x_gpio.h''
#include ''stm32f10x_adc.h''
//#include ''delay.h''
uint16_t ADCConvertedValue;
void
GPIO_Configuration(
void
);
void
ADC_Configuration(
void
);
int
main(
void
){
GPIO_Configuration();
ADC_Configuration();
while
(1){
// Start the conversion
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
// Wait until conversion completion
while
(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
// Get the conversion value
ADCConvertedValue = ADC_GetConversionValue(ADC1);
//ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
GPIO_Write(GPIOA, ADCConvertedValue >> 2);
//delay(100);
}
}
void
GPIO_Configuration(
void
) {
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
/* Configure PA.00-07 as digital push-pull output---------------------------*/
GPIO_DeInit (GPIOA);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 
| GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 
| GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure PB.00 (ADC Channel08) as analog input -------------------------*/
GPIO_DeInit (GPIOB);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void
ADC_Configuration(
void
)
{
ADC_InitTypeDef ADC_InitStructure;
/* ADCCLK = PCLK2/6 = 72/8 = 9MHz */
RCC_ADCCLKConfig(RCC_PCLK2_Div8);
/* Enable ADC1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* ADC1 Configuration ------------------------------------------------------*/
/* ADC1 and ADC2 operate independently */
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
/* Disable the scan conversion */
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
/* Don't do contimuous conversions */
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
/* Start conversin by software, not an external trigger */
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
/* Conversions are 12 bit - put them in the lower 12 bits of the result */
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
/* Say how many channels would be used by the sequencer */
ADC_InitStructure.ADC_NbrOfChannel = 1;
/* Now do the setup */
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel8 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_239Cycles5);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while
(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while
(ADC_GetCalibrationStatus(ADC1));
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_239Cycles5);
}

#stm32f103c8-adc
2 REPLIES 2
Walid FTITI_O
Senior II
Posted on May 24, 2016 at 12:51

Hi g..mario,

I recommend you to run and check the ADC example inside

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubef3.html?icmp=pf260613_pron_nb_jun2014&sc=stm32cubef3-pr

. Try the regular conversion of the ''ADC_Regular_injected_groups'' example at this path:

STM32Cube_FW_F1_V1.3.0\Projects\STM3210C_EVAL\Examples\ADC\ADC_Regular_injected_groups

-Hannibal-

AvaTar
Lead
Posted on May 24, 2016 at 13:41

> I recommend you to run and check the ADC example inside

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubef3.html?icmp=pf260613_pron_nb_jun2014&sc=stm32cubef3-pr

.

I would not recommend that...

Instead, connect your ADC inputs to a fixed voltage (say, GND and Vcc), and observe the conversion result. You adapt your code to could collect several values in RAM, and view them in the debugger.

To come to the point, your hardware might be too noisy for a 12-bit or 10-bit resolution.

Consider averaging, or reduce the output size to 8 bit or less.

BTW, I consider the kind of ''binary value'' output via LEDs as an ill-fated idea - it doesn't take the inertia of human visual perception into account.