I'm using an Arduino Mega 2560 which runs at 16Mhz.
I have two signals, 1khz square wave reference and and a phase shifted one. The Arduino generates the reference signal, sends it out to some external circuitry and it comes back shifted in phase. The smallest phase difference I need to be able to reliably measure is 0.144 degrees which is a 400nS difference for a 1khz signal.
A 16Mhz clock gives me a 62.5nS period, so that gives me about 7 clock cycles (437.5nS) to measure the difference. I'm assuming this is not enough, but I'm still trying it with what I have here.
Timer 1 is at 1:64 and generates the 1khz reference. It triggers a compare match every cycle.
Timer 5 is at 1:1 and counts the number of cycles between the reference and the shifted.
Interrupt code:
//Start timer 5 on every cycle of the reference
ISR(TIMER1_COMPA_vect){
TCNT5 = 0;
}
//Get the value of the ICR when an Input Capture Event occurs
ISR(TIMER5_CAPT_vect){
xTempCount = ICR5;
xStart = 1;
}
It works, but it's a little noisy. The timer count varies typically +10 to -10 but I see jumps to 48 or so.
As a test, I decided to trigger the interrupt through software precisely every 4000 clock cycles. Even then, the difference between the time stamps varied as much as 49. I'm just having some trouble understand where these differences are coming from.