stm32f103c8 ADC1 keeps briefly giving random values. perhaps i initialized it incorrectly?

below is a snipbit of my code that is initializing the ADC1. i am aware that i went over board on the for loops delays but i was trying to see if the issue was being caused by the lack of time needed to stabilize or calibrate. if anyone has a better idea on how i should fix this, please let me know. I'm a little new with this microcontroller and coding in general.

void GPIOInit(){

RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN; // enable port a clock

GPIOA->CRL = 0xc8bbbbbb; //sets all portA to alt push pull except A6 for MISO and set A7 to input for ADC

GPIOB->CRL = 0x444844CC; //set B1 and B0 to input pull down pull up
/*GPIOB->CRL |= GPIO_CRL_CNF0_1 | GPIO_CRL_CNF1_1;
GPIOB->CRL &= ~(GPIO_CRL_CNF0_0);
GPIOB->CRL &= ~(GPIO_CRL_CNF1_0);*/
}

void ADCInit (){

RCC->CFGR |= RCC_CFGR_ADCPRE_DIV6; //sets ADC clock frequency to 12mhz according to datasheet
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN | RCC_APB2ENR_AFIOEN; //enable ADC and alt function pins
RCC->AHBENR |= RCC_AHBENR_DMA1EN; //enable DMA
ADC1->SMPR2 |= ADC_SMPR2_SMP7_0 | ADC_SMPR2_SMP7_1 | ADC_SMPR2_SMP7_2; // setting sample rates to max
ADC1->SMPR2 |= ADC_SMPR2_SMP8_0 | ADC_SMPR2_SMP8_1 | ADC_SMPR2_SMP8_2; // setting sample rates to max
ADC1->SMPR2 |= ADC_SMPR2_SMP9_0 | ADC_SMPR2_SMP9_1 | ADC_SMPR2_SMP9_2; // setting sample rates to max
ADC1->SQR1 |= 3 << 20; //turning sequence mode on
ADC1->SQR3 |= 9 << 0; // setting channel 9 as the first in sequence
ADC1->SQR3 |= 8 << 5; // setting channel 8 as the second in sequence
ADC1->SQR3 |= 7 << 10; // setting channel 7 as the third in sequence

ADC1->CR1 |= ADC_CR1_SCAN; // setting ADC to scan mode
ADC1->CR2 |= ADC_CR2_DMA; // turning on DMa for ADC

DMA1_Channel1 ->CPAR = (uint32_t)(&(ADC1->DR)); // passing data register of ADC to DMA
DMA1_Channel1 ->CMAR = (uint32_t)currentSen; // passing array to DMA
DMA1_Channel1 ->CNDTR = 3; // set DMa to count 3 times
DMA1_Channel1 ->CCR |= DMA_CCR1_CIRC | DMA_CCR1_MINC | DMA_CCR1_MSIZE_0 | DMA_CCR1_PSIZE_0; // DMA settings
DMA1_Channel1 ->CCR |= DMA_CCR1_EN; // enable DMA

ADC1->CR2 |= ADC_CR2_CONT; // turn on ADC continuous mode
ADC1->CR2 |= ADC_CR2_ADON; // turn on ADC

for(int y = 0; y < 1000000; y++){}

ADC1->CR2 |= ADC_CR2_ADON; // turn on ADC again

for(int y = 0; y < 1000000; y++){} // wait for ADC to stabilize

ADC1 ->CR2 |= ADC_CR2_CAL; //calibrate ADC

for(int y = 0; y < 1000000; y++){} // wait for ADC to calibrate

}

More questions in this forum