Advertisement

Binary to BCD Conversion

Started by November 26, 2002 06:38 PM
4 comments, last by KrunkSplein 22 years, 2 months ago
Okay. I''m doing a project for one of my labs. Its a rather cumbersome digital design project and there is one last bit that I just can''t get. I have an 8-bit binary number that I need to somehow convert to BCD. I understand how to do it on paper - simply divide by decimal 10. The remainders are your least significant four, and the rest is your most significant four. The trouble lies in turning that into a logical circuit. Now, I know this is gamedev.net and not a TTL site, but I figure that there are those here who could answer this easily enough. To summarize: How do you convert an 8-bit binary number into BCD using logical gates, multiplexers (if necessary) and shift registers? Any help would be immensely appreciated. --------- Krunk
---------Krunk
The easiest way to go about this is to convert each digit to a single byte, and then go back and pack everything once we have the basic logic coded.

Like you said, we have to continually divide the number by 10 to extract each digit until we get zero. First we need to determine how many bytes we'll need. This is basically the number of digits, which we can find by taking log10 of our number and ceiling the value (in our case, simply add one to the result and truncate to an integer afterwards).

We should allocate enough memory to store those digits. Next comes the "parsing" of the number. To get the very first digit, we do a modulus operation with 10, and we can store it. To shift the decimal number to the right now, we simply divide by 10. The piece of code should look like something along the lines of this:

      int number = 125134;char *buffer = (char*)malloc((size_t)(log10(number) + 1));char *p = buffer;while(number){   *(p++) = number % 10;   number /= 10;}      


Now, to pack digit pairs into the same byte, the code has to change a little. But I'll leave that up to you

EDIT: Using my code you lose the original number, so you might want to save it somewhere first

EDIT2: Ack, sorry, I didn't actually answer your question :| Hopefully the code can help you turn it into logical circuitry though. I'll try to read up

[edited by - Zipster on November 26, 2002 8:29:10 PM]
Advertisement
If you can''t produce the BCD code directly, I think the simplest way would be to use a decimal counter together with a binary counter. Start both from zero and count them up to the wanted value.
KrunkSplein,

It would seem to me that turning that algorithm into a logical circuit is part of the challenge of the project that your teacher/professor is wanting you to solve. Its part of improving your problem-solving skills that you''ll use as a professional someday

You do seem to understand the logic for the conversion at one level. Have you written a small computer program in C/C++, Java, or BASIC to do the conversion? That code would be based on a set of explicit, logical steps that should map to circuit elements. You might have to represent each computer program logical statement as a collection of more than one circuit element, but there should be a mapping between the two.

The key thing here is that you understand which circuit elements have the effect of doing things like and, or, xor, nand combinations of the inputs. I would hope that sort of thing was covered by your TTL or earlier electronics classes.

I did a quick web search. Don''t have time to filter through everything, but this site could be helpful, maybe:

http://www.cs.pitt.edu/~novacky/L2L/Workshop3/

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
I did some searching myself and found this site:
http://www.play-hookey.com/digital/

I''m actually interested in doing this problem on my own just because I love this kind of stuff I''d chose coding bit manipulation routines over anything, man! Logic rocks
True horror story:

My last internship before I graduated was in a foreign land. I thought I was going there for an ASIC job. In fact, it was digital design. Not only that, but the company (very small, ~25 people) did not want to foot the bill for a VHDL compiler. They did all their digital design with schematic capture. I had to program an FPGA to interface between a CPU, external RAM and an Ethernet chip using nothing but gates, muxes & decoders. Of course, it''d been 2 years since my last digital design course. I called my parents back in the states and said "Find this book--I don''t remember the author, there should be ''digital'' in the title, it''s gray, and I need it NOW!!"

At the end of the five months I got the beast working, though.

This topic is closed to new replies.

Advertisement