cancel
Showing results for 
Search instead for 
Did you mean: 

I want know ADC setting is correct.

zzzjizon
Associate

I have just received the code from my predecessor.

The company said something garbage value in ADC sometime.

I think is must be ADC problem.

​I don't know what is wrong where cause it. Setting is wrong?

I use STM32F103RCT6 MCU.

I checked that ADC uses a single ADC and multiple channels.

I write here init code and ADC code.

void ADC_Initial(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

  ADC_InitTypeDef ADC_InitStructure;

  /* Enable ADC1 and GPIOC clock */

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);

  // GPIOC

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;

  GPIO_Init(GPIOC, &GPIO_InitStructure);

  // GPIOB

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

  

  /* ADC1 configuration ------------------------------------------------------*/

  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;                            //independent   v               dual

  ADC_InitStructure.ADC_ScanConvMode = DISABLE;                                 //multiChannel                  SingleChannel   v

  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//ENABLE;                  //not continuous Mode 

  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;

  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

  ADC_InitStructure.ADC_NbrOfChannel = ADC_CH_MAX+1;

  ADC_Init(ADC1, &ADC_InitStructure);

  /* ADC1 regular channel 9~15 configuration */ 

  ADC_RegularChannelConfig(ADC1, ADC_Channel_9 | ADC_Channel_10|ADC_Channel_11|ADC_Channel_12|ADC_Channel_13|ADC_Channel_14|ADC_Channel_15, 1, ADC_SampleTime_13Cycles5); //

  

  /* 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));

  

  /* Start ADC1 Software Conversion */ 

  ADC_SoftwareStartConvCmd(ADC1, ENABLE);

  ADC_BufferClear();//this is our code.

}

void ADC_ReadData(void)

{

    int i;

    for(i=0;i<ADC_CH_MAX;i++)

    {

        if(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET)

        {

            /// 만약�? ADC Enable�?� 안�?�어 있으면.

            ADC_RegularChannelConfig(ADC1, ADC_CH_BASE + i, 1, ADC_SampleTime_55Cycles5);

            ADC_Cmd(ADC1, ENABLE); //enable ADC1

            while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);

        }

        ADC_Buffer[i][ADC_BufferIndex] = ADC_GetConversionValue(ADC1);

    }

    ADC_BufferIndex++;

    if(ADC_BufferIndex >= ADC_SAMPLE_MAX)

        ADC_BufferIndex = 0;

    // Avg

    ADC_Average();

}

Thank you.

0 REPLIES 0