Problem with reading array that stores ADC SCAN values XMEGA-A3BU

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

I'm using Atmel Studio 7 and the microcontroller that i'm generating the code for is XMEGA A3BU. 

I'm using modified code from ATMEL ASF (ADC Scan example - XMEGA A3BU Xplained) with exception that i'm using usb and I want to print scanned ADC data.

 

I modified ADC reference, because i'm using external 2.048V reference and I changed those lines bellow, because differential measurements between (ADDCH_POS_PIN2 to ADDCH_POS_PIN9) and  ADCCH_NEG_PIN1. (see the attached picture). For that purpose I changed addch_set_pin_scan function as well.

	adcch_set_input(&adcch_conf, ADCCH_POS_PIN2, ADCCH_NEG_PIN1, 1);
	adcch_set_pin_scan(&adcch_conf, 2, 9);

Problem is that I get strange output on Putty (see the attached picture). It looks like that i'm printing ADC result array while conversation is still on. So my question is, how could I get an array of ADC scan results that i could use ?
Thank you for your effort and time in advance.

 

Best Regards,
Matjaz

Program Code:

/*
 *	\file
 *
 *	@brief User application.
 *
 *	Copyright (c) 2017-2018. All rights reserved.
 */

#include <asf.h>

#include <string.h> /* memset */
#include <unistd.h> /* close */
																														  				

struct adc_config adc_conf;
struct adc_channel_config adcch_conf;
static volatile int16_t adc_scan_results[8];

/* FUNCTION prototypes */
void my_vbus_action(bool b_high);
void usb_init(void);
static void adc_handler(ADC_t *adc, uint8_t ch_mask, adc_result_t result);


/*
 *	@brief USB ATTACH/DETACH
 *
 *	@details Function that attaches/detaches DataLogger USB when Vbus is  
 *			at certain level.
 */
void my_vbus_action(bool b_high)
{
	if(b_high)
	{
		udc_attach();	/* Attach USB device */
	}
	else
	{
		udc_detach();	/* Detach USB device */
	}
}

/*
 *	@brief USB initialization
 *
 *	@details Function stdio_usb_init() ans stdio_usb_enable() 
 *			enables use of printf();
 */
void usb_init(void)
{
	//udc_start();
	stdio_usb_init();
	stdio_usb_enable();
}

/*
 *	@brief Event system clock initialization
 *
 *	@details Initialize the event system clocks and link the conversion timer to the correct event channel.
 */
	static void evsys_init(void)
	{
		sysclk_enable_module(SYSCLK_PORT_GEN, SYSCLK_EVSYS);
		// Conversion 62500 times a second
		EVSYS.CH0MUX = EVSYS_CHMUX_PRESCALER_512_gc;
	}


/*
 * @brief ADC A Initialization
 *
 * @var adc_conf structure for ADC
 *
 */
static void adc_init_ADC_A(void)
{	
	// Clear the configuration structures.
	memset(&adc_conf, 0, sizeof(struct adc_config));
	memset(&adcch_conf, 0, sizeof(struct adc_channel_config));
	
	/* Configure the ADC module:
	 * - signed, 12-bit results
	 * - external voltage reference AREFA 2.048V
	 * - 200 kHz lock rate
	 * - channel0 triggered by event
	 * //- use the ADC current limiter in high setting mode
	 * - DISABLE the internal bandgap reference
	 * - callback function
	 */
	adc_read_configuration(&ADCA, &adc_conf);
	adcch_read_configuration(&ADCA, ADC_CH0, &adcch_conf);
		
	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_ON, ADC_RES_12, ADC_REF_AREFA);
	adc_set_clock_rate(&adc_conf, 200000UL);
	adc_set_conversion_trigger(&adc_conf, ADC_TRIG_EVENT_SWEEP, 1, 0);
	//adc_set_current_limit(&adc_conf, ADC_CURRENT_LIMIT_HIGH); // if HIGH then max sampling rate 0.5MSPS see datasheet page-353
	//adc_set_gain_impedance_mode(&adc_conf, ADC_GAIN_HIGHIMPEDANCE);
	adc_disable_internal_input(&adc_conf, ADC_INT_BANDGAP);
	adc_write_configuration(&ADCA, &adc_conf);
	
	adc_set_callback(&ADCA, &adc_handler);

	/* Configure ADC channel 0:
	 * - differential measurement between POS: ADC2-10 and NEG: ADC1 (0.4V reference)
	 * - interrupt at completed conversion
	 * - use the CH0 pin scan function
	 */
	adcch_set_input(&adcch_conf, ADCCH_POS_PIN2, ADCCH_NEG_PIN1, 1);
	adcch_set_interrupt_mode(&adcch_conf, ADCCH_MODE_COMPLETE);
	adcch_enable_interrupt(&adcch_conf);
	adcch_set_pin_scan(&adcch_conf, 2, 9);

	adcch_write_configuration(&ADCA, ADC_CH0, &adcch_conf);

	// Enable the ADC
	adc_enable(&ADCA);
}

/**
 * \brief Callback function for ADC interrupts
 *
 * \param adc Pointer to ADC module.
 * \param channel ADC channel number.
 * \param result Conversion result from ADC channel.
 */
static void adc_handler(ADC_t *adc, uint8_t ch_mask, adc_result_t result)
{
	static uint8_t current_scan_channel = 0;
	// Store the ADC results from the scan in the result array
	if (ch_mask & ADC_CH0) {
		adc_scan_results[current_scan_channel] = result;
		current_scan_channel++;
		// When 8 pins have been scanned the SCAN OFFSET wraps to zero
		if (current_scan_channel == 8) {
			current_scan_channel = 0;
		}
	}
}

/*
 *  @brief Main routine
 *
 *
 */
int main(void)
{
	sysclk_init();
	sleepmgr_init();
	irq_initialize_vectors();  // Authorize interrupts
	cpu_irq_enable();
	
	/* I/O pins Initialization */
	board_init();
	
	/* USB initialization */
	usb_init();

	/* Event System */
	evsys_init();

	// ADC Initialization 
	adc_init_ADC_A();

	while(1)
	{
		for(int i=0;i<=7;i++)
		{
			printf("ADC%d : %d\r\n",i,adc_scan_results[i]);
		}
		delay_ms(1000);
		printf("\r\n");
	}
}

Attachment(s): 

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

For:

 

http://asf.atmel.com/docs/latest...

 

what have you set CONFIG_ADC_CALLBACK_TYPE to?

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

I had it commented, but I uncommented it and it is doing the same thing...

// #define CONFIG_ADC_CALLBACK_TYPE uint16_t

But i have been looking on forum and i found this URL: http://www.avrfreaks.net/comment... , do you think this could be it ? Should i use this DMA ?

Best regards,
Matjaz

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

I misunderstood your suggestion, I forgot that I have signed conversions with my ADC...

 

Now I changed conf_adc.h file to:

/**
 * \file
 *
 * \brief Chip-specific ADC configuration
 *
 * Copyright (c) 2010-2015 Atmel Corporation. All rights reserved.
 *
 * \asf_license_start
 *
 * \page License
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. The name of Atmel may not be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * 4. This software may only be redistributed and used in connection with an
 *    Atmel microcontroller product.
 *
 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * \asf_license_stop
 *
 */
/*
 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
 */
#ifndef CONF_ADC_H
#define CONF_ADC_H

/* Refer to the ADC driver for detailed documentation. */
#define CONFIG_ADC_CALLBACK_ENABLE

#define CONFIG_ADC_CALLBACK_TYPE int16_t

// #define CONFIG_ADC_INTLVL        ADC_CH_INTLVL_LO_gc

#endif /* CONF_ADC_H */

And tried to somehow stop the incoming result from ADC with this code modification (marked as red text in code bellow), because I really can't figure out how to use DMA ...

 

/*
 *	\file
 *
 *	@brief User application.
 *
 *	Copyright (c) 2017-2018. All rights reserved.
 */

#include <asf.h>

#include <string.h> /* memset */
#include <unistd.h> /* close */
																														  				

struct adc_config adc_conf;
struct adc_channel_config adcch_conf;
static volatile int16_t adc_scan_results[8];
bool flag_sta = false;

/* FUNCTION prototypes */
void my_vbus_action(bool b_high);
void usb_init(void);
static void adc_handler(ADC_t *adc, uint8_t ch_mask, adc_result_t result);


/*
 *	@brief USB ATTACH/DETACH
 *
 *	@details Function that attaches/detaches DataLogger USB when Vbus is  
 *			at certain level.
 */
void my_vbus_action(bool b_high)
{
	if(b_high)
	{
		udc_attach();	/* Attach USB device */
	}
	else
	{
		udc_detach();	/* Detach USB device */
	}
}

/*
 *	@brief USB initialization
 *
 *	@details Function stdio_usb_init() ans stdio_usb_enable() 
 *			enables use of printf();
 */
void usb_init(void)
{
	//udc_start();
	stdio_usb_init();
	stdio_usb_enable();
}

/*
 *	@brief Event system clock initialization
 *
 *	@details Initialize the event system clocks and link the conversion timer to the correct event channel.
 */
	static void evsys_init(void)
	{
		sysclk_enable_module(SYSCLK_PORT_GEN, SYSCLK_EVSYS);
		// Conversion 62500 times a second
		EVSYS.CH0MUX = EVSYS_CHMUX_PRESCALER_512_gc;
	}


/*
 * @brief ADC A Initialization
 *
 * @var adc_conf structure for ADC
 *
 */
static void adc_init_ADC_A(void)
{	
	// Clear the configuration structures.
	memset(&adc_conf, 0, sizeof(struct adc_config));
	memset(&adcch_conf, 0, sizeof(struct adc_channel_config));
	
	/* Configure the ADC module:
	 * - signed, 12-bit results
	 * - external voltage reference AREFA 2.048V
	 * - 200 kHz lock rate
	 * - channel0 triggered by event
	 * //- use the ADC current limiter in high setting mode
	 * - DISABLE the internal bandgap reference
	 * - callback function
	 */
	adc_read_configuration(&ADCA, &adc_conf);
	adcch_read_configuration(&ADCA, ADC_CH0, &adcch_conf);
		
	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_ON, ADC_RES_12, ADC_REF_AREFA);
	adc_set_clock_rate(&adc_conf, 200000UL);
	adc_set_conversion_trigger(&adc_conf, ADC_TRIG_EVENT_SWEEP, 1, 0);
	//adc_set_current_limit(&adc_conf, ADC_CURRENT_LIMIT_HIGH); // if HIGH then max sampling rate 0.5MSPS see datasheet page-353
	//adc_set_gain_impedance_mode(&adc_conf, ADC_GAIN_HIGHIMPEDANCE);
	adc_disable_internal_input(&adc_conf, ADC_INT_BANDGAP);
	adc_write_configuration(&ADCA, &adc_conf);
	
	adc_set_callback(&ADCA, &adc_handler);

	/* Configure ADC channel 0:
	 * - differential measurement between POS: ADC2-10 and NEG: ADC1 (0.4V reference)
	 * - interrupt at completed conversion
	 * - use the CH0 pin scan function
	 */
	adcch_set_input(&adcch_conf, ADCCH_POS_PIN2, ADCCH_NEG_PIN1, 1);
	adcch_set_interrupt_mode(&adcch_conf, ADCCH_MODE_COMPLETE);
	adcch_enable_interrupt(&adcch_conf);
	adcch_set_pin_scan(&adcch_conf, 2, 9);

	adcch_write_configuration(&ADCA, ADC_CH0, &adcch_conf);

	// Enable the ADC
	adc_enable(&ADCA);
}

/**
 * \brief Callback function for ADC interrupts
 *
 * \param adc Pointer to ADC module.
 * \param channel ADC channel number.
 * \param result Conversion result from ADC channel.
 */
static void adc_handler(ADC_t *adc, uint8_t ch_mask, adc_result_t result)
{
	if (flag_sta == false){
		static uint8_t current_scan_channel = 0;
		// Store the ADC results from the scan in the result array
		if (ch_mask & ADC_CH0) {
			adc_scan_results[current_scan_channel] = result;
			current_scan_channel++;
			// When 8 pins have been scanned the SCAN OFFSET wraps to zero
			if (current_scan_channel == 8) {
				current_scan_channel = 0;
				flag_sta = true;
			}
		}
	}
}

/*
 *  @brief Main routine
 *
 *
 */
int main(void)
{
	sysclk_init();
	sleepmgr_init();
	irq_initialize_vectors();  // Authorize interrupts
	cpu_irq_enable();
	
	/* I/O pins Initialization */
	board_init();
	
	/* USB initialization */
	usb_init();

	/* Event System */
	evsys_init();

	// ADC Initialization 
	adc_init_ADC_A();

	while(1)
	{
		for(int i=0;i<=7;i++)
		{
			printf("ADC%d : %d\r\n",i,adc_scan_results[i]);
		}
		delay_ms(1000);
		printf("\r\n");
		flag_sta = false;
	}
}

but my output is still not correct (see attached picture).. Based on measurements on a circuit with voltmeter all ADC pins from 2 to 9 should be around 0. If I start don't use ADC SCAN and do conversions channel by channel on manual trigger I do obtain these results, but i'm doing DataLogger so I was hoping to obtain all measurements from these channel as fast as I can...

Please if somebody has an idea and is willing to share it I would be very thankful...

Thank you in advance...

 

Best regards,

Matjaz

Attachment(s):