Reading analog values from potentiometer through ADC in STM32F4 Discovery Board.

Thread Starter

BurhanBhos

Joined Apr 4, 2019
14
Hi, I am trying to read the analog values from potentiometer which is connected ADC channel 0, namely pin PA0 in my development board. I have attached the related part of the code. I got all of these registers' values from reference manual. I watched many videos and read articles and wrote this code, but there is an issue. The values that I get (I display them on LCD) are random and don't depend on the rotation of the potentiometer. PA0 behaves like if it is not connected to the potentiometer. Although I checked the reference manual and the datasheet, I couldn't find the problem. Please, can you show me what is problematic with my code?
C:
GPIOA->MODER |= GPIO_MODER_MODE0_Msk;//Set as PA0 as Analog Mode

RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;//Enable the ADC clock

ADC1->SQR1 =0x00000000;//1 Convertion
ADC1->SQR2 =0x00000000;
ADC1->SQR3 =0x00000000;//0'th channel will be converted first

ADC1->SMPR2 =0x00000005;//sampling time - 112 cycles
ADC1->CR2 |= ADC_CR2_CONT_Msk;//Enable continious mode
ADC1->CR1 &=~(ADC_CR1_SCAN_Msk);//Disable Scan mode
ADC1->CR2 |= ADC_CR2_ADON;//Enable ADC

while(1)
{
   ADC1->CR2 |= ADC_CR2_SWSTART_Msk;//Start convertion
   LCDSendAnInteger((ADC1->DR),7);//Display value in LCD
  notExactTimeDelay(1000000);
   LCDClearDisplay();
}
 

MrChips

Joined Oct 2, 2009
20,215
ARM MCU such as STM32F4 are much more complex than simpler Atmel or Microchip PIC.
It is very unlikely that you will get the ADC configured to run properly by setting individual register bits.
You would be best to start with a working example.

What software development platform are you using?
 

shteii01

Joined Feb 19, 2010
4,647
A pdf says that PA0 is a USER/Wake-Up Push Button by default. In the interests of doing as little as possible, just pick a different ADC pin. For example PA1 is ADC123_IN1 and is free so you can use it.
 
Top