AnsweredAssumed Answered

stm32 multiple ADC redings error

Question asked by hussik hat on Jun 11, 2018
Latest reply on Jun 11, 2018 by hussik hat

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 2048. 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