// Arduino Uno at 16MHz
// Best accuracy with an Arduino Uno that has a crystal instead of resonator.
// A NOP takes 62.5 ns.
// A change of a pin with writing to PINB also takes 62.5 ns.
// There are 16 NOP in a microsecond.
//
// Does the "while(true)" take time ?

void setup() 
{
  pinMode( 8, OUTPUT);                   // pin 8  = PORTB bit 0
  pinMode( 9, OUTPUT);                   // pin 9  = PORTB bit 1
  pinMode( 10, OUTPUT);                  // pin 10 = PORTB bit 2

  noInterrupts();                        // Stop Arduino services

  while( true)                           // forever
  {
    PINB = bit( 0);                      // HIGH
    __builtin_avr_delay_cycles (63);     // 4 us = 64 NOP (-1 for PINB instruction)
    PINB = bit( 1);                      // HIGH
    __builtin_avr_delay_cycles (15);     // 1 us = 16 NOP
    PINB = bit( 0);                      // LOW
    __builtin_avr_delay_cycles (47);     // 3 us = 48 NOP
    PINB = bit( 2);                      // HIGH
    __builtin_avr_delay_cycles (15);     // 1 us = 16 NOP
    PINB = bit( 1);                      // LOW
    __builtin_avr_delay_cycles (63);     // 4 us = 64 NOP
    PINB = bit( 2);                      // LOW
    __builtin_avr_delay_cycles (15791);  // 16000 NOP minus all the other code
  }
}

void loop() { }
D0D1D2D3D4D5D6D7GNDLOGIC