Author Topic: STM32G431KB ADC won't calibrate or work  (Read 1354 times)

0 Members and 1 Guest are viewing this topic.

Offline syntax333Topic starter

  • Regular Contributor
  • *
  • Posts: 158
  • Country: 00
STM32G431KB ADC won't calibrate or work
« on: March 27, 2020, 01:27:24 pm »
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: [Select]

#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 Analog
 
ADC12_COMMON->CCR |= (0b0001 << ADC_CCR_PRESC_Pos); //ADC Clock divided by 2
ADC1->CR &= ~(ADC_CR_DEEPPWD); //Disable ADC Deep-Power-Down mode
ADC1->CR |= ADC_CR_ADVREGEN; //Enable ADC Voltage Regulator
DummyDelay(5000);
ADC1->CR &= ~(ADC_CR_ADEN); //Disable ADC
ADC1->CR &= ~(ADC_CR_ADCALDIF); //Single Ended Mode Calibration
ADC1->CR |= ADC_CR_ADCAL; //Start Calibration
while(ADC1->CR & ADC_CR_ADCAL); //Wait Until Calibration Complete
 
 
 
ADC1->CFGR |= ADC_CFGR_CONT; //ADC continuous mode
ADC1->CFGR &= ~(ADC_CFGR_RES); //12-bit ADC
ADC1->SQR1 |= (1 << ADC_SQR1_L_Pos) | (15 << ADC_SQR1_SQ1_Pos); // Only 1 conversion for ADC channel 15
 
ADC1->CR |= ADC_CR_ADEN; //Enable ADC
while(!(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 Sequence
 
 
 
  while (1)
  {
 
  }
 
}
Code: [Select]
#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.
 

Online thm_w

  • Super Contributor
  • ***
  • Posts: 8177
  • Country: ca
  • Non-expert
Re: STM32G431KB ADC won't calibrate or work
« Reply #1 on: March 29, 2020, 09:00:58 pm »
maybe https://stackoverflow.com/questions/43876460/stm32f303-adc-gets-stuck-in-the-calibration

If you are using HAL why not let cubemx set up the clocks? This will also check if they are valid.
Profile -> Modify profile -> Look and Layout ->  Don't show users' signatures
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf