list p=16f877a
include "p16f877a.inc"
errorlevel -302
__config _LVP_OFF & _XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF & _BODEN_OFF & _DEBUG_OFF
cblock 0x020 ;start of general purpose registers
CCP1aH
CCP1aL
CCP1bH
CCP1bL
CCP2H
CCP2L
topH
endc
cblock 0x070
topL
divisL ;divisor Low byte[CCP1b-CCP1a]
divisH ;divisor high byte
b1
b2
bitcnt
remdrH
remdrL
divid0 ; lsb of divident [Used in Multiply as 4 quotient
divid1 ; and the same values are used in Divide a divident]
divid2
divid3 ; MSB
endc
; Start at the reset vector
org 0x000
goto Start
Start org 0x010
clrf PORTB
clrf PORTD
clrf PORTC
clrf CCP1aH
clrf CCP1aL
clrf CCP1bH
clrf CCP1bL
clrf CCP2H
clrf CCP2L
clrf TMR1H
clrf TMR1L
clrf topH
clrf topL
clrf divisH
clrf divisL
clrf b1
clrf b2
clrf bitcnt
clrf remdrH
clrf remdrL
clrf divid0 ; lsb of divident
clrf divid1
clrf divid2
clrf divid3 ; MSB
clrf CCPR1H
clrf CCPR1L
clrf CCPR2H
clrf CCPR2L
bsf STATUS,RP0 ;bank 1
bcf STATUS,RP1
movlw b'00000110'
movwf TRISC
movlw b'00000000'
movwf TRISB ;portb [7-0] outputs
movlw b'00000000'
movwf TRISD ;portd output
; all inputs DIGITAL
bcf STATUS,RP0 ;bank0
; bcf PIR1,CCP1IF
;**********************************************
Main:
movlw b'00000000' ;timer 1 using to capture, prescaler 1:1
movwf T1CON
bsf T1CON,TMR1ON
movlw b'00000101'
movwf CCP1CON ;start with CCP1 rising CAPTURE
movlw b'00000101'
movwf CCP2CON ;start CCP2 with rising CAPTURE
bcf PIR1,CCP1IF
bcf PIR2,CCP2IF
Wait:
btfss PIR1,CCP1IF
goto Wait
movf CCPR1H,W ;save the value in CCP1aH and CCP1aL {lower value}
movwf CCP1aH
movf CCPR1L,W
movwf CCP1aL
bcf PIR1,CCP1IF
;
Wait1:
btfss PIR1,CCP1IF
goto Wait1
movf CCPR1H,W ;save now the value in CCP1bH and CCP1bL
movwf CCP1bH
movf CCPR1L,W
movwf CCP1bL
bcf PIR1,CCP1IF ;clr flag CCP1
Wait2
btfss PIR2,CCP2IF
goto Wait2
movf CCPR2H,W ;save now the value in CCP2 H:L
movwf CCP2H
movf CCPR2L,W
movwf CCP2L
bcf PIR2,CCP2IF ;clr flag CCP2
;*********CCP1b-CCP1a*******************
SUB1:
movf CCP1aL,W
subwf CCP1bL,W
movwf divisL
btfss STATUS, C
goto BORROW1
goto SUB_1
BORROW1:
decf CCP1bH, F
SUB_1:
movf CCP1aH,W
subwf CCP1bH,W
movwf divisH
;*******CCP2-CCP1b****************
SUB2:
movf CCP1bL,W
subwf CCP2L,W
movwf topL
btfss STATUS, C
goto BORROW2
goto SUB_2
BORROW2:
decf CCP2H, F
SUB_2:
movf CCP1bH,W
subwf CCP2H,W
movwf topH
;***********Multiply topH:topL X b2:b1[ b2:b1=360]***********************
;****INT b2:b1 as 360******
movlw d'1'
movwf b2
movlw d'104'
movwf b1
;*************Calling Multiply & Divide sub routine*********
;---------------------------------------------------------
call Multiply
call divide
nop
nop
movf divid0,w ; Show result[LSB0,1] in binary on
movwf PORTB
;;;;;;FINALLLLLLLLL////*********
movf divid1,w ; LEDs connected to ports.
movwf PORTD
; movf divid2,w ; Show result[MSB2,3] in binary on
; movwf PORTB
; movf divid3,w ; LEDs connected to ports.
; movwf PORTD
goto Main ; Endless loop
;----------------------------------------------------
;************Multiply*********************************
Multiply
clrf divid3
clrf divid2
clrf divid1
movlw 0x80
movwf divid0
nextbit
rrf topH,f
rrf topL,f
btfss STATUS,C
goto nobit_l
movf b1,w
addwf divid1,f
movf b2, w
btfsc STATUS,C
incfsz b2, w
addwf divid2, f
btfsc STATUS,C
incf divid3, f
bcf STATUS,C
nobit_l
btfss topL, 7
goto nobit_h
movf b1,w
addwf divid2,f
movf b2, w
btfsc STATUS,C
incfsz b2, w
addwf divid3, f
nobit_h
rrf divid3,f
rrf divid2,f
rrf divid1,f
rrf divid0,f
btfss STATUS,C
goto nextbit
return
;**************DIVIDE*******************************
divide
movlw d'32' ; 32-bit divide by 16-bit
movwf bitcnt
clrf remdrH ; Clear remainder
clrf remdrL
dvloop
clrc ; Set quotient bit to 0
; Shift left dividend and quotient
rlf divid0, 1 ; lsb
rlf divid1, 1
rlf divid2, 1
rlf divid3, 1 ; lsb into carry
rlf remdrL, 1 ; and then into partial remainder
rlf remdrH, 1
skpnc ; Check for overflow
goto subd
movfw divisH ; Compare partial remainder and divisor
subwf remdrH,w
skpz
goto testgt ; Not equal so test if remdrH is greater
movfw divisL ; High bytes are equal
subwf remdrL,w
testgt
skpc ; Carry set if remdr >= divis
goto remrlt
subd
movfw divisL ; Subtract divisor from partial remainder
subwf remdrL, 1
skpc ; Test for borrow
decf remdrH, 1 ; Subtract borrow
movfw divisH
subwf remdrH, 1
bsf divid0,0 ; Set quotient bit to 1
; Quotient replaces dividend which is lost
remrlt
decfsz bitcnt, 1
goto dvloop
return
end