ATMega16 - Input Capture problems with timer1

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

Hi

I am trying to measure the time period coming from an IR remote. The low byte coming from ICR1L is correct (for shorter periods) but whenever I read the high byte it seem to be wrong. This is the very first period being measured. I am clocking timer at 32Khz and for a 9ms pulse I am getting values in the high byte that give me a period of about 247ms. Any suggestions?

// Timer 1 input capture interrupt service routine
interrupt [TIM1_CAPT] void timer1_capt_isr(void)
{

char TmpLow;
char TmpHi;
TmpLow = ICR1L; // low byte must be read before hi
TmpHi = ICR1H;
TCNT1H = 0;
TCNT1L = 0;
TCCR1B = TCCR1B ^ 0x40; // toggle edge detection flag
TIFR = TIFR | 0x10; //clear interupt flag after bit toggle
TCCR1B = TCCR1B | 0x04; //start timer

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

I don't think that you've shown enough of the code to determine what the problem might be. What you need is code to compute the difference in the ICR value between two successive edges (hint: don't clear the timer register).

For the first edge, just store the ICR value. For subsequent edges, compute the difference between the ICR value for the current edge and the last-stored ICR value and then store the current ICR value for the next edge.

You don't need the line with the comment "//start timer" - the timer will continue to run until you turn it off.

Also, you'll want an ISR for Timer1 overflow. If you get two overflow interrupts without an intervening input capture interrupt you'll know that too much time has elapsed to be measured.

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

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

"char" is a signed variable type. Weird things are going to happen when you are combining to signed variable "pieces" that should be unsigned.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Another thing to remember is that you can get random pulses from your IR sensor when nothing is being received which makes measurement a little more difficult.

For IR stuff and other low frequency things, I use my PC's soundcard and cooledit2000 software as an oscilloscope so I can see what is happening.

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

theusch wrote:
"char" is a signed variable type.

Does the standard actually say that?

(for example, GCC programs by default are usually built with a "-funsigned-char" parameter to the compiler so if signed/unsigned is not explicitly stated unsigned is used)

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

As is often the case, we aren't given all the information, are we?

Yes, "char" is a signed type. Whether the OP chooses to use a compile option for the unspecified compiler [but the ISR syntax hints at CodeVision] is what inquiring minds want to know.

We also don't see where the 8-bit pieces are put together, or how they are output.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Thanks to all who replied - sorry if some info missing. Yes it is Codevision and all the chars are set to unsigned by means of a compiler option. I have sorted out my problem - the Codevision wizard seems to have a bug regarding one of the timer set up bits.