cancel
Showing results for 
Search instead for 
Did you mean: 

Why my led is not responding according to my adc converted potentiometer value?

YagciOnur
Associate II

Im working on STM32F401CB microprocessor and im trying to read potentiometer value and according to the value turn on the led.Thats my aim but i think that i cannot read the value of pot because my led is not turning on any condition.I have used pa0 as analog input and pb0 as general purpose output.My ADC converted variable is potentiometer_value which is a 16bit variable because of i make a adc process in 12 bit resolution.I am trying to read adc converted potentiometer value with the read_adc function but my led is not turning on anyway,as a proof of that my if else condition is potentiometer value>0 but it still does not turn on.

 

I have linked my proyeus schematic down below.

My atollic code is this:

#include "stm32f4xx.h"
 
void rcc_config(void) {
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
 
}
 
void gpio_config(void) {
GPIO_InitTypeDef gpio_init_struct;
gpio_init_struct.GPIO_Mode=GPIO_Mode_AN;
gpio_init_struct.GPIO_OType=GPIO_OType_PP;
gpio_init_struct.GPIO_Pin=GPIO_Pin_0;
gpio_init_struct.GPIO_PuPd=GPIO_PuPd_NOPULL;
gpio_init_struct.GPIO_Speed=GPIO_Speed_2MHz;
GPIO_Init(GPIOA,&gpio_init_struct);
 
GPIO_InitTypeDef gpio_init_struct2;
gpio_init_struct2.GPIO_Mode=GPIO_Mode_OUT;
gpio_init_struct2.GPIO_OType=GPIO_OType_PP;
gpio_init_struct2.GPIO_Pin=GPIO_Pin_0;
gpio_init_struct2.GPIO_PuPd=GPIO_PuPd_NOPULL;
gpio_init_struct2.GPIO_Speed=GPIO_Speed_2MHz;
GPIO_Init(GPIOB,&gpio_init_struct2);
 
}
 
void adc_config(void) {
ADC_CommonInitTypeDef adc_common_init_struct;
adc_common_init_struct.ADC_DMAAccessMode=ADC_DMAAccessMode_Disabled;
adc_common_init_struct.ADC_Mode=ADC_Mode_Independent;
adc_common_init_struct.ADC_Prescaler=ADC_Prescaler_Div6;
adc_common_init_struct.ADC_TwoSamplingDelay=ADC_TwoSamplingDelay_20Cycles;
ADC_CommonInit(&adc_common_init_struct);
 
ADC_InitTypeDef adc_init_struct;
adc_init_struct.ADC_Resolution=ADC_Resolution_12b;
adc_init_struct.ADC_DataAlign=ADC_DataAlign_Right;
ADC_Cmd(ADC1,ENABLE);
adc_init_struct.ADC_ContinuousConvMode=ENABLE;
ADC_ContinuousModeCmd(ADC1,ENABLE);
ADC_Init(ADC1,&adc_init_struct);
 
ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_15Cycles);
 
}
 
uint16_t potentiometer_value;
 
uint16_t read_adc() {
ADC_SoftwareStartConv(ADC1);
while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)==RESET);
 
return ADC_GetConversionValue(ADC1);
ADC_ClearFlag(ADC1,ADC_FLAG_EOC);
 
 
}
 
int main(void)
{
rcc_config();
gpio_config();
adc_config();
ADC_ClearFlag(ADC1,ADC_FLAG_EOC);
GPIO_ResetBits(GPIOB,GPIO_Pin_0);
 
 
 
  while (1)
  {
  potentiometer_value=read_adc();
  if (potentiometer_value>0) {
  GPIO_SetBits(GPIOB,GPIO_Pin_0);
  }
  else {
  GPIO_ResetBits(GPIOB,GPIO_Pin_0);
  }
  }
}

 

 

 

Do you have an idea where does this error come from?

 

5 REPLIES 5
SofLit
ST Employee

Hello,

Just wondering why you're still using the old FW library of F4? it's obsolete.

You can migrate to STM32CubeF4. Also you can use STM32CubeMx tool to initialize your peripherals.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

what is the relation between my complaint and that?

It was a note and we are no more supporting this library. So if you are looking for ST support I suggest to use the new library.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
TDK
Guru

Step through the code and verify GPIOB registers are being modified as expected.

Probably they are, and that you're either (a) misinterpreting data or (b) pin isn't connected how you think or (c) pin is being driven by something else.

If you feel a post has answered your question, please click "Accept as Solution".

Are you talking about HAL library?