cancel
Showing results for 
Search instead for 
Did you mean: 

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

jugal
Associate III

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.

1 ACCEPTED SOLUTION

Accepted Solutions

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.

View solution in original post

7 REPLIES 7
MWeid.2
Associate II

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.

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

> RCC->CFGR |= RCC_CFGR2_ADCPRE12_DIV2;

You meant

RCC->CFGR2 |= RCC_CFGR2_ADCPRE12_DIV2;

JW

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

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

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.

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.