ADC doesn't read 0V

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm trying to read ADC0 from an xmega32A4U. Here my code:

 

ADC_Pointer = &ADCA;
ADC_ConvMode_and_Resolution_Config(ADC_Pointer, ADC_ConvMode_Signed, ADC_RESOLUTION_12BIT_gc);
ADC_Ch_InputMode_and_Gain_Config(&ADC_Pointer->CH0, ADC_CH_INPUTMODE_SINGLEENDED_gc, ADC_CH_GAIN_1X_gc);
ADC_Ch_InputMux_Config(&ADC_Pointer->CH0, ADC_CH_MUXPOS_PIN0_gc, ADC_CH_MUXNEG_PIN0_gc);
ADC_Prescaler_Config(ADC_Pointer, ADC_PRESCALER_DIV256_gc);
ADC_Reference_Config(ADC_Pointer, ADC_REFSEL_INTVCC_gc);
ADC_Ch_Interrupts_Config(&ADC_Pointer->CH0, ADC_CH_INTMODE_COMPLETE_gc, ADC_CH_INTLVL_LO_gc);
ADC_Pointer->CTRLB &= ADC_IMPMODE_bm;
ADC_Pointer->CTRLB |= ADC_CURRLIMIT_gm;
ADC_CalibrationValues_Load(ADC_Pointer);
ADC_Enable(ADC_Pointer);
ADC_Wait_32MHz(ADC_Pointer);

ISR(ADCA_CH0_vect) 
{
	uint16_t adc = ADC_Pointer->CH0RES;
	// store in my var
}

 

The MCU reads quite correctly the input voltages but if I short the pin to the ground I still read about 179-180 counts!

Is there something wrong in my configuration?

 

I know the older xmega (without -U suffix) suffered from a lot of ADC problems and I had to use a differential mode with the negative pin connected to GND. I hope now that is not necessary anymore and the mistake is mine!

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

They call that a feature, but it sounds more like a bug to me. See my page at http://barefootelectronics.com/x...

 

Quote:

The first surprise is I've selected Signed Mode in the CTRLB register. Why? Well, if you select Unsigned Mode, a zero input gives you a large positive number. I've heard this explained as a feature so you can measure negative voltages in Unsigned Mode. In my mind this is pretty bogus, if I was expecting a negative number, I'd have used Signed Mode. Anyhow, after much head scratching and unexpected bogus results, one of the freaks at avrfreaks.net suggested I try signed mode, and it gives me much more reasonable results.

274,207,281-1 The largest known Mersenne Prime

Measure twice, cry, go back to the hardware store

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Perhaps I understood it wrong, but I set signed mode. Anyway, I tried also unsigned mode but it doesn't seem a very different scenario.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The Xmega ADC is off-set from 0.

 

This makes it easier, depending upon the input circuitry, to measure zero crossings.

 

The exact off-set can vary a bit from chip to chip, but you should be able to measure it as part of your start-up routine, and then subtract it out as a DC bias from the readings.

 

JC