Advertisement
Guest User

Untitled

a guest
Jul 10th, 2017
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pic 16 1.83 KB | None | 0 0
  1. #include <p16f84a.inc>
  2.  
  3. ;1 MHz
  4. __config _CP_ON & _WDT_OFF & _PWRTE_ON & _RC_OSC
  5.  
  6. ;Useful macros
  7.  
  8. ;Copy 24-bit constant to address
  9. unpack24 macro address, value
  10.     movlw address
  11.     movwf FSR
  12.    
  13.     movlw value & 0xff
  14.     movwf INDF
  15.     movlw value >> 0x08 & 0xff
  16.     incf FSR,F
  17.     movwf INDF
  18.     movlw value >> 0x10 & 0xff
  19.     incf FSR,F
  20.     movwf INDF
  21.  
  22. endm
  23.  
  24. ;Copy 24-bit value to address
  25. copy24 macro address, value
  26.     movf value,W
  27.     movwf address
  28.     incf address,F
  29.  
  30.     movf value + 1,W
  31.     movwf address + 1
  32.     incf address + 1,F
  33.  
  34.     movf value + 2,W
  35.     movwf address + 2
  36.     incf address + 2,F
  37. endm
  38.  
  39. ;Compare 24-bit value to constant
  40. compare24 macro address, value
  41.     movf address,W
  42.     xorlw value & 0xff
  43.     btfss STATUS,Z
  44.     goto $ + 9
  45.  
  46.     movf address + 1,W
  47.     xorlw value >> 0x08 & 0xff
  48.     btfss STATUS,Z
  49.     goto $ + 5
  50.    
  51.     movf address + 2,W
  52.     xorlw value >> 0x10 & 0xff
  53.     btfsc STATUS,Z
  54. endm
  55.  
  56. ;Port B bits
  57. bLed equ 0
  58.  
  59. ;Flags bits
  60. fDirection equ 0
  61.  
  62. ;Delay
  63. delay_min equ d'50000' ; 0.25 s
  64. delay_max equ d'400000' ; 2 s
  65.  
  66.  
  67. ;Variables
  68. flags equ 0Ch
  69. i equ 0Dh ;24-bit
  70. n equ 10h ;24-bit
  71.  
  72. org 0
  73. goto begin
  74.  
  75. wait24
  76.     decfsz i,F
  77.     goto wait24
  78.     decfsz i + 1,F
  79.     goto wait24
  80.     decfsz i + 2,F
  81.     goto wait24
  82.     return
  83.  
  84. begin
  85. ;Configure port B
  86. bsf STATUS,RP0
  87. movlw b'00000'
  88. movwf TRISB
  89. bcf STATUS,RP0
  90. clrf PORTB
  91.  
  92. ;Set delay to 0.5 s
  93. unpack24 n,delay_min
  94. ;Clear flags
  95. clrf flags
  96.  
  97. main
  98. ;Turn LED on
  99. bsf PORTB,bLed
  100. ;Wait
  101. copy24 i,n
  102. call wait24
  103. ;Turn LED off
  104. bcf PORTB,bLed
  105. ;Wait
  106. copy24 i,n
  107. call wait24
  108. ;Compare n with max
  109. compare24 n,delay_max
  110. bsf flags,fDirection
  111. ;Compare n with min
  112. compare24 n,delay_min
  113. bcf flags,fDirection
  114. ;Clear CARRY
  115. bcf STATUS,C
  116. ;if(!fDirection)
  117. btfsc flags,fDirection
  118. goto slower
  119. ;n<<1
  120. rlf n,F
  121. rlf n + 1,F
  122. rlf n + 2,F
  123. ;goto $ + 3
  124. goto $ + 4
  125. ;else n>>1
  126. slower
  127. rrf n + 2,F
  128. rrf n + 1,F
  129. rrf n,F
  130.  
  131. goto main
  132. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement