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


Follow this link to go back

;=========================================================================
; brSQRT32
;
; Calculates the square root of a thirtytwo bit number using the
; binary restoring method.
;
; Result in ACCaHI:ACCaLO
; Mask in ACCbHI:ACCbLO
; Input in ACCcHI:ACCcLO:ACCdHI:ACCdLO
;
; Takes between 392 and 439 cycles (incl. call and return).
; Uses 58 words ROM, 8 bytes RAM including 4 holding the input.
;
;-------------------------------------------------------------------------

brSQRT32:
movlw 0x40 ; Initial value for Result is...
movwf ACCaHI ; ... 01000000 00000000
clrf ACCaLO,f ;

movlw 0xC0 ; Initial value for mask is...
movwf ACCbHI ; ... 11000000 00000000
clrf ACCbLO,f ; (second '1' is loop counter).

Sub_Cmp:movfp ACCaLO,WREG ; Compare root-so-far with current
subwf ACCcLO,f ; ... remainder.
movfp ACCaHI,WREG ;
subwfb ACCcHI,f ;
btfss ALUSTA,C ;
goto brstr ; (result is -ve, need to restore).

In1: movfp ACCbLO,WREG ; set the current bit in the result.
iorwf ACCaLO,f ;
movfp ACCbHI,WREG ;
iorwf ACCaHI,f ;

ShftUp: rlcf ACCdLO,f ;
rlcf ACCdHI,f ;
rlcf ACCcLO,f ;
rlcf ACCcHI,f ;

rrcf ACCbHI,f ; Shift mask right for next bit, whilst
rrcf ACCbLO,f ; ... shifting IN MSB from remainder.
btfsc ACCbHI,7 ; If MSB is set, unconditionally set the
goto USet1 ; ... next bit.

movfp ACCbLO,WREG ; Append '01' to root-so-far
xorwf ACCaLO,f ;
movfp ACCbHI,WREG ;
xorwf ACCaHI,f ;

btfss ALUSTA,C ; If second '1' in mask is shifted out,
goto Sub_Cmp ; ... then that was the last normal iteration.

movfp ACCaLO,WREG ; Last bit Generation.
subwf ACCcLO,f ; ... The final subtract is 17-bit (15-bit root
movfp ACCaHI,WREG ; ... plus '01'). Subtract 16-bits: if result
subwfb ACCcHI,f ; ... generates a carry, last bit is 0.
btfss ALUSTA,C ;
return

movlw 1 ; If result is 0 AND msb of is '0', result bit
btfsc ALUSTA,Z ; ... is 0, otherwise '1'.
btfsc ACCdHI,7 ;
xorwf ACCaLO,f ;
return

USet1: btfsc ALUSTA,C ; If mask has shifted out, leave. final bit
return ; ... has been set by iorwf at in1.
bcf ACCbHI,7 ; clear bit shifted in from input.

movfp ACCbLO,WREG ; Append '01' to root-so-far
xorwf ACCaLO,f ;
movfp ACCbHI,WREG ;
xorwf ACCaHI,f ;

movfp ACCaLO,WREG ; This subtraction is guaranteed not to
subwf ACCcLO,f ; ... cause a borrow, so subtract and
movfp ACCaHI,WREG ; ... jump back to insert a '1' in the
subwfb ACCcHI,f ; ... root.
goto In1 ;

brstr: movfp ACCaLO,WREG ; A subtract above at Sub_Cmp was -ve, so
addwf ACCcLO,f ; ... restore the remainder by adding.
movfp ACCaHI,WREG ; The current bit of the root is zero.
addwfc ACCcHI,f ;
goto ShftUp ;

Follow this link to go back






delicious
digg
reddit this Reddit this
Faves



 HOT in heaven!