Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "p12f1572.inc"
- ; CONFIG1
- ; __config 0xF1E4
- __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _BOREN_OFF & _CLKOUTEN_ON
- ; CONFIG2
- ; __config 0xDEFF
- __CONFIG _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_ON & _BORV_LO & _LPBOREN_OFF & _LVP_OFF
- UDATA
- BTNVAR: RES 1 ;Reserve 1B for BuTtoN VARiable
- ;BTNVAR Bit Words
- BTVSTATE EQU 0x00
- BTVFLAG EQU 0x01
- ;WPUEN Bit Word
- WPUEN EQU 0x07
- ;Clock Config Words
- SPLLEN_ON EQU 0x80
- SPLLEN_OFF EQU 0x00
- SCS_INTOSC EQU 0x02
- SCS_TIMER1 EQU 0x01
- SCS_FOSC EQU 0x00
- OSC_16M EQU 0x78
- OSC_8M EQU 0x70
- OSC_4M EQU 0x68
- OSC_2M EQU 0x60
- OSC_1M EQU 0x58
- OSC_HF500k EQU 0x50
- RES_VECT CODE 0x0000 ; processor reset vector
- GOTO START ; go to beginning of prog
- ISR_VECT CODE 0x0004 ;Interrupt Vector
- GOTO ISR ;When interrupted, go to Interrupt Service Routine
- MAIN_PROG CODE ; let linker place main program
- START: ;Setup of I/O, Clock, Interrupts, and Timer2
- BANKSEL OSCCON ;Go to Bank 1
- MOVLW SPLLEN_OFF | SCS_INTOSC | OSC_4M ;Load W with Clock Settings
- MOVWF OSCCON ;Copy W to OSCCON
- BANKSEL LATA ;Go to Bank 2
- CLRF LATA ;Clear LATch A for fresh I/O Data
- BANKSEL ANSELA ;Go to Bank 3
- CLRF ANSELA ;Clear ANalog SELect A to disable analog inputs
- BANKSEL OPTION_REG ;Go to Bank 1
- BCF OPTION_REG,WPUEN;Clear the Weak Pull Up ENable bit
- BANKSEL TRISA ;Go to Bank 1
- BCF TRISA,RA5 ;Clear RA5, makes it an output
- BSF TRISA,RA2 ;Set RA2, makes it an input
- ;INTCON is a core register, thus does not require a BANKSEL
- BSF INTCON,PEIE ;Set the Peripheral Interrupt Enable Bit
- BANKSEL PIE1 ;Go to Bank with Peripharal Interrupt Enable #1
- BSF PIE1,TMR2IE ;Set the TiMeR2 Interrupt Enable Bit
- BANKSEL T2CON ;Go to Bank with Timer2 CONfig
- MOVLW 0x27 ;Load 0x27 into W (1:64 PreSc, ON, 1:5 PostSc)
- MOVWF T2CON ;Copy W into Timer2 CONfig
- BANKSEL PR2 ;Go to Bank with PR2
- MOVLW 0x2E ;Load 46d into W
- MOVWF PR2 ;Copy W into PR2
- BANKSEL BTNVAR ;Go to Bank with BuTtoN VARiable
- CLRF BTNVAR ;Clear BTNVAR
- BSF INTCON,GIE ;Set the General Interrupt Enable Bit
- MAIN: ;Foreground routine
- BANKSEL BTNVAR
- BTFSS BTNVAR,BTVFLAG ;If BTVFLAG is set, toggle RA5
- GOTO MAIN ;Else, check the flag again
- BCF BTNVAR,BTVFLAG ;Clear the Button Flag
- BANKSEL PORTA ;Go to Bank 0
- MOVLW 0x20 ;Load W with XOR mask to toggle RA5 in PORTA
- XORWF PORTA,F ;XOR PORTA with the mask to toggle RA5
- GOTO MAIN ;Check again for the Button Flag
- ISR: ;Interrupt Service Routine
- BANKSEL PIR1 ;Go to Bank with Peripheral Interrupt Request #1
- BCF PIR1,TMR2IF ;Clear the TiMeR2 Interrupt Flag
- BANKSEL BTNVAR ;Go to Bank with BuTtoN VARiable
- BTFSC BTNVAR,BTVSTATE ;If BTVSTATE is clear, state is BTNUP
- GOTO BTNDOWN ;Else, state is BTNDOWN
- GOTO BTNUP
- BTNUP: ;Button is Up state
- BANKSEL PORTA ;Go to Bank 0
- BTFSC PORTA,RA2 ;If RA2 is clear, the button is down, change state
- RETFIE ;Else, return
- GOTO SETFLAG
- SETFLAG: ;Change RA5 Flag needs setting
- BANKSEL BTNVAR ;Go to Bank with BTNVAR
- BSF BTNVAR,BTVFLAG ;Set the Button Var Flag bit
- BSF BTNVAR,BTVSTATE ;Set the Button Var State bit
- RETFIE ;Return to foreground
- BTNDOWN: ;Button is Down state
- BANKSEL PORTA
- BTFSC PORTA,RA2 ;If RA2 is clear, the button is still down, return
- GOTO RAISEBTN ;Else, change state
- RETFIE ;Return to foreground
- RAISEBTN: ;The button has been released
- BANKSEL BTNVAR ;Go to Bank with BTNVAR
- BCF BTNVAR,BTVSTATE ;Clear the Button Var State bit
- RETFIE ;Return to foreground
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement