stm32多个ADC redings错误

我正在使用cubemx来启动项目,我正在使用的mcu是stm32L053C6.为此我有2个传感器和2个晶体管连接到adc输入.下面的代码显示了我如何从第一个温度传感器获得输入值电压.然后我为我想要阅读的每个值使用相同的代码.这里的问题是我读的第一个值总是错误的,它给我2048,它的分辨率为12位,所以……

我想知道如何解决这个问题?

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

读取2个温度传感器和晶体管的电压值的代码如下.我读了16次值并计算出平均值.但正如我所说,我读到的第一个值是2048.

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;

}

我该如何解决?它是否与代码或adc的启动有关?我能做的是读取第一个值而不是保存它,但我认为这不是正确的方法:)

void MX_ADC_Init(void)
    {


        /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) 
        */
      hadc.Instance = ADC1;
      hadc.Init.OversamplingMode = DISABLE;
      hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV1;
      hadc.Init.Resolution = ADC_RESOLUTION_12B;
      hadc.Init.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
      hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
      hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
      hadc.Init.ContinuousConvMode = DISABLE;
      hadc.Init.DiscontinuousConvMode = DISABLE;
      hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
      hadc.Init.DMAContinuousRequests = DISABLE;
      hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
      hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
      hadc.Init.LowPowerAutoWait = ENABLE;
      hadc.Init.LowPowerFrequencyMode = ENABLE;
      hadc.Init.LowPowerAutoPowerOff = ENABLE;
      HAL_ADC_Init(&hadc);

        sConfigAdc.Channel = ADC_CHANNEL_0;
        if(HAL_ADC_ConfigChannel(&hadc, &sConfigAdc) != HAL_OK){
            while(1){}
        }
            sConfigAdc.Channel = ADC_CHANNEL_1;
        if(HAL_ADC_ConfigChannel(&hadc, &sConfigAdc) != HAL_OK){
            while(1){}
        }

            sConfigAdc.Channel = ADC_CHANNEL_2;
        if(HAL_ADC_ConfigChannel(&hadc, &sConfigAdc) != HAL_OK){
            while(1){}
        }

            sConfigAdc.Channel = ADC_CHANNEL_3;
        if(HAL_ADC_ConfigChannel(&hadc, &sConfigAdc) != HAL_OK){
            while(1){}
        }

            sConfigAdc.Channel = ADC_CHANNEL_5;
        if(HAL_ADC_ConfigChannel(&hadc, &sConfigAdc) != HAL_OK){
            while(1){}
        }
            sConfigAdc.Channel = ADC_CHANNEL_6;
        if(HAL_ADC_ConfigChannel(&hadc, &sConfigAdc) != HAL_OK){
            while(1){}
        }
            sConfigAdc.Channel = ADC_CHANNEL_7;
        if(HAL_ADC_ConfigChannel(&hadc, &sConfigAdc) != HAL_OK){
            while(1){}
        }

            sConfigAdc.Channel = ADC_CHANNEL_8;
        if(HAL_ADC_ConfigChannel(&hadc, &sConfigAdc) != HAL_OK){
            while(1){}
        }
             /*
        sConfigAdc.Channel = ADC_CHANNEL_9;
        if(HAL_ADC_ConfigChannel(&hadc, &sConfigAdc) != HAL_OK){
            while(1){}
        }  */


    }

    void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
    {

      GPIO_InitTypeDef GPIO_InitStruct;
      if(hadc->Instance==ADC1)
      {
      /* USER CODE BEGIN ADC1_MspInit 0 */

      /* USER CODE END ADC1_MspInit 0 */
        /* Peripheral clock enable */
        __HAL_RCC_ADC1_CLK_ENABLE();

        /**ADC GPIO Configuration    
        PA0     ------> ADC_IN0
        PA1     ------> ADC_IN1
        PA2     ------> ADC_IN2
        PA3     ------> ADC_IN3
        PA5     ------> ADC_IN5
        PA6     ------> ADC_IN6
        PA7     ------> ADC_IN7
        PB0     ------> ADC_IN8
        PB1     ------> ADC_IN9 
        */
        GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3 
                              |GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
        GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

        GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
        GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

      /* USER CODE BEGIN ADC1_MspInit 1 */

      /* USER CODE END ADC1_MspInit 1 */
      }
    }

    void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
    {

      if(hadc->Instance==ADC1)
      {
      /* USER CODE BEGIN ADC1_MspDeInit 0 */

      /* USER CODE END ADC1_MspDeInit 0 */
        /* Peripheral clock disable */
        __HAL_RCC_ADC1_CLK_DISABLE();

        /**ADC GPIO Configuration    
        PA0     ------> ADC_IN0
        PA1     ------> ADC_IN1
        PA2     ------> ADC_IN2
        PA3     ------> ADC_IN3
        PA5     ------> ADC_IN5
        PA6     ------> ADC_IN6
        PA7     ------> ADC_IN7
        PB0     ------> ADC_IN8
        PB1     ------> ADC_IN9 
        */
        HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3 
                              |GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);

        HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0|GPIO_PIN_1);

      }
      /* USER CODE BEGIN ADC1_MspDeInit 1 */

      /* USER CODE END ADC1_MspDeInit 1 */
    } 

    /* USER CODE BEGIN 1 */

    /* USER CODE END 1 */

问题是,我想读取值temp1,transistor_1电压(ube1,vrb1,vrc1),temp2晶体管_2电压(ube2,vrb2,vrc2)

16次获得平均代码:

HAL_Delay(2);

  /* Set DAC at voltage level 1 (3.1v 0xF07)*/
  setDAC(0xF07);   // 
   printf("\n***************************\n");
  HAL_Delay(2);
  for(int i = 0; i < 16; i++){ 

    readRollingADC(0); // read All inputs 16 times. 

void shiftAverages(){
  for(int i = 0; i < 8; i++){
    experiments[i].temperature = (experiments[i].temperature >> 4);
    experiments[i].ube = (experiments[i].ube >> 4);
    experiments[i].vrb = (experiments[i].vrb >> 4);
    experiments[i].vrc = (experiments[i].vrc >> 4);
  }
}

我为不同的setDAC值做这些事情,然后输入函数readRollingADC()
每次我输入函数时,第一次读取总是2048然后它工作正常.

当我读取第一个值时,它似乎以某种方式让我高,那是自动存储在ADC寄存器中的那些东西吗?

//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)){}
  experiments[0+index].temperature += hadc.Instance->DR;
printf("\n ex0 i rolling %d\n", experiments[0+index].temperature); 
 HAL_ADC_Stop(&hadc);
   HAL_Delay(20);
  HAL_ADC_Start(&hadc);
  HAL_Delay(20);

  while(!(hadc.Instance->ISR & ADC_ISR_EOC)){}
  experiments[0+index].ube += hadc.Instance->DR;
   printf("\n ube si %d\n", experiments[0+index].ube); 
  HAL_ADC_Stop(&hadc);
   HAL_Delay(20);
  HAL_ADC_Start(&hadc);
  HAL_Delay(20);

我确实喜欢这个,这给了我所有频道的相同价值,这是你的意思,还是我应该写别的东西?

最佳答案
你有hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV和hadc.Init.ContinuousConvMode = DISABLE.我想你需要在每个通道转换后重启ADC.因此,在每次转换之间,您需要调用HAL_ADC_Stop,然后调用HAL_ADC_Start.你甚至不在转换之间清除ISR_EOC标志,你必须手动执行,这就是为什么最好调用HAL_ADC_PollForConversions来清除它.而且你需要调用HAL_ADC_Start与配置的通道完全相同的计数,即. 9在您的设置中,否则ADC通道选择将错过您希望读取的通道.
如果有超过5个通道,我建议使用带有与所配置通道完全相同数量的缓冲区的DMA,并设置hadc.Init.EOCSelection = ADC_EOC_SEQ_CONV.这样,使用单个HAL_ADC_Start_DMA,您将获得所有通道字节.通过少于5个通道,一个常规通道,其余注入通道和自动注入完成工作.
请记住将_LL_ADC_CALC_TEMPERATURE的读取ADC值转换为温度,并将__LL_ADC_CALC_DATA_TO_VOLTAGE转换为电压,从stm32l0_ll_adc.h开始.
你可以阅读stm32l0 hal drivers here.

转载注明原文:stm32多个ADC redings错误 - 代码日志