External interrupt

Discussion in 'Programmer's Corner' started by Dritech, Jan 7, 2012.

  1. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Hi all,

    I have read several tutorials about external interrupts, but still cannot manage to make it work [​IMG] The MC i am using is the PIC18F4550.

    One of the tutorials i used was the one below:

    http://www.hobbyprojects.com/pic_tut...utorial11.html

    The program that i am trying to do is the one below. It will start blinking an led at port c,2 and when an interrupt occurs (at int0), the led will stop blinking for few seconds.

    Code ( (Unknown Language)):
    1. ;REGISTER DECLERATIONS
    2.  
    3. REG2 EQU 60H
    4. REG3 EQU 61H
    5. REG4 EQU 62H
    6.  
    7.  
    8.  
    9. ORG 0000H               ;WHEN SUPPLY ON OR RESET IS PRESSED, PROGRAM WILL START FROM HERE (SEE DATASHEET PG 57)
    10. GOTO MAIN
    11.  
    12. ORG 0008H               ;HIGH PRIORITY VECTOR... THE PROGRAM WILL GO HERE WHEN INTERRUPT IS ENABLED
    13. GOTO ISR
    14.  
    15.  
    16. ;ALL INTIALIZATION HERE OF ALL PORTS AND PERIPHERALS HERE
    17.  
    18.  
    19. ;START MAIN PROGRAM HERE
    20.  
    21. MAIN
    22.  
    23. MOVLW B'000'              ;SET PORT C AS OUTPUT
    24. MOVWF TRISC
    25.  
    26. CLRF INTCON              ;CLEAR REGISTORS
    27. CLRF RCON
    28.  
    29. BSF RCON,7               ;SET IPIN TO 1
    30.  
    31. BSF INTCON,7             ;ENABLE ALL HIGH PRIORITY INTERRUPTS
    32. BSF INTCON,4             ;SET INT0 AS EXTERNAL INTERRUPT
    33.  
    34. BCF INTCON,1             ;CLEAR FLAG
    35.  
    36. MOVLW B'00000001'              ;SET PORT B,0 AS INPUT (SINCE IT IS AN INTERRUPT)
    37. MOVWF TRISB
    38.  
    39.  
    40. ;START BLINKING LED
    41.  
    42. LINE3
    43. CLRF REG2
    44. CLRF REG3
    45. MOVLW 30
    46. MOVWF REG4
    47. BSF PORTC,2
    48. LINE1
    49. DECFSZ REG2
    50. BRA LINE1
    51. DECFSZ REG3
    52. BRA LINE1
    53. DECFSZ REG4
    54. BRA LINE1
    55. BCF PORTC,2
    56. CLRF REG2
    57. CLRF REG3
    58. MOVLW 30
    59. MOVWF REG4
    60. LINE2
    61. DECFSZ REG2
    62. BRA LINE2
    63. DECFSZ REG3
    64. BRA LINE2
    65. DECFSZ REG4
    66. BRA LINE2
    67. GOTO LINE3
    68.  
    69.  
    70. ;INTERRUPT SERVICE ROUTINE
    71.  
    72. ISR
    73.  
    74. BCF PORTC,2    ;STOP LED AT PORT C FORM BLINKING WHEN THE INTERRUPT FLAG IS SET
    75. CLRF REG2
    76. CLRF REG3
    77. CLRF REG4
    78. LINE6
    79. DECFSZ REG2
    80. BRA LINE6
    81. DECFSZ REG3
    82. BRA LINE6
    83. DECFSZ REG4
    84. BRA LINE6
    85. BCF INTCON,1   ;SINCE THE PIC DOES NOT RESET THE INTERRUPT PIN AS 0 AUTOMATICALLY, IT HAS TO BE SET MANUALLY
    86. RETFIE         ; END INTERRUPT ROUTINE
    87.  
    88.  
    89.  
    90.  
    91. GOTO $      ; THIS WILL STOP THE MICRO FROM STARTING TO EXECUTRE THE PROGRAME AGAIN EVEN AFTER
    92.             ; SENDING IT TO THE END DIRECTIVE
    93.  
    94. END
    Can someone help me solve this please.

    Thanks in advance
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    To get code to compile I had to add:

    LIST P=18F4550 ;directive to define processor
    #include <P18F4550.INC> ;processor specific variable definitions

    Also, all lines require indentation for a build without warnings. Only labels can begin at position 1. That builds clean but has code another problem.

    Then you need to turn off the analog functions (they are always and properly enabled by default) by setting ADCON1 to 0x0F.

    I would guess this is one of the first programs you have ever written. You can get a nice jump start by using one of the template files Microchip gives you along with MPLAB. For your project you could have best begun using the supplied template file 18F4550TEMP.ASM as a start.

    Next, never use a register name or line label that is meaningless. "REG2" means nothing, while something like CounterB means a bit more and helps to make the code more meaningful. Try to imagine someone reading your code fresh, or you yourself reading your own code 2 years from now.
     
Loading...