cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F051 Discovery ADC Temperature Issue

gada
Associate III
Posted on September 26, 2014 at 08:16

Hello I have written a code refering to ADC example given in the peripheral library provided by ST.

I am using ADC in single conversion mode to measure the temperature. I will paste my code below. The problem is that code works in Debug Mode with break points at ADC code properly but in free running within debug or normal the ADC gives FFF as output. I am not understanding what is the issue, I tried adding delays within the ADC code but that didnot help. ADC Overrun keeps on happening. Even if EOC has occured the data is wrong ie FFF.

//ADC Code
void
adc_init(
void
){
ADC_InitTypeDef ADC_InitStructure;
// GPIO_InitTypeDef GPIO_InitStructure;
// TS_CAL1 = TS_CAL1_Address_High;
// TS_CAL2 = TS_CAL1_Address_Low;
/* GPIOC Periph clock enable */
// RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
/* ADC1 Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* Configure ADC Channel11 as analog input */
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
// GPIO_Init(GPIOC, &GPIO_InitStructure);
/* ADCs DeInit */
ADC_DeInit(ADC1);
/* Initialize ADC structure */
ADC_StructInit(&ADC_InitStructure);
/* Configure the ADC1 in continuous mode with a resolution equal to 12 bits */
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
ADC_Init(ADC1, &ADC_InitStructure);
/* Convert the ADC1 Channel 11 with 5 Cycles as sampling time */
// ADC_ChannelConfig(ADC1, ADC_Channel_10 , ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1, ADC_Channel_16 , ADC_SampleTime_239_5Cycles);
ADC_TempSensorCmd(ENABLE);
/* ADC Calibration */
ADC_GetCalibrationFactor(ADC1);
/* Enable the ADC peripheral */
ADC_Cmd(ADC1, ENABLE);
/* Wait the ADRDY flag */
while
(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY));
/* ADC1 regular Software Start Conv */
// ADC_StartOfConversion(ADC1);
}
uint16_t get_adc_count(
void
) {
uint16_t adc_raw_data = 0;
/* ADC1 regular Software Start Conv */
ADC_StartOfConversion(ADC1);
/* Test EOC flag */
while
(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
/* Test EOC flag */
/* Get ADC1 converted data */
adc_raw_data = ADC_GetConversionValue(ADC1);
ADC_ClearFlag(ADC1, ADC_FLAG_EOC | ADC_FLAG_OVR);
return
adc_raw_data;
}
//Main Code reading of temperature
temperature = ((V25 - get_adc_count())/AVG_SLOPE) + 25 ; 
//The get_adc_count returns 0xfff always 

#stm32-adc
1 REPLY 1
Posted on September 26, 2014 at 18:26

[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/ADC%20STM32F051%20Temperature%20Sensor%20Issue&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&TopicsView=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/AllItems.aspx&currentviews=11]Duplicate Thread

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..