2

I am using the xmega Atxmega32A4u,

as shown in this example XMega Timer and Microseconds I tried out but I am not able to generate the 1us to toggle the pin.

Please guide me to get the 1us delay.

I want to use this timer with 1wire protocol.

#define OUTFREQ     120000L

static void Timer_OW_ISR(void)
{
  gpio_toggle_pin(OW_READER);
}

void Timer_Init_OW(void)
{
   tc_enable(&TCC2);
   tc_set_overflow_interrupt_callback(&TCC2, Timer_OW_ISR);
   tc_set_wgm(&TCC2, TC_WGMODE_FRQ_gc);
   tc_write_period(&TCC2, sysclk_get_per_hz() / (64L*2L*OUTFREQ) - 1); 
}

void Start_Timer_OW(void)
{
  tc_write_clock_source(&TCC2, TC2_CLKSEL_DIV1_gc);
}

in main function I am calling the Timer_OW_ISR() function

Lundin
  • 195,001
  • 40
  • 254
  • 396
amar
  • 509
  • 3
  • 8
  • 17

3 Answers3

0

Download the manual and look at page 171. You want the FRQ waveform generation at the bottom of the page. Elsewhere in the chapter, you can see how to enable the pin you want to toggle.

The device runs at 2MHz by default, which is just fast enough to toggle the pin. You can change the clock to 32 MHz.

EDIT after question modified.

I see you are using the Atmel Software Framework. I don't have much experience with it. It still looks like you are not using ASF correctly.

You said you want to toggle a pin every 1 us. Is that correct? Perhaps you should be more specific in your question about what you are trying to do.

The difficulty with that fast frequency is that the clock on the device runs only a little faster than that frequency. This indicates you should not use an ISR (interrupt service routine). The routine will take too much time to run, and will not be able to achieve the 1 MHz frequency. (In any case, an ISR is meant to respond to an interrupt, not to be called from main().)

But you can achieve the pin toggle without using an ISR. Instead, let the clock toggle the pin directly. Use the FRQ functionality. You set the period in a register, and connect the clock to the correct pin using the event channel system. The details should be in the manual.

Do you have to use the ASF? Configuring the clock and processor speed is easy to do without it. Just assign certain registers to the correct values in your own C code.

UncleO
  • 8,299
  • 21
  • 29
  • I have been using the ASF. can I have any examples Regarding using FRQ..?? I have tried using the loop delays delays_us() even this resulting the 1.5us when used as delays_us(1); – amar Jan 19 '16 at 06:11
0

If you just want to toggle a pin with a specific frequency, then I would choose a PWM capable pin and use an CTC PWM mode.

This removes the ISR overhead and the unknown delays that are introduced by the ASF frameworks ISR dispatcher code. (if any - I have not that much experience with this framework or the ATXMegas (only with atmega and attiny))

vlad_tepesch
  • 6,681
  • 1
  • 38
  • 80
0

Check section 14.8.2 of the Xmega manual The following toggles pin PC.0 at 1Mhz using the internal 2MHz clock and the FRQ mode (Frequency Waveform Generation).

#include <avr/io.h>

/*
* FRQ frequency = Fclk/(2N*(CCA+1)
* where N represents the prescaler divider used
* In this example:  1000000 = 2000000/(2*1*(0+1))
*/
int main( void )
{
    PORTC_DIR |= 0x01;       //Set PC.0 as output port
    TCC0_CTRLA |= 0x01;      //Clock divider clk/1
    TCC0_CTRLB |= 0x01;      //FRQ mode
    TCC0_CTRLB |= 0x10;      //Channel selection CCAEN
    TCC0_CCA = 0x00;         //Output toggled on each compare match between CNT and CCA registers
    while(1);
}
Katu
  • 1,296
  • 1
  • 24
  • 38