Skip to main content
Associate II
February 12, 2025
Question

Cannot use PA4 and PA5 with the DAC and ADC (PA6)

Moved from this thread.

ADC usage configuration.

I have a project that already uses pins PA4 and PA5 with the DAC, but I cannot obtain a value when using pin PA6 to read a voltage level

This topic has been closed for replies.

8 replies

Associate II
February 12, 2025

Could you give me a simple solution?

mƎALLEm
ST Technical Moderator
February 12, 2025

What do you mean by simple solution?

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
mƎALLEm
ST Technical Moderator
February 12, 2025

Need to explain what do you mean by "I cannot obtain a value when using pin PA6 to read a voltage level"?

Always 0? constant value? else?

Need to attach your project so others can help you efficiently.

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
Associate II
February 12, 2025

Felipe_Nascimento_0-1739378580783.png

Always 0

I don't know if I'm configuring something wrong
mƎALLEm
ST Technical Moderator
February 12, 2025

You need to share your code ..

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
Associate II
February 12, 2025
int main(void)

{

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */



/* MCU Configuration--------------------------------------------------------*/



/* Reset of all peripherals, Initializes the Flash interface and the Systick. */

HAL_Init();



/* USER CODE BEGIN Init */



// uint8_t i = 0;



/* USER CODE END Init */



/* Configure the system clock */

SystemClock_Config();



/* USER CODE BEGIN SysInit */

DAC_Init();

DAC_Start();



/* USER CODE END SysInit */



/* Initialize all configured peripherals */

MX_GPIO_Init();

MX_DAC1_Init();

MX_I2C1_Init();

MX_USART2_UART_Init();

MX_TIM2_Init();

MX_ADC2_Init();



/* USER CODE BEGIN 2 */

ssd1306_Init();

ssd1306_Fill(Black);

ssd1306_UpdateScreen();

HAL_TIM_Base_Start_IT(&htim2);



/* USER CODE END 2 */



/* Infinite loop */

/* USER CODE BEGIN WHILE */



Menu_Select(select_position, select_mod);



while (1){



set_period = ((1.0 / frequency) * 1000000)/10;



uint32_t adc_value = readADC();

// float voltage_measured = (adc_value * 4.2) / 4095.0;



// if (voltage_measured <= 3.9) {

// status = 1;

// }

// else if (voltage_measured >= 3.9 && voltage_measured < 4.1) {

// status = 2;

// } else {

// status = 3;

// }

}
}


static void MX_DAC1_Init(void)

{



/* USER CODE BEGIN DAC1_Init 0 */



/* USER CODE END DAC1_Init 0 */



DAC_ChannelConfTypeDef sConfig = {0};



/* USER CODE BEGIN DAC1_Init 1 */



/* USER CODE END DAC1_Init 1 */



/** DAC Initialization

*/

hdac1.Instance = DAC1;

if (HAL_DAC_Init(&hdac1) != HAL_OK)

{

Error_Handler();

}



/** DAC channel OUT1 config

*/

sConfig.DAC_Trigger = DAC_TRIGGER_NONE;

sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;

if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_1) != HAL_OK)

{

Error_Handler();

}



/** DAC channel OUT2 config

*/

if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_2) != HAL_OK)

{

Error_Handler();

}

/* USER CODE BEGIN DAC1_Init 2 */



/* USER CODE END DAC1_Init 2 */



}


static void MX_ADC2_Init(void)

{



/* USER CODE BEGIN ADC2_Init 0 */



/* USER CODE END ADC2_Init 0 */



ADC_ChannelConfTypeDef sConfig = {0};



/* USER CODE BEGIN ADC2_Init 1 */



/* USER CODE END ADC2_Init 1 */



/** Common config

*/

hadc2.Instance = ADC2;

hadc2.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;

hadc2.Init.Resolution = ADC_RESOLUTION_12B;

hadc2.Init.ScanConvMode = ADC_SCAN_DISABLE;

hadc2.Init.ContinuousConvMode = DISABLE;

hadc2.Init.DiscontinuousConvMode = DISABLE;

hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;

hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;

hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;

hadc2.Init.NbrOfConversion = 1;

hadc2.Init.DMAContinuousRequests = ENABLE;

hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;

hadc2.Init.LowPowerAutoWait = DISABLE;

hadc2.Init.Overrun = ADC_OVR_DATA_PRESERVED;

if (HAL_ADC_Init(&hadc2) != HAL_OK)

{

Error_Handler();

}



/** Configure Regular Channel

*/

sConfig.Channel = ADC_CHANNEL_3; // PA6 = ADC2_IN3

sConfig.Rank = ADC_REGULAR_RANK_1;

sConfig.SamplingTime = ADC_SAMPLETIME_601CYCLES_5; // Tempo de amostragem rápido

if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)

{

Error_Handler();

}

/* USER CODE BEGIN ADC2_Init 2 */



/* USER CODE END ADC2_Init 2 */



}





static void MX_GPIO_Init(void)

{

GPIO_InitTypeDef GPIO_InitStruct = {0};

/* USER CODE BEGIN MX_GPIO_Init_1 */

/* USER CODE END MX_GPIO_Init_1 */



/* GPIO Ports Clock Enable */

__HAL_RCC_GPIOF_CLK_ENABLE();

__HAL_RCC_GPIOC_CLK_ENABLE();

__HAL_RCC_GPIOA_CLK_ENABLE();

__HAL_RCC_GPIOB_CLK_ENABLE();



/*Configure GPIO pin Output Level */

HAL_GPIO_WritePin(GPIOA, BRIDGE_H_CH1_1_Pin|BRIDGE_H_CH1_2_Pin|BRIDGE_H_CH2_2_Pin|BRIDGE_H_CH2_1_Pin, GPIO_PIN_RESET);



/*Configure GPIO pins : BT_DOWN_CH2_Pin BT_UP_CH2_Pin */

GPIO_InitStruct.Pin = BT_DOWN_CH2_Pin|BT_UP_CH2_Pin;

GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

GPIO_InitStruct.Pull = GPIO_PULLUP;

HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);



/*Configure GPIO pins : BRIDGE_H_CH1_1_Pin BRIDGE_H_CH1_2_Pin BRIDGE_H_CH2_2_Pin BRIDGE_H_CH2_1_Pin */

GPIO_InitStruct.Pin = BRIDGE_H_CH1_1_Pin|BRIDGE_H_CH1_2_Pin|BRIDGE_H_CH2_2_Pin|BRIDGE_H_CH2_1_Pin;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);



/*Configure GPIO pins : BT_UP_SELECT_Pin BT_DOWN_SELECT_Pin BT_SELECT_Pin BT_START_STOP_Pin */

GPIO_InitStruct.Pin = BT_UP_SELECT_Pin|BT_DOWN_SELECT_Pin|BT_SELECT_Pin|BT_START_STOP_Pin;

GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

GPIO_InitStruct.Pull = GPIO_PULLUP;

HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);



/*Configure GPIO pins : BT_UP_CH1_Pin BT_DOWN_CH1_Pin */

GPIO_InitStruct.Pin = BT_UP_CH1_Pin|BT_DOWN_CH1_Pin;

GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

GPIO_InitStruct.Pull = GPIO_PULLUP;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);





/* USER CODE BEGIN MX_GPIO_Init_2 */

/* USER CODE END MX_GPIO_Init_2 */

}

Associate II
February 12, 2025
uint32_t readADC(void) {
uint32_t adcValue = 0;
HAL_ADC_Start(&hadc2); // Inicia a conversão
// if (HAL_ADC_PollForConversion(&hadc2, 10) == HAL_OK) {
// adcValue = HAL_ADC_GetValue(&hadc2); // Obtém o valor convertido
// }
adcValue = HAL_ADC_GetValue(&hadc2); // Obtém o valor convertido

HAL_ADC_Stop(&hadc2); // Para o ADC após a leitura
return adcValue;
}

 

mƎALLEm
ST Technical Moderator
February 12, 2025

@Felipe_Nascimento wrote:
uint32_t readADC(void) {
uint32_t adcValue = 0;
HAL_ADC_Start(&hadc2); // Inicia a conversão
// if (HAL_ADC_PollForConversion(&hadc2, 10) == HAL_OK) {
// adcValue = HAL_ADC_GetValue(&hadc2); // Obtém o valor convertido
// }
adcValue = HAL_ADC_GetValue(&hadc2); // Obtém o valor convertido

HAL_ADC_Stop(&hadc2); // Para o ADC após a leitura
return adcValue;
}

 


You need also to provide the ADC configuration as well as the DAC + their GPIO configs

Meanwhile, you need to poll on the end of the conversion + you need to calibrate the ADC before to start. From the HAL example:

 

 if (HAL_ADCEx_Calibration_Start(&AdcHandle_slave, ADC_SINGLE_ENDED) != HAL_OK)

PS: please use </> button to paste your code. See this link.

 

 

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.