2014-11-30 02:44 PM
I'm currently working on a university project that involves the analogue to digital converter on the STM32F0 discovery board. I've studied the manual and (as far as I can tell), initialized the registers correctly, but no conversion is happening. Would anyone be able to point out flaws in my code or logic? This is all fairly new to me, so I might be missing something obvious.
int main(int argc, char* argv[]) { // Clock signals RCC->AHBENR |= RCC_AHBENR_GPIOAEN; myADC_Init(); while(running == 1) { uint16_t adcOutput = ADC1->DR; trace_printf(''%d'', adcOutput); } } return 0; } void myADC_Init() { // Set PA2 to analog mode // PA2 will serve as input to the ADC GPIOA->MODER |= GPIO_MODER_MODER2; //Enable the ADC Clock RCC->APB2ENR = RCC_APB2ENR_ADC1EN; //Calibrate the ADC ADC1->CR |= ADC_CR_ADCAL; //Wait until calibration is complete while(ADC1->CR & ADC_CR_ADCAL); //Turn on the ADC ADC1->CR |= ADC_CR_ADEN; //Wait until the ADC is ready while(!(ADC1->ISR & ADC_ISR_ADRDY)); // Ensure that the ready flag has been triggered in the ISR //ADC1->ISR = ADC_ISR_ADRDY; // ADC continuous mode (1 in bit 13) ADC1->CFGR1 = ADC_CFGR1_CONT; ADC1->CFGR1 = ADC_CFGR1_OVRMOD; // Channel 2 selected for conversion ADC1->CHSELR = ADC_CHSELR_CHSEL2; // Sampling time is 239.5 ADC clock cycles ADC1->SMPR |= 0x7; ADC1->CR |= ADC_CR_ADSTART; } I've connected a 3.3 V square wave to PA2 of the board, and confirmed this wave with an oscilloscope. My understanding is that, because the ADC is set to obtain input from PA2, it should automatically convert data connected to that pin? I've been printing the ADC data register values, and I only ever get 0. Obviously something is going wrong here. Any help is appreciated!2014-11-30 04:39 PM
[DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/ADC%20Help%20Needed%203359&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&TopicsView=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/AllItems.aspx¤tviews=3]Duplicate Thread