Toggle program branch ussing interrupot by button?


Closed Thread
Results 1 to 14 of 14
  1. #1
    Join Date
    Feb 2013
    Posts
    1,122

    Default Toggle program branch ussing interrupot by button?

    The idea is as follows.

    When launched, the LOOP1 in program is working and doing something. If user presses the button, interrupt is generated and LOOP2 engaged. When button pressed again, LOOP2 terminates and LOOP1 starts.

    Any example code?

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: Toggle program branch ussing interrupot by button?

    Have a flag (bit variable) which you toggle in the ISR.
    In the main program you poll the flag and decide which subroutine (loop) to execute.
    This will have the potential drawback of always finishing the current subroutine (loop) before switching, is that a problem?

    /Henrik.

  3. #3
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Default Re: Toggle program branch ussing interrupot by button?

    with PBP "on interrupt" it looks this figure is covered in the manual @ $ 5.66 ( RESUME xxx ) : you can choose the "return" adress xxx as you want ... so LOOP2 always completes.

    - also note if you do not clear the interrupt flag ( With a DEFINE ) you automatically re-enter the ISR when completed !

    - For DT interrupts ... I didn't check if the resume address can be freely chosen ...

    - last idea is simply to overwrite the interrupt return address on the stack ... ( with asm "push" and "pop" macros see midrange manual, interrupts chapter ), depending on the button state.

    Alain
    Last edited by Acetronics2; - 24th August 2013 at 09:38.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  4. #4
    Join Date
    Feb 2013
    Posts
    1,122


    Did you find this post helpful? Yes | No

    Default Re: Toggle program branch ussing interrupot by button?

    I don't want to include the button status check routine in the loops, since whole program works with microsecond delays and high accuracy. This is why I wanted to use interrupt. LOOP1 and LOOP2 are totally independent tasks, they don't use same variables, timings or whatsoever.

  5. #5
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    219


    Did you find this post helpful? Yes | No

    Default Re: Toggle program branch ussing interrupot by button?

    That's a good question...

    I wonder if something like this would work?

    Code:
    x VAR BIT
    
    ;----------------------- Setup Interrupts --------------------------------
    INCLUDE "DT_INTS-18.bas"     ; Darrel Taylor's Base Interrupt System
    INCLUDE "ReEnterPBP-18.bas"  ; Include if using PBP interrupts
    ASM
    INT_LIST  macro ; IntSource,          Label,  Type, ResetFlag?
        INT_Handler    INT0_INT,     Button_ISR,   PBP,  yes
        
        endm
        INT_CREATE             ; Creates the interrupt processor
    ENDASM
    
    ;----------------------[Initialise Interrupts]-----------------------
    @   INT_ENABLE  INT0_INT         ; Enable Button interrupt input
    x = 0       ' Clear button status flag bit
    
    PAUSE 100
    GOTO loop1  ' Start at loop1 routine
    
    ' ---------------- Independent program routines -------------------------
    loop1_start:
    @ INT_RETURN    ; Reset the ISR
    
    loop1:
        ' Perform one type of "time critical" function
    GOTO loop1
    
    
    loop2_start:
    @ INT_RETURN    ; Reset the ISR
    
    loop2:
        ' Perform a different type of "time critical" function   
    GOTO loop2
    
    ;--------------------[Program routine selector ISR]----------------------
    ; make sure button signal debounced
    
    Button_ISR:    
        x = !x      ' Toggle x status flag bit
        
        IF !x THEN
            GOTO loop1_start    ' Go here when x=0
        ELSE 
            GOTO loop2-start    ' Go here when x=1
        
    @ INT_RETURN
    Like Henrick said, the routines could be cut short anytime the button is pushed though.
    Louie

  6. #6
    Join Date
    Feb 2013
    Posts
    1,122


    Did you find this post helpful? Yes | No

    Default Re: Toggle program branch ussing interrupot by button?

    Thanks. As I can see, the code is for PIC18xxx family. Will it work on 12xx or 16xx ?

  7. #7
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    219


    Did you find this post helpful? Yes | No

    Default Re: Toggle program branch ussing interrupot by button?

    Not for PIC12's or 16's.
    You'll have to download DT's Interrupts for them here and replace entry with:
    Code:
    INCLUDE "DT_INTS-14.bas"    ; Base Interrupt System
    INCLUDE "ReEnterPBP.bas"    ; Include if using PBP interrupts
    I was kinda hoping someone would point out my errors in my suggestion, if any, before you went too far into a possible dead end.

    If you haven't tried DT's INTS, you're missing out on some awesome timing control when it comes to sequence of events. It took me a little while to understand but finally got some handle on it when I monitored two interrupts taking turns to run their routines, especially when one occurs while the other ISR is taking care of business.
    Louie

  8. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: Toggle program branch ussing interrupot by button?

    Hi,
    I was kinda hoping someone would point out my errors in my suggestion, if any, before you went too far into a possible dead end.
    Ok Louie, I'll take a stab at that if you wish... :-)

    If the program is looping around in Loop2 (x=1) when the interrupt occurs the ISR will toggle x to 0 and then GOTO Loop1_Start.
    At Loop1_Start there's an @INT_RETURN which will send it back into Loop2 since that's where it was when the interrupt occured.

    /Henrik.

  9. #9
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    219


    Did you find this post helpful? Yes | No

    Default Re: Toggle program branch ussing interrupot by button?

    Thanks Henrik, I stand corrected.

    CuriousOne, I tried to do something like this in the past and thought I came up with a better idea all of a sudden, but to avail.
    Louie

  10. #10
    Join Date
    Feb 2013
    Posts
    1,122


    Did you find this post helpful? Yes | No

    Default Re: Toggle program branch ussing interrupot by button?

    I found a sample code here:

    http://darreltaylor.com/DT_INTS-14/hello.html

    The code is OK for me, since I also need a selected loop indication via led. What if I mod it a like this:

    Code:
    '---[INT - interrupt handler]---------------------------------------------------
    ToggleLED1:
         TOGGLE LED1
    IF LED1=1 then goto LOOP1 ELSE GOTO LOOP2
    
    LOOP1:
    
    LOOP2: 
    @ INT_RETURN

  11. #11
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    219


    Did you find this post helpful? Yes | No

    Default Re: Toggle program branch ussing interrupot by button?

    Sorry for the delay, but I don’t think that would be a good solution since the ISR should only spend enough time in there to update values, take readings, turn things ON/OFF, etc.. Pretty much just get in and get out.

    Your example is like the one Henrik suggested but within the ISR.

    But got another wild idea if your tight loop can sacrifice just a little time to test the ISR flag bit:
    Code:
    INT VAR BIT
    
    INT = 0
    '------------------------
        
    loop1:
        code
        IF INT THEN GOTO loop2   ' Jump to loop2 after loop1 finished if interrupt triggered
    GOTO loop1                                                  
    
    loop2:
        more code
        IF !INT THEN GOTO loop1  ' Jump to loop1 after loop2 finished if interrupt triggered
    GOTO loop2
    
    
    ; --------------  ISR toggles a flag bit ----------
    Button_ISR:
        
        INT = !INT
        
    @ INT_RETURN
    This way, I'm thinking, any one of the loops can complete it's mission before jumping to the other routine should an interrupt happen. Or just continue in it's loop until the button is pushed.
    Louie

  12. #12
    Join Date
    Feb 2013
    Posts
    1,122


    Did you find this post helpful? Yes | No

    Default Re: Toggle program branch ussing interrupot by button?

    LOL, I already did in the same way, just it became necessary to rise clock frequency to 32mhz. But I don't use any interrupts, and it works just fine.

  13. #13
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    219


    Did you find this post helpful? Yes | No

    Default Did it already

    Hehe, that is funny.
    Glad you got it going.
    Louie

  14. #14
    Join Date
    Feb 2013
    Posts
    1,122


    Did you find this post helpful? Yes | No

    Default Re: Did it already

    Yes, but I still want to get with interrupts, because I have more choices to do with button, and putting many IF THENs slows down code considerably (it should run with 100 microsecond accuracy).

Similar Threads

  1. TOGGLE will not go LOW
    By SUNFLOWER in forum mel PIC BASIC Pro
    Replies: 17
    Last Post: - 10th August 2014, 16:53
  2. toggle switches and buttons-- need little help
    By electromark in forum General
    Replies: 2
    Last Post: - 25th October 2012, 22:35
  3. Toggle command
    By mr.sneezy in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 16th December 2011, 02:07
  4. toggle or count?
    By earltyso in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 26th May 2008, 08:54
  5. BRANCH/BRANCHL for GOSUB
    By selbstdual in forum PBP Wish List
    Replies: 11
    Last Post: - 20th February 2007, 14:29

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