1) Modify ElapsedMicros in an interrupt? 2) how much code should I put in the interru

Status
Not open for further replies.

3Dstereo

New member
Hey - first time posting. It has been a while since doing any micro-controller stuff and the teensy4 has so much power I am in awe. 600Mhz!! zoinkers so amazing.

1) Is it OK to modify the elapsedMicros in an interrupt?
I used to have it within the interrupt but then wasn't sure about "volatile" and if I could change the value inside the interrupt.

My current code is attached. It is an optical tach good for RPM's from 500-3500RPM with one pulse per revolution...

2) how much code is "ok" to put in the interrupt?
I used to have most of the RPM calculation in the interrupt(and moved it to the first IF statement in my loop), but had some other issues and moved it out when I was trying to figure it out. But I think the code would look cleaner if I could just do the RPM calculation in the interrupt-- so if it is OK to modify elapsedMicros inside of the Interrupt.

So the pseudo code is something like:

Interrupt_on_Falling edge { set a flag saying saw the interrupt}

loop {
is flag set and debounced?
- use elapsedMicros to find difference from last time
- set elapsedMIcros back to zero
- calc RPM
- reset flag

DisplayEverySoOften
- send average of RPM's since last display to the display
}

I do have some code to wait 250uS as a "debounce"-- but I don't think I actually need it anymore so may take it out.

Anyway, the real question I have is if it is OK to reset the elapsedMicros within an interrupt or if that would be bad. And if it is ok, should I move the RPM calculation stuff into the interrupt as well.

Thank you!

Lincoln
 

Attachments

  • Interrupt__WithElapsed_Question.ino
    5.6 KB · Views: 22
Elapsed micros is just a 32 bit unsigned integer with some operations on it involving simple math operations - so using it in an _isr will use some cycles - but not do anything unsafe and extreme.

If it of concern the math it does could be done manually easily enough taking micros() into a uint32_t myTime=micros() and then doing compares on it manually like : if ( micros() - myTime > 250 )

How much time in an _isr() is too much depends on the code at hand, and what else is going on, and how frequent the interrupt is

> As long as the _isr() code generally completes before the next call to it happens there is time left for other tasks, if the interrupts occur too fast to complete - then nothing else can happen

> if other time sensitive or hardware operations are in progress in the loop() code - then taking too long in the _isr() then that time could impair normal operation of those elements

There are other ways to cause confusion or problems if too long is taken or the tasks processed there call into complex code.
 
Awesome.

I was worried about it(specifically "ellapsedMillis object" not being "volatile" and changing it in the interrupt service routine. But sounds like that is not going to be a problem.

I think the interrupt will trigger in the worst case every 20milliseconds... so that seems like will be oodles of time to do stuff there. I won't do slow stuff like using the uarts -- really just doing integer math mostly.

Thank you for your help and insight, much appreciated.
Lincoln
 
Status
Not open for further replies.
Back
Top