在线时间3 小时
UID239467
ST金币37
蝴蝶豆0
注册时间2010-11-3
该用户从未签到
新手上路
- 最后登录
- 2020-5-14
|
系统电池供电,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);
}
|
|