刚开始用STM32L152系列MCU 用ADC检测电压遇到问题。ADC转换完成后EOC并没有置位。程序无法判断什么时候去读转换数据。请大家指点一下。
ADC初始化程序:
void MX_ADC_Init(void)
{
ADC_ChannelConfTypeDef sConfig;
__ADC1_CLK_ENABLE();
hadc.Instance = ADC1;
hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
hadc.Init.Resolution = ADC_RESOLUTION12b;
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc.Init.ScanConvMode = DISABLE;
hadc.Init.EOCSelection = ADC_EOC_SEQ_CONV; //single convertion
hadc.Init.LowPowerAutoWait = ADC_AUTOWAIT_DISABLE;//no LowPower when wait
hadc.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_DISABLE;//
hadc.Init.ChannelsBank = ADC_CHANNELS_BANK_A; //BANK A
hadc.Init.ContinuousConvMode = DISABLE; //do not continuous convertion
hadc.Init.NbrOfConversion = 1;
hadc.Init.DiscontinuousConvMode = DISABLE;
hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START; //software trriger
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;//no external trriger
hadc.Init.DMAContinuousRequests = DISABLE; //no DMA request
if (HAL_ADC_Init(&hadc) != HAL_OK)
{
while(1);
}
sConfig.Channel = ADC_CHANNEL_10;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_4CYCLES;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
while(1);
}
HAL_ADC_Start(&hadc);
}
主程序:
int main(void)
{
HAL_Init();
SystemClock_Config2();
MX_GPIO_Init();
MX_ADC_Init();
while (1)
{
__nop();
__nop();
__nop();
__nop();
ADC_State_Temp=HAL_ADC_GetState(&hadc);
if(ADC_State_Temp==HAL_ADC_STATE_EOC) //读不到标志
{
ADC1_Value_Temp=HAL_ADC_GetState(&hadc);
HAL_ADC_Start(&hadc);
HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_11);
}
}
} |