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

34 views Asked by At

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.

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



my proteus schematic

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

i think that i cannot read the value of pot because my led is not turning on any condition

0

There are 0 answers