fork download
  1. ; Program: LED Control with PB0 and PB1
  2. ; Microcontroller: PIC16F877A
  3. ; Function: PB0 -> LED OFF, PB1 -> LED ON
  4.  
  5. LIST P=16F877A ; Specify the PIC16F877A microcontroller
  6. INCLUDE <P16F877A.INC> ; Include file for register definitions
  7.  
  8. __CONFIG _XT_OSC & _WDT_OFF & _PWRTE_ON & _BODEN_OFF & _LVP_OFF
  9.  
  10. ; Start of code
  11. ORG 0x0000 ; Reset vector
  12. GOTO MAIN ; Jump to main program
  13.  
  14. ORG 0x0004 ; Interrupt vector
  15. GOTO ISR ; Jump to interrupt service routine
  16.  
  17. ; Main Program
  18. MAIN:
  19. BCF STATUS, RP0 ; Select Bank 0
  20. CLRF PORTB ; Clear PORTB output
  21. CLRF TRISB ; Configure PORTB as output
  22. BSF STATUS, RP0 ; Switch to Bank 1
  23. BSF TRISB, 0 ; Set RB0 (PB0) as input
  24. BSF TRISB, 1 ; Set RB1 (PB1) as input
  25. BCF STATUS, RP0 ; Return to Bank 0
  26.  
  27. BSF INTCON, INTE ; Enable external interrupt on RB0
  28. BSF INTCON, GIE ; Enable global interrupts
  29.  
  30. MAIN_LOOP:
  31. GOTO MAIN_LOOP ; Infinite loop
  32.  
  33. ; Interrupt Service Routine (ISR)
  34. ISR:
  35. BTFSC PORTB, 0 ; Check if PB0 (RB0) is pressed
  36. GOTO LED_OFF ; Turn LED off if PB0 is pressed
  37. BTFSC PORTB, 1 ; Check if PB1 (RB1) is pressed
  38. GOTO LED_ON ; Turn LED on if PB1 is pressed
  39.  
  40. BCF INTCON, INTF ; Clear the external interrupt flag
  41. RETFIE ; Return from ISR
  42.  
  43. LED_OFF:
  44. BCF PORTB, 7 ; Turn off LED (clear RB7)
  45. BCF INTCON, INTF ; Clear interrupt flag
  46. RETFIE ; Return from ISR
  47.  
  48. LED_ON:
  49. BSF PORTB, 7 ; Turn on LED (set RB7)
  50. BCF INTCON, INTF ; Clear interrupt flag
  51. RETFIE ; Return from ISR
  52.  
  53. END ; End of program
  54.  
Success #stdin #stdout 0.02s 25924KB
stdin
Standard input is empty
stdout
; Program: LED Control with PB0 and PB1
; Microcontroller: PIC16F877A
; Function: PB0 -> LED OFF, PB1 -> LED ON

    LIST P=16F877A          ; Specify the PIC16F877A microcontroller
    INCLUDE <P16F877A.INC>  ; Include file for register definitions

    __CONFIG _XT_OSC & _WDT_OFF & _PWRTE_ON & _BODEN_OFF & _LVP_OFF

; Start of code
    ORG 0x0000              ; Reset vector
    GOTO MAIN               ; Jump to main program

    ORG 0x0004              ; Interrupt vector
    GOTO ISR                ; Jump to interrupt service routine

; Main Program
MAIN:
    BCF STATUS, RP0         ; Select Bank 0
    CLRF PORTB              ; Clear PORTB output
    CLRF TRISB              ; Configure PORTB as output
    BSF STATUS, RP0         ; Switch to Bank 1
    BSF TRISB, 0            ; Set RB0 (PB0) as input
    BSF TRISB, 1            ; Set RB1 (PB1) as input
    BCF STATUS, RP0         ; Return to Bank 0

    BSF INTCON, INTE        ; Enable external interrupt on RB0
    BSF INTCON, GIE         ; Enable global interrupts

MAIN_LOOP:
    GOTO MAIN_LOOP          ; Infinite loop

; Interrupt Service Routine (ISR)
ISR:
    BTFSC PORTB, 0          ; Check if PB0 (RB0) is pressed
    GOTO LED_OFF            ; Turn LED off if PB0 is pressed
    BTFSC PORTB, 1          ; Check if PB1 (RB1) is pressed
    GOTO LED_ON             ; Turn LED on if PB1 is pressed

    BCF INTCON, INTF        ; Clear the external interrupt flag
    RETFIE                  ; Return from ISR

LED_OFF:
    BCF PORTB, 7            ; Turn off LED (clear RB7)
    BCF INTCON, INTF        ; Clear interrupt flag
    RETFIE                  ; Return from ISR

LED_ON:
    BSF PORTB, 7            ; Turn on LED (set RB7)
    BCF INTCON, INTF        ; Clear interrupt flag
    RETFIE                  ; Return from ISR

    END                     ; End of program