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
10 bits
Author: Nikolai Golovchenko - John Payson
This code was originally located @ http://www.piclist.com


Follow this link to go back

;***********************************************
; Square 10 bits
;
; 6 Aug 2000 by Nikolai Golovchenko
; Based on the original version of John Payson
;
; Input:
; SrcH:SrcL
; Output:
; DstH:DstM:DstL
;
; Instructions: 68
; Execution time(including return): 51+5*3+1=67
;
; Description:
; The goal is calculation of Dst = Src^2
;
; Src may be rewritten as:
; Src = SrcH*256 + SrcL
; Let's introduce variables a, ah, al, and b:
; a = ah*16 + al = SrcL
; b = SrcH
; Then
; Src^2 = (256*b+a)^2=65536*b^2+512*b*a+a^2
;
; Sqr4 routine calculates squares for 4 bit data
; (b, al, and ah) using look-up table.
;
; Now we can find b^2 by Sqr4 and multiply it by 65536,
; and calculate 512*b*a.
;
; To calculate a^2 let's expand it in byte halves:
; a^2=(16*ah+al)^2=256*ah^2+32*ah*al+al^2
;
; So,
; Src^2=65536*b^2+512*b*a+256*ah^2+32*ah*al+al^2
;
; This is the algorithm of how to square a 10 bit number
; using Sqr4 look-up table.
;
;***********************************************
Sqr10
clrf DstH ;clear result
clrf DstM
clrf DstL
clrc ;clear carry
;find 32*ah*al
movf SrcL, w ;w = SrcL
andlw 0x0F
btfsc SrcL, 4
addwf DstM, f
rrf DstM, f ;use carry after addition
rrf DstL, f
;DstM:DstL=ah<0>*al*256/2
btfsc SrcL, 5
addwf DstM, f
rrf DstM, f
rrf DstL, f
;DstM:DstL=(ah<0>*al*256/2 + ah<1>*al*256)/2=64*al(ah<0>+2*ah<1>)
btfsc SrcL, 6
addwf DstM, f
rrf DstM, f
rrf DstL, f
;DstM:DstL=32*al(ah<0>+2*ah<1>+4*ah<2>)
btfsc SrcL, 7
addwf DstM, f
;DstM:DstL=32*al(ah<0>+2*ah<1>+4*ah<2>+8*ah<3>)=32*ah*al
;(maximum value = 0x1C20)

;Now add squared al and ah
call Sqr4
addwf DstL, f ;add al^2
skpnc
incf DstM, f ;propagate carry to DstM

swapf SrcL, w
andlw 0x0F ;w = ah
call Sqr4
addwf DstM, f ;carry is reset

;At this point DstM:DstH contains a^2

;Add 512*b*a to Dst
;512*b*a=512*b*a=512*(2*b<1>+b<0>)*<128*a<7>+a<0:6>)=
;=65536*b*a<7>+256*(2*a<0:6>*b<0>+2*a<0:6>*b<1>+2*a<0:6>*b<0>)

movf SrcH, w ;Dst += 512*b*(128*a<7>) = 65536*b*a<7>
btfsc SrcL, 7
addwf DstH, f ;carry is reset

rlf SrcL, w ;w = a<0:6>*2 (a<7> is already used)
clrc
btfsc SrcH, 0
addwf DstM, f ;Dst += 512*b<0>*a<0:6>
skpnc
incf DstH, f

clrc ;Dst += 512*b<1>*a<0:6>
btfsc SrcH, 1
addwf DstM, f
skpnc
incf DstH, f

clrc ;Dst += 512*b<1>*a<0:6>
btfsc SrcH, 1
addwf DstM, f
skpnc
incf DstH, f

;Add 65536*b^2
movf SrcH, w
call Sqr4
addwf DstH, f
retlw 0 ; All done!
Sqr4: ;Look-up table for 4 bit squares
addwf PCL, f
DT 0,1,4,9,16,25,36,49,64,81,100,121,144,169,196,225

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

Follow this link to go back






delicious
digg
reddit this Reddit this
Faves



 HOT in heaven!