This is a really bizarre problem I've been having. I have 3 16-bit timers setup in input capture mode. Whenever these are triggered via my comparator, the HIGH byte in the counts (TCNT1H, TCNT4H, and TCNT5H) becomes mucked up (erroneously un-synchronized with the other counters). What is odd is that the lower byte remains in sync so this probably is not a "noise" problem (I think). I will be attaching an O-Scope here soon to evaluate the output more.
My code for my timer setup (which generally follows AVR130) is listed in this post here: Should I outsource my counting to something external? Micros is too slow. - #56 by system - Project Guidance - Arduino Forum
Here is the code I've written that momentarily stops the timers so that I can read them and transmit their values (and then resyncs the timers) to the computer for troubleshooting:
...
case 0xCC: //204 checks timer synchronization
//stop the timers to get a reading of their current value
GTCCR = (1<<TSM)|(1<<PSRSYNC);
Serial.write(TCNT1H);
Serial.write(TCNT1L);
Serial.write(TCNT4H);
Serial.write(TCNT4L);
Serial.write(TCNT5H);
Serial.write(TCNT5L);
GTCCR = 0; //continue counting
...
Here is the circuit that drives my input capture pins. The LM224 is used as an active low-pass filter that converts my PWM input to a ~DC voltage for the comparator (LM393). I've added some headers that are used with SHUNTS to bypass the comparator stage and take my input signal right into the input capture pin.
There are three circuits that look just like this. The output "TIMERn_INPUT_CAPTURE" is tied to the corresponding input capture pins on the ATMega 2560.
I've tied all the CHn_SIGNAL pins to a stimulus pin so that I can examine my checkout code. In reality the CH1_SIGNAL comes from a different source (in my application its the emitter side of a PhotoTransistor component but I've eliminated that input as being the problem).
With J# set to bypass this comparator circuit, the counts stay perfectly inline and the input capture values are reasonable (sometimes a few clicks off from one another, but I suspect that much with them running at 16MHz with no noise rejection enabled). Here is an image of the output to my computer from the controller that shows that the counts are matching:
Now when I re-enable my comparator and run the same code, the HIGH bytes becomes un-syncronized. What is odd is that my LOW bytes stay perfectly in sync!
Here is an example of what I get when I enable my comparator circuit (in this one only TCNT4H is off, but I've seen TCNT5H off before as well, relative to TCNT1H):
One note (not sure how it is related): Yesterday afternoon when I was troubleshooting this circuit/software it was warmer (~78 deg F) and the HIGH values were very off. This morning when I came in and went back into troubleshooting it was a little cooler (~72 deg F) and I noticed that the problem still exists, but the amount that the HIGH byte was off was only by 1 or 2 and not by 100+. Not sure if is related...
Thanks for any of your input. I highly appreciate it.
-Nic