本帖最后由 songxu125400 于 2017-5-18 21:09 编辑
大神们好,我刚接触STM32L0系列芯片,现在搞AD转换,用HAL库,可以正常转换,但是转换时间太长了,效率很低。不知道是我程序有问题还是什么望大神们指点,谢谢。下面是我用库写的
void vInitAdc(void)
{
AdcHandle.Instance = ADC1;
AdcHandle.Init.OversamplingMode = DISABLE;
AdcHandle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV1;
AdcHandle.Init.LowPowerAutoPowerOff = DISABLE;
AdcHandle.Init.LowPowerFrequencyMode = ENABLE;
AdcHandle.Init.LowPowerAutoWait = DISABLE;
AdcHandle.Init.Resolution = ADC_RESOLUTION_12B;
AdcHandle.Init.SamplingTime = ADC_SAMPLETIME_7CYCLES_5;
AdcHandle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
AdcHandle.Init.ContinuousConvMode = DISABLE;//ENABLE;
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
AdcHandle.Init.DMAContinuousRequests = DISABLE;
/* Initialize ADC peripheral according to the passed parameters */
if (HAL_ADC_Init(&AdcHandle) != HAL_OK)
{
;//Error_Handler();
}
/* ### - 2 - Start calibration ############################################ */
if (HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_SINGLE_ENDED) != HAL_OK)
{
;//Error_Handler();
}
}
unsigned int uiADC_Result(unsigned char ucChannel)
{
unsigned char i;
unsigned int uiMaxResult = 0, uiMinResult = 0xFFFF;
unsigned int j,uiResult = 0;
ADC_ChannelConfTypeDef sConfig;
ADC1->CHSELR=0;
ADC1->CHSELR=ucChannel;//sConfig.Channel;
HAL_ADC_Start(&AdcHandle);
for (i = 0;i < 6;i++)
{
HAL_ADC_PollForConversion(&AdcHandle, 10);
/* Check if the continous conversion of regular channel is finished */
if ((HAL_ADC_GetState(&AdcHandle) & HAL_ADC_STATE_REG_EOC) == HAL_ADC_STATE_REG_EOC)
{
j = HAL_ADC_GetValue(&AdcHandle);
}
if (j > uiMaxResult)
{
uiMaxResult = j;
}
if (j < uiMinResult)
{
uiMinResult = j;
}
uiResult += j;
}
uiResult -= uiMaxResult;
uiResult -= uiMinResult;
uiResult /= 4;
return uiResult;
} |