AnsweredAssumed Answered

ADC on STM32L15xx

Question asked by miller.andrew.003 on Jul 8, 2013
Latest reply on Jul 8, 2013 by Clive One
Hello,
I am currently stuck on a problem where I believe I have acquired the proper examples and tried to follow form, but I cannot get a value out of my ADC. On my device there is only 1 ADC (ADC1 on APB2). Below is some code I run from IAR debugging with a ST-Link. One problem right off the bat is that 'result' is consistently "unavailable", except when I make it static, then its filled with some unchanging gibberish. I don't know the significance of channels, and I realize that may be the issue, but I tried to iterate through all of the available channels to see if I can get anything, and I seem to hang around channel 5, where I'm stuck on the End of Conversion flag check. Please help!



void Enable_ADC(void)    {
  
      
      ADC_CommonInitTypeDef CommonInitStruct;
      ADC_InitTypeDef ADC_InitStructure;
      /*Get a Fresh Start*/
      ADC_DeInit(ADC1);
  
      RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
      /*Enable APB2 Clock for ADC1*/
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
      
      /*Set up Prescaler*/
      CommonInitStruct.ADC_Prescaler = ADC_Prescaler_Div4;
      ADC_CommonInit(&CommonInitStruct);
      
      /*Initialize ADC*/
      ADC_StructInit(&ADC_InitStructure);   
      ADC_Init(ADC1,&ADC_InitStructure);
      
      /*Enable ADC*/
      ADC_Cmd(ADC1,ENABLE);
    }

(snippet from InitGPIO)
...
/*Battery Measurment pins*/
  /*                                             PA1(ADC)                       */
  /*                                     R13       ||      R12                  */
  /*     PB15(OpenDrain)BatMeas   <<----/\/\/--->BatMon---/\/\/<++++++Vbat      */
  /*                                                                            */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
  GPIO_Init(GPIOB, &GPIO_InitStructure);


  GPIO_ResetBits(GPIOB,GPIO_Pin_15);


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}
...

int readADC1(uint8_t channel) { 
  
  ADC_RegularChannelConfig(ADC1, channel, 1, ADC_SampleTime_9Cycles); 
  // Start the conversion 
  ADC_SoftwareStartConv(ADC1); 
  // Wait until conversion completion 
  while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); 
  // Get the conversion value 
  return ADC_GetConversionValue(ADC1); 
}

int main(void)
{
   
    
    NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x3000);
    
    RCC_Configuration();
    
    /* Init I/O ports */
    Init_GPIOs ();
    
    Enable_ADC();
    int result = 0;
    int k = 0;
    while(1){
      
     
     result = readADC1(k);
      k++;
        if(k>25) k = 0;
    }
}



Outcomes