cancel
Showing results for 
Search instead for 
Did you mean: 

single channel ADC question, I am doing something really stupid !

markaren1
Associate II
Posted on June 09, 2011 at 20:32

Hi All,

All I am trying to do is convert a single channel of ADC. No interrupts, noDMA, this should be really simple.

The results I am getting are not consistent, so I must be doing somethingdaft.

I have an STM32 discovery and have attached VDD/2 to PC0.

init:

RCC->APB2ENR |=RCC_APB2ENR_ADC1EN;             // en clocksADC

RCC->CFGR |=RCC_CFGR_ADCPRE;                   //set ADC prescaler to div 8 (page 55/519)

GPIOC->CRL &=0xFFFFFFF0;                       //Configure PC0 as analog input. CNF1,CNF0 = 0, Mode = 00

ADC1->SMPR1 =0x07FFFFFF;                       //293 cycles per sample

ADC1->SMPR2 = 0xCFFFFFFF;

ADC1->CR2 =0x008E0001;                         //SW start, ADC enabled, Vref and temp enabled

ADC1->SQR1 =0x0000;                            //set sequence length to one, all input pointers to CH0

ADC1->SQR2 = 0x0000;

ADC1->SQR3 =0x0000;                            //presumably CH1

ADC1->CR1 =0x0000;                                     //no WD,no int, single mode

main code is run once ever second

        val =ADC1->DR;                         //read the last conversion

        ADC1->CR2 |=ADC_CR2_ADON;              // start the nextconversion

Not exactly rocket science

It is not obvious how to select the ADC input in single channel mode, so Iam assuming that SQ1[4:0] is the appropriate mechanism since changing it appearsto affect results.

Any thoughts please ?

Thanks,

Mark

2 REPLIES 2
hfs99
Associate II
Posted on June 10, 2011 at 03:27

I'm looking at a (high density) STM32F103xC,D, or E datasheet. Too lazy to check Discovery hardware, so please forgive / ignore if port to ADC channel mapping is not the same on your board.

On this datasheet, PC0 is ADC123_10, or ADC hannel 10. So you'd need to write 0x000a to ADC1->SQR3.

markaren1
Associate II
Posted on June 10, 2011 at 06:37

Thank you, yes I spotted that eventually.

Typo on my schematic.

Regards,

Mark