init adc of stm32f103c8 blue pill w/o hal

Thread Starter


Joined Jan 22, 2019
after setting up and starting the adc it would give me the correct values but would randomly jump close to zero frequently.

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.

RCC->CFGR |= RCC_CFGR_ADCPRE_DIV6; //sets ADC clock freguency to 12mhz accoring to datasheet
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN | RCC_APB2ENR_AFIOEN; //enable ADC and alt fuction pins
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

ADC1->CR2 |= ADC_CR2_CAL; //Enable calibration
while ((ADC1->CR2 >> 2) & 0x1UL); //Wait until calibration completed

ADC1->CR2 |= ADC_CR2_ADON; //Set CR2_ADON again to turn on ADC and start converting
for(int y = 0; y < 1000000; y++){}

Thread Starter


Joined Jan 22, 2019
What IDE platform are you using to develop-test-debug your code?
I am using keil. And I have used the debugging feature to see if every register was properly set and they are. Right now I'm wondering if the order in which I did it was incorrect or something?