Question
How can I read 4 inputs of ADC continously (using HAL)
I have initialized ADC1 and assigned it 4 channels (PA4-PA7 as IN4 to IN7). My initialization looks like this:
static void MX_ADC1_Init(void)
{
/* USER CODE BEGIN ADC1_Init 0 */
/* USER CODE END ADC1_Init 0 */
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC1_Init 1 */
/* USER CODE END ADC1_Init 1 */
/** Common config
*/
hadc1.Instance = ADC1;
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_4;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */
/* USER CODE END ADC1_Init 2 */
}I would like to read all 4 channels one after another (they represent pot positions). How can I address each channel? This is the code I'm trying but I don't see any specific channel being addressed. Rather than the one specified in the initialization.
The is the read loop:
while (1)
{
char buffer[8];
int ADCValue;
/* USER CODE BEGIN 3 */
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_1);
//delay_us(50);
delay_ms(10);
//pin_state = !pin_state;
//HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, pin_state);
// synchronous delay for 500 ms
//HAL_Delay(100);
if (HAL_ADC_Start(&hadc1) != HAL_OK)
{
/* Start Conversation Error */
Error_Handler();
}
if (HAL_ADC_PollForConversion(&hadc1, 500) != HAL_OK)
{
/* End Of Conversion flag not set on time */
// Error_Handler();
ADCValue=-1;
}
else
{
/* ADC conversion completed */
ADCValue = HAL_ADC_GetValue(&hadc1);
}
HAL_ADC_Stop(&hadc1);
sprintf(buffer,"%d",ADCValue);
lcd_setcursor(0,1);
lcd_string(buffer);
/* USER CODE END 3 */
}