2

I'm trying to bit bang out some data out of an atxmega128a3u and need to toggle a pin as fast as 4us but so far I'm not getting anywhere close to that...

Here I'm setting my timer for 88us but am getting around 146us.

    int main(void)
    {

    //CRYSTAL SETUP
        OSC_XOSCCTRL = OSC_FRQRANGE_12TO16_gc | OSC_XOSCSEL_XTAL_16KCLK_gc; // 16Mhz Crystal
        OSC_CTRL |= OSC_XOSCEN_bm;
        while(!(OSC_STATUS & OSC_XOSCRDY_bm)); //Wait for crystal to stabilize.
        CCP = CCP_IOREG_gc;
        CLK_CTRL = CLK_SCLKSEL_XOSC_gc;
        //END CRYSTAL SETUP

        cli();
        TCC0.PERL = 0x80; //88us
        TCC0.PERH = 0x05;
        TCC0.CTRLA = 0x01;
        TCC0.INTCTRLA = 0x02;
        PMIC.CTRL = 0x02;
        sei();
    }
ISR(TCC0_OVF_vect) {
   PORTF.OUTTGL = PIN3_bm;
}

enter image description here

How can I get a faster and more accurate response time?

bwoogie
  • 4,339
  • 12
  • 39
  • 72
  • It is possible to do this with USART or SPI module. The USART has a 2-byte input buffer. If you need more data to output, you can use the DMA module. I using this for bit signal output in a different project, you can get inspired: [XMEGA VGA output](http://blog.tyg-res.tk/2016/11/atmel-xmega-vga-output.html) . – Gabriel Cséfalvay Sep 21 '18 at 10:19

1 Answers1

3

Is that your complete code? If yes, the controller would reset after executing sei(); since the end of the program code has been reached. The delay you see on the oscilloscope probably is the start-up and crystal setup time.

Use a

while(true); 

construct at the end of the main. I put the volatile NOP instruction there to prevent the compiler from optimizing the empty while-loop away. You can omit it if there is any other code in the loop.

Rev
  • 5,827
  • 4
  • 27
  • 51
  • The compiler will never optimize away an endless loop. – JimmyB Jan 15 '16 at 09:32
  • @HannoBinder: Thats not universally true. But you are right regarding my example, since the iteration statement is a constant. From the C11 Standard: _"An iteration statement whose controlling expression is not a constant expression, [...] may be assumed by the implementation to terminate."_ So an endless loop can be optimized out depending on the iteration statement and contents of the loop. – Rev Jan 15 '16 at 10:58