2020-09-03 11:17 AM
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.
Solved! Go to Solution.
2020-09-05 08:05 AM
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.
2020-09-03 11:27 AM
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.
2020-09-03 11:35 AM
Okay i just did it. The first loop is the problem, the calibration is not successful.
2020-09-03 12:56 PM
> RCC->CFGR |= RCC_CFGR2_ADCPRE12_DIV2;
You meant
RCC->CFGR2 |= RCC_CFGR2_ADCPRE12_DIV2;
JW
2020-09-03 07:45 PM
Okay I fixed that line, but the main problem persists.
2020-09-03 09:30 PM
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
2020-09-04 12:35 AM
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.
2020-09-05 08:05 AM
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.