搜索
查看: 1234|回复: 1

stm32l431RC ADC 采集Vbat 不准的问题

[复制链接]

该用户从未签到

2

主题

15

帖子

0

蝴蝶豆

新手上路

最后登录
2020-5-14
发表于 2020-5-11 13:08:08 | 显示全部楼层 |阅读模式
系统电池供电,3.6V电池,经过一个二极管(3A),大概是3.3V到Vbat .
测量电压为2.98V 左右。
继续采用直流电源供电,当输入Vbat 3.0v 时,测量为 2.86V左右。
同时还发现一些怪现象,aADCxConvertedData[0],明显比后续的采集值小很多。
哪里操作有问题?


原理图

原理图


/* Definition of ADCx conversions data table size */
#define ADC_CONVERTED_DATA_BUFFER_SIZE   ((uint32_t)  10)   /* Size of array aADCxConvertedData[] */
static uint16_t   aADCxConvertedData[ADC_CONVERTED_DATA_BUFFER_SIZE];
/**
  * @brief  Conversion complete callback in non-blocking mode.
  * @param hadc ADC handle
  * @retval None
  */
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
  /* Prevent unused argument(s) compilation warning */
        if(BinarySem_ADCHandle)
                osSemaphoreRelease(BinarySem_ADCHandle);

  /* NOTE : This function should not be modified. When the callback is needed,
            function HAL_ADC_ConvCpltCallback must be implemented in the user file.
   */
}
/**
  * @brief  测量电池电压
  * @param[IN]
  * @retval None
  */
uint32_t getBattaryTemperatureMeasure(uint8_t bat_flag)
{
          ADC_ChannelConfTypeDef sConfig = {0};

          memset(aADCxConvertedData,0,sizeof(aADCxConvertedData));
          /* ### - 1 - Initialize ADC peripheral #################################### */
          hadc1.Instance          = ADC1;
          if (HAL_ADC_DeInit(&hadc1) != HAL_OK)
          {
            /* ADC de-initialization Error */
            Error_Handler();
          }

          hadc1.Init.ClockPrescaler        = ADC_CLOCK_SYNC_PCLK_DIV1;      /* Synchronous clock mode, input ADC clock divided by 2*/
          hadc1.Init.Resolution            = ADC_RESOLUTION_12B;            /* 12-bit resolution for converted data */
          hadc1.Init.DataAlign             = ADC_DATAALIGN_RIGHT;           /* Right-alignment for converted data */
          hadc1.Init.ScanConvMode          = DISABLE;                       /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */
          hadc1.Init.EOCSelection          = ADC_EOC_SINGLE_CONV;           /* EOC flag picked-up to indicate conversion end */
          hadc1.Init.LowPowerAutoWait      = DISABLE;                       /* Auto-delayed conversion feature disabled */
          hadc1.Init.ContinuousConvMode    = ENABLE;                        /* Continuous mode enabled (automatic conversion restart after each conversion) */
          hadc1.Init.NbrOfConversion       = 1;                             /* Parameter discarded because sequencer is disabled */
          hadc1.Init.DiscontinuousConvMode = DISABLE;                       /* Parameter discarded because sequencer is disabled */
          hadc1.Init.NbrOfDiscConversion   = 1;                             /* Parameter discarded because sequencer is disabled */
          hadc1.Init.ExternalTrigConv      = ADC_SOFTWARE_START;            /* Software start to trig the 1st conversion manually, without external event */
          hadc1.Init.ExternalTrigConvEdge  = ADC_EXTERNALTRIGCONVEDGE_NONE; /* Parameter discarded because software trigger chosen */
          hadc1.Init.DMAContinuousRequests = ENABLE;                        /* ADC DMA continuous request to match with DMA circular mode */
          hadc1.Init.Overrun               = ADC_OVR_DATA_OVERWRITTEN;      /* DR register is overwritten with the last conversion result in case of overrun */
          hadc1.Init.OversamplingMode      = DISABLE;                       /* No oversampling */
          /* Initialize ADC peripheral according to the passed parameters */
          if (HAL_ADC_Init(&hadc1) != HAL_OK)
          {
            Error_Handler();
          }


          /* ### - 2 - Start calibration ############################################ */
          if (HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED) !=  HAL_OK)
          {
            Error_Handler();
          }

          /* ### - 3 - Channel configuration ######################################## */
          sConfig.Channel      = ADC_CHANNEL_VBAT;                /* Sampled channel number */
          sConfig.Rank         = ADC_REGULAR_RANK_1;          /* Rank of sampled channel number ADCx_CHANNEL */
          sConfig.SamplingTime = ADC_SAMPLETIME_6CYCLES_5;   /* Sampling time (number of clock cycles unit) */
          sConfig.SingleDiff   = ADC_SINGLE_ENDED;            /* Single-ended input channel */
          sConfig.OffsetNumber = ADC_OFFSET_NONE;             /* No offset subtraction */
          sConfig.Offset = 0;                                 /* Parameter discarded because offset correction is disabled */
          if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
          {
            Error_Handler();
          }

          /* ### - 4 - Start conversion in DMA mode ################################# */
          if (HAL_ADC_Start_DMA(&hadc1,
                                (uint32_t *)aADCxConvertedData,
                                ADC_CONVERTED_DATA_BUFFER_SIZE
                               ) != HAL_OK)
          {
            Error_Handler();
          }
          uint32_t ret = 0 ;
          if(osOK == osSemaphoreWait(BinarySem_ADCHandle,10))
          {
                  uint32_t adc_val = 0;//aADCxConvertedData[1];
                  for (int i = ADC_CONVERTED_DATA_BUFFER_SIZE/2 ; i < ADC_CONVERTED_DATA_BUFFER_SIZE ; i++)
                  {
                          adc_val += aADCxConvertedData;
                  }
                  adc_val = adc_val  / (ADC_CONVERTED_DATA_BUFFER_SIZE/2);
                  ret = __HAL_ADC_CALC_DATA_TO_VOLTAGE(2500,adc_val,ADC_RESOLUTION_12B);
                  ret *= 3;
          }

          return (ret);
}


回复

使用道具 举报

该用户从未签到

2

主题

15

帖子

0

蝴蝶豆

新手上路

最后登录
2020-5-14
 楼主| 发表于 2020-5-14 09:13:46 | 显示全部楼层
通过两天调试,讲采样周期设置长一些,比较接近了。要大于 ADC_SAMPLETIME_12CYCLES_5  。技术支持一直没响应。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条

Archiver|手机版|小黑屋|论坛-意法半导体STM32/STM8技术社区

GMT+8, 2024-4-25 19:58 , Processed in 0.157004 second(s), 33 queries .

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.

快速回复 返回顶部 返回列表