cancel
Showing results for 
Search instead for 
Did you mean: 

Read potentiometer Value ?

bmwael1
Associate II
Posted on December 29, 2012 at 15:19

Hi,

I want to program a potentiometer connected in PC2 as input using

STM32F4

I use this program

GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;

/***********************************************
/* Configure ADC Channel 12 pin as analog input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/***********************************************
/*** ADC1 regular channel 12 configuration ***/
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* Enable ADC1 /***********************************************
ADC_Cmd(ADC1, ENABLE);
* Read potentiometer (voltage) value
*/
int
POTGetValue(
void
)
{
ADC_SoftwareStartConv(ADC1);
while
(ADC_GetSoftwareStartConvStatus(ADC1));
return
ADC_GetConversionValue(ADC1);
}

Did someone know what is missing her because i can't read anything if i write ''

POTGetValue

'' in main Thanks
6 REPLIES 6
Posted on December 29, 2012 at 15:38

Did someone know what is missing here because i can't read anything if i write ''POTGetValue'' in main

 

Like configuring the channel you want to sample?

  /* ADC1 regular channel12 configuration *************************************/

  ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 1, ADC_SampleTime_3Cycles);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
bmwael1
Associate II
Posted on December 29, 2012 at 15:44

I have to add this line to my program ?

bmwael1
Associate II
Posted on December 29, 2012 at 16:24

I add this line to my program and the problem persist 

Reading potentiometer Value gives always 0 

raptorhal2
Lead
Posted on December 29, 2012 at 16:44

Like changing the ADC Enable line from a comment to executable code.

Cheers, Hal

bmwael1
Associate II
Posted on December 29, 2012 at 19:41

Yes it's uncomment just error of paste text in the forum.

bmwael1
Associate II
Posted on December 29, 2012 at 21:28

It works !

This line is missing in my program

/* Enable ADC clock (This seems to be a duplicate statement) */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

Thank you