user39577: I have been looking for the phase difference between two square signals and this is the code:
#include <avr/io.h>
#include <avr/interrupt.h>


void setup() {
  //Serial.begin(9600);
  pinMode(2, INPUT);    //set pins 2 and 3 as inputs and 11 as output
  pinMode(3, INPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);

  cli();           // disable all interrupts
  TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);  //set up timer2 to generate a PWM
  TCCR2B = _BV(CS20);    //set prescalar value to 1
  //TCCR1A = 0;
  //TCCR1B = _BV(CS20);
  //TCCR1B |= (1 << CS12); //256 values
  TCNT2  = 0;
  sei();             //enable all interrupts

  attachInterrupt(digitalPinToInterrupt(2), t1_ISR, RISING);     //enable interrupt service routines on pins 2 and 3
  attachInterrupt(digitalPinToInterrupt(3), t2_ISR, RISING);

  //Serial.print(OCR2A);
  //Serial.print(OCR1A);

}

volatile uint8_t t1, t2;

void loop() {
  if (t2 == t1)
  {
    OCR2A = 0;
    OCR2B = 0;
  }
  //compare time that one signal goes high to time the other signal goes high
  else if (t2 > t1)
  {
    OCR2A = (t2 - t1); //output PWM based on phase difference on pin 11
    OCR2B = 0;
  }
  else if (t2 < t1)
  {
    OCR2B = (t1 - t2);  //output PWM on pin 11
    OCR2A = 0;
  }
}
}

void t1_ISR() {         //interrupt if signal on pin 2 goes high and store counter
  t1 = TCNT2;           //value to variable t1
}

void t2_ISR() {         //interrupt if signal on pin 3 goes high and store counter
  t2 = TCNT2;           //value to variable t2
}

I want to operate the input pulses at 40 kHz, how can I do this and read the difference of phase? I am using the Arduino Mega2560

user7508: There are a few things very wrong here.
First problem: you are clocking Timer 2 with the prescaler set to 1.
This means the timer will roll over every 256 CPU cycles. Your
input signal, on the other hand, has a period of 25 µs, which is
400 CPU cycles (I am assuming a 16 MHz Arduino). Unless you
know beforehand that the phase difference is within some window less
than 16 µs wide, your readings will be ambiguous.
You have to make sure the rollover period of your timer is larger than
the period of the input signal. This means either using a larger
prescaler (8 should do), which will decrease your phase resolution,
or using a 16-bit timer instead of an 8-bit timer.
Second problem: you are using a signed integer type for the timer
readings. This makes little sense. Worse: you are computing differences
like t2-t1, which can overflow. Signed integer overflow is undefined
behavior in C++, and
invoking it is always a bug.
You should declare t1 and t2 as unsigned, i.e. either
volatile uint8_t, or volatile uint16_t (if using a 16-bit timer).
Then you don't get overflows: you get instead a well-defined rollover
behavior.
Third problem: you are doing comparisons like if (t2 > t1). This,
again, makes no sense, if only because the timer is repeatedly
rolling-over to zero. You should instead just compute t2-t1 and work
your way from that.

user30005: 
how can I do this and read the difference of phase?

a few simple ways:
1) poll the input signals: For example, once a pin has gone high, wait for the other pin to go high as well.
2) xor the input signals: wait for the XOR of both inputs to go high, wait for it to go low. essentially the 1st approach;
3) use interrupts: once the first signal goes high, start a timer; once the 2nd signal to go high, stop the timer.
4) use input capture: ...
...

39577: <p>I have been looking for the phase difference between two square signals and this is the code:</p>

<pre class="lang-cpp prettyprint-override"><code>#include &lt;avr/io.h&gt;
#include &lt;avr/interrupt.h&gt;


void setup() {
  //Serial.begin(9600);
  pinMode(2, INPUT);    //set pins 2 and 3 as inputs and 11 as output
  pinMode(3, INPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);

  cli();           // disable all interrupts
  TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);  //set up timer2 to generate a PWM
  TCCR2B = _BV(CS20);    //set prescalar value to 1
  //TCCR1A = 0;
  //TCCR1B = _BV(CS20);
  //TCCR1B |= (1 &lt;&lt; CS12); //256 values
  TCNT2  = 0;
  sei();             //enable all interrupts

  attachInterrupt(digitalPinToInterrupt(2), t1_ISR, RISING);     //enable interrupt service routines on pins 2 and 3
  attachInterrupt(digitalPinToInterrupt(3), t2_ISR, RISING);

  //Serial.print(OCR2A);
  //Serial.print(OCR1A);

}

volatile uint8_t t1, t2;

void loop() {
  if (t2 == t1)
  {
    OCR2A = 0;
    OCR2B = 0;
  }
  //compare time that one signal goes high to time the other signal goes high
  else if (t2 &gt; t1)
  {
    OCR2A = (t2 - t1); //output PWM based on phase difference on pin 11
    OCR2B = 0;
  }
  else if (t2 &lt; t1)
  {
    OCR2B = (t1 - t2);  //output PWM on pin 11
    OCR2A = 0;
  }
}
}

void t1_ISR() {         //interrupt if signal on pin 2 goes high and store counter
  t1 = TCNT2;           //value to variable t1
}

void t2_ISR() {         //interrupt if signal on pin 3 goes high and store counter
  t2 = TCNT2;           //value to variable t2
}
</code></pre>

<p>I want to operate the input pulses at 40 kHz, how can I do this and read the difference of phase? I am using the Arduino Mega2560</p>
 7508: <p>There are a few things very wrong here.</p>

<p>First problem: you are clocking Timer 2 with the prescaler set to 1.
This means the timer will roll over every 256&nbsp;CPU cycles. Your
input signal, on the other hand, has a period of 25&nbsp;µs, which is
400&nbsp;CPU cycles (I am assuming a 16&nbsp;MHz Arduino). Unless you
know beforehand that the phase difference is within some window less
than 16&nbsp;µs wide, your readings will be ambiguous.</p>

<p>You have to make sure the rollover period of your timer is larger than
the period of the input signal. This means either using a larger
prescaler (8&nbsp;should do), which will decrease your phase resolution,
or using a 16-bit timer instead of an 8-bit timer.</p>

<p>Second problem: you are using a <em>signed</em> integer type for the timer
readings. This makes little sense. Worse: you are computing differences
like <code>t2-t1</code>, which can overflow. Signed integer overflow is <a href="https://en.wikipedia.org/wiki/Undefined_behavior" rel="nofollow noreferrer">undefined
behavior</a> in C++, and
invoking it is <em>always a bug</em>.</p>

<p>You should declare <code>t1</code> and <code>t2</code> as unsigned, i.e. either
<code>volatile uint8_t</code>, or <code>volatile uint16_t</code> (if using a 16-bit timer).
Then you don't get overflows: you get instead a well-defined rollover
behavior.</p>

<p>Third problem: you are doing comparisons like <code>if (t2 &gt; t1)</code>. This,
again, makes no sense, if only because the timer is repeatedly
rolling-over to zero. You should instead just compute <code>t2-t1</code> and work
your way from that.</p>
 30005: <blockquote>
  <p>how can I do this and read the difference of phase?</p>
</blockquote>

<p>a few simple ways:</p>

<p>1) poll the input signals: For example, once a pin has gone high, wait for the other pin to go high as well.</p>

<p>2) xor the input signals: wait for the XOR of both inputs to go high, wait for it to go low. essentially the 1st approach;</p>

<p>3) use interrupts: once the first signal goes high, start a timer; once the 2nd signal to go high, stop the timer.</p>

<p>4) use input capture: ...</p>

<p>...</p>
