Menu

Interrupt during a wait

Help
viscomjim
2015-04-04
2015-04-05
  • viscomjim

    viscomjim - 2015-04-04

    I have a program that has several "sub programs" that will run. These sub programs are selected using an interrupt to select the next sub program. So as one presses the button, the next sub program runs in a loop until the next button press.

    One of the sub programs will have several long wait statements in it, as shown as an example in "sub prog3". If the button is pressed while the unit is executing the wait, the interrupt does increment the progselect properly, but it jumps back to the wait till it is done and then runs the next sub program.

    If I add a "goto main" inside the interrupt subroutine to get out of the wait 30 s, the program fails. I know for sure that the interrupt is working during the wait, it just goes back to waiting till it is done and then goes to the next sub program.

    Is there a better way to go about this? It works very well as long as it is not waiting for a long period of time.

    Thanks for looking!!!!!

    #chip 12F1840 , 16
    
    #DEFINE SWITCH PORTA.3
    DIR SWITCH IN
    
    SET IOCAN3 ON
    ON Interrupt PORTBCHANGE CALL SWTEST 'INTERRUPT WHEN SWITCH GOES LOW
    
    PROGSELECT = 1
    
    '*********************** MAIN PROGRAM *******************
    MAIN:
    
    SELECT CASE PROGSELECT
    
    CASE 1
    PROG1
    
    CASE 2
    PROG2
    
    CASE 3
    PROG3
    
    END SELECT
    
    GOTO MAIN
    
    '*********************** SUB PROGRAM 1 *******************
    SUB PROG1
    DO
     'DO A BUNCH OF STUFF
    LOOP WHILE PROGSELECT = 1
    END SUB
    
    '*********************** SUB PROGRAM 2 *******************
    SUB PROG2
    DO
     'DO A BUNCH OF STUFF
    LOOP WHILE PROGSELECT = 2
    END SUB
    
    '*********************** SUB PROGRAM 3 *******************
    SUB PROG3
    DO
     'DO A BUNCH OF STUFF
    WAIT 30 S   '<--- CAN'T GET OUT OF THIS WITH BUTTON PRESS
    LOOP WHILE PROGSELECT = 3
    END SUB
    
    '*********************** SWITCH INTERRUPT *******************
    SUB SWTEST
    IF SWITCH = 0 THEN 'DEBOUNCE ROUTINE FROM FORUM
    BUTTONCOUNT = 0
    DO WHILE SWITCH = 0
    wait 10 ms
    buttoncount +=1
    LOOP
    END IF
    if buttoncount > 3 then
        PROGSELECT = PROGSELECT + 1
        IF PROGSELECT = 4 THEN PROGSELECT = 1
    END IF        
    SET IOCAF3 OFF
    'GOTO MAIN  <--- TRY TO GET OUT OF WAIT HERE CRASHES PROGRAM
    END SUB
    
     
  • William Roth

    William Roth - 2015-04-05

    Instead of a single long delay use lots of smaller delays inside of a loop.

    Example:

    Dim delaycnt as word
    
    '' wait 30 sec
    for delaycnt = 1 to 3000
        wait 10 ms
       if (whatever)  exit for
    next
    
     

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.