Xmega 16E5 - ADC basics...

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

Hi everyone,

I'd like to write simple functions for configuring and reading ADC (by pooling). Could you look at them to make me sure that they are right? I ask you because I get wrong results when the resolution is 12 bits.  robiw

 

void ADCconfig(void)
{
	//ADC config: 8bit single ended, no curr limit, ref = VCC/1.6, gain = x1
	ADCA.CTRLA = ADC_ENABLE_bm;  //ADC Enabled
	ADCA.CTRLB = ADC_CURRLIMIT_NO_gc|ADC_RESOLUTION_8BIT_gc;  //300ksps max sampling rate
	ADCA.REFCTRL = ADC_REFSEL_INTVCC_gc;  //Internal VCC / 1.6
	ADCA.PRESCALER = ADC_PRESCALER_DIV32_gc;  //1MHz @ 32MHz
	ADCA.CH0.CTRL = ADC_CH_GAIN_1X_gc|ADC_CH_INPUTMODE_SINGLEENDED_gc;  //Single-ended input, no gain
	ADCA.CH0.MUXCTRL = ADC_CH_MUXPOS_PIN0_gc;  //pin PA0
}
uint8_t ADCread(void)
{
	ADCA.CTRLA |= ADC_START_bm;  //Start new conversion
	while(!(ADCA.CH0.INTFLAGS & ADC_CH_IF_bm));  //Wait for the end of the conversion

	ADCA.CH0.INTFLAGS = ADC_CH_IF_bm;  //Clear Channel Interrupt Flag (not cleared by hw)

	return ADCA.CH0RESL;  //Return 8-bit result
}

 

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

robiw wrote:
I get wrong results when the resolution is 12 bits

your function only returns an 8 bit value, so yes it will be the wrong value.

change it to return a 16 bit value, and return the whole result.

jim

 

 

 

 

 

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

Yes. It is obvious that the result type should be uint16 and I changed It...r

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

robiw wrote:

return ADCA.CH0RESL;  //Return 8-bit result

this line may also need changing

ie

return ADCA.CH0RES; // compiler knows how to read full register value

 

 

 

 

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

Yes, I knew about this butt still it seems to work wrong. What about the rest? r

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

robiw wrote:

ADCA.CTRLB = ADC_CURRLIMIT_NO_gc|ADC_RESOLUTION_8BIT_gc;  //300ksps max sampling rate

it looks like this line needs changing too, why are you not seeing these?

 

 

 

 

 

 

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

I saw this and knew it needs changing. R