beginer question

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

Hi,

I'm complitly new to the X series, and there is manything I don't understant like. How / Where I define the AD Input0 will be on a pin?.

Here is my code

int main(void)
{
	int result0;
	ADCInit();
	
    while(1)
    {
	result0 = (ADCA.CH0RES); 

    }	
}


static void ADCInit(void) 
{ 
   ADCA.CTRLA = 0x01;
   ADCA.PRESCALER = ADC_PRESCALER_DIV64_gc; 
   ADCA.REFCTRL = ADC_REFSEL_VCC_gc; 
   ADCA.EVCTRL = ADC_SWEEP_0123_gc ; 

   ADCA.CH0.CTRL = ADC_CH_INPUTMODE_SINGLEENDED_gc; 
   ADCA.CH0.MUXCTRL = ADC_CH_MUXPOS_PIN0_gc; 

   ADCA.CTRLB |= ADC_FREERUN_bp;

}

Thanks for your time
Sylvain Bissonnette

Yours truly,
Sylvain Bissonnette
www.microsyl.com

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

You might want to start with the following application note available on the Atmel website:

AVR1300 - Using the XMEGA ADC

If you have AVR Studio 5.0 installed, there are example projects for most of the peripherals for all the Starter/Evaluation kits.

Gamu The Killer Narwhal
Portland, OR, US
_________________
Atmel Studio 6.2
Windows 8.1 Pro
Xplained boards mostly

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

To answer this specific question, for many of the special purpose pins, it's pre-determined which pin or set of pins can be used. For the ADC on most XMEGAs, you can technically use any of the 8 pins associated with that port (port A in your example).

In your code, this is controlled by this line:
ADCA.CH0.MUXCTRL = ADC_CH_MUXPOS_PIN0_gc;

This selects pin PA0 as the input.

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

So I have ADCA and ADCB who give me a total of 16 AD input from PortA & B

Yours truly,
Sylvain Bissonnette
www.microsyl.com

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

There are 2 Xmega datasheets you need to get: Manual x ( x will be whatever letter is in uC you use ) and Preliminary for whatever letter-series you're using . Then see "pinout and pin function" section in Preliminary .

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

Ok Now I find the pinout, After 1 week I can't make my small code to work and I really need your help. I just try to read a analog value. Any help will be welcome

#include 

void ADCInit(void);

int main(void)
{
    int result0;
    ADCInit();
	
    while(1)
    {
	result0 = (ADCA.CH0RES); 
    }	
}


void ADCInit(void) 
{ 
   ADCA.CTRLA = 0x01;
   ADCA.PRESCALER = ADC_PRESCALER_DIV64_gc; 
   ADCA.REFCTRL = ADC_REFSEL_VCC_gc; 
   ADCA.EVCTRL = ADC_SWEEP_0123_gc ; 

   ADCA.CH0.CTRL = ADC_CH_INPUTMODE_SINGLEENDED_gc; 
   ADCA.CH0.MUXCTRL = ADC_CH_MUXPOS_PIN0_gc; 

   ADCA.CTRLB |= ADC_FREERUN_bp;
}

Yours truly,
Sylvain Bissonnette
www.microsyl.com

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

Quote:
I just try to read a analog value.
Then you should sweep channel 0 only, not multiple channels, until you know how to work the adc better . Make it as SIMPLE as possible . I haven't used the ( busted ) ADC system, but you should find an example from the app notes, maybe 1505 .

Tell us how you hooked up ALL ADC PINs and voltages on them . Use a pot on adc_input for now .

How do you know it's not working, you don't have ANY debug code in there ?

You really need to edit your code posts and use code tags, esp. since you have 400+ posts and should know better ( it's not easy reading the 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

Last Edited: Fri. Dec 16, 2011 - 02:02 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Indeed, as indianajones11 notes, start with ADCA.EVCTRL set to 0 to keep things simple (and because that's the only channel you've configured). Then, feed an analog voltage source somewhere between 0 and (VCC / 1.6) volts to pin PA0. Presumably you're actually doing something with the value of "result0" so that you can see what the value is?

If it still seems like nothing is happening, try setting "PR.PRPA = 0" to ensure that the ADC is not disabled for power reduction purposes.

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

here is simple example:
ATxmega16A4, 32 MHz


unsigned int ad0, ad1, ad2, ad3;
unsigned int adc0, adc1, adc2, adc3, adc0_cnt, adc_t;
bit  adc_rdy;
..
..
..


void adc_init()
{
        //ADC_CalibrationValues_Load(&ADCA);      
        // not used        

        // Set up ADC A to have signed conversion mode and 12 bit resolution. 
        ADCA.CTRLB = 0; // convmode = 0, Freerun = 0, Resolution = 12 bit
        
        // Set sample rate. = 5 = div128, 6 = div256, 7 = div512 
        ADCA.PRESCALER = 0x7;
        
        // Set reference voltage on ADC A to be 1.0 V.
        ADCA.REFCTRL = 0x10;    // ref = vcc/1.6

        // Setup channel 0, 1, 2 and 3 (single ended gain = 1) 
        ADCA.CH0.CTRL = ADC_CH_GAIN_1X_gc | ADC_CH_INPUTMODE_SINGLEENDED_gc;         
        ADCA.CH1.CTRL = ADC_CH_GAIN_1X_gc | ADC_CH_INPUTMODE_SINGLEENDED_gc;         
        ADCA.CH2.CTRL = ADC_CH_GAIN_1X_gc | ADC_CH_INPUTMODE_SINGLEENDED_gc;         
        ADCA.CH3.CTRL = ADC_CH_GAIN_1X_gc | ADC_CH_INPUTMODE_SINGLEENDED_gc;
                

        // Set input to the channels in ADC A to be PIN ADC0, ADC1, ADC2, ADC3 
        ADCA.CH0.MUXCTRL =0;                                  
        ADCA.CH1.MUXCTRL =0x8;                                  
        ADCA.CH2.MUXCTRL =0x10;                                  
        ADCA.CH3.MUXCTRL =0x18;                                  
        
        // Setup sweep of all four virtual channels. 
        ADCA.EVCTRL = ADC_SWEEP_gm| ADC_EVACT0_bm; // = 0xC1; 
        
        ADCA.CH3.INTCTRL = ADC_CH_INTLVL1_bm; 

        // Enable PMIC interrupt level med or low. 
        PMIC.CTRL |= PMIC_MEDLVLEX_bm; //PMIC_LOLVLEX_bm;

        // Enable ADC A 
        ADCA.CTRLA |= ADC_ENABLE_bm;
        // Enable free running mode. 
        ADCA.CTRLB |= ADC_FREERUN_bm;

}


// ADC interrupt service routine
interrupt [ADCA_CH3_vect] void adc_isr(void)
{

        adc0_cnt++;
        adc0+= ADCA.CH0RES;   
        adc1+= ADCA.CH1RES;   
        adc2+= ADCA.CH2RES;   
        adc3+= ADCA.CH3RES;   
        if(adc0_cnt==8)
        {
                ad0 = adc0;
                ad1 = adc1;
                ad2 = adc2;
                ad3 = adc3;
                adc0 = adc1 = adc2 = adc3 = 0;
                adc0_cnt = 0;
                adc_t++;
        }                  
                        
        adc_rdy = 1; 
        
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Ana57

Thanks a lot for your code, I will only be able to try it next week, I will keep you inform of my progress

Yours truly,
Sylvain Bissonnette
www.microsyl.com

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

Note: ADC is different in models XmegaA and XmegaD, previous code was for XmegaA.

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

I found the problem, it was not my code, I make a error because I didn't understand well the AVC Studio 5.0, I had place break point to close to the start conversion and the result didn't had the time to be setup. I want to thanks everyone that help.

Yours truly,
Sylvain Bissonnette
www.microsyl.com