2025-02-10 06:46 AM
Hello there,
I have created my own project following the preconfigured project "ADC_RegularConversion_Interrupt". In my project I want to get the internal temperature sensor value and want to display it on the terminal thus using the UART example in this project as well.
However, I stuck forever inside HAL_ADC_GetValue() function. I am aware about the fact the priority of adc needs to be >= 1 (since only systick can have the highest priority => 0).
see adc config below with read temperature function:
static void init_adc(void)
{
adc_handle.Instance = ADCx;
if(HAL_ADC_DeInit(&adc_handle) != HAL_OK)
{
Error_Handler();
}
adc_handle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4;
adc_handle.Init.Resolution = ADC_RESOLUTION_12B;
adc_handle.Init.ScanConvMode = DISABLE; /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */
adc_handle.Init.ContinuousConvMode = ENABLE; /* Continuous mode enabled to have continuous conversion */
adc_handle.Init.DiscontinuousConvMode = DISABLE; /* Parameter discarded because sequencer is disabled */
adc_handle.Init.NbrOfDiscConversion = 0;
adc_handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; /* Conversion start triggered at each external event */
adc_handle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
adc_handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
adc_handle.Init.NbrOfConversion = 1;
adc_handle.Init.DMAContinuousRequests = DISABLE;
adc_handle.Init.EOCSelection = DISABLE;
if(HAL_ADC_Init(&adc_handle) != HAL_OK)
{
Error_Handler();
}
}
static void init_adc_channel(void)
{
adc_channel_conf.Channel = ADC_CHANNEL_TEMPSENSOR;
adc_channel_conf.Rank = 1;
adc_channel_conf.SamplingTime = ADC_SAMPLETIME_15CYCLES;
adc_channel_conf.Offset = 0;
if(HAL_ADC_ConfigChannel(&adc_handle, &adc_channel_conf) != HAL_OK)
{
Error_Handler();
}
//start the conversion process
if(HAL_ADC_Start_IT(&adc_handle) != HAL_OK)
{
Error_Handler();
}
}
float read_temperature(ADC_HandleTypeDef *adc_handle)
{
adc_val = HAL_ADC_GetValue(adc_handle);
float Vref = 3.3;
float Vsense = (adc_val * Vref) / 4095.0; //for 12bit resolution 2^12 = 0 <-> 4095
float avg_slope = 0.0025; //2.5 mV/C = 0.0025 V/C
float V25 = 0.76;
float temp_celcius = ((Vsense - V25) / avg_slope) + 25.0;
return temp_celcius;
}
see below ADC MSP config:
void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
{
GPIO_InitTypeDef GPIO_InitStruct;
/*##-1- Enable peripherals and GPIO Clocks #################################*/
/* ADC Periph clock enable */
ADCx_CLK_ENABLE();
/* Enable GPIO clock ****************************************/
ADCx_CHANNEL_GPIO_CLK_ENABLE();
/*##-2- Configure peripheral GPIO ##########################################*/
/* ADC Channel GPIO pin configuration */
GPIO_InitStruct.Pin = ADCx_CHANNEL_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(ADCx_CHANNEL_GPIO_PORT, &GPIO_InitStruct);
/*##-3- Configure the NVIC #################################################*/
/* NVIC configuration for ADC interrupt */
HAL_NVIC_SetPriority(ADCx_IRQn, 1, 1);
HAL_NVIC_EnableIRQ(ADCx_IRQn);
}
/**
* @brief ADC MSP De-Initialization
* This function frees the hardware resources used in this example:
* - Disable the Peripheral's clock
* - Revert GPIO to their default state
* @PAram hadc: ADC handle pointer
* @retval None
*/
void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)
{
/*##-1- Reset peripherals ##################################################*/
ADCx_FORCE_RESET();
ADCx_RELEASE_RESET();
/*##-2- Disable peripherals and GPIO Clocks ################################*/
/* De-initialize the ADC Channel GPIO pin */
HAL_GPIO_DeInit(ADCx_CHANNEL_GPIO_PORT, ADCx_CHANNEL_PIN);
}
Thanks,
Solved! Go to Solution.
2025-02-10 10:30 AM
Actually, I got it working.
Since I am still getting the gist of my new stm32f767, I did not have an adc interrupt callback set up in "stm32f7xx_it.c" file, thats why it was not getting any value from HAL_ADC_GetValue().
Thanks,
2025-02-10 07:01 AM
So have you stepped into the HAL_ADC_GetValue() function to see where, exactly, it's getting stuck, and what it's waiting for?
2025-02-10 07:03 AM
all what it does in this function to fetch the value from data register
return hadc->Instance->DR;
thanks,
2025-02-10 10:30 AM
Actually, I got it working.
Since I am still getting the gist of my new stm32f767, I did not have an adc interrupt callback set up in "stm32f7xx_it.c" file, thats why it was not getting any value from HAL_ADC_GetValue().
Thanks,