2011-06-09 11:32 AM
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
2011-06-09 06:27 PM
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.2011-06-09 09:37 PM
Thank you, yes I spotted that eventually.
Typo on my schematic.
Regards,
Mark