cancel
Showing results for 
Search instead for 
Did you mean: 

stm32 multiple ADC redings error

hussik hat
Associate
Posted on June 11, 2018 at 12:14

hi,

I'm using cubemx to initiate the project, the mcu I'm using is stm32L053C6. To that I have 2 sensors and 2 transistors connected to adc inputs. The code below shows how I get the input value, voltage, from the first temperature sensor. Then I use the same code for each value I want to read. The problem here is that the first value I read is always wrong, it gives me 2048, and it's 12 bits resolution, so...

I wonder how I can fix this?

//Start ADC readingif(HAL_ADC_Start(&hadc) != HAL_OK){ while(1) {}}//Wait for EOC (end of conversion)while(!(hadc.Instance->ISR & ADC_ISR_EOC)){}//Read ADC value//Repeat for all channels. while(!(hadc.Instance->ISR & ADC_ISR_EOC)){}experiments[0+index].temperature += hadc.Instance->DR;printf('\n ex0 i rolling %d\n', experiments[0+index].temperature); 

The code for reading the voltage values for the 2 temperature sensors and transistors is as below. I read the values 16 times and calculate an average. But as I said the first value I read is 2 Is there an calibration missing? somthing that I should add so that the first reading doesent give me high?

void readRollingADC(int index){ HAL_Delay(1); //Start ADC reading if(HAL_ADC_Start(&hadc) != HAL_OK){ while(1) {} } //Wait for EOC (end of conversion) while(!(hadc.Instance->ISR & ADC_ISR_EOC)){} //Read ADC value *> ****//Repeat for all channels. while(!(hadc.Instance->ISR & ADC_ISR_EOC)){} experiments[0+index].temperature +=> hadc.Instance->DR; printf('\n ex0 i rolling %d\n', experiments[0+index].temperature);***** while(!(hadc.Instance->ISR & ADC_ISR_EOC)){} experiments[0+index].ube += hadc.Instance->DR; while(!(hadc.Instance->ISR & ADC_ISR_EOC)){} experiments[0+index].vrb += hadc.Instance->DR; while(!(hadc.Instance->ISR & ADC_ISR_EOC)){} experiments[0+index].vrc += hadc.Instance->DR; while(!(hadc.Instance->ISR & ADC_ISR_EOC)){} experiments[1+index].temperature += hadc.Instance->DR;printf('\n ex1 i rolling %d\n', experiments[1+index].temperature); while(!(hadc.Instance->ISR & ADC_ISR_EOC)){} experiments[1+index].ube += hadc.Instance->DR; while(!(hadc.Instance->ISR & ADC_ISR_EOC)){} experiments[1+index].vrb += hadc.Instance->DR; while(!(hadc.Instance->ISR & ADC_ISR_EOC)){} experiments[1+index].vrc += hadc.Instance->DR;}

How do I fix this? Does it have something to do with the code or the initiation of the adc? What I can do is to read the first value and not save it, but I don't think that's the proper way

2 REPLIES 2
Tilen MAJERLE
ST Employee
Posted on June 11, 2018 at 13:05

Hello,

The problem here is that the first value I read is always wrong, it gives me 2048, and it's 12 bits resolution, so...

What are the other values you read after 2048? Please note that 2048 is in valid range of 12-bits.

Best regards,

Tilen

hussik hat
Associate
Posted on June 11, 2018 at 13:27

ok, sure I should have mentioned that, it gives 1654 which according to the data sheet for the temp. sensor is approx 25 deg C. The second temp. sensor give approx the same value 16xx 

best regards, 

Hussik