AnsweredAssumed Answered

Stm32f051 adc for multiple channels

Question asked by haak.oleg on Apr 7, 2015
Latest reply on Apr 9, 2015 by baird.hal.001
Hello everyone,

I have problem with the ad conversion into the stm32f051 microcontroller.

My Destination is: AD-convert for multiple channels PC0, PC1, PC2, PC3, PB0 und PB1

My Problem: TheAD valuesdo not match thevoltage values atthe respective channel

My Code:
// Initalization Reset and clock control (RCC)
void init_RCC(void)
{
    /* GPIO Ports and DMA1 Clock avtivate */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA
                                            | RCC_AHBPeriph_GPIOB
                                            | RCC_AHBPeriph_GPIOC
                                            | RCC_AHBPeriph_GPIOD, ENABLE);
      
    /* SPI1 and SPI2 Clock activate */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
      
    /* USART clock activate */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  
    /* TIM14 clock activate */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);   
      
    /* ADC clock activate */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
}
  
void init_ADC()
{
    /* Structure definition GPIO_InitStruct at GPIO_initTypDef */
    GPIO_InitTypeDef GPIO_InitStruct;
      
    /* Structure definition ADC_InitStruct at ADC_initTypDef */
    ADC_InitTypeDef ADC_InitStruct;
      
    /* Configure ADC pins : PC0, PC1, PC2, PC3 */
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_1;
    GPIO_Init(GPIOC, &GPIO_InitStruct);
      
    /* Configure ADC pins : PB0, PB1 */
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_1;
    GPIO_Init(GPIOB, &GPIO_InitStruct);
  
    /* Put everythink back to power-on defaults */
    ADC_DeInit(ADC1); 
  
    /* Initalize ADC structure */
    ADC_StructInit(&ADC_InitStruct);
  
    /* ADC-Structure initialization */
    ADC_InitStruct.ADC_ContinuousConvMode = DISABLE; /* After the end of conversion, the next conversion will be started automatically if ENABLE */
    ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; /* Data align into the right from LSB */
    ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None; /* Data with not external trigger configuration */
    ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b; /* ADC resolution from 12 Bit */
    ADC_InitStruct.ADC_ScanDirection = ADC_ScanDirection_Upward; /* ADC scans the channel upward */
    ADC_Init(ADC1, &ADC_InitStruct); /* ADC1 initalize with configuration parameters */
      
    /* Aktivate ADC */
    ADC_Cmd(ADC1, ENABLE);
}
  
uint16_t readADC1(uint8_t channel)
{
    /* Activate ADC1 in coninous mode */
    //ADC_DiscModeCmd(ADC1, ENABLE);
    /* Channel configuration */
    ADC_ChannelConfig(ADC1, channel, ADC_SampleTime_1_5Cycles);
  /* Start the conversion */
    ADC_StartOfConversion(ADC1);
  /* Wait until conversion completion */
  while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
  /* Get the conversion value */
    uint16_t Value = ADC_GetConversionValue(ADC1);
    /* Clear EOC Flag */
    ADC_ClearFlag(ADC1, ADC_FLAG_EOC);  
  return Value;
}
  
int main(void)
{   
    init_RCC(); /* Reset and Clock Control initialized */
      
    init_GPIO(); /* General Purpose Inputs- and Outputs initialized */
  
    if (SysTick_Config(SystemCoreClock / reloadValueDiv))
  
    /* Capture error */ 
    while (1);
  }
      
    init_SPI(); /* Serial Peripheral Interface initialized */
      
    //init_USART(); /* Universal Synchronous and Asynchronous Receiver Transmitter initialized */
      
    init_NVIC(); /* Nested Vectored Interrupt Controller initialized */
      
    //init_Timer(); /* Timer initialized */
      
    GPIO_WriteBit(GPIOB, GPIO_Pin_2, Bit_SET); /* ADC_RESET = nRESET set high */    
  init_ADS8332(ads8332_configuration_setting); /* ADS8332 Initialized with ads8332_configuration_setting*/  
    if((get_ADS8332_configuration() & 0x0FFF) != (ads8332_configuration_setting & 0x0FFF)) /* If the receive ads8332 configuration is not equal, than capture error. */
    {
    /* Capture error */ 
    while (1);      
    }
    GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_SET); /* ADC_CS = nCS set high */
    GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET); /* nCONVST = nCS set high */
  
    init_ADC(); /* ADC initialized */
      
    while (1)
  {
        GPIO_WriteBit(GPIOA, GPIO_Pin_5, Bit_SET);
        for(i=0;i<8;i++){
            spiDataBufferSPI1[i] = get_ADS8332_adc_value(i);    
        }
  
        for(j=0;j<6;j++){
            value[j] = readADC1(j);     
              
        }
  
  
          
    /* Wait rest cycle time in this delay-function */
    Delay(CYCLE_TIME);
        GPIO_WriteBit(GPIOA, GPIO_Pin_5, Bit_RESET);
  }
}
Please help me,I'm new to thisarea.

Haak

Outcomes