please dont rip this site

PIC Microcontroller Basic Math Division Tutorial

Normally division algorithms follow the way children are tought to operate. Let's take an example:

  • 16546 is the numerator, 27 the divisor
  • z: start with the left-most digit:
  • if  1 < 27 then add the second digit
  • if 16 < 27 then add the third digit
  • 165 > 27, so integer-divide 165 div 27 = 6
  • get the remainder, which is 165 - 6 * 27 = 3
  • now restart at z with the remainder

With RISC-technology, at assembler level, the tests are operated with subtractions, checking whether the results are negative, zero or positive. The integer-division is done by successive subtractions until the result is negative. A counter then indicates how often subtractions were made.

There is a faster way; CORDIC methods are a very different approach to mathematical operations. The higher speed of the algorithms is the result of a divide and conquer approach. Let's see how CORDIC division works:

Suppose you want to integer-divide 8710 = 10101112 through 610 = 1102.

numerator 01010111   base_index := 00000001  = 1

divisor   00000110       result:=0

- rotate divisor and base_index until the most significant bits of numerator and divisor are equal:


          00001100                 00000010  = 2

          00011000                 00000100  = 4

          00110000                 00001000  = 8

          01100000                 00010000  = 16

 - now subtract both numerator and altered divisor: 

          01010111-

          01100000

         ----------

            < 0

- if negative -which is the case here- rotate back divisor and base_index one digit to the right:


          00110000                 00001000  = 8

 - substract again rotated divisor from numerator: 

          01010111-

          00110000

         ----------

          00100111, positive remainder

 - now replace the divisor by the remainder: 

      new numerator:= 00100111

 - this time add the base_index to result: 

      result:= result(0) + 8  = 8

 - now rotate to the right divisor and base_index one digit: 

          00011000                00000100  = 4

 - subtract again: 

          00100111-

          00011000

         ----------

          00001111, remainder positive, so

      new numerator:=00001111 

      result:=result + base_index = 8+4 = 12 

 - rotate: 

          00001100                00000010  = 2

 - substract : 

          00001111-

          00001100

         ----------

          00000011, remainder positive, so

      new numerator:=00000011 

      result:=result + base_index = 12+2 = 14 

 - rotate: 

          00000110                00000001  = 1

 - subtract: 

          00000011-

          00000110

         ----------

            < 0, so do nothing

 - stop 

Here is PIC 16F84 and 628 code:

DIVV8
	MOVF  TEMPY8,F
	BTFSC STATUS,Z ;SKIP IF NON-ZERO
	RETURN
	CLRF  RESULT8
	MOVLW  1
	MOVWF  IDX16
SHIFT_IT8
	BCF   STATUS,C
	RLF   IDX16,F
	BCF   STATUS,C
	RLF   TEMPY8,F
	BTFSS  TEMPY8,7
	GOTO  SHIFT_IT8
DIVU8LOOP
	MOVF  TEMPY8,W
	SUBWF  TEMPX8
	BTFSC   STATUS,C
	GOTO   COUNT8
	ADDWF   TEMPX8
	GOTO  FINAL8
COUNT8
	MOVF  IDX16,W
	ADDWF  RESULT8
FINAL8
	BCF   STATUS,C
	RRF   TEMPY8,F
	BCF   STATUS,C
	RRF   IDX16,F
	BTFSS   STATUS,C
	GOTO   DIVU8LOOP
	RETURN

 

SUB16
	MOVF    TEMPY16_H,W
	MOVWF   TEMPYY
	MOVF    TEMPY16,W
	SUBWF   TEMPX16
	BTFSS   STATUS,C
	INCF    TEMPYY,F
	MOVF    TEMPYY,W
	SUBWF   TEMPX16_H
RETURN
ADD16BIS
	MOVF    TEMPY16,W
	ADDWF   TEMPX16
	BTFSC   STATUS,C
	INCF    TEMPX16_H,F
	MOVF    TEMPY16_H,W
	ADDWF   TEMPX16_H
	RETURN
DIVV16
	MOVF TEMPY16,F
	BTFSS  STATUS,Z
	GOTO  ZERO_TEST_SKIPPED
	MOVF TEMPY16_H,F
	BTFSC STATUS,Z
	RETURN
ZERO_TEST_SKIPPED
	MOVLW   1
	MOVWF   IDX16
	CLRF    IDX16_H
	CLRF    RESULT16
	CLRF    RESULT16_H
SHIFT_IT16
	BCF     STATUS,C
	RLF     IDX16,F
	RLF     IDX16_H,F
	BCF     STATUS,C
	RLF     TEMPY16,F
	RLF     TEMPY16_H,F
	BTFSS   TEMPY16_H,7
	GOTO    SHIFT_IT16
DIVU16LOOP
	CALL    SUB16
	BTFSC   STATUS,C
	GOTO    COUNTX
	CALL    ADD16BIS
	GOTO    FINALX
COUNTX
	MOVF    IDX16,W
	ADDWF   RESULT16
	BTFSC   STATUS,C
	INCF    RESULT16_H,F
	MOVF    IDX16_H,W
	ADDWF   RESULT16_H
FINALX
	BCF     STATUS,C
	RRF     TEMPY16_H,F
	RRF     TEMPY16,F
	BCF     STATUS,C
	RRF     IDX16_H,F
	RRF     IDX16,F
	BTFSS   STATUS,C
	GOTO    DIVU16LOOP
	RETURN

... somewhere in the code CALL DIVV16

Note that these programs work only for unsigned variables. Worst case for DIVV8 is about 144 cycles, which at 20 MHz is about 30 microseconds. The advantage of this algorithm appears more clearly, if larger variables should be used.

Bergthaller C Iulian Says: " Based on this tutorial I make a subroutine for 24bits by 16 division. " +

Comments:


file: /Techref/microchip/math/div/tutorial.htm, 7KB, , updated: 2018/1/4 16:37, local time: 2024/11/22 10:31,
TOP NEW HELP FIND: 
13.58.200.78: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://ecomorder.com/techref/microchip/math/div/tutorial.htm"> Fast division for PICs</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?

 

Welcome to ecomorder.com!

 

Welcome to ecomorder.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .