Input Capture on Mega128

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello all,

Does anybody know if it's possible to automatically clear the timer/counter register (TCNT3) upon receipt of an input capture event?

I know it's possible to automatically clear the counter for an _output_ capture event. But it appears to me it's not possible for an input capture event. At the moment I'm clearing the TCNT3 counter in my input capture interrupt service routine. Just wondering if anyone knows of a trick to clear it automatically.

Thanks very much.

Frank.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Open-source AVR harddisk-based MP3 player with USB 2.0 at
http://www.frankvh.com/mp3player
AVR MP3 Player Pages

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I was puzzled at first read of your post. But then I realized that you are using the ICP to time events, not count them?

If so, you are trying to reset the counter at the first event so that the second event is the time?

When I used this feature in an app (in ancient '8535, not the newfangled chips with all the counter options), I finally figured out that I had to save the first value anyway. Then when the next value came along I just did the 16-bit subtract to get the "elapsed"--timer rollover comes out in the wash. This way there are no problems with interrupt latency, assuming it isn't greater than the time between events.

Maybe you can trick it using CTC & ICR3: "In CTC mode the counter is
cleared to zero when the counter value (TCNTn) matches either the OCRnA (WGMn3:0 = 4) or the ICRn (WGMn3:0 = 12)."

Interesting question. The 16-bit subtract with no latency problems and no additional servicing is only a few cycles, though.

Lee

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

you're correct Frank. The avr will not clear the counter automatically.
Clearing the TCNT yourself is the way to go. Just make sure that this is the first thing you do in the interrupt routing. I clear the counter using 8-bit access
and the read the ICR register :

TCNT3H = 0;
TCNT3L = 0;
elapsedtime = ICR3;

Clearing the timer as soon as possible avoid the latency-problems

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"Clearing the timer as soon as possible avoid the latency-problems"

No, it really doesn't. Say when the capture event occurs you are in the middle of another interrupt routine for a period of time. When that routine is finished, another higher-priority interrupt may be ready to be serviced, resulting in more latency. By the time you get to the code fragment you gave, TCNT3 could be several/many counts different than the ICR3 value.

As I previously posted, you can let Timer3 run free and do the subtract. As long as the servicing gets done before the next capture, there is no problem.

My pseudocode for this (variables are unsigned int):

this_sample = ICR3;
elapsed_time = this_sample - previous_sample;
previous_sample = this_sample;

Lee

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the comments. It certainly looks like, if I want to clear TCNT3, I have to do it myself. Which in fact is what I'm currently doing. Lee, your comment about higher-priority interrupts is well taken.

Thanks all,

Frank.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Open-source AVR harddisk-based MP3 player with USB 2.0 at
http://www.frankvh.com/mp3player