Why my microprocessor cannot read analog potentiometer value

54 views Asked by At

I am writing a stm32f401cb code which will control led according to the analog to digital converted potentiometer value.My pa2 has been chosen as an analog input.My pb2 has been chosen as an generel purpose output which is connected to the led My proteus schematic

I think my adc does not convert the analog value to digital because my led is not turning anyway.I have tried every resistance value but same.Why it can be?Normally my led should turn on when pot value is greater than 4300 My led is not turning on anyway

#include "stm32f4xx.h"
uint16_t value=0;

void gpioa_config(void) {
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
    GPIO_InitTypeDef gpioa_init_struct;
    gpioa_init_struct.GPIO_Pin=GPIO_Pin_2;
    gpioa_init_struct.GPIO_Mode=GPIO_Mode_AN;
    gpioa_init_struct.GPIO_OType=GPIO_OType_PP;
    gpioa_init_struct.GPIO_PuPd=GPIO_PuPd_NOPULL;
    gpioa_init_struct.GPIO_Speed=GPIO_Speed_25MHz;
    GPIO_Init(GPIOA,&gpioa_init_struct);
}

void gpiob_config(void) {
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
    GPIO_InitTypeDef gpiob_init_struct;
    gpiob_init_struct.GPIO_Pin=GPIO_Pin_2;
    gpiob_init_struct.GPIO_Mode=GPIO_Mode_OUT;
    gpiob_init_struct.GPIO_OType=GPIO_OType_PP;
    gpiob_init_struct.GPIO_PuPd=GPIO_PuPd_NOPULL;
    gpiob_init_struct.GPIO_Speed=GPIO_Speed_25MHz;
    GPIO_Init(GPIOB,&gpiob_init_struct);
}

void adc_config() {
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
    ADC_InitTypeDef adc_init_struct;
    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_Div8;
    ADC_CommonInit(&adc_common_init_struct);

    adc_init_struct.ADC_Resolution=ADC_Resolution_12b;
    ADC_Init(ADC1,&adc_init_struct);
    ADC_Cmd(ADC1,ENABLE);
}
uint16_t adc_read() {
    ADC_RegularChannelConfig(ADC1,ADC_Channel_2,1,ADC_SampleTime_480Cycles);
    ADC_SoftwareStartConv(ADC1);

    while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)==RESET) {

    }

    return ADC_GetConversionValue(ADC1);
}

int main(void)
{
    gpioa_config();
    gpiob_config();
    adc_config();
    GPIO_ResetBits(GPIOB,GPIO_Pin_2);

  while (1)
  {
      value=adc_read();
      if (value>4300) {
          GPIO_SetBits(GPIOB,GPIO_Pin_2);
      }
      else {
          GPIO_ResetBits(GPIOB,GPIO_Pin_2);
      }
  }
}
1

There are 1 answers

0
Martin Brown On

I think your problem stems from the fact that your ADC is 12 bits 0-4096 and the default hardware scaling is to the low end of the word. Your potentiometer can never reach a voltage that will return 4300 (which is above full scale)!

Try something more mid-range like 2048 or 1024 and you should get correct behaviour.

If you don't then an easy way to check the ADC is working is delay by the amount that you last read from the ADC and toggle an output bit with a small loudpspeaker or crystal earpiece on it. Then you can hear the effect of altering the position of the pot. Lighting an LED or not is a bit too binary. Mark space ratio is the other way of signalling (ie varying the LED's apparent brightness in response to the potentiometer position).