STM32F030C8T6 ADC using HAL interface not giving expected results (returns 4095)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-06-12 11:57 PM - edited ‎2023-11-20 03:23 AM
I am trying to get the ADCs operational on my board that uses an STM32F030C8 processor.
PA0 pin analog seems to read OK ~2200 which is approx OK.
Reading CH16 and CH17 (Temp sensor, VrefINT) always return a value of 4095.
Main()
we call the Init
MX_ADC_Init();
Inside the Main While loop I tried reading all ADC channels
while(1)
{
for(uint16_t i=0; i<20; i++)
{
ADC1->CHSELR = ADC_CHSELR_CHANNEL(i);
HAL_ADC_Start(&hadc); // start the adc
uint16_t tmp =HAL_ADC_PollForConversion(&hadc, 100);
adc_val[i] = HAL_ADC_GetValue(&hadc); // get the adc value
HAL_ADC_Stop(&hadc); // stop adc
HAL_Delay (500); // wait for 500ms
}
}
The ADC.c file is more or less directly from the STM32cube MX tool
in the ADC.c
/* ADC init function */
void MX_ADC_Init(void)
{
/* USER CODE BEGIN ADC_Init 0 */
/* USER CODE END ADC_Init 0 */
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC_Init 1 */
/* USER CODE END ADC_Init 1 */
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc.Instance = ADC1;
hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
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();
}
/** Configure for the selected ADC regular channel to be converted.
*/
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5; // rate used for all below since not overwritten
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel to be converted.
*/
sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5; // rate used for all below since not overwritten
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel to be converted.
*/
sConfig.Channel = ADC_CHANNEL_VREFINT;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5; // rate used for all below since not overwritten
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC_Init 2 */
}
void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(adcHandle->Instance==ADC1)
{
/* USER CODE BEGIN ADC1_MspInit 0 */
/* USER CODE END ADC1_MspInit 0 */
/* ADC1 clock enable */
__HAL_RCC_ADC1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**ADC GPIO Configuration
PA0 ------> ADC_IN0
*/
GPIO_InitStruct.Pin = NTC_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(NTC_GPIO_Port, &GPIO_InitStruct);
}
The ADC register look Ok (to me, but im sure im missing something).
The return values from main are like this
I would appreciate any pointers any one has here because Ive been looking over this for the past week without to much success.
Regards
Richard
Solved! Go to Solution.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-06-20 10:41 PM
The issue was that a 5V RS485 chip was connected to the UART pins which are on the port A pins.
After changing the RS485 chip to a 3v2 supply everything is working normally now.
I found the clue on other posts where they mentioned that the analogue pins of type TTa cannot have 5V on them if you are using the ADC otherwise the catch diode must somehow cause this overflow condition.
Issue resolved - After a week of trying all sorts of software tests. It always the same when you are convinced its software it will always be hardware !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-06-20 10:41 PM
The issue was that a 5V RS485 chip was connected to the UART pins which are on the port A pins.
After changing the RS485 chip to a 3v2 supply everything is working normally now.
I found the clue on other posts where they mentioned that the analogue pins of type TTa cannot have 5V on them if you are using the ADC otherwise the catch diode must somehow cause this overflow condition.
Issue resolved - After a week of trying all sorts of software tests. It always the same when you are convinced its software it will always be hardware !