Fullscreen
Loading...
 

System Workbench for STM32 Help

Forums » System Workbench for STM32» ADC conversion for multiple channels but not together

ADC conversion for multiple channels but not together

Hi

I am using the 32MF072 device and I have 8 ADC inputs.
What I want to do is to just read each input then read another at some other time.
So I dont really want to use DMA.
I have used the code from the examples in the CUBEMX files, and I can get it to work but it
doesnt work correctly all the time. I just have a voltage set on the pin but sometimes it reads the
correct voltage and other times 0V. Here is the code below. So really I am just selecting the channel
I want then starting the conversion. But maybe I have some of the settings wrong?
Thanks
Jon

static void MX_ADC_Init(void)
{

ADC_ChannelConfTypeDef sConfig;

/**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc.Instance = ADC1;
hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc.Init.Resolution = ADC_RESOLUTION_12B;
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc.Init.LowPowerAutoWait = DISABLE;
hadc.Init.LowPowerAutoPowerOff = DISABLE;
hadc.Init.ContinuousConvMode = DISABLE;
hadc.Init.DiscontinuousConvMode = DISABLE;
hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc.Init.DMAContinuousRequests = DISABLE;
hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
if (HAL_ADC_Init(&hadc) != HAL_OK)
{
_Error_Handler(FILE, LINE);
}
}

uint16_t MX_ADC_Start(uint32_t channel)
{
ADC_ChannelConfTypeDef sConfig;
uint16_t uhADCxConvertedValue = 0;

/**Configure for the selected ADC regular channel to be converted.
*/
sConfig.Channel = channel;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
sConfig.SamplingTime = ADC_SAMPLETIME_28CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}


/*##-3- Start the conversion process #######################################*/
if (HAL_ADC_Start(&hadc) != HAL_OK)
{
/* Start Conversation Error */
Error_Handler();
}

/*##-4- Wait for the end of conversion #####################################*/
/* For simplicity reasons, this example is just waiting till the end of the
conversion, but application may perform other tasks while conversion
operation is ongoing. */
if (HAL_ADC_PollForConversion(&hadc, 10) != HAL_OK)
{
/* End Of Conversion flag not set on time */
Error_Handler();
}
else
{
/* ADC conversion completed */
/*##-5- Get the converted value of regular channel ########################*/
uhADCxConvertedValue = HAL_ADC_GetValue(&hadc);
}

return uhADCxConvertedValue;
}


Show posts: