跟猫一起酸汤面系列:第45天 获取供电电压

低功耗应用,需要时刻监控电池的状态,作为一个专题来研究
实测L011的电压可以取到,温度没有取到

/* Note: On device STM32L011, calibration parameter TS_CAL1 is not available. */
/*       Therefore, helper macro __LL_ADC_CALC_TEMPERATURE() is not available.*/

电压首先要启用ADC中的Vrefint Channel,程序中添加一个头文件stm32l0xx_ll_adc.h

uint16_t readchannel_adc1(uint32_t Channel)
{
  ADC_ChannelConfTypeDef adcConf;
  uint16_t adcData = 0;
  while (__HAL_PWR_GET_FLAG(PWR_FLAG_VREFINTRDY) == RESET) {};
  __HAL_RCC_ADC1_CLK_ENABLE();
  HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED );
  adcConf.Channel = ADC_CHANNEL_MASK;
  adcConf.Rank = ADC_RANK_NONE; 
  HAL_ADC_ConfigChannel( &hadc, &adcConf);
  adcConf.Channel = Channel;
  adcConf.Rank = ADC_RANK_CHANNEL_NUMBER;
  HAL_ADC_ConfigChannel( &hadc, &adcConf);
  HAL_ADC_Start(&hadc);
  HAL_ADC_PollForConversion( &hadc, HAL_MAX_DELAY );
  adcData += HAL_ADC_GetValue(&hadc);
  __HAL_ADC_DISABLE(&hadc);
  __HAL_RCC_ADC1_CLK_DISABLE();
  return adcData;
}

关键就这一句

uint16_t vdda_mV = __LL_ADC_CALC_VREFANALOG_VOLTAGE(readchannel_adc1(ADC_CHANNEL_VREFINT),LL_ADC_RESOLUTION_12B);

参考资料:
https://blog.csdn.net/weixin_33910137/article/details/92637450

标签: none

添加新评论