Why my microprocessor stm32f401cb always reads the first adc channel PA0

20 Views Asked by At

I'm writing a code and my aim is reading both potentiometers which are connected to pins pa0 and pa1 and according to the results,turning the leds on and off.I have used ADC1 and in channels pa0 and pa1 and i have used pb0 and pb1 as outputs.Normally,the function which is named as read_adc should read the ports in order of pa0 and pa1 and move the values to the variable which i have defined as value[].Pa0's adc value should be moved to value[0] and pa1's adc value should be moved value[1] but according to my detections,my program does not read pin pa1.I understood this in this way,i m controlling leds as you see with value[0] and value[1].When i change the value of the second potentiometer(pa1),the led connected to pb1 does not get affected by this.But when i change the value of the first potentiometer(pa0) the led connected to pb1 gets affected by this.Here is the proof. firstly first potentiometer is lower than 2000

first potentiometer is greater than 2000

also you can see in the first schematic second potentiometer is always bigger than 2000 but pb1 does not get affected by this

So i cannot read pa1 as youcan understand.

My code is like this


#include "stm32f4xx.h"
void gpio_config(void) {
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
    GPIO_InitTypeDef gpioa_init_struct;
    gpioa_init_struct.GPIO_Mode=GPIO_Mode_AN;
    gpioa_init_struct.GPIO_OType=GPIO_OType_PP;
    gpioa_init_struct.GPIO_Pin=GPIO_Pin_0 | GPIO_Pin_1;
    gpioa_init_struct.GPIO_PuPd=GPIO_PuPd_NOPULL;
    gpioa_init_struct.GPIO_Speed=GPIO_Speed_100MHz;
    GPIO_Init(GPIOA,&gpioa_init_struct);

    GPIO_InitTypeDef gpiob_init_struct;
    gpiob_init_struct.GPIO_Mode=GPIO_Mode_OUT;
    gpiob_init_struct.GPIO_OType=GPIO_OType_PP;
    gpiob_init_struct.GPIO_Pin=GPIO_Pin_0 | GPIO_Pin_1;
    gpiob_init_struct.GPIO_PuPd=GPIO_PuPd_NOPULL;
    gpiob_init_struct.GPIO_Speed=GPIO_Speed_100MHz;
    GPIO_Init(GPIOB,&gpiob_init_struct);
}
void adc_config(void) {
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);

    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_Div4;
    adc_common_init_struct.ADC_TwoSamplingDelay=ADC_TwoSamplingDelay_5Cycles;
    ADC_CommonInit(&adc_common_init_struct);

    ADC_InitTypeDef adc_init_struct;
    adc_init_struct.ADC_ContinuousConvMode=ENABLE;
    adc_init_struct.ADC_DataAlign=ADC_DataAlign_Right;
    adc_init_struct.ADC_ExternalTrigConv=ADC_ExternalTrigConvEdge_None;
    adc_init_struct.ADC_NbrOfConversion=2;
    adc_init_struct.ADC_Resolution=ADC_Resolution_12b;
    adc_init_struct.ADC_ScanConvMode=ENABLE;
    ADC_Init(ADC1,&adc_init_struct);
    ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_3Cycles);
    ADC_RegularChannelConfig(ADC1,ADC_Channel_1,2,ADC_SampleTime_3Cycles);
}
uint16_t value[2];
unsigned int i=0;
uint16_t read_adc() {
    ADC_ContinuousModeCmd(ADC1,ENABLE);
    ADC_Cmd(ADC1,ENABLE);
    ADC_SoftwareStartConv(ADC1);
    if(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)==SET) {

        value[i]=ADC_GetConversionValue(ADC1);
        i++;
        if (i==2) {
                if(value[0]>1000) {
                        GPIO_SetBits(GPIOB,GPIO_Pin_0);
                    }
                else {
                        GPIO_ResetBits(GPIOB,GPIO_Pin_0);
                    }
                if(value[1]>2000) {
                        GPIO_SetBits(GPIOB,GPIO_Pin_1);
                    }
                else {
                        GPIO_ResetBits(GPIOB,GPIO_Pin_1);
                    }


                i=0;
            }
    }


}


int main(void)
{
    gpio_config();
    adc_config();
    GPIO_ResetBits(GPIOB,GPIO_Pin_0 | GPIO_Pin_1);

  while (1)
  {
      read_adc();



  }
}

proteus schematics are up

Why this problem comes you think?

also you can see in the first schematic second potentiometer is always bigger than 2000 but pb1 does not get affected by this

So i cannot read pa1 as youcan understand.

0

There are 0 best solutions below