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
32 bits by 16 using 16 bits by 16
Author: Nikolai Golovchenko
This code was originally located @ http://www.piclist.com


Follow this link to go back

; x = 60*1e6/y
;
; x, x+1 - rpm
; y, y+1 - pulse width in 1 us units
;
FindRPM
clrf x
movlw 0x87
movwf x+1
movlw 0x93
movwf x+2
movlw 0x03
movwf x+3
goto div32by16to16

; uint16 x = uint32 x / uint16 y
;
; Input:
; x, x+1, x+2, x+3 - 32 bit unsigned integer dividend (x - lsb, x+3 - msb)
; y, y+1 - 16 bit unsigned integer divisor
; Output:
; x, x+1 - 16 bit unsigned integer quotient
; Temporary:
; counter
; temp - remainder extension
;
; Note: result must fit in 16 bits for routine to work
; correctly

div32by16to16
goto div16by16loopinit ;or just move the label

; uint16 x = uint16 x / uint16 y
;
; Input:
; x, x+1 - 16 bit unsigned integer dividend (x - lsb, x+1 - msb)
; y, y+1 - 16 bit unsigned integer divisor
; Output:
; x, x+1 - 16 bit unsigned integer quotient
; Temporary:
; counter
; x+2, x+3 - 16 bit remainder
; temp - remainder extension
; Size: 36 instructions
; Max timing: 6+16*(5+13+3)-1+2+2=345 cycles


div16by16
clrf x+2 ;clear
clrf x+3 ;remainder
div16by16loopinit
clrf temp ;clear remainder extension
movlw 16
movwf counter
setc ;first iteration will be subtraction
div16by16loop
;shift in next result bit and shift out next
;dividend bit to remainder

rlf x, f ;shift lsb
rlf x+1, f ;shift msb
rlf x+2, f
rlf x+3, f
rlf temp, f

movf y, w
btfss x, 0
goto div16by16add

;subtract divisor from remainder
subwf x+2, f
movf y+1, w
skpc
incfsz y+1, w
subwf x+3, f
movlw 1
skpc
subwf temp, f
goto div16by16next

div16by16add
;add divisor to remainder
addwf x+2, f
movf y+1, w
skpnc
incfsz y+1, w
addwf x+3, f
movlw 1
skpnc
addwf temp, f

div16by16next
;carry is next result bit
decfsz counter, f
goto div16by16loop

;shift in last bit
rlf x, f
rlf x+1, f
return

Follow this link to go back






delicious
digg
reddit this Reddit this
Faves



 HOT in heaven!