Searching \ for 'Binary to BCD was: separating digits (decimal disp' in subject line. ()
Help us get a faster server
FAQ page: www.piclist.com/techref/index.htm?key=separating+digits
Search entire site for: 'Binary to BCD was: separating digits (decimal disp'.

Truncated match.
PICList Thread
'Binary to BCD was: separating digits (decimal disp'
1999\03\24@185418 by

Stewart Pye wrote:
>
> Thanks for the help, but it's not exactly what I was looking for. I need to
> display it as a decimal number. I've done it on an 8051 but the pic doesn't
> have divide. There's got to be some way to do it but it eludes me.

Oh, I didn't read your original post (closely). Here's something I
posted a while back that should do the trick. But I'm sure others will
soon tell you that there are other techniques too. In fact, Reggie just
posted something less than a week ago that does the same thing for 16
bit numbers. Mike Keitz has a cute little looping trick that is
optimized version of what you'll find in Microchip's appnotes. If you
subscribe to the Pic list long enough, you'll see these routines posted
every three months or so. Maybe we should put all of the 'answers' to
all pic questions on a web and then the piclist can be become 100% [OT]
instead of 90% :).

===============================================================
Ancient post:

Here's the 28-cycle (non-looping) version that I promised yesterday.
It's based on binary comparisons. It's one of those routines that
is very difficult to comment. So I didn't. However it takes advantage
of this little trick to quickly ascertain the ones' digit:

If you look at the ones' digit for 2^N you see this pattern:
n = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11 ...
2^n % 10 = 1, 2, 4, 8, 6, 2, 4, 8, 6, 2, 4, 8 ...

If it wasn't for the annoying 6's, you could simply sum the nibbles to
get and get the ones' digit (after a relatively simple binary to BCD
conversion). However, it's simple enough to test if bit 4, 8,...
are high and to subtract 1 and then add 6 (or simply add 5) if they
are.

The second observation is that the sum of all of the tens' digits is
less than 16, and thus can fit into one nibble. This simplifies having
to deal with overflow until after all of the digits have been added
together.

The third observation is that the BCD result is greater than 200 only
if the most significant bit is set.

;********************************
;binary_to_bcd - 8-bits
;
;Input
;  bin  - 8-bit binary number
;Outputs
; hundreds - the hundreds digit of the BCD conversion
; tens_and_ones - the tens and ones digits of the BCD conversion

binary_to_bcd:

CLRF    hundreds
SWAPF   bin,W
ADDWF   bin,W
ANDLW   00001111b
SKPNDC
ADDLW  0x16
SKPNDC
ADDLW  0x06
ADDLW   0x06
SKPDC
ADDLW  -0x06

BTFSC   bin,4
ADDLW   0x16 - 1 + 0x6
SKPDC
ADDLW  -0x06

BTFSC   bin,5
ADDLW  0x30

BTFSC   bin,6
ADDLW  0x60

BTFSC   bin,7
ADDLW  0x20

ADDLW   0x60
RLF     hundreds,F
BTFSS   hundreds,0
ADDLW  -0x60

MOVWF   tens_and_ones
BTFSC   bin,7
INCF   hundreds,F

Scott

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