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
This code was originally located @ http://www.piclist.com


Follow this link to go back

;****************************************************
;max time in loop: 26 cycles
DIVIDE_48by23

; 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

setc
DIVIDE_LOOP_48by23
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

MOVF Divisor+2,W ; get LSB of divisor
btfss Dividend+5, 0
goto Div48by23_add

; subtract 23 bit divisor from 24 bit temp
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.
GOTO DIVIDE_SKIP_48by23 ; carry was set, subtraction ok, continue with next bit

Div48by23_add
; result of subtraction was negative restore temp
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_48by23
DECFSZ BitCount,F ; decrement loop counter
GOTO DIVIDE_LOOP_48by23 ; another run
; 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
RETLW 0x01 ; done

;****************************************************
;max time in loop: 30 cycles
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

clrf Temp2

MOVLW D'48' ; initialize bit counter
MOVWF BitCount

DIVIDE_LOOP_48by24
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

rlf Temp2, f

MOVF Divisor+2,W ; get LSB of divisor
btfsc Temp2, 7
goto Div48by24_add

; subtract 24 bit divisor from 24 bit temp
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.

movlw 1
skpc
subwf Temp2, f
GOTO DIVIDE_SKIP_48by24 ; carry was set, subtraction ok, continue with next bit

Div48by24_add
; result of subtraction was negative restore temp
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

movlw 1
skpnc
addwf Temp2, f

DIVIDE_SKIP_48by24
DECFSZ BitCount,F ; decrement loop counter
GOTO DIVIDE_LOOP_48by24 ; another run
; 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
RETLW 0x01 ; done

;****************************************************

Follow this link to go back






delicious
digg
reddit this Reddit this
Faves



 HOT in heaven!