ADC SCAN not working as expected

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

I'm trying to automate the reading of my ADC inputs on my Xmega E5 so i'm using the SCAN register.  I'm expecting the INPUTOFFSET to increment one after each reading but it is incrementing once every other time.

 

ISR(ADCA_CH0_vect)
{
	static int n = 0;
	uint16_t val = ADCA.CH0.RES;
	int pos = (ADCA.CH0.SCAN>>ADC_CH_INPUTOFFSET_gp);

	sensorData[pos-1] = val;
}

in the screenshot below you can see the yay[] array is updated to reflect whether or not the value stored in SCAN:INPUTOFFSET has changed.

 

 

other relevant code:

 

void SetupADC(void)
{
  ADCA.CTRLA = 0;
  ADCA.CTRLB = 0;
  ADCA.REFCTRL = 0;
  ADCA.CH0.INTCTRL = 0;
  ADCA.CH0.MUXCTRL = 0;
  ADCA.CH0.CTRL = 0;
  ADCA.PRESCALER = 0;
  ADCA.CAL = 0;

  ADCA.CALL = ReadSignatureByte(0x20) ; //ADC Calibration Byte 0
  ADCA.CALH = ReadSignatureByte(0x21) ; //ADC Calibration Byte 1
  
  ADCA.PRESCALER |= ADC_PRESCALER_DIV32_gc;
  
  ADCA.CTRLB = ADC_CURRLIMIT_NO_gc;
  ADCA.CTRLB |= (0<<ADC_CONMODE_bp);
  ADCA.CTRLB |= (0<<ADC_FREERUN_bp);
  ADCA.CTRLB |= (ADC_RESOLUTION_8BIT_gc);
  ADCA.REFCTRL |= ADC_REFSEL_AREFA_gc;
  ADCA.REFCTRL |= (0<<ADC_BANDGAP_bp) | (0<<ADC_TEMPREF_bp);
  ADCA.CH0.CTRL |= ADC_CH_INPUTMODE_SINGLEENDED_gc;
  ADCA.CH0.MUXCTRL |= ADC_CH_MUXPOS_PIN1_gc;
  ADCA.CH0.SCAN = (12<<ADC_CH_INPUTSCAN0_bp) | (0<<ADC_CH_INPUTOFFSET0_bp); //12 inputs starting at 1
  ADCA.CH0.INTCTRL = ADC_CH_INTMODE_COMPLETE_gc;
  ADCA.CH0.INTCTRL |= ADC_CH_INTLVL1_bm | ADC_CH_INTLVL0_bm; // 0x03
  ADCA.CTRLA |= ADC_ENABLE_bm;
  
}

ISR(TCC4_OVF_vect)
{
	int offset = (ADCA.CH0.SCAN>>ADC_CH_INPUTOFFSET_gp);
	
	if (!(ADCA.CH0.CTRL & ADC_CH_START_bm))
	{
		if ((ADCA.CH0.SCAN>>ADC_CH_INPUTOFFSET_gp) >= 12)
		{
			ADCA.CH0.SCAN = (12<<ADC_CH_INPUTSCAN0_bp) | (0<<ADC_CH_INPUTOFFSET0_bp); 
		}
		ADCA.CH0.CTRL |= ADC_CH_START_bm;
	}
}

What am I not understanding?  Help much appreciated.

 

This topic has a solution.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Problem found.  It was the timer.  I was not clearing the timer interrupt so it was trying to run continuously.