cancel
Showing results for 
Search instead for 
Did you mean: 

ADC with STM32F303VC

bee
Associate II
Posted on August 19, 2013 at 07:48

I want read ADC of STM32F303VC then display status  on three LED (If ADC>1000 LED red bright,ADC<500 LED green bright and extant LED ogrange bright) programmed code below,but i don't find error.Thanks everyone!

#include ''main.h''

__IO uint16_t  AD= 0,DA=0, ADC1ConvertedVoltage = 0, calibration_value = 0;

__IO uint32_t TimingDelay = 0;

ADC_InitTypeDef       ADC_InitStructure;

ADC_CommonInitTypeDef ADC_CommonInitStructure;

GPIO_InitTypeDef      GPIO_InitStructure;

/////////////////////////////////////////////////////////////////////////////////////////

int main(void)

{

  RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div2);

  

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE);

      

  /* Setup SysTick Timer for 1 µsec interrupts  */

  if (SysTick_Config(SystemCoreClock / 1000000))

  { 

    /* Capture error */ 

    while (1)

    {}

  }

////////////////LED///////////////////////////////////

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9| GPIO_Pin_10| GPIO_Pin_11;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(GPIOE, &GPIO_InitStructure);

///////////////////////ADC///////////////////////////////

  /* GPIOC Periph clock enable */

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

  /* Configure ADC Channel2 as analog input (Pin 1 of Port A-PA1) */

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

  ADC_StructInit(&ADC_InitStructure);

////////////////////////////////////////////////////////////////////////////////////////////////

  /* Calibration procedure */  

  ADC_VoltageRegulatorCmd(ADC1, ENABLE);

  

  /* Insert delay equal to 10 µs */

  Delay(10);

  ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single);

  ADC_StartCalibration(ADC1);

  

  while(ADC_GetCalibrationStatus(ADC1) != RESET );

  calibration_value = ADC_GetCalibrationValue(ADC1);

     

  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;                                                                    

  ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode;                    

  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;             

  ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_OneShot;                  

  ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0;          

  ADC_CommonInit(ADC1, &ADC_CommonInitStructure);

  

  ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable;

  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; 

  ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;         

  ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;

  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

  ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable;   

  ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable;  

  ADC_InitStructure.ADC_NbrOfRegChannel = 1;

  ADC_Init(ADC1, &ADC_InitStructure);

   ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_4Cycles5);

//////////////////////////////////////////////////////////////////////////////////////////////  

  

  ADC_Cmd(ADC1, ENABLE);

  /* wait for ADRDY */

  while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY));

  

  

  ADC_StartConversion(ADC1);   

  

  /* Infinite loop */

  while (1)

  {

    /* Test EOC flag */

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

    AD =ADC_GetConversionValue(ADC1);

    DA =(AD *3300)/0xFFF;

//////////////////////////////////////////////////////////////////////////////////// 

while(AD>1000)

  { 

GPIO_SetBits(GPIOE,GPIO_Pin_9);

      }

while(AD<500)

  {

GPIO_SetBits(GPIOE,GPIO_Pin_11);

      }

GPIO_SetBits(GPIOE,GPIO_Pin_10);

  }

}

////////////////////////////////////////////////////////////////////////////////////

void Delay(__IO uint32_t nTime)

  TimingDelay = nTime;

  while(TimingDelay != 0);

}

and my project here:

http://www.mediafire.com/download/zbuszqh91kkah6f/ADC.7z

5 REPLIES 5
raptorhal2
Lead
Posted on August 21, 2013 at 16:49

You appear to have done a faithful adaptation of the library example. Our problem is that you have not identified what is happening or not happening when you run the program. For example, if it gets to the while at the end, at least one LED should illuminate.

The example was designed for an evaluation board with an HSE. The Discovery uses the HSI, so check that your ADC Clock value is correct.

The next test is to step through the program with a debugger an see if it hanging in a calibration, or not reading the ADC value correctly, or .........

Cheers, Hal

Posted on August 21, 2013 at 17:39

The example was designed for an evaluation board with an HSE. The Discovery uses the HSI, so check that your ADC Clock value is correct.

Doesn't the F3-Discovery push 8 MHz into HSE input via the MCO pin of the F103 used by the ST-Link which does have an 8 MHz crystal? See User Manual and SB12

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
raptorhal2
Lead
Posted on August 21, 2013 at 18:53

Clive1, you are correct. Thanks, Hal

bee
Associate II
Posted on August 22, 2013 at 05:15

Thanks you very much!but  according to user manual ADC clock of STM32F303VC  it user HCLK clock:

mailto:

raptorhal2
Lead
Posted on August 22, 2013 at 15:04

So what happens when you step through the code in Debug mode ?

Cheers, Hal