ADC in Atxmega256a3u

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

hi all,

I am using Atxmega256A3u microcontroller, I want to use a light sensor with it.

Can anyone give me the reference code for ADC read for IAR tool chain?

thank you,
regards,
shreyas.

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

There are many app notes on the Xmega on Atmel's site. Check out AVR1300, Xmega adc ( with code ).

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

shreyas,

Here's some working XMEGA ADC code:

void ADC_init() {
PORTA.DIRCLR = 0x03; // configure PORTA pin0,1 as input
ADCA.CTRLA = ADC_ENABLE_bm;	// enable adc
ADCA.CTRLB = ADC_RESOLUTION_12BIT_gc;	 // 12 bit conversion
ADCA.REFCTRL = ADC_REFSEL_AREFB_gc;
ADCA.PRESCALER = ADC_PRESCALER_DIV8_gc;	 // peripheral clk/8
ADCA.CH0.CTRL = ADC_CH_INPUTMODE_SINGLEENDED_gc;	 // single ended
}

uint16_t adc_read(uint8_t adc_pin){
	
ADCA.CH0.MUXCTRL = adc_pin<<3;	
	ADCA.CH0.CTRL |= ADC_CH_START_bm; // start conversion on channel 0
	while(!ADCA.CH0.INTFLAGS);
	return(ADCA.CH0RES);
	
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"Working" example doesn't work for me on an atxmega32a4u. The result from the ADC seems mostly constant no matter what PIN0 is connected to. AREFB is connected to 1.6V. I can connect PIN0 (PA0) to GND or 1.6V yet the ADC is returning constant 1200 or thereabouts. No change between GND and 1.6V. This makes no sense.

I also tried with REFCTRL set to ADC_REFSEL_VCC_gc, no change. Also with every possible prescaler value, no change.

Also I have tried reading every single pin 0-15 and they are all the same.

I've been banging my head for days. Any ideas?

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

OK, I changed the adc_read function to clear the intflag before doing a conversion (ADCA.CH0.INTFLAGS=1). Now I don't get the same value for all channel and things work better.

It seems that PIN0 is unusable as an ADC input. It has weird values, when connected to GND its value is the 1200-1300 or so. When at AREF it's 1700 or so. The other pins work normally with values from 190 (GND) to 4095 (AREF).