incorrect joystick sensor data when access it using STM32F4 discovery

109 Views Asked by At

I want to access joystick sensor data using 10bit ADC2 and ADC3 of stm32f4 discovery by divide the two variable resistor data as X and Y. All I want is when when I pull the analog maximally, the Y ADC data shows 0 and when I push the analog, it shows 1024 value. and for X data uses the same analogy. when I use arduino it did great and the data was good. the middle position of the analog gave me value around 512. But it did not go well when I use stm32f4 discovery. the middle value gave me around 810 and value when i push maximally the data get weird and it shows around 769. both X and Y gave me the same behavior.

void ADCinit_SoftTrigger()      //configurations of adc(sensor) reader
{
   GPIO_InitTypeDef GPIO_InitStructure;
   ADC_InitTypeDef  ADC_InitStructure;
   ADC_CommonInitTypeDef ADC_CommonInitStructure;

   RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2 | RCC_APB2Periph_ADC3, ENABLE);
   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

   GPIO_Init(GPIOA, &GPIO_InitStructure);

   ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult;
   ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
   ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
   ADC_CommonInit(&ADC_CommonInitStructure); //

   ADC_InitStructure.ADC_Resolution = ADC_Resolution_10b;
   ADC_InitStructure.ADC_ScanConvMode = DISABLE;
   ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
   ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
   ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
   ADC_InitStructure.ADC_NbrOfConversion = 1;
   ADC_Init(ADC2, &ADC_InitStructure);

   ADC_RegularChannelConfig(ADC2, ADC_Channel_2, 1, ADC_SampleTime_3Cycles);
   ADC_Init(ADC3, &ADC_InitStructure);
   ADC_RegularChannelConfig(ADC3, ADC_Channel_3, 1, ADC_SampleTime_3Cycles);

   ADC_Cmd(ADC2, ENABLE);
   ADC_Cmd(ADC3, ENABLE);
}

uint16_t ADC_read(ADC_TypeDef* ADCX,uint8_t channel,uint8_t ADC_SampleTime)
{
   ADC_RegularChannelConfig(ADCX,channel,1,ADC_SampleTime); 
   ADC_SoftwareStartConv(ADCX);
   while(ADC_GetFlagStatus(ADCX, ADC_FLAG_EOC)==0);
   return ADC_GetConversionValue(ADCX);
}

void read_sensor()
{
  x=ADC_read(ADC2,ADC_Channel_2,ADC_SampleTime_144Cycles); //pin A2
  y=ADC_read(ADC3,ADC_Channel_3,ADC_SampleTime_144Cycles); //pin A3

  sprintf(bufADC,"\nX : %d     Y : %d",x, y );
  USART_puts(bufADC);
} 
0

There are 0 best solutions below