ADC and CDC working togheter.

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

Hi,
I have a problem with cdc and adc working togheter. I used adc and cda exaples from Atmel Studio 6(ASF). I'm using at32uc3a Xplained kit. This is my code:

#include 
#include 
#include 
#include 
#include 

int value=0;
int main (void)
{

	AVR32_ADC.mr |= 0x1 << AVR32_ADC_MR_PRESCAL_OFFSET;
	adc_configure(&AVR32_ADC);
	adc_enable(&AVR32_ADC, 1);
	sysclk_init();
	board_init();

	irq_initialize_vectors();

	cpu_irq_enable();

	stdio_usb_init();

	while (true) {
		delay_ms(100);
adc_start(&AVR32_ADC);


value = adc_get_value(&AVR32_ADC,1);
		
			printf("value:, &d   ",value); // echo to output
		
	}
}

I have problem with adc_get_value(&AVR32_ADC,1) function, debugger don't show changes in sr register, problem dosen't exist if i don't use sysclk_init() function, but this is nessesary for using cdc.
Please help.

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

You need to configure the clocks correctly. Comment out the lines for the Peripheral Clock Management Options (as indicated below) in the conf_clock.h file in the USB CDC example code when combining with the ADC conf_clock.h file.

// ===== Peripheral Clock Management Options

//#define CONFIG_SYSCLK_INIT_CPUMASK   (0)
//#define CONFIG_SYSCLK_INIT_PBAMASK   (0)
//#define CONFIG_SYSCLK_INIT_PBBMASK   (0)
//#define CONFIG_SYSCLK_INIT_HSBMASK   (0)
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Solved. I used

sysclk_enable_pba_module(SYSCLK_ADC);

next to sysclk_init() function.

edit:
I have next problem. In this case i wanna use timer and usb cdc togheter. This is my code:

#define EXAMPLE_TC                 (&AVR32_TC0)
//! \note TC Channel 0 is used.
#define EXAMPLE_TC_CHANNEL         0
//! \note IRQ0 line of TC0 module channel 0 is used.
#define EXAMPLE_TC_IRQ             AVR32_TC0_IRQ0
//! \note IRQ Group of TC0 module
#define EXAMPLE_TC_IRQ_GROUP       AVR32_TC0_IRQ_GROUP
//! \note Interrupt priority 0 is used for TC in this example.
#define EXAMPLE_TC_IRQ_PRIORITY    AVR32_INTC_INT0

#if defined (__GNUC__)
__attribute__((__interrupt__))
#elif defined (__ICCAVR32__)
#pragma handler = EXAMPLE_TC_IRQ_GROUP, 1
__interrupt
#endif
static void tc_irq(void)
{

}

/**
 * \brief TC Initialization
 *
 * Initializes and start the TC module with the following:
 * - Counter in Up mode with automatic reset on RC compare match.
 * - fPBA/8 is used as clock source for TC
 * - Enables RC compare match interrupt
 * \param tc Base address of the TC module
 */
static void tc_init(volatile avr32_tc_t *tc)
{
	// Options for waveform generation.
	static const tc_waveform_opt_t waveform_opt = {
		// Channel selection.
		.channel  = EXAMPLE_TC_CHANNEL,
		// Software trigger effect on TIOB.
		.bswtrg   = TC_EVT_EFFECT_NOOP,
		// External event effect on TIOB.
		.beevt    = TC_EVT_EFFECT_NOOP,
		// RC compare effect on TIOB.
		.bcpc     = TC_EVT_EFFECT_NOOP,
		// RB compare effect on TIOB.
		.bcpb     = TC_EVT_EFFECT_NOOP,
		// Software trigger effect on TIOA.
		.aswtrg   = TC_EVT_EFFECT_NOOP,
		// External event effect on TIOA.
		.aeevt    = TC_EVT_EFFECT_NOOP,
		// RC compare effect on TIOA.
		.acpc     = TC_EVT_EFFECT_NOOP,
		/*
		 * RA compare effect on TIOA.
		 * (other possibilities are none, set and clear).
		 */
		.acpa     = TC_EVT_EFFECT_NOOP,
		/*
		 * Waveform selection: Up mode with automatic trigger(reset)
		 * on RC compare.
		 */
		.wavsel   = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER,
		// External event trigger enable.
		.enetrg   = false,
		// External event selection.
		.eevt     = 0,
		// External event edge selection.
		.eevtedg  = TC_SEL_NO_EDGE,
		// Counter disable when RC compare.
		.cpcdis   = false,
		// Counter clock stopped with RC compare.
		.cpcstop  = false,
		// Burst signal selection.
		.burst    = false,
		// Clock inversion.
		.clki     = false,
		// Internal source clock 3, connected to fPBA / 8.
		.tcclks   = TC_CLOCK_SOURCE_TC3
	};

	// Options for enabling TC interrupts
	static const tc_interrupt_t tc_interrupt = {
		.etrgs = 0,
		.ldrbs = 0,
		.ldras = 0,
		.cpcs  = 1, // Enable interrupt on RC compare alone
		.cpbs  = 0,
		.cpas  = 0,
		.lovrs = 0,
		.covfs = 0
	};
	// Initialize the timer/counter.
	tc_init_waveform(tc, &waveform_opt);

	/*
	 * Set the compare triggers.
	 * We configure it to count every 1 milliseconds.
	 * We want: (1 / (fPBA / 8)) * RC = 1 ms, hence RC = (fPBA / 8) / 1000
	 * to get an interrupt every 10 ms.
	 */
	tc_write_rc(tc, EXAMPLE_TC_CHANNEL, (sysclk_get_pba_hz() / 8 / 1000));
	// configure the timer interrupt
	tc_configure_interrupts(tc, EXAMPLE_TC_CHANNEL, &tc_interrupt);
	// Start the timer/counter.
	//tc_start(tc, EXAMPLE_TC_CHANNEL);
	tc->channel[0].ccr=1<<2|1<<0;
}


int ch=12345;
int main (void)
{
	volatile avr32_tc_t *tc = EXAMPLE_TC;
	/* Initialize basic board support features.
	 * - Initialize system clock sources according to device-specific
	 *   configuration parameters supplied in a conf_clock.h file.
	 * - Set up GPIO and board-specific features using additional configuration
	 *   parameters, if any, specified in a conf_board.h file.
	 */
		//TC
	sysclk_init();
	//sysclk_enable_peripheral_clock(EXAMPLE_TC);	
	AVR32_PM.pbamask|=1<<16;
	cpu_irq_disable();
	INTC_init_interrupts();
	// Register the RTC interrupt handler to the interrupt controller.
	INTC_register_interrupt(&tc_irq, EXAMPLE_TC_IRQ, EXAMPLE_TC_IRQ_PRIORITY);
	// Enable the interrupts
	cpu_irq_enable();	
	irq_initialize_vectors();
	cpu_irq_enable();
	stdio_usb_init();
	tc_init(tc);
	
	board_init();

	
	AVR32_ADC.mr |= 0x1 << AVR32_ADC_MR_PRESCAL_OFFSET;
	adc_configure(&AVR32_ADC);

	/* Enable the ADC channels. */
	adc_enable(&AVR32_ADC, 1);

	

	while (true) 
		{
			printf("d",ch); // echo to output
			}
}

If i comment tc->channel[0].ccr=1<<2|1<<0; (tc_start) function i can connect with USB, if i non-comment that finction my computer cant recognise my uC, i dont know why that happen...