Menu

Return From Interrupt Sub to a Specific Spot in the Code

Chay Foss
2017-02-07
2017-02-07
  • Chay Foss

    Chay Foss - 2017-02-07

    Is it possible to return from an interrupt subroutine to a specific spot (Label) in the code?

    I'm making a frequency counter/tach. The setup is a 16F74 at 20MHZ with a 4 bit serial LCD interface. The PIC reads a square wave on porta.1. I do some wait commands to deterine when the pin goes high and low, and use Timer1 to count the period, then I do some calcs and display the values. All is working great there.

    I set up an interrupt for Timer1 fro when there is no frequency present, or it's a very slow frequency (IE: The engine isn't running). This works great too. Basically I just display "Engine not running" using locate and print commands.

    The difficulty is:

    1) The interrupt can occur any time during the code
    2) The return from the interrupt is to the next bit of code, where ever that happens to be

    Normally that's a good thing. In this case, when I come back from the interrupt I may be in the pin low/high detection, which, when the signal is off, never happens, so the program hangs until the frequency becomes present again. Alternatively, if the code happens to be in the display writing procedure when the interrupt happens, I come back and overwrite my "engine off" message which looks weird!

    What I would like to do is come back from the SUB to a specific point in the code, where I can reset the timer and then start it again, looking for either the interrupt to happen again, or pick up the waveform if one happens to be there.

    I tried using a goto command from within the SUB. This worked, but because the code never returned from the SUB the interrupt flag was never reset and therefore it wouldn't detect again. The interrupt code executed once and then would not again.

    Any help would be appreciated,

    Thanks,

    Chay Foss

     
  • kent_twt4

    kent_twt4 - 2017-02-07

    I would suggest setting a flag like PinHigh = True in the interrupt sub rather than trying to go back to a specific location in Main (although that may or may not work). Then liberally spread (as required) conditional tests on that flag in Main so as to act on and reset that flag in another sub. That should keep from getting caught up in a never ending loop.

     
  • Anobium

    Anobium - 2017-02-07

    Exiting the ISR routine with a goto will mess up the stack handler. I think Kent's idea sound great.

     
  • Chris Roper

    Chris Roper - 2017-02-07

    Why not just disable Interupts in the the display sub and use a flag to show if the Frequancy is detected befor waiting for an edge detection?

     
  • Chay Foss

    Chay Foss - 2017-02-10

    Kent_twt4...The problem is that I'm currently using wait commands to see if the input goes true, or if it goes false. If I set a flag in the code and it comes back to the line of code waiting for a true, it will still hang.

    I'm going to investigate using do/while loops instead of wait commands, so that the flag can be read as part of the code. That way it shouldn't hang.

    Chris, I don't have a display sub, I write to the display twice. Once from the interrupt code, once from the normal frequency aquisition code. So, if it writes from the interrupt code, then return to the normal code it either hangs or overwrites the interrupt message with the normal code message.

    Really what I need is for the interrupt to block/skip the 'normal' code.

    Still trying to figure it out...it'll come to me I'm sure!!

    Chay

     
  • kent_twt4

    kent_twt4 - 2017-02-10

    I missed the fact about the frequency meter, this has been approached recently in the forum here https://sourceforge.net/p/gcbasic/discussion/579126/thread/8b1aa325/
    The code in the link would be a useful for a narrow range test case, that can be improved upon for the more generic full range of rpm.

    Use TMR0 and prescales along with an interrupt to count rollovers. That along with the TMR1 interrupt for the period should handle everything.

     
  • Chay Foss

    Chay Foss - 2017-02-10

    Yeah I was thinking about a dual timer appoach but hadn't reasoned out how it would work. Thanks for the info.

    Chay

     
  • William Roth

    William Roth - 2017-02-12

    @Chay

    Unless you are married to the 16F74 you should condider using a PIC 16F18875. This chip has a 24-bit Signal Measurement Timer (SMT) that operates independantly of the Core MCU. The SMT can be configured to constantly monitor the input pulses and calculate the period.

    All the main program has to do then is poll the SMT register when it wants the current period measurment value. So the program is never waiting and does not have to calculate the period.

    There is no need to use an interuput to detect low RPM or engine Stopped. Just have the main routine poll a flag. If the flag is set then the engine is stopped or at a very low RPM.

    William

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.