Understanding interrupt on port state change

Discussion in 'Embedded Systems and Microcontrollers' started by ke5nnt, Jan 30, 2010.

  1. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    I need help in understanding how a state change interrupt works. More specifically, what kind of event can create a state change.

    My specific example is, a PIC micro powered with 5V to Vdd, and a switch to a port B pin that is normally open (at 0V). Will throwing the switch to supply that Port B pin with +5V be considered a "state change"?

    If that does induce a state change, would it essentially make that bit go from 0 to 1 assuming the voltage remains constant until the switch is thrown again?

    If yes, can I tell the the software to: (paraphrasing here)

    run normal program
    test port b pin skip if 0
    if 1 (voltage applied) goto a subroutine
    if 0 continue normal program


    I understand the need to enable interrupts in the INTCON register and also clear any interrupt flags after the interrupt is complete.

    Thanks
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Hi Ke5nnt
    Which PIC uC do you use, and what language do you program in
     
  3. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    1,015
    69
    Depending on the PIC, I believe you can configure the interrupt to fire on either low-to-high, high-to-low or both.

    The program you describe does not appear to need this interrupt, you would just read the switch input pin each time and take the appropriate branch if the pin is high.
    (Don't forget the pulldown resistor to ensure a low level when the switch is open).

    I've used interrupt on change, but only as a means of waking up a battery powered device when a keypad button was pressed. Whilst the device was running normally, I polled the inputs during it's RTC interrupt routine.
     
  4. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    At the moment a PIC 16F628A, and Assembly.

    Thanks, I'll try that. Appreciate the input (no pun intended).

    Regards.
     
  5. eng1ne

    Member

    Dec 4, 2009
    97
    3
    I am not really expanding on what others have said, they are right.

    To approach from a different perspective, an interrupt is analogous to a ringer on a telephone. It interrupts you to tell you you have a phone call.

    Without a ringer, you would have to repeatedly pick up the phone to see if anyone is there - this is the approach that you have suggested in your psuedo code.

    Neither way is better, it depends on your application. If you have such a dull life that you don't mind sitting next to the phone all day and checking every 5 minutes, then do it! Otherwise, buy a telephone with a ringer!
     
  6. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    I am apparently doing something wrong still, as it does not seem to want to work. Here is part of my code (PIC16F628A):

    Code ( (Unknown Language)):
    1.  
    2. ;************************************************************************
    3. ;PROGRAM INITIALIZATION
    4. INIT    BSF        STATUS,5    ;BANK 1
    5.         MOVLW    B'00000000'
    6.         MOVWF    TRISB        ;ALL PORT B ARE OUTPUT
    7.         MOVLW    B'00000011'
    8.         MOVWF    TRISA        ;RA0 AND RA1 INPUT, OTHERS OUTPUT
    9.         BCF        STATUS,5    ;BANK 0
    10.         BTFSC    PORTA,0        ;NIGHT MODE DESIRED?
    11.         GOTO    NMRY        ;FLASH RED/YELLOW
    12.         BTFSC    PORTA,1        ;OTHER NIGHT MODE DESIRED?
    13.         GOTO    NMRR        ;FLASH RED/RED
    14. ;*************************************************************************
    15. ;NORMAL SIGNAL OPERATION
    16. NORM    MOVLW    B'00001001'
    17.         MOVWF    PORTB        ;ALL TRAFFIC LANES RED
    18.         CALL    T3SEC        ;FOR 3 SECONDS
    19. NORM1    MOVLW    B'01001001'
    20.         MOVWF    PORTB        ;MAIN LANE TURN ARROW GREEN, ALL OTHERS RED
    21.         CALL    T10SEC        ;FOR 10 SECONDS
    22.         MOVLW    B'00011001'
    23.         MOVWF    PORTB        ;MAIN LANE YELLOW, ALL OTHERS RED
    24.         CALL    T4SEC        ;FOR 4 SECONDS
    25.         MOVLW    B'00001001'
    26.         MOVWF    PORTB        ;ALL TRAFFIC LANES RED
    27.         CALL    T3SEC        ;FOR 3 SECONDS
    28.         BTFSC    PORTA,0        ;R/Y NIGHT MODE DESIRED?
    29.         GOTO    NMRY        ;GO TO R/Y NIGHT MODE
    30.         BTFSC    PORTA,1        ;R/R NIGHT MODE DESIRED?
    31.         GOTO    NMRR        ;GO TO R/R NIGHT MODE
    I have 10K pull down resistors on RA0 and RA1. Regardless of whether 5V is applied to either RA0 or RA1, the program remains in "NORM".
     
  7. eng1ne

    Member

    Dec 4, 2009
    97
    3
    Post the T3SEC subroutine.
     
  8. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    Maybe I should have said the program just stays in normal operation and doesn't divert to either NMRY or NMRR. The program does not get hung up after the T3SEC subroutine, it continues flawlessly into "NORM1"...
     
  9. eng1ne

    Member

    Dec 4, 2009
    97
    3
    So presumably, you get the different combinations of outputs, i.e. main line yellow, all others red, which repeats itself?

    Is the bottom line the end of your code? Where does it GOTO if R/R Night Mode is not desired?

    Out of interest, have you employed any sort of switch debouncing on the inputs? Assuming they are mechanical switches.
     
  10. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    Switch debounce no, simply because it's nowhere near a precision or instantaneous action. The software is simply meant to read for a high state on either RA0 or RA1 at certain intervals --- and either remain in normal operation or go to 1 of 2 other operations depending on which pin is high (voltage applied) but not both.

    In other words, if RA0 and 1 are both low (no voltage) the program (a traffic signal) will function in normal mode, i.e. main lanes cycle green yellow red, then cross street cycles.

    If voltage is applied to RA0, the light will cycle until all lanes are red, and then read that pin RA0 is high, and go to a flashing light where yellow flashes on the main traffic lanes, and the cross street flashes red.

    If Voltage is applied to RA1, the light will cycle until all lanes are red, then read that pin RA1 is high, and go to a flashing light where red flashes all directions.

    When voltage is removed from the pin (whichever is high), the light will go to a brief transition mode then back to normal operation.

    The program in its entirety follows:

    Code ( (Unknown Language)):
    1.  
    2. ;************************************************************************
    3. ;TRAFFIC CONTROL SIGNAL                                                    *
    4. ;THIS SIGNAL IS INTENDED TO CONTROL TRAFFIC AT A 4-WAY INTERSECTION        *
    5. ;WITH THE "MAIN" ROAD HAVING A GREEN ARROW AND INDEPENDENT YELLOW        *
    6. ;CONTROL FOR STOPPING TURN TRAFFIC.  SECONDARY ROAD IS STANARD R-Y-G    *
    7. ;LIGHTS.  THIS PROGRAM ALSO HAS 2 "NIGHT MODE" OPERATIONS WHERE            *
    8. ;MAIN LANES FLASH YELLOW WHILE SECONDARY ROAD FLASHES RED, OR            *
    9. ;4-WAY FLASHING RED DEPENDING ON POSITION OF A PERIPHERAL SWITCH        *
    10. ;                                                                        *
    11. ;CREATED BY RYAN GOFF FOR THE PIC16F628A                                *
    12. ;************************************************************************
    13. ;CONFIGURATION
    14.     LIST        P=PIC16F628A
    15.     #INCLUDE    P16F628A.INC
    16.     __CONFIG B'10000100000000'
    17.     ERRORLEVEL    -302
    18. ;
    19.     ORG        01
    20.     GOTO    INIT
    21. ;
    22.     ORG        04
    23.     GOTO    INIT
    24. ;
    25.     COUNT1    EQU    22
    26.     COUNT2    EQU    23
    27. ;*************************************************************************
    28. ;SUBROUTINES
    29. T500MS    MOVLW    0x32
    30.         MOVWF    COUNT1
    31.         MOVLW    0x04
    32.         MOVWF    COUNT2
    33. T500MS1    DECFSZ    COUNT1        ;500MS DELAY FOR NIGHT MODE FLASHING
    34.         GOTO    $+2
    35.         DECFSZ    COUNT2
    36.         GOTO    T500MS1
    37.         RETURN
    38. ;
    39. T3SEC    MOVLW    0xBF
    40.         MOVWF    COUNT1
    41.         MOVLW    0x13
    42.         MOVWF    COUNT2
    43. T3SEC1    DECFSZ    COUNT1        ;3 SECOND DELAY FOR 4-WAY RED
    44.         GOTO    $+2
    45.         DECFSZ    COUNT2
    46.         GOTO    T3SEC1
    47.         RETURN
    48. ;
    49. T4SEC    MOVLW    0xFF
    50.         MOVWF    COUNT1
    51.         MOVLW    0x19
    52.         MOVWF    COUNT2
    53. T4SEC1    DECFSZ    COUNT1        ;4 SECOND DELAY
    54.         GOTO    $+2
    55.         DECFSZ    COUNT2
    56.         GOTO    T4SEC1
    57.         RETURN
    58. ;
    59. T10SEC    MOVLW    0xFF
    60.         MOVWF    COUNT1
    61.         MOVLW    0x40
    62.         MOVWF    COUNT2
    63. T10SEC1    DECFSZ    COUNT1        ;10 SECOND DELAY FOR GREEN TURN ARROW
    64.         GOTO    $+2
    65.         DECFSZ    COUNT2
    66.         GOTO    T10SEC1
    67.         RETURN
    68. ;
    69. T20SEC    MOVLW    0xFF
    70.         MOVWF    COUNT1
    71.         MOVLW    0x80
    72.         MOVWF    COUNT2
    73. T20SEC1    DECFSZ    COUNT1        ;20 SECOND DELAY FOR GREEN LIGHT
    74.         GOTO    $+2
    75.         DECFSZ    COUNT2
    76.         GOTO    T20SEC1
    77.         RETURN
    78. ;************************************************************************
    79. ;PROGRAM INITIALIZATION
    80. INIT    BSF        STATUS,5    ;BANK 1
    81.         MOVLW    B'00000000'
    82.         MOVWF    TRISB        ;ALL PORT B ARE OUTPUT
    83.         MOVLW    B'00000011'
    84.         MOVWF    TRISA        ;RA0 AND RA1 INPUT, OTHERS OUTPUT
    85.         BCF        STATUS,5    ;BANK 0
    86.         BTFSC    PORTA,0        ;NIGHT MODE DESIRED?
    87.         GOTO    NMRY        ;FLASH RED/YELLOW
    88.         BTFSC    PORTA,1        ;OTHER NIGHT MODE DESIRED?
    89.         GOTO    NMRR        ;FLASH RED/RED
    90. ;*************************************************************************
    91. ;NORMAL SIGNAL OPERATION
    92. NORM    MOVLW    B'00001001'
    93.         MOVWF    PORTB        ;ALL TRAFFIC LANES RED
    94.         CALL    T3SEC        ;FOR 3 SECONDS
    95. NORM1    MOVLW    B'01001001'
    96.         MOVWF    PORTB        ;MAIN LANE TURN ARROW GREEN, ALL OTHERS RED
    97.         CALL    T10SEC        ;FOR 10 SECONDS
    98.         MOVLW    B'00011001'
    99.         MOVWF    PORTB        ;MAIN LANE YELLOW, ALL OTHERS RED
    100.         CALL    T4SEC        ;FOR 4 SECONDS
    101.         MOVLW    B'00001001'
    102.         MOVWF    PORTB        ;ALL TRAFFIC LANES RED
    103.         CALL    T3SEC        ;FOR 3 SECONDS
    104.         BTFSC    PORTA,0        ;R/Y NIGHT MODE DESIRED?
    105.         GOTO    NMRY        ;GO TO R/Y NIGHT MODE
    106.         BTFSC    PORTA,1        ;R/R NIGHT MODE DESIRED?
    107.         GOTO    NMRR        ;GO TO R/R NIGHT MODE
    108.         MOVLW    B'10000001'
    109.         MOVWF    PORTB        ;MAIN TRAFFIC LANES GREEN, TURN ARROWS OFF, SECONDARY RED
    110.         CALL    T20SEC        ;FOR 20 SECONDS
    111.         MOVLW    B'00010001'
    112.         MOVWF    PORTB        ;MAIN TRAFFIC LANES YELLOW + SECONDARY RED
    113.         CALL    T4SEC        ;FOR 4 SECONDS
    114.         MOVLW    B'00001001'
    115.         MOVWF    PORTB        ;ALL TRAFFIC LANES RED
    116.         CALL    T3SEC        ;FOR 3 SECONDS
    117.         BTFSC    PORTA,0        ;R/Y NIGHT MODE DESIRED?
    118.         GOTO    NMRY        ;GO TO R/Y NIGHT MODE
    119.         BTFSC    PORTA,1        ;R/R NIGHT MODE DESIRED?
    120.         GOTO    NMRR        ;GO TO R/R NIGHT MODE
    121.         MOVLW    B'00001100'
    122.         MOVWF    PORTB        ;SECONDARY GREEN, MAIN LANES RED
    123.         CALL    T20SEC        ;FOR 20 SECONDS
    124.         MOVLW    B'00001010'
    125.         MOVWF    PORTB        ;SECONDARY YELLOW, MAIN LANES RED
    126.         CALL    T4SEC        ;FOR 4 SECONDS
    127.         MOVLW    B'00001001'
    128.         MOVWF    PORTB        ;ALL TRAFFIC LANES RED
    129.         CALL    T3SEC        ;FOR 3 SECONDS
    130.         BTFSC    PORTA,0        ;R/Y NIGHT MODE DESIRED?
    131.         GOTO    NMRY        ;GO TO R/Y NIGHT MODE
    132.         BTFSC    PORTA,1        ;R/R NIGHT MODE DESIRED?
    133.         GOTO    NMRR        ;GO TO R/R NIGHT MODE
    134.         GOTO    NORM1        ;OR REMAIN IN NORMAL CYCLE OPERATION
    135. ;**************************************************************************
    136. ;R/Y NIGHT MODE OPERATION.  PORTA 0
    137. NMRY    MOVLW    B'00010000'
    138.         MOVWF    PORTB        ;MAIN LANES ALL YELLOW, ALL OTHERS OFF
    139.         CALL    T500MS        ;FOR 1/2 SECOND
    140.         MOVLW    B'00000001'
    141.         MOVWF    PORTB        ;SECONDARY RED, ALL OTHERS OFF
    142.         CALL    T500MS        ;FOR 1/2 SECOND
    143.         BTFSS    PORTA,0        ;STILL WANT R/Y NIGHT MODE?
    144.         GOTO    TRANS        ;IF NO, TRANSITION TO NORMAL CYCLE MODE
    145.         BTFSC    PORTA,1        ;SWITCH TO R/R NIGHT MODE?
    146.         GOTO    NMRR        ;THEN GO TO R/R NIGHT MODE
    147.         GOTO    NMRY        ;OR STAY IN THIS NIGHT MODE
    148. ;**************************************************************************
    149. ;R/R NIGHT MODE OPERATION.  PORTA 1
    150. NMRR    MOVLW    B'00001000'
    151.         MOVWF    PORTB        ;MAIN LANES RED, ALL OTHERS OFF
    152.         CALL    T500MS        ;FOR 1/2 SECOND
    153.         MOVLW    B'00000001'
    154.         MOVWF    PORTB        ;SECONDARY RED, ALL OTHERS OFF
    155.         CALL    T500MS        ;FOR 1/2 SECOND
    156.         BTFSC    PORTA,0        ;R/Y NIGHT MODE DESIRED?
    157.         GOTO    NMRY        ;THEN GO TO R/Y NIGHT MODE
    158.         BTFSS    PORTA,1        ;R/R NIGHT MODE STILL DESIRED?
    159.         GOTO    TRANS        ;NO, TRANSITION TO NORMAL CYCLE MODE
    160.         GOTO    NMRR        ;YES, STAY IN R/R NIGHT MODE
    161. ;**************************************************************************
    162. ;TRANSITION FROM NIGHT MODE TO NORMAL
    163. TRANS    MOVLW    B'10000001'
    164.         MOVWF    PORTB        ;MAIN TRAFFIC LANES GREEN, SECONDARY RED
    165.         CALL    T10SEC        ;FOR 10 SECONDS
    166.         MOVLW    B'00010001'
    167.         MOVWF    PORTB        ;MAIN TRAFFIC LANES YELLOW. SECONDARY RED
    168.         CALL    T4SEC        ;FOR 4 SECONDS
    169.         GOTO    NORM        ;GO TO NORMAL CYCLE OPERATION
    170. ;**************************************************************************
    171.         END
     
  11. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Your code is very strange. It has some fundamental flaws as I can see.
    When an interrupt is serviced:
    • The GIE is cleared to disable any further interrupt.
    • The return address is pushed onto the stack.
    The PC is loaded with 0004h.
    Then you are done in your ISR you do the retfie instruction. Stack is POPed and Top-of-Stack (TOS) is loaded in the PC. Interrupts are enabled by setting Global interrupt Enable bit, GIE
    I can not see you have implemented such structure in your program
    Code ( (Unknown Language)):
    1.  
    2.     org 0
    3.      goto      Main
    4.      nop
    5.      nop
    6.      nop
    7. ISR:
    8.     ;Put your interrupt service routine her(ISR)
    9.     ;
    10.     ;
    11.     ;
    12. retfie   ;End it with this command the program will jump back to main program
    13.  
    14.  
    15. Main:
    16. ;
    17. ;
    18.  
     
  12. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    Added a line in the initial setup to disable comparators (CMCON bits 0:2 changed to 1, all others 0)

    RA0 and RA1 should have already been specified as input based on the line of code:
    MOVLW B'00000011'
    MOVWF TRISA

    In response to t06afre:
    Several people on this thread have said I don't need to use "interrupts" for this function, and I tend to agree. The program is only intended to move around to different parts if during a bit test on RA0 or RA1 either pin is found to be high (a 1 rather than a 0).
     
  13. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    Disabling the comparators did the trick. Program functions as intended. Thanks for all the help.
     
Loading...