16F676 Interupt on Change pin determination


Closed Thread
Results 1 to 18 of 18

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Well, that definately looks better, and it may work better for now, but it still needs a couple things.

    The Interrupt "Handler" should be enclosed within DISABLE/ENABLE statements to keep the Interrupt Handler itself from being interrupted again.

    The clearing of the Flag should be done in the handler, not the point that execution resumes. If you RESUME without clearing the flag first, you can end up right back in the Handler again.

    And, "resume BASE" should only be "RESUME".
    When specifying a location to RESUME to, it essentially does a GOSUB to that routine when the Handler is finished. However, in this program, the PIC is told to go to Sleep, without ever returning from that GOSUB. So on each interrupt, it pushes another address on the stack, which overflows after 8 interrupts. For a 16F, the Stack will wrap around to the beginning again without causing a reset, so you may never see the problem right now, but if there are any other subroutines involved, they will never get a chance to RETURN, or will return to the wrong place.

    Because of the "Spaghetti" nature of this program, these problems may not be evident because it just falls back into the program and continues on, but when you start adding things later, you will lose a large portion of whatever hair you may have left trying to figure it out.
    <br>
    DT

  2. #2
    Join Date
    Oct 2004
    Location
    Fayetteville Arkansas
    Posts
    44


    Did you find this post helpful? Yes | No

    Default

    Thank you Darrel. I appriciate the help.

  3. #3
    Join Date
    May 2004
    Location
    New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    Hi Darrel,
    I'm a bit confused by the idea that a "RESUME (label)" is like a GOSUB. From the following quote in the PBP manual, it sounds more like RESUME (label) acts like a GOTO:

    When the RESUME statement is encountered at the end of the BASIC
    interrupt handler, it sets the GIE bit to re-enable interrupts and returns to
    where the program was before the interrupt occurred. If RESUME is
    given a label to jump to, execution will continue at that location instead.
    All previous return addresses will be lost in this case.
    I've always used RESMUE with a label, as I want to RESUME at a known location in the code, and have never had any issues, even in large programs with lots of other sub-routines.

    Any further info would be great. Thanks,

    Arch

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    BUT as it erase all previous return address... it just clear the stack at the same time imho.

    Let's say you're limited to a 4 level deep stack, everytime you'll RESUME you will eat one in place in the stack. Since your code don't do any Gosubs, it never give you problem, but one day, if you're at the stack limit, and then you add a RESUME LABEL, you may get this problem. Something to be tested i guess.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    Join Date
    May 2004
    Location
    New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mister_e
    Since your code don't do any Gosubs, it never give you problem, but one day, if you're at the stack limit, and then you add a RESUME LABEL, you may get this problem. Something to be tested i guess.
    Hi Steve,
    Well, that's what got me wondering. In other code with lots of gosubs, using a RESUME (label) has not caused any problems.
    If the stack gets cleared with the RESUME (label), how would the stack overflow occur?

    Thanks,
    Arch

    EDIT
    Interesting info Darrel. Can't figure how any other code I've written with ON INTERRUPT continues to work - by the look of what you posted, the code should crash or go off in odd directions - but for some reason it does work OK even after a RESUME (label) followed by several subroutines. I'll have to try and figure that one out one of these days.

    On that little sample above - I don't think the DISABLE / ENABLE are needed, as the interrupt handler comes before the ON INTERRUPT statement (at least that's what the manual says).
    EndEDIT
    Last edited by Archilochus; - 5th May 2006 at 04:22.

  6. #6
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Forget me... see Darrel's explanation above. Sorry i didn't write a single code line and look the compiled file OR, like Darrel, look the According Macro.

    Once again i failed

    But BTW, if you never had any failure, it's probably because you don't use much nested gosub AND/OR you're a lucky guy!

    Thanks Darrel, i just see some interesting stuff to do with that RST?RP now I think i'll go deeper in the .LIB when i'll be back in Canada.
    Last edited by mister_e; - 5th May 2006 at 04:21.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  7. #7
    Join Date
    May 2004
    Location
    New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    I'm not sure how to interpret this bit from the same file that Darrel mentioned above:

    ;************************************************* ***************
    ;* ONINT?LL : Macro - On Interrupt Goto *
    ;* *
    ;* Input : L interrupt handler label *
    ;* : L interrupt checker label *
    ;* Output : None *
    ;* *
    ;* Notes : *
    ;************************************************* ***************

    ONINT?LL macro Inthand, Intchk
    local label
    bsf INTCON, GIE
    L?GOTO label
    Intchk btfsc INTCON, GIE
    return
    L?GOTO Inthand
    label
    endm
    ONINT_USED = 1
    endmod
    From this, it looks like PBP does a GOTO to get to the interrupt handler (inthand?) - so no address would be placed on the stack since the handler is not a subroutine, is that right?

    Arch

  8. #8
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    OK, so let's look at how the whole process of ON INTERRUPT works.

    When an Interrupt occurs, the hardware turns off the GIE bit, pushes the current address on the stack and then vectors to address 4.

    At address 4, PBP only puts 1 opcode. RETURN

    This pops the hardware generated address from the stack and immediately returns execution to where it was before the interrupt. But leaves the GIE bit turned off, so that no other interrupts can occur until the last one is handled. Because, it can only be Handled, after PBP is done with whatever it was doing at the time.

    Then in-between each and every line of code (that's not DISABLED) it does a CALL to the interrupt "Checker" which simply checks to see if GIE is a 0 or not. If it is 0, it means an interrupt occurred and it does the GOTO that you see in the ONINT?LL macro;

    It's that CALL to the checker that puts the address on the stack, so that it can return back to the point in-between lines that it jumped from.

    So again, if a return never happens, the stack builds up.

    An interesting side note is that many people think you can turn OFF all interrupts by clearing the GIE bit (myself included previously). But what that really does with ON INTERRUPT is to make PBP think that an interrupt has happened, it calls the handler (which shouldn't do anything since no Flags are set) and then turns GIE back on again. In order to turn off interrupts globally, that section of code must be DISABLED.

    Can't figure how any other code I've written with ON INTERRUPT continues to work.
    It all boils down to the one statement in the manual that seems so innocuous.
    All previous return addresses will be lost in this case.

    As long as you never want to go back to the sections of code that got interrupted, then the stack simply wraps around, and only NEW gosubs past that point will be returned to. That's not something that any of my programs would be able to tolerate.

    On that little sample above - I don't think the DISABLE / ENABLE are needed, as the interrupt handler comes before the ON INTERRUPT statement (at least that's what the manual says).
    Yup, you're right, anything before the ON INTERRUPT statement is considered DISABLED. My Bad.

    HTH,
    &nbsp; Darrel

  9. #9
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Yes, the manual is a bit vague. But if we take a look at the macro's themself it should be a little clearer.

    These are from the PBPPIC14.mac file.
    Code:
    ;****************************************************************
    ;* RESUME?    : Macro - Resume                                  *
    ;*                                                              *
    ;* Input      : None                                            *
    ;* Output     : None                                            *
    ;*                                                              *
    ;* Notes      :                                                 *
    ;****************************************************************
    
    RESUME? macro
            RST?RP
            retfie
        endm
      endmod
    
    ;****************************************************************
    ;* RESUME?L   : Macro - Resume Label                            *
    ;*                                                              *
    ;* Input      : Label                                           *
    ;* Output     : None                                            *
    ;*                                                              *
    ;* Notes      :                                                 *
    ;****************************************************************
    
    RESUME?L macro Label
            bsf     INTCON, GIE
            L?GOTO  Label
        endm
      endmod
    With only the RESUME it does a retfie (Return from Interrupt) which causes the hardware to turn GIE back on and pops the last address off the stack to return to.

    But, if you include a label with the resume, all it does is set the GIE bit manually, then GOTO's the label. At this point, the address from the interrupt is still on the stack. Once the Subroutine has finished and does a RETURN. It will return to the point where the interrupt first happened.

    If the subroutine doesn't do a RETURN, the stack keeps building up.

    Also, that subroutine should be enclosed in DISABLE/ENABLE statments to keep it from being interrupted before it returns.

    If you're OK with the idea of the stack wrapping around and Discarding all previous gosubs after an interrupt, then it'll still work that way on a 16F. But don't try it with an 18F.

    HTH,
    &nbsp; Darrel

Similar Threads

  1. Is this a K Type sensor?
    By jessey in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 21st November 2009, 14:55
  2. HELP !!! How change the voltage of a pin ????
    By stormdacta in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 21st August 2007, 21:55
  3. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 18:27
  4. Another RTC, DS1287
    By DavidK in forum Code Examples
    Replies: 0
    Last Post: - 12th December 2006, 18:07
  5. 16F676 Interupt
    By BGreen in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 5th March 2006, 21:56

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts