Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;*******************************************************************
  2. ;                               TEMPLATE PROVIDED BY CENTRE
  3.  
  4. ;         TITLE:  Vault
  5. ;         AUTHOR: Dominik Tarnowski
  6. ;
  7.  
  8. ;******************************************************************
  9. ; PROGRAM DESCRIPTION:
  10. ;
  11. ; Program takes an input of 4 numbers between 0 and 9 and unlocks the vault provided that the inputs match a pre-defined passcode.
  12. ;
  13. ;*********************************************************************
  14. ;           DEFINITIONS
  15. ;*********************************************************************
  16.     list    p=16F88                 ; tells the assembler which PIC chip to program for
  17.     radix   dec                     ; set default number radix to decimal
  18.     ;radix  hex                     uncomment this to  set  radix to hex
  19.     __config h'2007', 0x3F50    ; internal oscillator, RA5 as i/o, wdt off
  20.     __config h'2008', 0x3FFF   
  21.     errorlevel -302                 ; hide page warnings
  22.  
  23. W               EQU h'00'   ; pointer to Working register
  24. F               EQU h'01'   ; pointer to file
  25.  
  26. ;****** REGISTER USAGE ******
  27.  
  28. ; Register page 1
  29. TRISA   EQU h'85'   ; data direction registers
  30. TRISB   EQU h'86'
  31. OSCCON  EQU h'8F'   ; internal oscillator speed
  32. ANSEL   EQU h'9B'   ; ADC port enable bits
  33.  
  34. ; Register page 0  
  35. INDF    EQU h'00'
  36. STATUS  EQU h'03'   ; status
  37. PORTA   EQU h'05'   ; input / output ports
  38. FSR EQU h'04'
  39. PORTB   EQU h'06'
  40. INTCON  EQU h'0B'   ; interrupt control
  41. ADRESH  EQU h'1E'   ; ADC result
  42. ADCON0  EQU h'1F'   ; ADC control
  43.  
  44. B0  EQU h'20'
  45. B1  EQU h'21'
  46. B2  EQU h'22'
  47. B3  EQU h'23'
  48. B4  EQU h'24'
  49. B5  EQU h'25'
  50. B6  EQU h'26'
  51. B7  EQU h'27'
  52. B8  EQU h'28'
  53. B9  EQU h'29'
  54. B10 EQU h'2A'
  55. B11 EQU h'2B'
  56. B12 EQU h'2C'
  57. B13 EQU h'2D'
  58. B14 EQU h'2E'
  59. B15 EQU h'2F'
  60. B16 EQU h'30'
  61. B17 EQU h'31'
  62. B18 EQU h'32'
  63. B19 EQU h'33'
  64. B20 EQU h'34'
  65. B21 EQU h'35'
  66. B22 EQU h'36'
  67. B23 EQU h'37'
  68. B24 EQU h'38'
  69. B25 EQU h'39'
  70.  
  71. N   EQU h'3A'
  72. INPUT   EQU h'3B'
  73.  
  74. WAIT1   EQU h'3C'   ; counters used in wait delays
  75. WAIT10  EQU h'3D'
  76. WAIT100 EQU h'3E'
  77. WAIT1000    EQU h'3F'
  78. ADCTEMP EQU h'40'   ; adc loop counter
  79.  
  80. ;****** REGISTER BITS ******
  81.  
  82. C           EQU h'00'   ; carry flag
  83. Z               EQU h'02'   ; zero flag
  84. RP0             EQU h'05'   ; register page bit
  85. INT0IF          EQU h'01'   ; interrupt 0 flag
  86. INT0IE      EQU h'04'   ; interrupt 0 enable
  87. GIE             EQU h'07'   ; global interrupt enable
  88.  
  89. ;*********************************************************************
  90. ;           VECTORS
  91. ;*********************************************************************
  92.  
  93. ;The PIC16F88 reset vectors
  94.  
  95.     ORG     h'00'           ; reset vector address
  96.     goto    start   ; goes to first instruction on reset/power-up
  97. ;*********************************************************************
  98. ;           SUBROUTINES
  99. ;*********************************************************************
  100. ; Predefined wait subroutines - wait1ms, wait10ms, wait100ms, wait1000ms
  101.  
  102. wait1ms     ; (199 x 5) + 5 instructions = 1000us = 1ms @ 4MHz resonator
  103.     movlw d'199'    ; 1
  104.     movwf WAIT1     ; 1
  105. loop5ns
  106.     clrwdt          ; 1 this loop 1+1+1+2 = 5 instructions
  107.     nop             ; 1
  108.     decfsz WAIT1,; 1
  109.     goto loop5ns    ; 2
  110.     nop             ; 1
  111.     return              ; 2
  112. wait10ms
  113.     movlw d'10'         ; 10 x 1ms = 10ms
  114.     movwf WAIT10   
  115. loop10ms
  116.     call    wait1ms
  117.     decfsz  WAIT10,F   
  118.     goto    loop10ms     
  119.     return          
  120.  
  121. wait100ms
  122.     movlw d'100'    ; 100 x 1ms = 100ms
  123.     movwf WAIT100  
  124. loop100ms
  125.     call    wait1ms
  126.     decfsz  WAIT100,F
  127.     goto    loop100ms    
  128.     return          
  129.  
  130. wait1000ms
  131.     movlw   d'10'     ; 10 x 100ms = 1000ms
  132.     movwf   WAIT1000     
  133. loop1000ms
  134.     call    wait100ms  
  135.     decfsz  WAIT1000,F
  136.     goto    loop1000ms 
  137.     return          
  138.  
  139. ;*********************************************************************
  140. ;           MAIN PROGRAM
  141. ;*********************************************************************
  142.  
  143. ;****** INITIALISATION ******
  144. start
  145.     bsf         STATUS,RP0  ; select register page 1
  146.     movlw   b'01100000' ; set to 4MHz internal operation
  147.     movwf   OSCCON 
  148.     clrf    ANSEL       ; disable ADC (enabled at power-up)
  149.     bcf         STATUS,RP0  ; select register page 0
  150.  
  151. ;The data direction registers TRISA and TRISB live in the special register set. A '1' in
  152. ;these registers sets the corresponding port line to an Input, and a
  153. ;'0' makes the corresponding line an output.
  154.  
  155. Init
  156.     clrf        PORTA       ; make sure PORTA output latches are low
  157.         clrf        PORTB       ; make sure PORTB output latches are low  
  158.     bsf         STATUS,RP0  ; select register page 1
  159. ;Modify the next line to correspond with your input output reqirements 
  160.     movlw   b'00000011' ; set port A data direction (0 = output bit, 1 = input bit)
  161.     movwf   TRISA       ;
  162. ;Modify the next line to correspond with your input output requirements
  163.     movlw   b'11111111' ; set port B data direction (0 = output bit, 1 = input bit)
  164.     movwf   TRISB       ;  
  165.     bcf         STATUS,RP0  ; select register page 0
  166.  
  167. ;****** MAIN PROGRAM ******
  168. ;************* remove semicolons from next two lines to enable interrupt routine************
  169.     ; bsf       INTCON,INT0IE  ; set external interrupt enable
  170.     ; bsf       INTCON,GIE        ; enable all interrupts
  171.  
  172. main
  173. ;*********************************************************************
  174.  
  175. clrf N
  176. movlw h'20'
  177. movwf FSR
  178.    
  179. ; set the passcode
  180. movlw b'00010001'
  181. movwf B0
  182. movlw b'00010001'
  183. movwf B2
  184. movlw b'00010001'
  185. movwf B4
  186. movlw b'00010001'
  187. movwf B6
  188.    
  189. ;where 2^n is the amount of digits
  190. DIGIT_COUNT EQU d'3'
  191.  
  192. ; debug: set the value
  193. ;movlw b'00010001'
  194. ;movwf B1
  195. ;movlw b'00010001'
  196. ;movwf B3
  197. ;movlw b'00010001'
  198. ;movwf B5
  199. ;movlw b'00010001'
  200. ;movwf B7
  201.  
  202. movlw b'0010'
  203. movwf INPUT
  204. call handle_input
  205.  
  206. compare
  207.     movf INDF, W
  208.     incf FSR
  209.     subwf INDF
  210.    
  211.     btfss STATUS, Z
  212.     goto lock
  213.    
  214.     incf FSR
  215.    
  216.     btfsc FSR, DIGIT_COUNT
  217.     goto unlock
  218.    
  219.     incf N
  220.     goto compare
  221.  
  222. lock
  223.     goto lock
  224.    
  225. unlock
  226.     goto unlock
  227.  
  228. loop
  229.     call get_input
  230.     btfss INPUT, 7
  231.     goto loop_next
  232.  
  233.     subwf INPUT
  234.     btfsc STATUS, Z
  235.     goto loop_next
  236.    
  237.     call next
  238.  
  239.     call handle_input_debug
  240.     goto loop
  241.  
  242. loop_next
  243.     call next
  244.     goto loop
  245.  
  246. next
  247.     movwf INPUT
  248.     return
  249.  
  250. ; returns all 1s if no keys are pressed
  251. ; if key pressed, its binary equivalent will be stored in the W register    
  252. get_input
  253.     movlw b'11111111'
  254.     btfsc PORTB, 0
  255.     movlw b'0'
  256.     btfsc PORTB, 1
  257.     movlw b'1'
  258.     btfsc PORTB, 2
  259.     movlw b'10'
  260.     btfsc PORTB, 3
  261.     movlw b'11'
  262.     btfsc PORTB, 4
  263.     movlw b'100'
  264.     btfsc PORTB, 5
  265.     movlw b'101'
  266.     btfsc PORTB, 6
  267.     movlw b'110'
  268.     btfsc PORTB, 7
  269.     movlw b'111'
  270.  
  271.     btfsc PORTA, 0
  272.     movlw b'1000'
  273.     btfsc PORTA, 1
  274.     movlw b'1001'
  275.    
  276.     return
  277. ;todo: delete
  278. handle_input_debug
  279.     movf INPUT, W
  280.     bsf PORTA, 2
  281.     call wait100ms
  282.     bcf PORTA, 2
  283.     call wait100ms
  284.    
  285.     return
  286.  
  287. handle_input
  288.     movlw h'21'
  289.     movwf FSR
  290.    
  291.    
  292.     movf INPUT, W
  293.     movwf INDF
  294.    
  295.     return
  296.    
  297. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement