Home     Contact     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science     RSS     Terms of services     Privacy policy  
   
 Home      Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science   



All about PIC microcontrollers

Within these pages, you can find many useful pieces of code mostly in assembly for the Microchip PIC micro controller family. A lot of people have spend many hours trying to put the bits and bytes together. If the code is NOT written by a member of the PCB Heaven community, then a link will be added above the code with the original website that this code was found.
Because the code is copied to our servers, you should know that:

  • The responsible web master of the website that the code is taken, has been informed and he has agreed to copy the code
  • All emails from the above contact have been kept as records but due to personal privacy cannot be shown in public.
  • The author of the code is always clearly indicated above the code. In some cases the author is unknown. If you happen to be the author of the code or you know the person who wrote it, please inform us by email and it will be added ASAP.

We would personally like to send the credits to all the people that managed to write some very interesting code and publish it, and special thanx to the people that originally hosted those code snippets and gave us the permission to copy them.


View code
48 by 24 bit
Author: Nikolai Golovchenko - Scott Dattalo - Tony Kubek
This code was originally located @ http://www.piclist.com


Follow this link to go back

***********************************************************************
; DIVIDE_48by24 - Routine to divide a 48 bit number with a 24 bit number

; result upto 48 bits !
;
; Formula: Dividend = Dividend/Divisor
;
; Format: Little endian, Ram = msb, Ram+x = lsb ( where
x=bytesize-1)
; Ram used: Dividend 6 bytes ( 48 bits )
; Divisor 3 bytes ( 24 bits )
; Temp 3 bytes
; BitCount 1 byte for loop counting ( and temp. saving of
carry )
;
; Divisor is preserved
; Dividend holds result
; Returns with zero in W if failed ( division with zero )
; Else returns with one in w
;
; Based on pseudo-code from Nikolai Golovchenko [golovchenko@mail.ru]
;
;
DIVIDE_48by24

; Test for zero division
MOVF Divisor,W
IORWF Divisor+1,W
IORWF Divisor+2,W
BTFSC STATUS,Z
RETLW 0x00 ; divisor = zero, not possible to calculate return with zero in w

; prepare used variables
CLRF Temp
CLRF Temp+1
CLRF Temp+2

MOVLW D'48' ; initialize bit counter
MOVWF BitCount

CLRC ; clear carry at entry

DIVIDE_LOOP_48by24
; a) first time carry cleared
; b) every other bit carry contains the result of
; last subtraction ( division ).
; shift in carry as lowest bit
RLF Dividend+5,F
RLF Dividend+4,F
RLF Dividend+3,F
RLF Dividend+2,F
RLF Dividend+1,F
RLF Dividend,F
; shift in highest bit from dividend through carry in temp
RLF Temp+2,F
RLF Temp+1,F
RLF Temp,F
; save carry status in bitcounter temporarily ( possible overflow from temp msb )
BTFSC STATUS,C
BSF BitCount,7 ; set bit ( carry was set, temp>24bit)

; subtract 24 bit divisor from 24 bit temp
MOVF Divisor+2,W ; get lsb
SUBWF Temp+2,F ; subtract

MOVF Divisor+1,W ; get middle byte
SKPC ; if overflow ( from prev. subtraction )
INCFSZ Divisor+1,W ; incresase source
SUBWF Temp+1,F ; and subtract from dest.

MOVF Divisor,W ; get top byte
SKPC ; if overflow ( from prev. subtraction )
INCFSZ Divisor,W ; increase source
SUBWF Temp,F ; and subtract from dest.


; BTFSC STATUS,C ; save carry status again to use for negative check
; BSF BitCount,7 ; yes carry was set.

; if was carry set ( saved in top bit of bitcount ):
; a) Temp overflowed ( i.e. temp > divisor )
; b) Result was positive from subtraction ( i.e. temp > divisor )
; if carry was cleared:
; a) temp < divisor

; BTFSC BitCount,7
; GOTO DIVIDE_SKIP_48by24 ; carry was set, subtraction ok, continue with next bit
;Scott Dattalo says:
;...[the lines above which are commented out can be] reduced to:

skpc
btfsc BitCount,7
goto DIVIDE_SKIP_48by24

; result of subtraction was negative restore temp
MOVF Divisor+2,W ; get LSB of divisor
ADDWF Temp+2,F ; add it to the lsb of temp

MOVF Divisor+1,W ; middle byte
BTFSC STATUS,C ; check carry for overflow from previous addition
INCFSZ Divisor+1,W ; if carry set we add 1 to the source
ADDWF Temp+1,F ; and add it if not zero in that case Product+Multipland=Product

MOVF Divisor,W ; MSB byte
BTFSC STATUS,C ; check carry for overflow from previous addition
INCFSZ Divisor,W
ADDWF Temp,F ; handle overflow

DIVIDE_SKIP_48by24
; CLRC ; clear carry
; carry should now reflect if divison was possible ( i.e. temp > divisor )
; so if it was set, set carry to be shifted in as lowest bit of Dividend
; BTFSC BitCount,7
; BSF STATUS,C ; restore carry
; BCF BitCount,7 ; clear bit used for saving carry
; DECFSZ BitCount,F ; decrement loop counter
; GOTO DIVIDE_LOOP_48by24 ; another run
;Scott Dattalo says:
;...[the lines above which are commented out can be] reduced to:

rlf BitCount,W ;Restore the carry
bcf BitCount,7 ;clear temporary carry
decfsz BitCount,F
goto DIVIDE_LOOP_48by24


; finally shift in the last carry
RLF Dividend+5,F
RLF Dividend+4,F
RLF Dividend+3,F
RLF Dividend+2,F
RLF Dividend+1,F
RLF Dividend,F
NOP
NOP
RETLW 0x01 ; done

Follow this link to go back






delicious
digg
reddit this Reddit this
Faves



 HOT in heaven!