STM32G431KB ADC won't calibrate or work - Page 1
Forum section: electronics / general

author: syntax333
author_rank: Regular Contributor
timestamp: March 27, 2020, 01:27:24 pm
content: Hi I am currently working on a project where I have to use single channel ADC with continuous conversion mode of STM32G431KB on NUCLEO board. However my code stucks at while loop where calibration needs to happen.Also I noticed that ADC12_Common register do not take value of 0b0001 which is divide clock by two. I am giving 42.5MHz ADC from PLLP divider and was planning to divide it by 2.Code:#include "main.h"#include "SysClock_Config.h"void DummyDelay(int x){int temp = x;while(temp){temp--;}}void Initialization(void){RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN | RCC_AHB2ENR_ADC12EN;GPIOB->MODER |= GPIO_MODER_MODER0; //Set PB0 as AnalogADC12_COMMON->CCR |= (0b0001 << ADC_CCR_PRESC_Pos); //ADC Clock divided by 2ADC1->CR &= ~(ADC_CR_DEEPPWD); //Disable ADC Deep-Power-Down modeADC1->CR |= ADC_CR_ADVREGEN; //Enable ADC Voltage RegulatorDummyDelay(5000);ADC1->CR &= ~(ADC_CR_ADEN); //Disable ADCADC1->CR &= ~(ADC_CR_ADCALDIF); //Single Ended Mode CalibrationADC1->CR |= ADC_CR_ADCAL; //Start Calibrationwhile(ADC1->CR & ADC_CR_ADCAL); //Wait Until Calibration CompleteADC1->CFGR |= ADC_CFGR_CONT; //ADC continuous modeADC1->CFGR &= ~(ADC_CFGR_RES); //12-bit ADCADC1->SQR1 |= (1 << ADC_SQR1_L_Pos) | (15 << ADC_SQR1_SQ1_Pos); // Only 1 conversion for ADC channel 15ADC1->CR |= ADC_CR_ADEN; //Enable ADCwhile(!(ADC1->ISR & ADC_ISR_ADRDY)); //Wait Until ADC Enables}int main(void){HAL_Init();SystemClock_Config();Initialization();ADC1->CR |= ADC_CR_ADSTART; //Start ADC Sampling/Conversion Sequencewhile (1){}}Code:#include "main.h"void Error_Handler(void){}void SystemClock_Config(void){RCC_OscInitTypeDef RCC_OscInitStruct = {0};RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};/** Configure the main internal regulator output voltage*/HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);/** Initializes the CPU, AHB and APB busses clocks*/RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;RCC_OscInitStruct.HSEState = RCC_HSE_ON;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV6;RCC_OscInitStruct.PLL.PLLN = 85;RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV8;RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK){Error_Handler();}/** Initializes the CPU, AHB and APB busses clocks*/RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_8) != HAL_OK){Error_Handler();}SysTick->CTRL &= ~(SysTick_CTRL_ENABLE_Msk);}NOTE: I am using STM32CubeIDE.

author: thm_w
author_rank: Super Contributor
timestamp: March 29, 2020, 09:00:58 pm
content: maybehttps://stackoverflow.com/questions/43876460/stm32f303-adc-gets-stuck-in-the-calibrationIf you are using HAL why not let cubemx set up the clocks? This will also check if they are valid.