Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;*******************************************************************
- ; TEMPLATE PROVIDED BY CENTRE
- ; TITLE: Vault
- ; AUTHOR: Dominik Tarnowski
- ;
- ;******************************************************************
- ; PROGRAM DESCRIPTION:
- ;
- ; Program takes an input of 4 numbers between 0 and 9 and unlocks the vault provided that the inputs match a pre-defined passcode.
- ;
- ;*********************************************************************
- ; DEFINITIONS
- ;*********************************************************************
- list p=16F88 ; tells the assembler which PIC chip to program for
- radix dec ; set default number radix to decimal
- ;radix hex uncomment this to set radix to hex
- __config h'2007', 0x3F50 ; internal oscillator, RA5 as i/o, wdt off
- __config h'2008', 0x3FFF
- errorlevel -302 ; hide page warnings
- W EQU h'00' ; pointer to Working register
- F EQU h'01' ; pointer to file
- ;****** REGISTER USAGE ******
- ; Register page 1
- TRISA EQU h'85' ; data direction registers
- TRISB EQU h'86'
- OSCCON EQU h'8F' ; internal oscillator speed
- ANSEL EQU h'9B' ; ADC port enable bits
- ; Register page 0
- STATUS EQU h'03' ; status
- PORTA EQU h'05' ; input / output ports
- PORTB EQU h'06'
- INTCON EQU h'0B' ; interrupt control
- ADRESH EQU h'1E' ; ADC result
- ADCON0 EQU h'1F' ; ADC control
- P0 EQU h'20' ; general use byte registers B0 to B27
- P1 EQU h'21'
- V0 EQU h'22'
- V1 EQU h'23'
- N EQU h'24'
- INPUT EQU h'25'
- B6 EQU h'26'
- B7 EQU h'27'
- B8 EQU h'28'
- B9 EQU h'29'
- B10 EQU h'2A'
- B11 EQU h'2B'
- B12 EQU h'2C'
- B13 EQU h'2D'
- B14 EQU h'2E'
- B15 EQU h'2F'
- B16 EQU h'30'
- B17 EQU h'31'
- B18 EQU h'32'
- B19 EQU h'33'
- B20 EQU h'34' ; used in interrupt routine
- B21 EQU h'35' ; used in interrupt routine
- B22 EQU h'36'
- B23 EQU h'37'
- B24 EQU h'38'
- B25 EQU h'39'
- B26 EQU h'3A'
- B27 EQU h'3B'
- WAIT1 EQU h'3C' ; counters used in wait delays
- WAIT10 EQU h'3D'
- WAIT100 EQU h'3E'
- WAIT1000 EQU h'3F'
- ADCTEMP EQU h'40' ; adc loop counter
- ;****** REGISTER BITS ******
- C EQU h'00' ; carry flag
- Z EQU h'02' ; zero flag
- RP0 EQU h'05' ; register page bit
- INT0IF EQU h'01' ; interrupt 0 flag
- INT0IE EQU h'04' ; interrupt 0 enable
- GIE EQU h'07' ; global interrupt enable
- ;*********************************************************************
- ; VECTORS
- ;*********************************************************************
- ;The PIC16F88 reset vectors
- ORG h'00' ; reset vector address
- goto start ; goes to first instruction on reset/power-up
- ;*********************************************************************
- ; SUBROUTINES
- ;*********************************************************************
- ; Predefined wait subroutines - wait1ms, wait10ms, wait100ms, wait1000ms
- wait1ms ; (199 x 5) + 5 instructions = 1000us = 1ms @ 4MHz resonator
- movlw d'199' ; 1
- movwf WAIT1 ; 1
- loop5ns
- clrwdt ; 1 this loop 1+1+1+2 = 5 instructions
- nop ; 1
- decfsz WAIT1,F ; 1
- goto loop5ns ; 2
- nop ; 1
- return ; 2
- wait10ms
- movlw d'10' ; 10 x 1ms = 10ms
- movwf WAIT10
- loop10ms
- call wait1ms
- decfsz WAIT10,F
- goto loop10ms
- return
- wait100ms
- movlw d'100' ; 100 x 1ms = 100ms
- movwf WAIT100
- loop100ms
- call wait1ms
- decfsz WAIT100,F
- goto loop100ms
- return
- wait1000ms
- movlw d'10' ; 10 x 100ms = 1000ms
- movwf WAIT1000
- loop1000ms
- call wait100ms
- decfsz WAIT1000,F
- goto loop1000ms
- return
- ;*********************************************************************
- ; MAIN PROGRAM
- ;*********************************************************************
- ;****** INITIALISATION ******
- start
- bsf STATUS,RP0 ; select register page 1
- movlw b'01100000' ; set to 4MHz internal operation
- movwf OSCCON
- clrf ANSEL ; disable ADC (enabled at power-up)
- bcf STATUS,RP0 ; select register page 0
- ;The data direction registers TRISA and TRISB live in the special register set. A '1' in
- ;these registers sets the corresponding port line to an Input, and a
- ;'0' makes the corresponding line an output.
- Init
- clrf PORTA ; make sure PORTA output latches are low
- clrf PORTB ; make sure PORTB output latches are low
- bsf STATUS,RP0 ; select register page 1
- ;Modify the next line to correspond with your input output reqirements
- movlw b'00000000' ; set port A data direction (0 = output bit, 1 = input bit)
- movwf TRISA ;
- ;Modify the next line to correspond with your input output requirements
- movlw b'00000000' ; set port B data direction (0 = output bit, 1 = input bit)
- movwf TRISB ;
- bcf STATUS,RP0 ; select register page 0
- ;****** MAIN PROGRAM ******
- ;************* remove semicolons from next two lines to enable interrupt routine************
- ; bsf INTCON,INT0IE ; set external interrupt enable
- ; bsf INTCON,GIE ; enable all interrupts
- main
- ;*********************************************************************
- ; set the passcode
- movlw b'00010001'
- movwf P0
- movlw b'00010001'
- movwf P1
- ; debuging purposes only: set input values
- movlw b'00000010'
- movwf PORTA
- movlw b'00000000'
- movwf PORTB
- ; initialise variables
- clrf N
- clrf V0
- clrf V1
- loop
- call get_input
- movwf INPUT
- call wait100ms
- call get_input
- subwf INPUT
- ; if not the same button pressed after 100ms, loop again
- btfss STATUS,Z
- goto loop
- movwf INPUT
- sublw b'11111111'
- ; if input = 11111111
- btfss STATUS,Z
- call handle_input; called when any button is pressed
- goto loop
- handle_input
- movf INPUT, W
- movwf PORTB ; debugging purposes
- btfss N, 1; if n = 0 or n = 1
- call write_v0
- btfsc N, 1; if n = 2 or n = 3
- call write_v1
- incf N
- btfsc N, 2; if n = 4
- call check_passcode
- return
- ; returns all 1s if no keys are pressed
- ; if key pressed, its binary equivalent will be stored in the W register
- get_input
- movlw b'11111111'
- btfsc PORTA, 0
- movlw b'1'
- btfsc PORTA, 1
- movlw b'1'
- btfsc PORTA, 2
- movlw b'10'
- btfsc PORTA, 3
- movlw b'11'
- btfsc PORTA, 4
- movlw b'100'
- btfsc PORTA, 5
- movlw b'101'
- btfsc PORTA, 6
- movlw b'110'
- btfsc PORTA, 7
- movlw b'111'
- ; btfsc PORTB, 7
- ; movlw b'1000'
- ; btfsc PORTB, 6
- ; movlw b'1001'
- return
- write_v0
- btfss N, 0; if n = 0
- call write_v0_n0
- btfsc N, 0; if n = 1
- call write_v0_n1
- return
- write_v0_n0
- movf INPUT, W
- movwf V0
- return
- write_v0_n1
- rlf V0, F
- bcf V0, 0; clear rightmost bit as rlf seems to put 1 there the 1st time when it should be 0?
- rlf V0, F
- rlf V0, F
- rlf V0, F
- movf INPUT, W
- addwf V0
- return
- write_v1
- btfss N, 0; if n = 2
- call write_v1_n2
- btfsc N, 0; if n = 3
- call write_v1_n3
- return
- write_v1_n2
- movf INPUT, W
- movwf V1
- return
- write_v1_n3
- rlf V1, F
- bcf V1, 0; clear rightmost bit as rlf seems to put 1 there the 1st time when it should be 0?
- rlf V1, F
- rlf V1, F
- rlf V1, F
- movf INPUT, W
- addwf V1
- return
- check_passcode
- movf P0, W
- subwf V0
- btfsc STATUS, Z
- call check_passcode2
- ; TODO: if V0 != P0 -> LOCK
- ; TODO: if V1 != P1 -> LOCK
- ; TODO: else unlock
- btfss STATUS, Z
- call lock
- return
- check_passcode2
- movf P1, W
- subwf V1
- btfsc STATUS, Z
- call unlock
- return
- unlock
- movlw b'10000001'
- movwf PORTB
- return
- lock
- movlw b'11000111'
- movwf PORTB
- return
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement