2020-03-16 10:37 AM
I am trying to setup the ADC channel on PA0. I have first set the pin mode to additional function. Then I have followed the examples provided in the reference manual. However, it doesn't turn on anything, either the calibration or the enable. It is not even selecting channel 0. Am I missing something or did something wrong? This is my ADC related code below:
int main()
{
//Set PA0 to Additional function mode
GPIOA->MODER |= (3 << 0) ;
//Ensure ADC is Disabled
if ((ADC1->CR & ADC_CR_ADEN) != 0)
ADC1->CR &= (uint32_t)(~ADC_CR_ADEN);
//Start ADC Calibration
ADC1->CR |= ADC_CR_ADCAL;
//Wait for ADC Calibration to Complete
while ((ADC1->CR & ADC_CR_ADCAL) != 0)
{
}
//Wait for TIM14 is Enabled
//TIM14 is used as a forever interrupt loop in my while function as a clock
while ((TIM14->CR1 & TIM_CR1_CEN) == 0)
{
}
//Select TIM14 by writing 00 in CKMODE
ADC1->CFGR2 &= (~ADC_CFGR2_CKMODE);
//Enable ADC
ADC1->CR |= ADC_CR_ADEN;
while ((ADC1->ISR & ADC_ISR_ADRDY) == 0)
{
}
//Enable Continuous Mode
ADC1->CFGR1 |= ADC_CFGR1_CONT;
//Select Channel 0
ADC1->CHSELR |= ADC_CHSELR_CHSEL0;
//Select Sampling Mode, TODO: MAY NEED TO ADJUST LATER
ADC1->SMPR |= ADC_SMPR_SMP_0 | ADC_SMPR_SMP_1 | ADC_SMPR_SMP_2;
//Disable External Trigger
ADC1->CFGR1 &= (~ADC_CFGR1_EXTEN);
//Wait for ADC is Ready
while ((ADC1->ISR & ADC_ISR_ADRDY) == 0)
{
}
while (1)
{
//Start Conversion
ADC1->CR |= ADC_CR_ADSTART;
//Fetch Converted Data
ADC_Data = ADC1 ->DR;
}
}
Thank you!
Solved! Go to Solution.
2020-03-16 05:15 PM
You need to enable clock for each peripheral in RCC.
See examples at the end of RM.
Also the timer's CR1.CEN need to be set explicitly.
JW
2020-03-16 05:15 PM
You need to enable clock for each peripheral in RCC.
See examples at the end of RM.
Also the timer's CR1.CEN need to be set explicitly.
JW
2020-03-17 06:15 AM
Thank you @Community member ! It is all set now.
I thought I only need to setup ADC Clock Mode at CKMODE in ADC_CFGR2.
I added the following code and it works now.
//Enable ADC Interface Clock
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;
//Enable HSI14 Clock
RCC->CR2 |= RCC_CR2_HSI14ON;
//Wait for HSI14 Clock to be Ready
while ((RCC->CR2 & RCC_CR2_HSI14RDY) == 0);