Gabotronics Forum :: General Electronics
Welcome Guest   
 Subject :The xMega ADC.. 2013-09-15 03:09:16 
Torby
Joined: 2012-11-01 14:04:19
Posts: 30
Location: Chicago Suburbs

 

xMaster Gabriel,

In an ATTiny, it took less than an hour to made the ADC read a voltage, including discovering I was reading the wrong pin. On xMega, it's been 4 days so far. "Doesn't anybody know how to make this work?" I was poking around with my dear Gabotron, when I realized, "Here's an xMega using it's ADC! xMaster Gabriel must know how to make them work!"

 

So, how do you make these blame things work? If I try it single ended, it always tells me 0xfff (full scale). if I try it double ended, it always tells me -2048 (negative full scale).

It's an xMega192D3

Pin B4 is 0.72v

The AVCC is 3.3v with a .1 uF cap to ground just like all the other vcc pins. 

I've programmed Port B like this:

PORTB.DIR = ~(1<<4);
PORTB.PIN0CTRL = (3<<3);
PORTB.PIN1CTRL = (3<<3);
PORTB.PIN2CTRL = (3<<3);
PORTB.PIN3CTRL = (3<<3);
//PORTB.PIN4CTRL = (3<<3);
PORTB.PIN5CTRL = (3<<3);
PORTB.PIN6CTRL = (3<<3);
PORTB.PIN7CTRL = (3<<3);

And my measure voltage code currently is:

int BatteryVoltage(void)
{
//return 42 ;
ADCA.CTRLB = ADC_CONMODE_bm;
ADCA.REFCTRL = ADC_REFSEL_VCC_gc ; // vcc/1.6 reference, no bandgap, no temperature
ADCA.EVCTRL = 0 ; // no events
ADCA.PRESCALER = ADC_PRESCALER_DIV512_gc ; // Clock/512 = 62.5K, I think
ADCA.CTRLA = ADC_ENABLE_bm ; // Enable the ADC
_delay_us(400); // Wait at least 25 clocks
ADCA.CH0.CTRL = ADC_CH_GAIN_1X_gc | ADC_CH_INPUTMODE_DIFF_gc; // Gain = 1, Differential
ADCA.CH0.MUXCTRL = (12<<3) | 5; // Channel 12 is battery, use pad ground for neg side
ADCA.CH0.INTCTRL = 0 ; // No interrupt
ADCA.CH0.CTRL |= ADC_CH_START_bm; // Start conversion
while (ADCA.INTFLAGS==0) ; // Wait for complete
int v = ADCA.CH0RES ; // * 4000 / 4096 ; // Battery voltage in millivolts
ADCA.INTFLAGS = ADC_CH0IF_bm; // Clear the int flag
ADCA.CTRLA = 0 ; // Disable the ADC
return v ;
}

Man, your editor makes posting code HARD. Perhaps you intend to discourage freaks from posting stupid questions?

IP Logged
 Subject :Re:The xMega ADC.. 2013-09-16 19:01:40 
ganzziani
Joined: 2012-04-02 15:42:39
Posts: 463
Location: Sarasota, FL
 

Hi Torby!

I took a look at the register setup but I can't see what's wrong, Try this:

Change:

//PORTB.PIN4CTRL = (3<<3);
to:
PORTB.PIN4CTRL = 0x07; // Digital Input Disable on pin PB4

IP Logged
Gabriel Anzziani
 Subject :Re:The xMega ADC.. 2013-09-17 12:24:47 
Torby
Joined: 2012-11-01 14:04:19
Posts: 30
Location: Chicago Suburbs
Well, yours is the first suggestion that makes sense, and I've tried many. The ADCA.CH0RES still says -2048.
IP Logged
 Subject :Re:The xMega ADC.. 2013-09-28 02:48:01 
Torby
Joined: 2012-11-01 14:04:19
Posts: 30
Location: Chicago Suburbs

I got it working, but it would go haywire when I changed the mux to sample something else. The guy at Atmel suggested sampling twice when I switched mux.

 

IP Logged
 Subject :Re:The xMega ADC.. 2013-12-06 11:25:07 
Torby
Joined: 2012-11-01 14:04:19
Posts: 30
Location: Chicago Suburbs
Oh yes. In case anybody is reading and trying to make the adc work, I posted my results at http://www.barefootelectronics.com/xMegaADC.aspx
IP Logged
Page # 


Powered by ccBoard