Mator

PIC16F684 LED Switch

Nov 5th, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MPASM 3.51 KB | None | 0 0
  1. ;*******************************************************************************
  2. ;
  3. ;    CS 131: Assembly Language Programming
  4. ;    Filename:
  5. ;    Date:
  6. ;    Author:
  7. ;    Description:
  8. ;
  9. ;*******************************************************************************
  10. ; PIC16F684 Configuration Bit Settings
  11.  
  12. ; ASM source line config statements
  13.  
  14. #include "p16F684.inc"
  15.  
  16. ; CONFIG
  17. ; __config 0xF0F4
  18.  __CONFIG _FOSC_INTOSCIO & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_OFF & _IESO_OFF & _FCMEN_OFF
  19.  
  20.  
  21. ;*******************************************************************************
  22. ; Variables
  23. ;*******************************************************************************
  24.  
  25. GPR_VAR     UDATA
  26. loop1       RES     1
  27. loop2       RES     1
  28. loop3       RES     1
  29. flag        RES     1
  30.  
  31. ;*******************************************************************************
  32. ; Reset Vector
  33. ;*******************************************************************************
  34.  
  35. RES_VECT    CODE    H'0000'     ; processor reset vector
  36.             goto    START       ; go to beginning of program
  37.  
  38. ; TODO ADD INTERRUPTS HERE IF USED
  39.  
  40. ;*******************************************************************************
  41. ; MAIN PROGRAM
  42. ;*******************************************************************************
  43.  
  44. MAIN_PROG   CODE                ; let linker place main program
  45.  
  46. START
  47.             banksel PORTC
  48.             movlw   B'00110000'
  49.             movwf   PORTC
  50.             movlw   H'07'
  51.             movwf   CMCON0      ; Set up digital I/O
  52.             banksel ANSEL
  53.             clrf    ANSEL
  54.             movlw   B'00110000'
  55.             movwf   TRISC
  56.  
  57.             bcf     STATUS, RP0
  58.             bsf     PORTC, RC0
  59.  
  60.             movlw   H'50'       ; Initialize looping at H'40'
  61.             clrf    flag        ; Set flag to 0
  62.             ;incf    flag
  63.  
  64. ; Execute the loops
  65. LOOPSTART
  66.             movwf   loop1       ; Move current W value in loop1 counter
  67. SLOOP1
  68.             movwf   loop2       ; Move current W value into loop2 counter
  69.             decfsz  loop1       ; Loop 1 step
  70.             goto    SLOOP2      ; Loop 1 body is Loop 2
  71.             goto    SWITCH      ; Switch and rotate after loop 1 is completed
  72. SLOOP2
  73.             movwf   loop3       ; Move current W value into loop3 counter
  74.             decfsz  loop2       ; Loop 2 step
  75.             goto    SLOOP3      ; Loop 2 body is Loop 3
  76.             goto    SLOOP1      ; When Loop 2 is done, goto beginning of Loop 1
  77. SLOOP3
  78.             nop
  79.             decfsz  loop3
  80.             goto    SLOOP3
  81.             goto    SLOOP2
  82.  
  83. ; Switch the direction of the rotation by changing flag if we're at the
  84. ; rightmost or leftmost LED.
  85. SWITCH
  86.             btfsc   PORTC,RC3   ; Switch to rotating right if RC3 is on
  87.             goto    RIGHT
  88.             btfsc   PORTC,RC0
  89.             goto    LEFT        ; Switch to rotating left is RC0 is on
  90.             goto    ROTATE      ; Rotate if we don't switch
  91. RIGHT
  92.             decf    flag
  93.             goto    ROTATE
  94. LEFT
  95.             incf    flag
  96.             goto    ROTATE
  97.  
  98. ; Rotate to the left/right based on the value in flag
  99. ROTATE
  100.             bcf     STATUS,C    ; Rotation uses the carry bit.
  101.                                 ; We set it to zero to be safe.
  102.             btfss   flag,0
  103.             goto    NOTONE
  104.             goto    ISONE
  105. NOTONE
  106.             rrf     PORTC
  107.             goto    LOOPSTART
  108. ISONE
  109.             rlf     PORTC
  110.             goto    LOOPSTART
  111.  
  112.  
  113.  
  114.             END
Advertisement
Add Comment
Please, Sign In to add comment