IntervalTimer and Interrupts

Status
Not open for further replies.

airpanther

Active member
Good day,

I have a function that I would like to run at regular intervals without disrupting the rest of the program. I'm using an IntervalTimer, which basically sets a series of pins high then low, which should then trigger the associated interrupts that generate a "1" or "0" on "falling."

When I run the code below from the "loop" function, I get 0111, which is exactly the output I want. But when I run it in an IntervalTimer, the output is simply 01. Basically, no matter how many times I set the pins high then low, I only get back "01". I've tried setting the priority, which doesn't seem to help. Does IntervalTimer cause interrupts to stop running?

Code excerpt:

const int binaryZeroPin = 9;
const int binaryOnePin = 10;

IntervalTimer iTimer1;

void setup()
{
iTimer1.begin(iTimer_tick, 10000);
}

void loop()
{
// Other stuff
}

void iTimer_tick()
{
// Trigger an interrupt that outputs a "0"
digitalWrite(binaryZeroPin, HIGH);
delayMicroseconds(15);
digitalWrite(binaryZeroPin, LOW);

// Trigger an interrupt that outputs a "1"
digitalWrite(binaryOnePin, HIGH);
delayMicroseconds(15);
digitalWrite(binaryOnePin, LOW);

// Trigger an interrupt that outputs a "1"
digitalWrite(binaryOnePin, HIGH);
delayMicroseconds(15);
digitalWrite(binaryOnePin, LOW);

// Trigger an interrupt that outputs a "1"
digitalWrite(binaryOnePin, HIGH);
delayMicroseconds(15);
digitalWrite(binaryOnePin, LOW);
}

Thanks so much for any input you can provide!

Robert
 
You need to add this in setup()

Code:
  pinMode(binaryZeroPin, OUTPUT);
  pinMode(binaryOnePin, OUTPUT);

You might also wish to add 3 more delayMicroseconds(), since you're writing 6 times to the same pin. The LOW time will be very short. Here's the waveforms your code produces (with pinMode added) when running on a Teensy 3.6.

file.png
 
You need to add this in setup()

Code:
  pinMode(binaryZeroPin, OUTPUT);
  pinMode(binaryOnePin, OUTPUT);

You might also wish to add 3 more delayMicroseconds(), since you're writing 6 times to the same pin. The LOW time will be very short. Here's the waveforms your code produces (with pinMode added) when running on a Teensy 3.6.

View attachment 14434

Paul,

Thanks so much for the recommendation! I have corrected the waveform. I tried added delayMicroseconds() to various spots with various durations. However, I still get the same thing. I also tried longer/shorter frequencies for the IntervalTimer.

If I run the function from "Loop" it works perfectly, but if I run it as an IntervalTimer, I only trip each interrupt one time; Even if I take the pin high/low 20 times, each of the two interrupts only activates one time and I get back "01" every time instead of "0111" or "0101011001010" or whatever.

Also worth noting, every other command in the (short) IntervalTimer works fine... the associated interrupts just aren't firing. But they DO fire if I run the exact same code from "Loop." Any thoughts?

Robert
 
Last edited:
I have no idea what you're talking about with "trip each interrupt one time" or "I get back "01" every time instead of "0111" or "0101011001010" or whatever."

The code you showed creates waveforms on 2 pins. That's all I can see here. I have no idea what else you're doing. I can't see your project, nor your computer screen, nor read your mind over the internet. All I can see if what you've shown, and the extent of what you've shown is generating waveforms on 2 pins.

We're pretty good at helping on this forum when you post complete code and show what you're really trying to do. Hopefully this helped with the 2 pins....

Maybe all this talk is about whatever you're connecting to those 2 signals? Maybe it's another Teensy? Or some non-Teensy board? Or some other system? Who knows?!
 
Like Paul,

I have no idea of what you are actually doing...

Hard to know what you mean in the code:
Code:
// Trigger an interrupt that outputs a "0"
digitalWrite(binaryZeroPin, HIGH);
delayMicroseconds(15);
digitalWrite(binaryZeroPin, LOW);
What interrupt? Do you have something else connected up to pins 9 and 10?

Do you have an Interrupt Handler set for these? Again only guessing as don't see enough here to know...
But have you done anything with the Interrupt priorities? That is are they probably all set to the default Priority? 128?

If so when you are in the Interrupt handler for IntervalTimer my guess is that the IO pins handler will not preempt the code and as such will wait to call you interrupt handler when your Interval Timers Interrupt returns...

Wonder what happens if you add: iTimer1.pririty(192);
To your startup code. This puts the Interval Timers Interrupt at a lower priority, which should allow the default port handler interrupts to preempt...
 
Like Paul,

I have no idea of what you are actually doing...

Hard to know what you mean in the code:
Code:
// Trigger an interrupt that outputs a "0"
digitalWrite(binaryZeroPin, HIGH);
delayMicroseconds(15);
digitalWrite(binaryZeroPin, LOW);
What interrupt? Do you have something else connected up to pins 9 and 10?

Do you have an Interrupt Handler set for these? Again only guessing as don't see enough here to know...
But have you done anything with the Interrupt priorities? That is are they probably all set to the default Priority? 128?

If so when you are in the Interrupt handler for IntervalTimer my guess is that the IO pins handler will not preempt the code and as such will wait to call you interrupt handler when your Interval Timers Interrupt returns...

Wonder what happens if you add: iTimer1.pririty(192);
To your startup code. This puts the Interval Timers Interrupt at a lower priority, which should allow the default port handler interrupts to preempt...

KurtE, that was it!! Thanks SO much, and thanks to you Paul for getting the conversation going and suggesting the second delay. I had changed the priority, but hadn't set it high enough.

Thanks a bunch to you both!

Robert
 
Status
Not open for further replies.
Back
Top