How to make a PIC18F4550 use A/D interrupts to blink an LED

Discussion in 'Homework Help' started by Halim.Akiki, Jan 23, 2015.

  1. Halim.Akiki

    Thread Starter Member

    Dec 29, 2014
    44
    1
    1. What I am trying to do
    I am attempting to make a PIC18F4550 use A/D interrupts to blink an LED at RA1. [Interrupt LED]
    The frequency of the blinking LED is determined by a potentiometer linked to RA0.
    To gauge when the loop is outside of the interrupt routine, I have added an LED at RA2 that is set to blink when the program loops in the main code. [Main LED]

    2. The Problem
    The Interrupt LED blinks properly, responding to potentiometer changes for 30 or so cycles, after which, only the Main LED blinks, unresponsive to potentiometer changes.
    It seems clear that the code no longer interrupts after a point and the main code loops thereafter.
    However, I am at a loss at what causes the code to stop interrupting.

    3. Code

    Code (Text):
    1.  
    2. ;=========PROGRAM INFORMATION=========
    3. ;Designed to sense voltage from pot, convert to digital and use result to set delay time of a blinking LED.
    4. ;Run off internal oscillator - 1MHz mode (default int osc)
    5. ;Conversion voltage references set to VDD and VSS
    6. ;Analog input in RA0
    7. ;Blinking LED positioned RA1
    8. ;Main loop test-LED positioned RA2 (visual cue of looping main code)
    9.  
    10. ;=========PIC SPECIFIC IMPORTS=========
    11. #include<P18F4550.INC>
    12.  
    13. ;=========CONFIGURATION BITS=========
    14.     CONFIG WDT = OFF    ; watchdog timer off
    15.     CONFIG MCLRE = OFF  ; MCLR pin off, no debugging, no need to set pin hi
    16.     CONFIG DEBUG = OFF  ; debug mode off
    17.     CONFIG FOSC = INTOSCIO_EC   ; internal oscillator
    18.     CONFIG XINST = OFF  ; disable extended instruction set
    19.     CONFIG CP0 = OFF    ; disable code protection
    20.     CONFIG LVP = OFF    ; disable low voltage programming
    21.  
    22. ;=========VARIABLE DECLARATION=========
    23. Delay1 res 2    ;delay loop variables
    24. DelTmr res 2    ;delay loop multiplier, will be influenced by result of A/D conversion
    25.  
    26. ;=========VECTORS=========
    27.         ;RESET
    28. RESET_VECTOR    CODE    0x0000
    29.         goto    init    ; go to initialisation
    30.  
    31.         ;INTERRUPT
    32. INT_VECTOR      CODE    0x0008
    33.         ;Blink + Delay
    34.         btg     LATA, LATA1 ; blink RA1
    35.         call    delay
    36.      
    37.         ;Reset interrupt
    38.         ;bcf        PIR1, ADIF  ; clear A/D interrupt flag [no effect]
    39.         ;bsf        PIE1, ADIE  ; enable A/D interrupt [no effect]
    40.         bsf     INTCON, GIE ; enable interrupts
    41.  
    42.         goto    main    ; return to main loop
    43.  
    44. ;=========METHODS=========
    45. ;DELAY
    46. delay:  movff ADRESH, DelTmr ;move 8bit A/D result to DelTmr
    47.      
    48.         infsnz  DelTmr, F   ; make sure no overflows occur
    49.         decf    DelTmr, F   ;
    50.  
    51. dloop:  decfsz  Delay1
    52.         goto    dloop
    53.         decfsz  DelTmr
    54.         goto    dloop
    55.  
    56.         return
    57.  
    58. ;=========INITIALIZATION=========
    59.         ;Pins
    60. init:   clrf    PORTA   ; clear porta
    61.         clrf    TRISA   ; porta > outputs
    62.         clrf    LATA    ; clear porta latch
    63.         bsf     TRISA, TRISA0   ; RA0 > input
    64.      
    65.         ;No Internal Osc selection, will keep osc at 1MHz
    66.  
    67.         ;A/D Module
    68.         movlw   b'00001110' ; AN0 set to analog input + VSS,VDD ref.
    69.         movwf   ADCON1      ;
    70.         clrf    ADCON0     ; select AN0 (channel 0) as A/D channel
    71.         movlw   b'00001000' ; results are left-justified, Tac = 2*Tosc
    72.         movwf   ADCON2      ;
    73.      
    74.         bsf     ADCON0,ADON ; enable A/D Conversion Module
    75.      
    76.         ;Interrupts
    77.         bcf     RCON, IPEN  ; disable interrupt priority
    78.         bsf     INTCON, PEIE    ; enable peripheral interrupts
    79.         bcf     PIR1, ADIF  ; clear A/D interrupt flag
    80.         bsf     PIE1, ADIE  ; enable A/D interrupt
    81.         bsf     INTCON, GIE ; enable interrupts globally
    82.      
    83. ;=========MAIN CODE=========
    84. main:   bsf     ADCON0, GO_DONE ; start A/D Conversion
    85.  
    86. inf:      
    87.         btg     LATA, LATA2 ; blink RA2
    88.         call    delay       ;
    89.  
    90.         goto    inf ; infinite loop
    91.  
    92.         END
    93. ;=========END OF CODE=========
    94.  
    Moderators note: please use code tags for pieces of code
     
    Last edited by a moderator: Jan 23, 2015
  2. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,675
    2,722
    Use 'RETFIE' instead of 'goto main' to exit the interrupt.

    Also, no need to muck with the gie or adie flags.
     
Loading...