Skip to main content
jugal
Associate II
September 3, 2020
Solved

Can anyone help me figure out what I'm doing wrong? (trying to use ADC on STM32F3 discovery board)

  • September 3, 2020
  • 7 replies
  • 1895 views

I wrote this code to initialize ADC1. What Im trying to do is take input from a Joystick and map it accordingly and send a corresponding PWM output to a LED.

 RCC->CFGR |= RCC_CFGR2_ADCPRE12_DIV2;
 RCC->AHBENR |= RCC_AHBENR_ADC12EN; //enable clock 
	ADC1->CR &= ~(ADC_CR_ADVREGEN_1 | ADC_CR_ADVREGEN_0); 
	ADC1->CR |= ADC_CR_ADVREGEN_0; //voltage regulator enable sequence
	ADC1->CR &= ~ADC_CR_ADCALDIF;
	ADC1->CR |= ADC_CR_ADCAL;	 //start calibration							
	while((ADC1->CR)& ADC_CR_ADCAL);	//wait till calibrated
	ADC1->CR |= ADC_CR_ADEN;
	while(!( (ADC1->ISR) & ADC_ISR_ADRDY ));
	ADC1->SQR1 |= ADC_SQR1_SQ1_1 | ADC_SQR1_SQ1_0; 	 //channel 3 in sequence 1				
	ADC1->SMPR1 |= ADC_SMPR1_SMP3_2 | ADC_SMPR1_SMP3_1; //sampling rate for ch3 		 
	ADC1->CFGR |= ADC_CFGR_CONT; 	//set to continuous conversion mode
	ADC1->CR |= ADC_CR_ADSTART;

The program is stuck in one of the while loops (I'm guessing due to some problem with ADC calibration? idk). I know this because the rest of my code never executes, if it did, I would be able to see an output on the LED. It would be a great help if someone can help me figure out what I'm doing wrong here.

This topic has been closed for replies.
Best answer by jugal

Problem solved thanks! i just set the CKMODE bits. From the ref manual:

01: HCLK/1 (Synchronous clock mode). This configuration must be enabled only if the AHB

clock prescaler is set to 1 (HPRE[3:0] = 0xxx in RCC_CFGR register) and if the system clock

has a 50% duty cycle.

7 replies

MWeid.2
Associate
September 3, 2020

Can you do something like light up a LED after the first white loop? Then you know if the first or second while loop is the problem.

jugal
jugalAuthor
Associate II
September 3, 2020

Okay i just did it. The first loop is the problem, the calibration is not successful.

waclawek.jan
Super User
September 3, 2020

> RCC->CFGR |= RCC_CFGR2_ADCPRE12_DIV2;

You meant

RCC->CFGR2 |= RCC_CFGR2_ADCPRE12_DIV2;

JW

jugal
jugalAuthor
Associate II
September 4, 2020

Okay I fixed that line, but the main problem persists.

waclawek.jan
Super User
September 4, 2020

Still looping in calibration, or is it something else?

Read it and check all registered content, including RCC.

PLL is set how?

Try clocking ADC from AHB.

JW

jugal
jugalAuthor
Associate II
September 4, 2020

okay I'll try these. I haven't messed with PLL or any other clock settings, everything is at default settings. Yes, it is still stuck in calibration.