cancel
Showing results for 
Search instead for 
Did you mean: 

Read adc value from potentiometer

Jelle Beyens
Associate II
Posted on May 02, 2017 at 12:07

Hi

I am trying to read an adc value from a potentiometer with the following code:

         uint32_t readADCServo()

         {

         uint32_t adcValue, adcValueDegrees;

         HAL_ADC_Start(&hadc1);

         if (HAL_ADC_PollForConversion(&hadc1, 1)!=HAL_OK)

         {

           }

         while ((HAL_ADC_GetState(&hadc1) & HAL_ADC_STATE_REG_EOC ) == HAL_ADC_STATE_REG_EOC)

         {

         adcValue = HAL_ADC_GetValue(&hadc1);

         }

         HAL_Delay(1);

         adcValueDegrees = (adcValue * 180 / 4095) -90;

         return adcValueDegrees;

         }

The only value i get in adcValue is 

536872088. No matter the position of the potentiometer.

Does anyone have a soluton for my problem?

#read-adc-potentiometer #adc
1 ACCEPTED SOLUTION

Accepted Solutions
Posted on May 03, 2017 at 15:24

My problem is fixed, I started a new program with just my adc in it and now it is working. Probably a something wrong in my other code. Thanks for the replies!

View solution in original post

9 REPLIES 9
Posted on May 02, 2017 at 12:26

The only value i get in adcValue is 

536872088.

How do you know that?

That number looks like a variable's address, 0x20000498

JW

S.Ma
Principal
Posted on May 02, 2017 at 13:34

Thanks to give details on which STM32 part number is being used for better understanding.

Usually there are examples of how to use the peripheral such as ADCs on Nucleo or Discovery BSP to inspire from.

Posted on May 02, 2017 at 12:31

In the microvision debugger in the watch window, this number is under the tab 'value' for adcValue.

Posted on May 02, 2017 at 12:42

Not sure when exactly you check the contents of

adcValue

in the debugger, but this variable is placed on the stack.

As soon as you leave the '

readADCServo()

' context, it is gone ...
Posted on May 02, 2017 at 14:26

I use the stm32f429I, and already checked the example programs. But they are not very useful

Posted on May 02, 2017 at 18:56

[referring to AvaTar's post]

Make adcValue global (i.e. define it outside the function). Any change in behaviour?

JW

Posted on May 03, 2017 at 11:17

I have done that. You have to make a variable global to be able to read it in the watch window.

Posted on May 03, 2017 at 12:02

Not in the toolchains I know. In proper context, the debugger shows local variables in the watch window as well.

Have you tried to check the ADC result register directly in the debugger ? This might clear the interrupt flag, though.

I don't use Cube, so I do not comment on the code.

However, Jan made a valid point IMHO - the reported value looks suspiciously like an address, rather than a value.

Posted on May 03, 2017 at 15:24

My problem is fixed, I started a new program with just my adc in it and now it is working. Probably a something wrong in my other code. Thanks for the replies!