please dont rip this site

PIC Microcontoller Math Method

Divide 16 bits by the constant value 10 by Alec_t

from: http://www.chatzones.co.uk/discus/messages/7/DivideByTen-11542.txt

;Subroutine to divide by 10 using the binary fraction equivalent of 0.1.
;Argument: 16-bit dividend in DDHI,DDLO. 
;Returns: 16-bit quotient in QTHI,QTLO.
METHOD1	CLRF	QTHI	; Clear quotient accumulator 
	CLRF	QTLO	;
	MOVLW	B'00011001'	; 1/10 = binary .0001100110011001 recurring 
	MOVWF	DRHI	; ...loaded into divisor registers
	MOVLW	B'10011001'	;
	MOVWF	DRLO	;
	MOVLW	16	; Initialise loop counter (16 gives max accuracy)
	MOVWF	COUNT	;
SHIFT	BCF	STATUS,C	;
	RRF	DDHI,F	; Halve the dividend
	RRF	DDLO,F	;
	RLF	DRLO,F	; Shift the divisor 
	RLF	DRHI,F	; ...so the left-most bit goes into carry
	BTFSC	STATUS,C	; Was the left-most bit a '1'?
	CALL	ADDIT	; Yes, so accumulate the partial dividend
	DECF	COUNT,F	; Bump the loop count
	BTFSC	STATUS,Z	; Is it zero?
	RETURN		; Yes. Done.
	GOTO	SHIFT	; No. Loop for next divisor bit
	RETURN				
;Subroutine to accumulate partial results. Called by METHOD1.
;Argument: Current partial result in DDHI,DDLO.
;Returns: Accumulated quotient in QTHI,QTLO.
ADDIT	MOVF	DDLO,W	; Get the partial result LSB
	ADDWF	QTLO,F	; Add it to the quotient LSB
	BTFSC	STATUS,C	; Carry generated?
	INCF	QTHI,F	; Yes. Bump quotient MSB
	MOVF	DDHI,W	; No. Get the partial result MSB
	ADDWF	QTHI,F	; Add it to the quotient MSB
	RETURN		; Return

;Subroutine to divide by 10 by repeated subtraction of 10.
;Argument: 16-bit dividend in DDHI,DDLO. 
;Returns: 16-bit quotient in QHI,QLO, remainder in DDLO.
METHOD2	CLRF	QHI	; Clear quotient registers
	CLRF	QLO	;
	MOVLW	10	; W = divisor
STEP	SUBWF	DDLO,F	; Subtract 10 from dividend LSB
	BTFSC	STATUS,C	; Carry?
	GOTO	BUMP	; No. Jump
	MOVF	DDHI,F	; Yes. Is dividend MSB 0?
	BTFSC	STATUS,Z	;
	GOTO	DONE	; Yes. Done
	DECF	DDHI,F	; No. Decrement dividend MSB
BUMP	INCF	QLO,F	; Bump quotient LSB
	BTFSC	STATUS,Z	; Is it 0?
	INCF	QHI,F	; Yes. Increment quotient MSB
	GOTO	STEP	; Loop until dividend is <0
DONE	ADDWF	DDLO,F	; Correct the dividend LSB
	RETURN

file: /Techref/microchip/math/div/16byconst10-at.htm, 2KB, , updated: 2011/12/20 08:41, local time: 2024/12/27 03:39,
TOP NEW HELP FIND: 
3.144.237.52:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://linistepper.com/techref/microchip/math/div/16byconst10-at.htm"> Divide 16 bits by the constant value 10 by Alec_t</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?