please dont rip this site

PIC Microcontoller Radix Math Method

Binary to BCD unpacked 16 bit to 5 digit

From: Juan Mayoral

Based on these two documents from the code library:

1): Binary to BCD half-packed 8 bit to 3 digit 
    From: Scott Dattalo, notes 

2): Binary to BCD unpacked 16 bit to 5 digit 
    From: John Payson via Scott Dattalo 

I wrote this linear code to convert binary to bcd unpacked.
The code isn't too short (131 words), but it always takes the
same time. 

aar0 = binary number low byte
aar1 = binary number high byte
aac0 = bcd number ones
aac1 = bcd number tens
aac2 = bcd number hundreds
aac3 = bcd number thousands
aac4 = bcd number ten-thousands
 
b16_d5
        swapf   aar0,w	; partial ones sum in low byte
        addwf   aar0,w	;
        andlw   0x0f	;
        skpndc		;
        addlw 	0x16	;
        skpndc		;
        addlw  	0x06	;
        addlw   0x06	;
        skpdc		;
	addlw  	-0x06	; wmax=3:0
;
        btfsc   aar0,4	; complete ones sum in low byte
        addlw   0x15+0x06
        skpdc 
        addlw  	-0x06	; wmax=4:5
	movwf	aac0	; save sum in aac0
;
;     8+      4+     2+     1+     8+     4+    2+    1+
;    20
;   100      60     30     15+
;   ----------------------------------------------------
;   128      64     32     16      8      4     2     1
;
        swapf   aar1,w	; partial ones sum in high byte
        addwf   aar1,w	;
        andlw   0x0f	;
        skpndc		;
        addlw 	0x16	;
        skpndc		;
        addlw  	0x06	;
        addlw   0x06	;
        skpdc		;
	addlw  	-0x06	; wmax=3:0
;
	btfsc   aar1,0	; complete ones sum in high byte
        addlw   0x05+0x06
        skpdc 
        addlw  	-0x06	; wmax=3:5
;
        btfsc   aar1,4
        addlw   0x15+0x06
        skpdc 
        addlw  	-0x06	; wmax=5:0
;
	addlw	0x06	; include previous sum
	addwf	aac0,w
        skpdc 
        addlw  	-0x06	; wmax=9:5, ones sum ended
;
	movwf	aac0
	movwf	aac1
	swapf	aac1,f
	movlw	0x0f
	andwf	aac0,f	; save total ones sum in aac0
	andwf	aac1,f	; save partial tens sum in aac1
;
;     8+      4+     2+     1+     8+     4+    2+    1+
;                           5+
;    60      80     90     10+                        5+
;   700     300    100     80     40     20    10    50 
; 32000   16000   8000   4000   2000   1000   500   200
; ------------------------------------------------------
; 32768   16384   8192   4096   2048   1024   512   256
;
			; complete tens sum in low and high byte
	rrf	aar1,w	; rotate right high byte once
	andlw	0x0f	; clear high nibble
	addlw	0x06	; adjust bcd
	skpdc
	addlw	-0x06	; wmax=1:5
;
	addlw	0x06	; include previous sum
	addwf	aac1,w
        skpdc 
        addlw  	-0x06	; wmax=2:4
;
        btfsc   aar0,5
        addlw   0x03+0x06
        skpdc 
        addlw  	-0x06	; wmax=2:7
;
        btfsc   aar0,6
        addlw   0x06+0x06
        skpdc 
        addlw  	-0x06	; wmax=3:3
;
        btfsc   aar0,7
        addlw   0x12+0x06
        skpdc 
        addlw  	-0x06	; wmax=4:5
;
        btfsc   aar1,0
        addlw   0x25+0x06
        skpdc 
        addlw  	-0x06	; wmax=7:0
;
        btfsc   aar1,5
        addlw   0x09+0x06
        skpdc 
        addlw  	-0x06	; wmax=7:9
;
        btfsc   aar1,6
        addlw   0x08+0x06
        skpdc 
        addlw  	-0x06	; wmax=8:7
;
        btfsc   aar1,7
        addlw   0x06+0x06
        skpdc 
        addlw  	-0x06	; wmax=9:3, tens sum ended
;
	movwf	aac1	; save total tens sum in aac1
	swapf	aac1,w
	andlw	0x0f	; load partial hundreds sum in w
;
;     8+      4+     2+     1+     8+     4+    2+    1+
;    20+                    5+
;   100+     60+    30+    10+
;   ----------------------------------------------------
;   128      64     32     16      8      4     2     1
;
;     8+      4+     2+     1+     8+     4+    2+    1+
;                           5+
;    60+     80+    90+    10+                        5+
;   700     300    100     80+    40+    20+   10+   50+
; 32000   16000   8000   4000   2000   1000   500   200+
; ------------------------------------------------------
; 32768   16384   8192   4096   2048   1024   512   256
;
			; complete hundreds sum in high byte
        btfsc   aar1,1
        addlw   0x05+0x06
        skpdc 
        addlw  	-0x06	; wmax=1:4
;
        btfsc   aar1,5
        addlw   0x01+0x06
        skpdc 
        addlw  	-0x06	; wmax=1:5
;
        btfsc   aar1,6
        addlw   0x03+0x06
        skpdc 
        addlw  	-0x06	; wmax=1:8
;
        btfsc   aar1,7
        addlw   0x07+0x06
        skpdc 
        addlw  	-0x06	; wmax=2:5, hundreds sum ended
;
	movwf	aac2	; save total hundreds sum in aac2
	swapf	aac2,w
	movwf	aac3	; save partial thousands sum in aac3	
	movlw	0x0f	; clear high nibble
	andwf	aac1,f	;
	andwf	aac2,f	;
	andwf	aac3,f	;
;
;     8+      4+     2+     1+     8+     4+    2+    1+
;                           5+
;    60+     80+    90+    10+                        5+
;   700+    300+   100+    80+    40+    20+   10+   50+
; 32000   16000   8000   4000   2000   1000   500+  200+
; ------------------------------------------------------
; 32768   16384   8192   4096   2048   1024   512   256
;
			; complete thousands sum in low and high byte
	rrf	aar1,w	; rotate right high byte twice
	movwf	aac4	;
	rrf	aac4,w	;
	andlw	0x0f	; clear high nibble
	addlw	0x06	; adjust bcd
	skpdc		;
	addlw	-0x06	; wmax=1:5
;
	addlw	0x06	; include previous sum
	addwf	aac3,w
	skpdc
	addlw	-0x06	; wmax=1:7
;
        btfsc   aar1,6
        addlw   0x16+0x06
	skpdc
	addlw	-0x06	; wmax=3:3
;
        btfsc   aar1,7
        addlw   0x32+0x06
	skpdc
	addlw	-0x06	; wmax=6:5, thousands sum ended
;
	movwf	aac3	; save total thousands sum in aac3
	movwf	aac4	;
	swapf	aac4,f	; save ten-thousands sum in aac4
	movlw	0x0f	; clear high nibble
	andwf	aac3,f	;
	andwf	aac4,f	;

P.S.: I am a beginner in the task of programming micro controllers.  
      And all suggestion that improves this code will be welcome.
Juan Mayoral (from the end of the world).
;*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *

Comments:


file: /Techref/microchip/math/radix/b2bu-16b5d-jm.htm, 6KB, , updated: 2013/5/16 11:18, local time: 2024/12/25 21:11,
TOP NEW HELP FIND: 
18.116.85.96:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://linistepper.com/techref/microchip/math/radix/b2bu-16b5d-jm.htm"> PIC Microcontoller Radix Math Method </A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?