Button Toggle PIC12F1572 Assembly Code

Mar 1st, 2018
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include ""
  3. ; CONFIG1
  4. ; __config 0xF1E4
  6. ; CONFIG2
  7. ; __config 0xDEFF
  10.     UDATA
  11. BTNVAR: RES 1   ;Reserve 1B for BuTtoN VARiable
  12. ;BTNVAR Bit Words
  13. BTVSTATE EQU 0x00
  14. BTVFLAG EQU 0x01
  16. ;WPUEN Bit Word
  17. WPUEN EQU 0x07
  19. ;Clock Config Words
  20. SPLLEN_ON EQU 0x80
  21. SPLLEN_OFF EQU 0x00
  23. SCS_INTOSC EQU 0x02
  24. SCS_TIMER1 EQU 0x01
  25. SCS_FOSC EQU 0x00
  27. OSC_16M EQU 0x78
  28. OSC_8M EQU 0x70
  29. OSC_4M EQU 0x68
  30. OSC_2M EQU 0x60
  31. OSC_1M EQU 0x58
  32. OSC_HF500k EQU 0x50
  34. RES_VECT  CODE    0x0000            ; processor reset vector
  35.     GOTO    START                   ; go to beginning of prog
  36. ISR_VECT CODE 0x0004    ;Interrupt Vector
  37.     GOTO ISR        ;When interrupted, go to Interrupt Service Routine
  39. MAIN_PROG CODE                      ; let linker place main program
  41. START:  ;Setup of I/O, Clock, Interrupts, and Timer2
  42.     BANKSEL OSCCON  ;Go to Bank 1
  43.     MOVLW SPLLEN_OFF | SCS_INTOSC | OSC_4M  ;Load W with Clock Settings
  44.     MOVWF OSCCON    ;Copy W to OSCCON
  45.     BANKSEL LATA    ;Go to Bank 2
  46.     CLRF LATA       ;Clear LATch A for fresh I/O Data
  47.     BANKSEL ANSELA  ;Go to Bank 3
  48.     CLRF ANSELA     ;Clear ANalog SELect A to disable analog inputs
  49.     BANKSEL OPTION_REG  ;Go to Bank 1
  50.     BCF OPTION_REG,WPUEN;Clear the Weak Pull Up ENable bit
  51.     BANKSEL TRISA   ;Go to Bank 1
  52.     BCF TRISA,RA5   ;Clear RA5, makes it an output
  53.     BSF TRISA,RA2   ;Set RA2, makes it an input
  54.     ;INTCON is a core register, thus does not require a BANKSEL
  55.     BSF INTCON,PEIE ;Set the Peripheral Interrupt Enable Bit
  56.     BANKSEL PIE1    ;Go to Bank with Peripharal Interrupt Enable #1
  57.     BSF PIE1,TMR2IE ;Set the TiMeR2 Interrupt Enable Bit
  58.     BANKSEL T2CON   ;Go to Bank with Timer2 CONfig
  59.     MOVLW 0x27      ;Load 0x27 into W (1:64 PreSc, ON, 1:5 PostSc)
  60.     MOVWF T2CON     ;Copy W into Timer2 CONfig
  61.     BANKSEL PR2     ;Go to Bank with PR2
  62.     MOVLW 0x2E      ;Load 46d into W
  63.     MOVWF PR2       ;Copy W into PR2
  64.     BANKSEL BTNVAR  ;Go to Bank with BuTtoN VARiable
  65.     CLRF BTNVAR     ;Clear BTNVAR
  66.     BSF INTCON,GIE  ;Set the General Interrupt Enable Bit
  67. MAIN:   ;Foreground routine
  69.     BTFSS BTNVAR,BTVFLAG    ;If BTVFLAG is set, toggle RA5
  70.     GOTO MAIN       ;Else, check the flag again
  71.     BCF BTNVAR,BTVFLAG      ;Clear the Button Flag
  72.     BANKSEL PORTA   ;Go to Bank 0
  73.     MOVLW 0x20      ;Load W with XOR mask to toggle RA5 in PORTA
  74.     XORWF PORTA,F   ;XOR PORTA with the mask to toggle RA5
  75.     GOTO MAIN       ;Check again for the Button Flag    
  76. ISR:    ;Interrupt Service Routine
  77.     BANKSEL PIR1    ;Go to Bank with Peripheral Interrupt Request #1
  78.     BCF PIR1,TMR2IF ;Clear the TiMeR2 Interrupt Flag
  79.     BANKSEL BTNVAR  ;Go to Bank with BuTtoN VARiable
  80.     BTFSC BTNVAR,BTVSTATE   ;If BTVSTATE is clear, state is BTNUP
  81.     GOTO BTNDOWN    ;Else, state is BTNDOWN
  82.     GOTO BTNUP    
  83. BTNUP:  ;Button is Up state
  84.     BANKSEL PORTA   ;Go to Bank 0
  85.     BTFSC PORTA,RA2 ;If RA2 is clear, the button is down, change state
  86.     RETFIE      ;Else, return
  87.     GOTO SETFLAG
  88. SETFLAG:    ;Change RA5 Flag needs setting
  89.     BANKSEL BTNVAR  ;Go to Bank with BTNVAR
  90.     BSF BTNVAR,BTVFLAG  ;Set the Button Var Flag bit
  91.     BSF BTNVAR,BTVSTATE ;Set the Button Var State bit
  92.     RETFIE      ;Return to foreground
  93. BTNDOWN:    ;Button is Down state
  95.     BTFSC PORTA,RA2 ;If RA2 is clear, the button is still down, return
  96.     GOTO RAISEBTN   ;Else, change state
  97.     RETFIE      ;Return to foreground
  98. RAISEBTN:   ;The button has been released
  99.     BANKSEL BTNVAR  ;Go to Bank with BTNVAR
  100.     BCF BTNVAR,BTVSTATE ;Clear the Button Var State bit
  101.     RETFIE      ;Return to foreground
  102. END
RAW Paste Data