ervinne

Microcontroller Systems Exam

Oct 27th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MPASM 3.33 KB | None | 0 0
  1.     #INCLUDE "P16F84A.INC"
  2.  
  3.     __CONFIG _XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF
  4.  
  5.     ERRORLEVEL -302
  6.  
  7.     ;----------------------------------------------------------
  8.     ;   variable declaration
  9.     COUNTERA    EQU H'10'
  10.     COUNTERB    EQU H'11'
  11.     COUNTER     EQU H'12'
  12.     CONFIG_ADDR EQU H'13'
  13.     VAL         EQU H'14'
  14.  
  15.     ORG     0x00
  16.     GOTO    START
  17.  
  18.     ORG     0x04
  19.     GOTO    ON_INT
  20.  
  21. START
  22.     ;----------------------------------------------------------
  23.     ;   register configurations
  24.  
  25.     ;----------I/O Configurations------------------------------
  26.     BSF     STATUS, RP0
  27.     MOVLW   0x00
  28.     MOVWF   TRISA
  29.  
  30.     MOVLW   B'00000001'
  31.     MOVWF   TRISB
  32.     ;----------End of I/O Configurations-----------------------
  33.  
  34.     ;----------Counter Configurations--------------------------
  35.     MOVLW   0x07
  36.     MOVWF   OPTION_REG
  37.     ;----------End of Counter Configurations-------------------
  38.  
  39.     ;----------Interrupt Configurations------------------------
  40.     BCF     STATUS, RP0
  41.     BSF     INTCON, GIE
  42.     BSF     INTCON, INTE
  43.     ;BSF        STATUS, RP0
  44.     ;----------End of Interrupt Configurations-----------------
  45.  
  46.     ;----------General Initialization--------------------------
  47.     CLRF    PORTA
  48.     CLRF    PORTB
  49.     ;----------End of General Initialization-------------------
  50.  
  51. RESTART                 ;   start of the counter, initializes
  52.     MOVLW   0x00        ;   the port A to the first two values to
  53.     MOVWF   PORTA       ;   display
  54.     CALL    DELAY
  55.  
  56.     MOVLW   0x01
  57.     MOVWF   COUNTER
  58.  
  59. COUNT
  60.     ;---------------------------------------------------------
  61.     ;   loop of the program
  62.     MOVF    COUNTER, W  ;   the counter decrements and will be displayed
  63.     MOVWF   PORTA       ;   to the port A
  64.     CALL    DELAY
  65.  
  66.     INCF    COUNTER, F
  67.  
  68.     MOVF    COUNTER, W
  69.    
  70.     XORLW   0x0A        ;   once it reaches A... not 9 (since i need to display 9)
  71.     BTFSS   STATUS, Z   ;   skip the count and restart
  72.    
  73.     GOTO    COUNT
  74.     GOTO    RESTART
  75.  
  76. DELAY
  77.     ;---------------------------------------------------------
  78.     ;   create a delay
  79.     MOVLW   D'1'
  80.     MOVWF   COUNTERA
  81.  
  82. LOOPA
  83.     CALL    DELAYA
  84.     DECFSZ  COUNTERA, 1
  85.     GOTO    LOOPA
  86.     RETLW   0x00
  87.  
  88. DELAYA
  89.     MOVLW   D'15'
  90.     MOVWF   COUNTERB
  91.  
  92. LOOPB
  93.     CALL    DELAYB
  94.     DECFSZ  COUNTERB, 1
  95.     GOTO    LOOPB
  96.     RETLW   0x00
  97.  
  98. DELAYB
  99.     CLRF     TMR0
  100.     BCF     INTCON, T0IF
  101.  
  102. LOOPC
  103.     BTFSS   INTCON, T0IF
  104.     GOTO     LOOPC
  105.     RETLW    0x00
  106.  
  107. ON_INT
  108.     BSF     PORTB, 1
  109.     BCF     INTCON, INTF
  110.  
  111. ON_STORE_DATA
  112.     ;   EEPROM initialization
  113.     BCF     STATUS, RP0
  114.     CLRF    EEADR
  115.     CLRF    EEDATA
  116.  
  117.     MOVLW   0x00
  118.     MOVWF   CONFIG_ADDR
  119.     MOVWF   EEADR
  120.  
  121.     MOVF    COUNTER, W      ;   put the value of the counter to working register
  122.     MOVWF   EEDATA          ;   then put the value to the EEPROM Data
  123.  
  124.     BSF     STATUS, RP0
  125.  
  126.     BCF     INTCON, GIE
  127.  
  128.     BSF     EECON1, WREN    ;   write the data to the EEPROM
  129.     MOVLW   0x55            ;   according to the data sheet
  130.     MOVWF   EECON2
  131.     MOVLW   0xAA
  132.     MOVWF   EECON2
  133.     BSF     EECON1, WR
  134.  
  135.     BSF     INTCON, GIE
  136.  
  137.     ;   verify the data after storing it
  138. ON_VERIFY_DATA
  139.     BCF     STATUS, RP0
  140.  
  141.     MOVLW   0x00            ;   initialize the address where the
  142.     MOVWF   EEADR           ;   EEPROM will read (address 0x00)
  143.  
  144.     BSF     STATUS, RP0
  145.  
  146.     BCF     EECON1, WREN   
  147.     BCF     EECON1, WR
  148.     BSF     EECON1, RD
  149.  
  150.     BCF     STATUS, RP0
  151.     MOVF    EEDATA, W
  152.     MOVWF   VAL
  153.  
  154.     RRF     VAL, F          ;   rotate the value of the VAL variable
  155.     RRF     VAL, F          ;   4 times to produce the output to the
  156.     RRF     VAL, F          ;   4 most significant bits
  157.     RRF     VAL, F
  158.     RRF     VAL, F
  159.    
  160.     MOVF    VAL, W
  161.     MOVWF   PORTB
  162.  
  163. INT_LOOP                    ;   stops the execution of the countdown
  164.     MOVF    COUNTER, W      ;   by looping until the switch is
  165.     MOVWF   PORTA           ;   turned on again
  166.     BTFSC   PORTB, 0
  167.     GOTO    COUNT
  168.     GOTO    ON_INT
  169.    
  170.     END
Advertisement
Add Comment
Please, Sign In to add comment