Searching \ for '[PIC]: Scale a 16 bit number to 3 decimal digits' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devices.htm?key=pic
Search entire site for: 'Scale a 16 bit number to 3 decimal digits'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: Scale a 16 bit number to 3 decimal digits'
2001\09\18@201403 by Dwayne Reid

flavicon
face
Good day to all.

Has anyone run across a routine to scale a 16 bit number to 3 decimal
digits (0-999).  I'm sure that I saw this a couple of years ago but can't
find it in my snippets file or on piclist.com.

Any thoughts?

dwayne


Dwayne Reid   <spam_OUTdwaynerTakeThisOuTspamplanet.eon.net>
Trinity Electronics Systems Ltd    Edmonton, AB, CANADA
(780) 489-3199 voice          (780) 487-6397 fax

Celebrating 17 years of Engineering Innovation (1984 - 2001)

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Do NOT send unsolicited commercial email to this email address.
This message neither grants consent to receive unsolicited
commercial email nor is intended to solicit commercial email.

--
http://www.piclist.com hint: To leave the PICList
.....piclist-unsubscribe-requestKILLspamspam@spam@mitvma.mit.edu


2001\09\18@215123 by Spehro Pefhany

picon face
At 06:08 PM 9/18/01 -0600, you wrote:
>Good day to all.
>
>Has anyone run across a routine to scale a 16 bit number to 3 decimal
>digits (0-999).  I'm sure that I saw this a couple of years ago but can't
>find it in my snippets file or on piclist.com.

Xs = ((x/10)*9)/59  gets you pretty close.

Best regards,


=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Spehro Pefhany --"it's the network..."            "The Journey is the reward"
speffspamKILLspaminterlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com
Contributions invited->The AVR-gcc FAQ is at: http://www.bluecollarlinux.com
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

--
http://www.piclist.com hint: To leave the PICList
.....piclist-unsubscribe-requestKILLspamspam.....mitvma.mit.edu


2001\09\19@014636 by Bob Blick

face
flavicon
face
;BINARY TO ASCII, 16 BIT TO 3DIGITS (0.. 999)
;by Nikolai Golovchenko
;Binary to decimal conversion (0.. 999)
;
;Input: NumH: NumL
;Output Hund: Tens: Ones
;
;If Input > 999 Output will roll over, e. g.
;for input= 5678 output= 678.
;
;
;Size: 34 instructions
;Execution time (max) including return:
;22+ 5* 9- 1+ 5* 6- 1+ 4* 3- 1+ 2 = 108 cycles
;
;5- July- 2000 by Nikolai Golovchenko
bin2dec999
       movf NumH, w
       addlw 241
       addwf NumH, w
       movwf Hund ;b_ 2 = 2a_ 2 - 15
       addwf Hund, w
       addwf Hund, w
       addlw 253
       movwf Tens
       swapf NumL, w
       andlw 0x0F
       addwf Tens, f
       addwf Tens, f ;b_ 1 = 6a_ 2 + 2a_ 1 - 48
       addwf NumH, w
       sublw 251
       movwf Ones
       addwf Ones, f
       addwf Ones, f
       addwf Ones, f
       movf NumL, w
       andlw 0x0F
       addwf Ones, f ;b_ 0 = a_ 0 - 4( a_ 2 + a_ 1) - 20
       movlw 10
bin2dec999a ;9 cycles max
       addwf Ones, f
       decf Tens, f
       skpc
       goto bin2dec999a
bin2dec999b ;6 cycles max
       addwf Tens, f
       decf Hund, f
       skpc
       goto bin2dec999b
bin2dec999c ;3 cycles max
       addwf Hund, f
       skpc
       goto bin2dec999c
       return

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2001\09\19@092331 by mike

flavicon
face
On Tue, 18 Sep 2001 18:08:40 -0600, you wrote:

>Good day to all.
>
>Has anyone run across a routine to scale a 16 bit number to 3 decimal
>digits (0-999).  I'm sure that I saw this a couple of years ago but can't
>find it in my snippets file or on piclist.com.
>
>Any thoughts?
Do a 16x16 Multiply by 1000 and use the top byte as the result. Use
1000 rather than 999 to get closer rounding.
If code is tight and accuracy not too important, if you can multiply
by 1024 instead this will use less code - just limit the number to
63936 and shift it down 6 places.

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2001\09\19@133911 by Nikolai Golovchenko

flavicon
face
Mike,

That's a very nice suggestion! Effectively, you multiply the number by
1000/65536, which is within 0.1% of error if compared to a 999/65535
multiplier, and also gives 999 when input is 65535.

As an easier alternative (and faster), try
http://www.piclist.com/cgi-bin/constdivmul.exe

It does this in 46 instructions/cycles (code is below)

If you need it shorter, the multiplication by 1000 can be combined
with division by 65536 like this:

; Input: y_hi:y_lo
; Output: y_hi:y_lo
; Temporary: x_hi, x_lo, count, temp1, temp0

; initialize multiplier
       movlw 1000 >> 8
       movwf x_hi
       movlw 1000 & 0xFF
       movwf x_lo
; initialize counter (10 iterations)
       movlw 10
       movwf count
; clear sum
       clrf temp0
       clrf temp1
mul1000_loop
; move a new bit from x_hi:x_lo to carry, starting with the lowest
       rrf x_hi, f
       rrf x_lo, f
; if carry set, add input
       skpc
        goto mul1000_add_done
       movfw y_lo
       addwf temp0, f
       movfw y_hi
       skpnc
        incfsz y_hi, w
         addwf temp1, f
mul1000_add_done
; now carry is either cleared if no addition was done, or set
; appropriately as the result of addition. shift it and sum right
       rrf temp1, f
       rrf temp0, f

; next bit
       decfsz count, f
        goto mul1000_loop

; now, we need to shift the sum 6 more bits (to save space, counter
; could have been initialized with 16 and these shifts wouldn't be
; necessary)
       movlw 6
       movwf count
mul1000_shift_loop
       clrc
       rrf temp1, f
       rrf temp0, f
       decfsz count, f
        goto mul1000_shift_loop

; copy temp to y
       movfw temp1
       movwf y_hi
       movfw temp0
       movwf y_lo
; done!

That's about 12 instructions shorter (or 19 if the mul loop is done 16
times), which might be useful...


Nikolai

; ACC = ACC * 0.0152438
; Temp = TEMP
; ACC size = 16 bits
; Error = 0.5 %
; Bytes order = little endian
; Round = no

; ALGORITHM:
; Clear accumulator
; Add input / 64 to accumulator
; Substract input / 2048 from accumulator
; Add input / 8192 to accumulator
; Move accumulator to result
;
; Approximated constant: 0.0152588, Error: 0.0985727 %

;     Input: ACC0 .. ACC1, 16 bits
;    Output: ACC0 .. ACC1, 10 bits
; Code size: 46 instructions

       cblock
       ACC0
       ACC1
       ACC2
       TEMP0
       TEMP1
       endc

;copy accumulator to temporary
       movf    ACC1, w
       movwf   TEMP1
       movf    ACC0, w
       movwf   TEMP0


;shift accumulator right 2 times
       clrc
       rrf     ACC1, f
       rrf     ACC0, f
       clrc
       rrf     ACC1, f
       rrf     ACC0, f

;substract temporary from accumulator
       subwf   ACC0, f
       movf    TEMP1, w
       skpc
       incfsz  TEMP1, w
       subwf   ACC1, f

;shift accumulator right 5 times
       swapf   ACC0, w
       andlw   0x0F
       movwf   ACC0
       swapf   ACC1, w
       movwf   ACC1
       andlw   0xF0
       xorwf   ACC1, f
       iorwf   ACC0, f
       movlw   0xF0
       skpc
       iorwf   ACC1, f
       rlf     ACC1, w
       rrf     ACC1, f
       rrf     ACC0, f

;add temporary to accumulator
       movf    TEMP0, w
       addwf   ACC0, f
       movf    TEMP1, w
       skpnc
       incfsz  TEMP1, w
       addwf   ACC1, f

;shift accumulator right 6 times
       rlf     ACC0, f
       rlf     ACC1, f
       clrf    ACC2
       rlf     ACC2, f
       rlf     ACC0, f
       rlf     ACC1, f
       rlf     ACC2, f
       movf    ACC1, w
       movwf   ACC0
       movf    ACC2, w
       movwf   ACC1


; Generated by http://www.piclist.com/cgi-bin/constdivmul.exe (November 17, 2000 version)
; Wed Sep 19 17:18:46 2001 GMT


Wednesday, September 19, 2001, 6:23:56 AM, Mike Harrison wrote:
{Quote hidden}

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


More... (looser matching)
- Last day of these posts
- In 2001 , 2002 only
- Today
- New search...