Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <p16f84a.inc>
- ;1 MHz
- __config _CP_ON & _WDT_OFF & _PWRTE_ON & _RC_OSC
- ;Useful macros
- ;Copy 24-bit constant to address
- unpack24 macro address, value
- movlw address
- movwf FSR
- movlw value & 0xff
- movwf INDF
- movlw value >> 0x08 & 0xff
- incf FSR,F
- movwf INDF
- movlw value >> 0x10 & 0xff
- incf FSR,F
- movwf INDF
- endm
- ;Copy 24-bit value to address
- copy24 macro address, value
- movf value,W
- movwf address
- incf address,F
- movf value + 1,W
- movwf address + 1
- incf address + 1,F
- movf value + 2,W
- movwf address + 2
- incf address + 2,F
- endm
- ;Compare 24-bit value to constant
- compare24 macro address, value
- movf address,W
- xorlw value & 0xff
- btfss STATUS,Z
- goto $ + 9
- movf address + 1,W
- xorlw value >> 0x08 & 0xff
- btfss STATUS,Z
- goto $ + 5
- movf address + 2,W
- xorlw value >> 0x10 & 0xff
- btfsc STATUS,Z
- endm
- ;Port B bits
- bLed equ 0
- ;Flags bits
- fDirection equ 0
- ;Delay
- delay_min equ d'50000' ; 0.25 s
- delay_max equ d'400000' ; 2 s
- ;Variables
- flags equ 0Ch
- i equ 0Dh ;24-bit
- n equ 10h ;24-bit
- org 0
- goto begin
- wait24
- decfsz i,F
- goto wait24
- decfsz i + 1,F
- goto wait24
- decfsz i + 2,F
- goto wait24
- return
- begin
- ;Configure port B
- bsf STATUS,RP0
- movlw b'00000'
- movwf TRISB
- bcf STATUS,RP0
- clrf PORTB
- ;Set delay to 0.5 s
- unpack24 n,delay_min
- ;Clear flags
- clrf flags
- main
- ;Turn LED on
- bsf PORTB,bLed
- ;Wait
- copy24 i,n
- call wait24
- ;Turn LED off
- bcf PORTB,bLed
- ;Wait
- copy24 i,n
- call wait24
- ;Compare n with max
- compare24 n,delay_max
- bsf flags,fDirection
- ;Compare n with min
- compare24 n,delay_min
- bcf flags,fDirection
- ;Clear CARRY
- bcf STATUS,C
- ;if(!fDirection)
- btfsc flags,fDirection
- goto slower
- ;n<<1
- rlf n,F
- rlf n + 1,F
- rlf n + 2,F
- ;goto $ + 3
- goto $ + 4
- ;else n>>1
- slower
- rrf n + 2,F
- rrf n + 1,F
- rrf n,F
- goto main
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement