cancel
Showing results for 
Search instead for 
Did you mean: 

ADC Help Needed

eric23
Associate
Posted on November 30, 2014 at 23:45

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!

#adc-help
3 REPLIES 3
Posted on December 01, 2014 at 01:37

Perhaps you're not ready for register level coding, try looking at the examples posted on the forum, and using the standard peripheral library.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
eric23
Associate
Posted on December 01, 2014 at 04:10

Well it wouldn't matter if I'm not ready. I've been assigned a project and I plan on completing it. As for whether I'm ready, of course I am. Why wouldn't I be? I understand the material just fine. If all programmers gave up because of small bugs in their code on the first attempt, nobody would learn anything.

Anyways, as per the project specifications, I can't use the library. Only registers are allowed.

I did get it working. The board I was using wasn't working properly. I switched to another board in the lab and the ADC works fine.

Posted on December 01, 2014 at 18:54

If all programmers gave up because of small bugs in their code on the first attempt, nobody would learn anything.

Nobody asked you to give up, just suggested looking at the problem from a different perspective, allowing you to compare how the register configuration differs between the two. It would provide an expeditious way to sanity check the hardware, regardless of whether the final project requirements are to deliver register level C or assembler.

If all programmers had to ''ask the internet'' they'd all be pretty worthless.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..