cancel
Showing results for 
Search instead for 
Did you mean: 

I am using STM32F407vg, i wrote a code for ADC while debugging value is not updating no errors as well in the code. i connected a 10K potentiometer to PA0, i checked with DMM no issues. I am adding my code snippet please let me know the mistake.

psati
Associate II
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_adc.h"
 
/* Private variables ---------------------------------------------------------*/
static __IO uint32_t uwTimingDelay;
RCC_ClocksTypeDef RCC_Clocks;
 
int Voltage = 0;
 
/* Private function prototypes -----------------------------------------------*/
static void Delay(__IO uint32_t nTime);
void ADC1_cha0_config(void);
 
 
int main(void)
{
	
  /* SysTick end of count event each 10ms */
  RCC_GetClocksFreq(&RCC_Clocks);
  SysTick_Config(RCC_Clocks.HCLK_Frequency / 100);
  
ADC1_cha0_config();
	
  /* Infinite loop */
  while (1)
  {
		ADC_SoftwareStartConv(ADC1);
		while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC))
		{
			Voltage = ADC_GetConversionValue(ADC1);
		}
		if(Voltage == 0)
		{
			//nothing
		}
		Delay(100);
  }
}
 
void ADC1_cha0_config() //PA0
{
	/* Enable the GPIOA and ADC clock */ 
//  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
	
	/* GPIOA and ADC structures */
	GPIO_InitTypeDef GPIO_Adc_Stru;
	ADC_InitTypeDef ADC1_Init_Stru;
	
	GPIO_Adc_Stru.GPIO_Mode = GPIO_Mode_AN;
	GPIO_Adc_Stru.GPIO_Pin = GPIO_Pin_0;
	GPIO_Adc_Stru.GPIO_PuPd = GPIO_PuPd_NOPULL;
	
	/* ADC struct initialization*/
	ADC1_Init_Stru.ADC_ContinuousConvMode = DISABLE;
	ADC1_Init_Stru.ADC_Resolution = ADC_Resolution_8b;
	ADC1_Init_Stru.ADC_DataAlign = ADC_DataAlign_Right;
	ADC1_Init_Stru.ADC_ExternalTrigConv = DISABLE;
	ADC1_Init_Stru.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
	ADC1_Init_Stru.ADC_NbrOfConversion = 1;
	ADC1_Init_Stru.ADC_ScanConvMode = DISABLE;
	
	GPIO_Init(GPIOA,&GPIO_Adc_Stru);
	ADC_Init(ADC1,&ADC1_Init_Stru);
	
	ADC_Cmd(ADC1,ENABLE);
	ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_84Cycles);
}	
 
void Delay(__IO uint32_t nTime)
{ 
  uwTimingDelay = nTime;
 
	
  while(uwTimingDelay != 0);
}
 
void TimingDelay_Decrement(void)
{
  if (uwTimingDelay != 0x00)
  { 
    uwTimingDelay--;
  }
}

1 REPLY 1
Ozone
Lead II

Are you aware that the EOC interrupt flag gets reset when reading the ADC_DR register ? The debugger is no exception.

Having the ADC_DR register in the debugger view will change the code flow.

The clock to the GPIO port acting as IDC_in must be enabled, too.

The SPL used to have an ADC_CommonInit() function one needs to call as well.