'Binary to BCD was: separating digits (decimal disp'
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% :).
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
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
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
; bin - 8-bit binary number
; hundreds - the hundreds digit of the BCD conversion
; tens_and_ones - the tens and ones digits of the BCD conversion
ADDLW 0x16 - 1 + 0x6
More... (looser matching)
- Last day of these posts
- In 1999
, 2000 only
- New search...