## Binary to BCD packed and ASCII, 32 bit to 10 digits

from Ron Kreymborg and Mike Keitz

[ed: rough guess is that about 2200 instructions will be executed]

;******************************************************************
;
; Test program for 32-bit unsigned binary to BCD and BCD to ASCII.
;
;******************************************************************
title   "32-bit binary to ascii"
list p=pic16f84,r=dec,n=80,x=off,st=off
include <p16f84.inc>
errorlevel -302         ; no bank warnings
errorlevel -305		; no default dest warnings

#define	number	123456789

local	m,n,p
p = number / 16777216
movlw	p
movwf	bin
m = (number - p * 16777216) / 65536
movlw	m
movwf	bin+1
n = (number - p * 16777216 - m * 65536) / 256
movlw	n
movwf	bin+2
movlw	number - p * 16777216 - m * 65536 - n * 256
movwf	bin+3
endm

CBLOCK	0x0c
bin:4		; 32-bit binary number (unsigned)
bcd:10		; 10 BC digits or 10 ascii chars
pti,pto		; pointers
ii
temp
cnt
ENDC

call	b2bcd		; convert to 32-bit binary to 10 bcd
call	bcd2a		; convert 10 bcd to 10 ascii
goto	\$

;******************************************************************
; Convert the 10 binary coded digits (5 bytes) starting at
; <bcd> into an ascii string also starting at <bcd>. Original
; bcd digits are lost.

bcd2a	movlw	bcd+9
movwf	pto		; destination pointer
movlw	bcd+4
movwf	pti		; source pointer
movlw	5		; 5 bytes to process
movwf	cnt

bcd2a1	movf	pti,w		; get current input pointer
movwf	fsr
decf	pti,f		; prepare for next
movf	indf,w		; get 2 bcds
movwf	temp		; save for later
movf	pto,w		; get current output pointer
movwf	fsr
decf	pto,f		; prepare for next
decf	pto,f
movf	temp,w		; get digits back
andlw	0x0f		; process lsd
movwf	indf		; to output
decf	fsr,f
swapf	temp,w		; process msd
andlw	0x0f
movwf	indf		; to output
decfsz	cnt		; all digits?
goto	bcd2a1
return			; yes

;******************************************************************
; Convert 32-bit binary number at <bin> into a bcd number
; at <bcd>. Uses Mike Keitz's procedure for handling bcd
; adjust; Modified Microchip AN526 for 32-bits.

b2bcd	movlw	32		; 32-bits
movwf	ii		; make cycle counter
clrf	bcd		; clear result area
clrf	bcd+1
clrf	bcd+2
clrf	bcd+3
clrf	bcd+4

b2bcd2	movlw	bcd		; make pointer
movwf	fsr
movlw	5
movwf	cnt

; Mike's routine:

b2bcd3	movlw	0x33
btfsc	indf,3		; test if low result > 7
andlw	0xf0		; low result >7 so take the 3 out
btfsc	indf,7		; test if high result > 7
andlw	0x0f		; high result > 7 so ok
subwf	indf,f		; any results <= 7, subtract back
incf	fsr,f		; point to next
decfsz	cnt
goto	b2bcd3

rlf	bin+3,f		; get another bit
rlf	bin+2,f
rlf	bin+1,f
rlf	bin+0,f
rlf	bcd+4,f		; put it into bcd
rlf	bcd+3,f
rlf	bcd+2,f
rlf	bcd+1,f
rlf	bcd+0,f
decfsz	ii,f		; all done?
goto	b2bcd2		; no, loop
return			; yes

end

Questions:

• TakeThisOuTbtjlynchSTOPspam at SPAMgmail.com asks: " How do you convert an 8 bit unsigned binary number into a BCD number ?
Brent" James Newton replies: See 8 bit to BCD (half packed) and then just seperate out the nibbles by making a copy, then anding one with 0F and the other with F0 followed by a nibble swap.+

+

• The code does not appear to work, neither does the code from AN526. I am using a PIC18F452 and have converted the following PIC16 commands: rlf -> rlcf ( rotate left through carry ) FSR -> FSR0L ( setting FSR0H to 0x00, because variables reside in lower memory ) INDF -> INDF0 My only assumption is that i must be doing something really stupid. I tried the code for a bin value of 0xffff and got 06 85 53 ? Can anyone shed some light on this ?+

Interested:

• 32-bit binary to BCD (b2bcd) is a very neat algorithm - one thing to note is that the input 32-bit binary is assumed to be big-endian, i.e. most significant byte comes first.+
• TakeThisOuTwalter.pielSTOPspam at SPAMsasol.com
Note that this code as written only works with default radix of 10, otherwise the constants (123456789 and 32), get interpreted as hex numbers. Put a period in front (.123456789 and .32) to make it more idiot proof
+

Code:

• BIN2DEC
MOVLW	0XFF
MOVWF	BIN1
MOVLW	0X04
MOVWF	BIN2
MOVLW	0XFF
MOVWF	BIN3
MOVLW	0X04
MOVWF	BIN4
MOVLW	.32		; 32-BITS
MOVWF	II		; MAKE CYCLE COUNTER
CLRF	DIGIT1
CLRF	DIGIT2
CLRF	DIGIT3
CLRF	DIGIT4
CLRF	DIGIT5

B2BCD2	LFSR	FSR0,DIGIT1		; MAKE POINTER
MOVLW	.5
MOVWF	CNT

; MIKE'S ROUTINE:

B2BCD3	MOVLW	0X33
BTFSC	INDF0,3		; TEST IF LOW RESULT > 7
ANDLW	0XF0		; LOW RESULT >7 SO TAKE THE 3 OUT
BTFSC	INDF0,7		; TEST IF HIGH RESULT > 7
ANDLW	0X0F		; HIGH RESULT > 7 SO OK
SUBWF	INDF0,F		; ANY RESULTS <= 7, SUBTRACT BACK
INCF	FSR0L,F		; POINT TO NEXT
DECFSZ	CNT
GOTO	B2BCD3

RLCF	BIN4,F		; GET ANOTHER BIT
RLCF	BIN3,F
RLCF	BIN2,F
RLCF	BIN1,F
RLCF	DIGIT5,F		; PUT IT INTO BCD
RLCF	DIGIT4,F
RLCF	DIGIT3,F
RLCF	DIGIT2,F
RLCF	DIGIT1,F
DECFSZ	II,F		; ALL DONE?
GOTO	B2BCD2		; NO, LOOP
RETLW	.0		; yes

+

 file: /Techref/microchip/math/radix/b2bp-32b10d.htm, 6KB, , updated: 2013/2/9 12:06, local time: 2023/2/5 21:18, TOP NEW HELP FIND:  3.235.186.94:LOG IN

 ©2023 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! PIC Microcontoller Radix Math Method Binary to BCD packed and ASCII, 32 bit to 10 digits

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.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant"

 PICList 2023 contributors: o List host: MIT, Site host massmind.org, Top posters @none found - Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr.

.