cancel
Showing results for 
Search instead for 
Did you mean: 

ADC input pin is driving a constant voltage of 2.9V

Mnemocron
Associate III

Using a resistor voltage divider (100k+47k) to break a range of 11V - 14V down to an ADC input range below 5V. For 11V i expect 3.5V, for 14V I expect it to be 4.5V.

(see schematic)

STM8s903 - ADC_IN_2 on PC4 (physical pin 14)

IAR embedded workbench 3.11.2 and STM8 SPL V2.3.0

The ADC works as expected on the selected pin. It converts values in the range of 400 - 600 and even a 0 code if I short circuit the bottom resistor to GND.

However, the voltage at the input pin and voltage divider is wrong. Meaning, the GPIO is probably not floating but driving this point. I have assembled to identical boards and both behave the same, tge first board has 2.5V the second has 2.9V.

This voltage stays at this value even if I change the top voltage from 11 to 14V.

Where it gets really strange is that this is not even always the case. For like 10% of the times, I actually observed voltages 3.5V up to 4.5V at the ADC input pin. Unfortunately, I have not been able to isolate the condition where this happens. It is independent of the STlink (swim) being connected or not.

Also on the schematic, ignore pins 13 / 15, they are disconnected from GND.

Why is this happening? It just looks like such an easy thing but fails to work. What setting am I using wrong.

Schematic:

0693W00000Dlmz8QAB.png 

Code:

/* GPIO Init -----------------------------------------------------------------*/
void GPIO_Config(void)
{
  GPIO_DeInit(GPIOA);
  GPIO_DeInit(GPIOB);
  GPIO_DeInit(GPIOC);
  GPIO_DeInit(GPIOD);
  
  //GPIO_Init (GPIOB, GPIO_PIN_4, GPIO_MODE_OUT_PP_LOW_FAST); // Pin 12 PB4 OUT - CNTRL_1 Signal
  GPIO_Init (GPIOC, GPIO_PIN_6, GPIO_MODE_OUT_PP_LOW_FAST); // Pin 16 PC6 OUT - RGB_LED RGBW Pulse Data
  GPIO_Init (GPIOD, GPIO_PIN_2, GPIO_MODE_OUT_PP_LOW_FAST); // Pin 19 PD2 OUT - DIM Signal (enables SG LED Power)
  GPIO_Init (GPIOA, GPIO_PIN_1, GPIO_MODE_OUT_PP_LOW_FAST); // Pin  5 PA1 OUT - USB_EN
 
  GPIO_Init (GPIOC, GPIO_PIN_4, GPIO_MODE_IN_FL_NO_IT);     // Pin 14 PC4 ADC - 12V measure
  GPIO_Init (GPIOD, GPIO_PIN_3, GPIO_MODE_IN_FL_NO_IT);     // Pin 20 PD3 ADC - 12V measure
  
}

u16 ADC1_ReadVal(ADC1_Channel_TypeDef ch)
{
  u16 v[4] = {0,0,0,0};
  ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS,
             ch,
             ADC1_PRESSEL_FCPU_D18,
             ADC1_EXTTRIG_TIM,
             DISABLE,
             ADC1_ALIGN_RIGHT,
             ADC1_SCHMITTTRIG_ALL,
             DISABLE);
  
  ADC1_Cmd(ENABLE);
  ADC1_StartConversion();
  
  for(u8 i=0; i<4; i++){
    while(ADC1_GetFlagStatus(ADC1_FLAG_EOC) == RESET);
    v[i] = ADC1_GetConversionValue();
    ADC1_ClearFlag(ADC1_FLAG_EOC);
  }
  ADC1_DeInit();
  
  // averaging
  u32 val = 0;
  for(u8 i=0; i<4; i++){
    val += v[i];
  }
  return (u16)(val/4);
}

1 REPLY 1
Paul1
Lead

Maybe:

  • Put a scope on your 5V power to look for dips
  • Check for loose connections, especially on power, especially for perfboard/breadboard.
  • Is the Prog Vdd connection configured for 3V? (Setting in your programmer software/config)

Paul