cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f103 adc connected to potentiometer

aman
Associate II
Posted on May 10, 2013 at 09:00

i have connected a potentiometer(via stm32f103 3.3 v) to stm32f103 adc channel 1 (ADC1_IN1), with continuous mode, sampling 1.5. i am getting the values in DR register, and want to change the values during run time by changing resistor values through potentiometer but somehow values are not swinging that much as wanted. i am getting all flags in SR register. am i doing something wrong in implementing this... plz help me

#stm32 #board #stm32l #discovery #adc #adc #discovery #board
9 REPLIES 9
Posted on May 10, 2013 at 13:07

Make sure the ADC clock is less than 14 MHz, use a sample time significantly greater than 1.5, try 28.5, the range (right aligned) should be 0 .. 4095, for a single turn pot attached to the rails, measuring the center.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
aman
Associate II
Posted on May 10, 2013 at 13:48

thanks clive1 for a reply..

i had done the same thing......

have look at my code...

RCC->APB2ENR=0x200;

 

RCC->CFGR=0x0000;

GPIOA->CRL=0x00;

 

ADC1->CR2=0x01;

ADC1->SMPR2=0x28;

//ADC1->CR2=0x400000;

ADC1->SQR1=0x000000;

ADC1->CR1=0x20;

//ADC1->CR2 =0x02;

//ADC1->CR2 |=0x400000;

//while(ADC1->CR2 & 0x5)

ADC1->CR2 |=0x04;

ADC1->CR2 |=0x02;

//while(ADC1->CR2 & 0x00)

//{}

ADC1->CR2 |=0x01;

while();

value comes in b/w 3C4-3D6 in DR... not changing to higher or lower value when changing the resistor value

 

Posted on May 10, 2013 at 18:13

have look at my code...

I have a policy of not supporting register level code.

You might want to make sure you enable the GPIOA clock, and suitably configure the pin.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on May 10, 2013 at 18:24

#include ''stm32F10x.h''
/******************************************************************************/
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
/* Enable ADC1 and GPIOA clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA, ENABLE);
/* Configure PA.01 (ADC Channel1) as analog input -------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE; // Single Channel
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; // Scan on Demand
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel1 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_55Cycles5);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(1)
{
if (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == SET)
{
int adc;
adc = ADC_GetConversionValue(ADC1);
printf(''ADC %d
'',adc);
/* Probably overkill */
ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
} // sourcer32@gmail.com
while(1); /* does not exit - kind of important */
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
aman
Associate II
Posted on May 15, 2013 at 09:12

thanks for relpy clive1, is it not good to code in register level?

the code you had written is more like the same i posted except you have used calibration.

Does the calibration affecting my reading? How can i use calibration in loop like you did into register like coding.

aman
Associate II
Posted on May 15, 2013 at 09:18

Andrew Neil
Evangelist III
Posted on May 15, 2013 at 10:36

''is it not good to code in register level?''

If you're going to program at the register level, then you  must have a solid understanding of the registers and their functions and how to sonfigure them.

If you want people to review your code for free, don't expect them to have to wade through the arcane details of register-level minutiae!
aman
Associate II
Posted on May 15, 2013 at 11:16

thanks neil , i am new to this stuff and now have understood...

but problem remains the same

Posted on May 15, 2013 at 13:42

is it not good to code in register level?

It has it's place, but requires a much more thorough understanding of how the silicon behaves than the average beginner is likely to possess. If you want to waste hours on minutia and nuances, then have at it, but you own all that work.

the code you had written is more like the same i posted except you have used calibration. Does the calibration affecting my reading?

The F1 requires calibration, as I recall, your very quick conversion will also cause the result to vary quite a lot. A thorough review of the reference manual would be enlightening.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..