cancel
Showing results for 
Search instead for 
Did you mean: 

Hi, I am trying to obtain values from potentiomater which is connected to PA0 (ADC channel 0), through ADC.

BMoha.19.45
Associate II

However, the values that I obtain (I show them on LCD display) are not only inconsistent, but also the potentiometer's rotation doesn't effect the value? I have searched a lot and tried different ways but I couldn't find the problem in my code. Please, can you help me where I did wrong? I have attached the related part of the code with comments.

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();
}

18 REPLIES 18
S.Ma
Principal

Just for sanity, remove your potentiometer and disconnect PA0 from your HW board.

Then, activate the pull-up resistor and check the ADC converted value.

Rince and repeat with only pull-down

Rince and repeat with both pull-up and down.

If all above returns values from ADC which looks like it is indeed ok, then the chosen terminals of your potentiometer might be the wrong ones (check with ohmmeter).

I will surely try.

I did, but nothing different. Again values change randomly without affected. Am I choose the channel 0 correctly in the code?

PA0 is connected to user button and is pulled down by a series of resistors.

Check also in your code that this pin is not configurated from somewhere else eg as SYS_WKUP

Thank you for your answer. The interesting thing is that when I change the above code for PinA1 the same happens :(

S.Ma
Principal

Review the board schematics which we don't have.

In debug mode, look at the HW peripheral registers carefully and find out what's wrong manually playing the registers.

Thank you very much. I did it and noticed one interesting thing.

ADC1->CR2 |= ADC_CR2_SWSTART_Msk; //Start convertion

By this line, I start the conversion. However, in order to start conversion the SWSTART bit of this register should be one. By this line of code I want to make it one. But, interestingly, it does nothing. It doesn't change the content of the register. Before this line of code the register value was 0x00000003 and after it should be 0x40000003, but it stays the same. I don't know why this happens? :sad_but_relieved_face:

This bit clears by HW as soon as the conversion starts.

It is impossible to observe this transition (or to record it) with STLink onboard debugger.

By the way i don't see if GPIOA's. clock is enabled.

Thank you very very much. I don't know how I forgot to enable GPIOA clock. That solved the whole problem.