What is wrong with this? (Variables in interrupt undeclared)

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

I'm still quite a novice, but I have looked around for a solution to this problem and... I haven't been able to find anything. So I beg of you:

ISR(TIMER1_CAPT_vect){

/*This subroutine checks was it start of pulse (rising edge)

or was it end (fallingedge)and performs required operations*/

	if (ICES1) //if high level
            {
			//save start time
            int rising=ICR1;
            //set to trigger on falling edge
            TCCR1B = (0<<ICES1);
			}
	else
            {
            //save falling time
            int falling=ICR1;
            //rising edge triggers next
            TCCR1B = (1<<ICES1);
            int counts=falling-rising;
            /*you can convert coutns to seconds and send to LCD*/
            }

}

That is just the ISR. Basically, I am trying to use an ultrasonic ranger that outputs Pulse width. It also outputs analog, but for some reason, I didn't want to set up this AVR to do ADC. If that would be much easier, please let me know.

It seemed like the PWM was more stable... even though it comes from the same source as analog. This sensor also outputs serial, but it is all garbled and would need decoding. It sends out ascii instead of raw bytes...

Anyway. Thank you very much!

UPDATE:

I declared the variables as volatile uint16_t outside the ISR. I'm going to test it soon.

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

> I declared the variables as volatile uint16_t outside the ISR.

Which variables? All the variables you are using inside that ISR are
declared locally, so they are completely discarded at the end of the
ISR. In fact, as all the computed values are not used at all, the
optimizer will kick them out of the final program.

It seems you'd better start with a basic C tutorial and/or book.
The "K&R" (Kernighan/Ritchie, The C Programming Language) is one of
the most frequently cited books for that purpose.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

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

First, you haven't given us enough information. What exactly is the problem (just saying that it doesn't work isn't enough)? Showing us just the ISR makes the assumption that everything else is correct. Show us the timer and port initialization. Show us a schematic. And also tell us what AVR you are using.

That being said, these lines are probably wrong:

            //set to trigger on falling edge 
            TCCR1B = (0<<ICES1); 

           //rising edge triggers next 
            TCCR1B = (1<<ICES1); 

The first line sets all bits of TCCR1B to 0, and the second sets the !CES1 bit to 1 and all other bits to 0. Since most 16 bit timers have the pre-scaler bits in TCCR1B, then both of these lines will turn the timer off.

I would recommend reading the "Programming 101" thread in the tutorials forum.

Regards,
Steve A.

The Board helps those that help themselves.

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

Thank you very much for the response, and I never realized the error of something = (1<<something) versus |= or &=

I think that will make a few headaches go away. Instead of going the pulse-width measuring route, I ended up measuring analog voltage. Since the sensor doesn't sense at very close ranges, I'm really just using it as a comparator.

(which makes me now think that I could've just used a hardware comparator and read it digitally. Hindsight 20-20)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
if (ICES1) //if high level 

I find it exceedingly unlikely that this line is doing what you think when I later see:

TCCR1B = (1<<ICES1);

I'll bet the if() was supposed to be something like:

if (TCCR1B & (1<<ICES1)) //if high level 

because I'll bet ICES1 is really just a number between 0 and 7

Cliff