PIC Microcontroller math library


24 bits by 16 division

This is an example for 0x068FB0 to 0x0503 division (a2 a1 a0 /b1 b0), results will be in d2 d1 d0

A tutorial and a description about how this CORDIC division method it works can be found at www.piclist.com
The project source code for simulation in MPLAB IDE it is available for download

24 bits by 16 division for pic 16f628 microcontroller

init
         movlw   0x06    ;d2 d1 d0 = 430000/b1 b0
         movwf   a2
         movlw   0x8F
         movwf   a1
         movlw   0xB0
         movwf   a0
         movlw   0x05
         movwf   b1
         movlw   0x03
         movwf   b0
         call    divv24
         goto	fin
 ;********************************************************************** 
; d2 d1 d0 = a2 a1 a0 / b1 b0
divv24
         clrf    b2
         movf    b0,F    ; test division to null
         btfss   STATUS,Z        
         goto    test_z_ok
         movf    b1,F
         btfsc   STATUS,Z
         return
test_z_ok
         movlw   0x01
         movwf   temp0
         clrf    temp1
         clrf    temp2
         clrf    d0              ; init result
         clrf    d1
         clrf    d2
shift_24
         bcf     STATUS,C        ; rotate to left up b2,7 =1 in this case 
         rlf   temp0,F   ; multiplication counter 
         rlf   temp1,F
         rlf   temp2,F
         bcf     STATUS,C
         rlf   b0,F
         rlf   b1,F
         rlf   b2,F
         btfss   b2,7    ; 430000/b2b1b0 
 ; work faster if shift it  is according with a2a1a0, 
 ; in this case a2=0x06 so I replace last command with this
 ;       btfss   b2,2    ; 430000/b2b1b0 
         goto    shift_24
wdh32
         call    store_a
         call    sub32   ; ok substract
         btfsc   STATUS,C        ; a1-b1 <0
         goto    partial ; if a1-b1>0 then countx
         call    restore
         goto    rest
partial
         movf    temp0,W ; add temp0 to result
         addwf   d0,F
         btfsc   STATUS,C        
         incf    d1,F
         movf    temp1,W
         addwf   d1,F
         btfsc   STATUS,C        ; btfsc STATUS,C 
         incf    d2,F
         movf    temp2,W
         addwf   d2,F
rest
         bcf     STATUS,C
         rrf     b2,F
         rrf     b1,F    ; rotate to right for b and t then repeat
         rrf     b0,F
         bcf     STATUS,C
         rrf     temp2,F
         rrf     temp1,F
         rrf     temp0,F
         btfss   STATUS,C        ; if temp0<>0 then wdh32
         goto     wdh32

        return
sub32
         movf    b0,W 
         subwf   a0,F 
         movf    b1,W 
         btfss   STATUS,C 
         incfsz  b1,W
         subwf   a1,F 
         movf    b2,W 
         btfss   STATUS,C 
         incfsz  b2,W
         subwf   a2,F 
         return
store_a
         movf    a0,W
         movwf   count0
         movf    a1,W
         movwf   count1
         movf    a2,W
         movwf   count2
         return
restore
         movf    count0,W
         movwf   a0
         movf    count1,W
         movwf   a1
         movf    count2,W
         movwf   a2
         return


Back to my home page

Last updated January, 2010

© Copyright 2010 Bergthaller Iulian-Alexandru