Duty cycle calculations using Timer/Counter on AVR32UC3A0512

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

Objective: Calculate the duty cycle of the pulse on XC0 using Timer/Counter in BURST mode. 

 

Set-up:

Within an External Interrupt ISR, we have to read the SPI bus and Counter register, which is not feasible for wider pulses as the counter register will be stuck at the maximum time taken by the ISR.

Counter is triggered on positive edge of XC0 but read within this ISR

 

Challenges: How do I set-up the counter such that there is an interrupt generated on the negative edge, so as to read and reset the counter then? 

 

Thank you!

 

Reference code is below...

 

// Intiializaton of timer/counter
void init_tc_shadc_port()
{
	// GPIO Pin Map options
	static const gpio_map_t TC_GPIO_MAP =
	{
		{TC_SHADC_EXTCLK0_PIN, TC_SHADC_EXTCLK0_FUNCTION}
	};
	
	// TC Waveform configuration options 
	tc_waveform_opt_t tc_shadc_waveform_opt =
	{
		.channel	= TC_SHADC_CHANNEL_ID,
		.burst		= TC_BURST_CLK_AND_XC0,
		.tcclks     = TC_CLOCK_SOURCE_TC2,
		.eevt		= TC_EXT_EVENT_SEL_XC0_OUTPUT,
		.eevtedg	= TC_SEL_FALLING_EDGE,
		.enetrg		= 1
	};

	// Assign and enable GPIO pins to the ADC function
	gpio_enable_module(TC_GPIO_MAP, sizeof(TC_GPIO_MAP) / sizeof(TC_GPIO_MAP[0]));
	tc_init_waveform(tc, &tc_shadc_waveform_opt);
	tc_start(tc, TC_SHADC_CHANNEL_ID);
}

// External Interrupt ISR
ISR(gpio_particle_int_handler, AVR32_GPIO_IRQ_0 + (EXTADC_INTERRUPTN/8), 3)
{
	if (is_main_loop == TRUE && gpio_get_pin_interrupt_flag(EXTADC_INTERRUPTN))
	{
		// Function call to read SPI bus 
		// Counter read in read_shadc function call
		read_shadc(); 
		gpio_clear_pin_interrupt_flag(EXTADC_INTERRUPTN);
	}	
    
}

 

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

Have an external flag that indicates which edge is currently active.

Inside the ISR ;
- use the flag to do your timing actions,
- change the EDGE to the other edge,
- change the flag,

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

The easiest way to do this is to connect the same signal to TIOA and TIOB timer inputs, TIOA configured to trigger on rising edge and TIOB configured to detect falling edge.

 

If you know the frequency already then this will directly give you the ON time of the pulse.

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

I was just playing around with these years ago. Attached the code........ Hope it works for you....... was able to find only the C file...... could not find the whole project.

Attachment(s): 

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

Some more code.... 

 

Hope it helps

Attachment(s):