ASF code for reading ADC

1 post / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can please someone check this code for me?

The purpose of the code is to read pins 0 and 1 from ADCA by using the scan function. Everything works but the result obtained is always zero on both channels.

I tried to change the reference (bandgap, VCC/1.6 and so on) with no results. I am also sure that AVCC is ok.

The MCU is an ATXMEGA-128B1

Thanks

 

This is the initialisation:

void initAnalogMeter(void)
{
	struct adc_channel_config ADCChannelConfiguration;
	struct adc_config ADCConfiguration;

	ioport_configure_pin(VOX_PIN, IOPORT_DIR_INPUT | IOPORT_INPUT_DISABLE);
	ioport_configure_pin(BATTERY_PIN, IOPORT_DIR_INPUT | IOPORT_INPUT_DISABLE);

	// setup for ADC used to monitor battery level
	sysclk_enable_module(ANALOG_ADC_SYSCLK,PR_ADC_bm);
	adc_read_configuration(ANALOG_ADC, &ADCConfiguration);
	adc_set_clock_rate(&ADCConfiguration, 1000UL);
	adc_set_conversion_trigger(&ADCConfiguration, ADC_TRIG_MANUAL, 1, 0);
	adc_set_conversion_parameters(&ADCConfiguration,ADC_SIGN_OFF,ADC_RES_12,ADC_REF_BANDGAP);
	adc_set_current_limit(&ADCConfiguration,ADC_CURRENT_LIMIT_HIGH);

	adcch_read_configuration(ANALOG_ADC, ADC_ANALOG_CH, &ADCChannelConfiguration);
	adcch_set_interrupt_mode(&ADCChannelConfiguration,ADCCH_MODE_COMPLETE);
	adcch_enable_interrupt(&ADCChannelConfiguration);
	adcch_set_pin_scan(&ADCChannelConfiguration,ADC_VOX_PIN,ADC_BATTERY_PIN);
	// perform this operation only once. Positive pin will be changed automatically
	// thanks to scan
	adcch_set_input(&ADCChannelConfiguration,ADC_BATTERY_PIN,ADCCH_NEG_NONE,1);
	adcch_write_configuration(ANALOG_ADC,ADC_ANALOG_CH,&ADCChannelConfiguration);

	// define the callback that updates the average value
	adc_set_callback(ANALOG_ADC,readAnalogMeter);

	// Now make effective the configurations
	adc_write_configuration(ANALOG_ADC,&ADCConfiguration);
	adc_enable(ANALOG_ADC);
	adc_start_conversion(ANALOG_ADC,ADC_ANALOG_CH);
}

and this is the callback function:

void readAnalogMeter(ADC_t* adc, uint8_t ch_mask, adc_result_t result)
{
	struct adc_channel_config ADCChannelConfiguration;
static uint8_t currentIndex;

	adcch_read_configuration(ANALOG_ADC,ADC_ANALOG_CH,&ADCChannelConfiguration);
	switch ((ADCChannelConfiguration.scan & ADC_CH_OFFSET_gm) >> ADC_CH_OFFSET_gp)
	{
		case ADC_BATTERY_PIN:
			avgVOX[currentIndex] = result;
			break;
		case ADC_VOX_PIN:
			avgBatteryLevel[currentIndex] = result;
			// both data are stored, increment the index
			currentIndex = (currentIndex + 1) & (BATTERY_SAMPLES - 1);
			break;
			
		default:
			break;
	}
	// in both cases restart a conversion
	adc_start_conversion(ANALOG_ADC,ADC_ANALOG_CH);
}