2021-08-27 05:56 PM
Hi,
I configure the ADC as the following code; but sometimes(not everytime) the code will run to Line 70 : "Beer_Out_Dis(1);" ,and ADC will not work.
Did I do something wrong? Thanks verymuch.
/* ADC1 init function */
static void MX_ADC1_Init(void)
{
ADC_ChannelConfTypeDef sConfig;
/**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = ENABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 7;
hadc1.Init.DMAContinuousRequests = ENABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
HAL_ADC_Init(&hadc1);
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_0; //USB -> PA0 Voltage
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_28CYCLES;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_1; //USB -> PA1 Current
sConfig.Rank = 2;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_4; // J2 -> PA4
sConfig.Rank = 3;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_5; // J10 -> PA5
sConfig.Rank = 4;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_6; // J4 -> PA6
sConfig.Rank = 5;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_7; // J13 -> PA7
sConfig.Rank = 6;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_9; //POWER ->PB1
sConfig.Rank = 7;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&Ad_Data[0], 7);
if ( HAL_ADC_Start(&hadc1)!=HAL_OK)
{
Beer_Out_Dis(1);
}
}
Solved! Go to Solution.
2021-08-27 08:12 PM
Don't call both HAL_ADC_Start_DMA and HAL_ADC_Start. If it's busy with the first call, the second one will fail. If you want to use DMA, use HAL_ADC_Start_DMA. If you want to use polling, use HAL_ADC_Start.
2021-08-27 08:12 PM
Don't call both HAL_ADC_Start_DMA and HAL_ADC_Start. If it's busy with the first call, the second one will fail. If you want to use DMA, use HAL_ADC_Start_DMA. If you want to use polling, use HAL_ADC_Start.
2021-08-27 08:21 PM
Thanks very much! I understand and I will modify the code.