AnsweredAssumed Answered

stm32f103c8 adc result varies a lot

Question asked by g..mario on May 23, 2016
Latest reply on May 24, 2016 by AvaTar
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 239.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);
     
}

Outcomes