Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Stuck on 4Bit Binary to BCD, I think.

Status
Not open for further replies.

Mike0rr

New Member
Preface; I am working on building a calculator that can add and subtract 0-99 (0-127 but only 2, 7Seg displays) and I am planning to do it all in basic logic gate IC's. No Full Adder chips, no Register IC's, just logic gates. I have been self teaching myself throughout most of this project with YouTube and Google and prototyping it on MineCraft as it is a good medium for me to learn through and uses real life logic gates. (This will end up on a circuit board in real life, just clarifying.) The design I have may not be the most efficient, but it "should" still work and I am learning/improving through experimentation. I am 100% open to improvement suggestions in the design, I just mean to explain why I have done some of the possibly odd and inefficient circuits that have given me the parameters to work within.
-
I have made a smaller, single digit input Adder before but now that I am expanding the size I have run into an issue. I need to represent 0-99 on my inputs. From this issue, I have looked into Registers, Hexadecimal and a few other subjects but I think I have found the terminology I was looking for finally, "BCD". I have 10 inputs (0-9) and I convert this into 4Bits of Binary and need to have those saved to a register and stacked to ones and tens places. I understand the concept behind "Shift, add 3" and what not but I don't know what to call the circuit I'm looking to build.

I'm taking a Decimal input, converting it to Binary saving it as a 4Bit number, taking a second Decimal input and saving it as another 4Bit number and I'm looking for the Circuit to combine both of these 4Bit numbers (1's and 10's place) into a 2 digit Binary number with a maximum of 7Bits (0-99 in Binary).

So I basically have two questions to ask here:
Smaller question:
-Am I even going down the correct path here, or is this a silly way to go about putting my Decimal inputs into an Adding/Subtracting machine?
Main question:
-What is the name of the circuit I am looking for here. I commonly look up IC's logic gate schematic so if there is a common IC for this, a simple ID or link would be great. I have two 4bit Binary inputs and need one Binary output that could be 7bits long. A different way to word this is; I need two single digit inputs to be combined into one Binary number. I need 4 and 5 to represent 45 and not 4 and 5.

I apologize for any rough wording and long descriptions. My bottle neck seems to be my lack of terminology and if I knew how to describe my situation shorter I could likely find the answer on my own through Google. I hope this doesn't come off as a dumb question, I'm just stuck and don't know a better way to handle this issue. I appreciate any help you guys have on the subject and if this has already been covered in another post I apologize again. I checked around a bit but couldn't really find this issue, at least not with my limited terminology and I'm very new here.

Thanks for your time everyone : )
 
Hi Mike, Welcome to ETO.

Here is some code from another member here that may help you.
Code:
Binary to BCD half-packed 8 bit to 3 digit
From Mike McLaren, K8LH, (Westland, MI, USA)

While collaborating on a little 10F200 project recently (256 words of program memory and 16 bytes RAM) I came up with the following little Bin2Bcd routine. The additional code (PrintIt, PutDigit, etc.) simply prints the "0".."255" number as right justified with leading zero suppression;

;******************************************************************
;                                                                 *
;  8 bit to 3 digit half-packed BCD, Mike McLaren, K8LH (Jan-09)  *
;                                 *
;   input: WREG, 0x00..0xFF, 0..255                 *
;  output: tens, 0x00..0x25, packed bcd                 *
;          ones, 0x00..0x09                     *
;                                 *
;  12 words, 2 variables
;
tens    equ     0x1A            ; packed BCD 'hundreds' & 'tens'
ones    equ     0x1B            ; single BCD 'ones'
mask    equ     0x1C            ;

Bin2Bcd
        clrf    tens            ;
        decf    tens,F          ; preset 'tens' to -1
div10   movwf   ones            ;
        incf    tens,F          ; bump 'tens', 0x00..0x25
        movlw   6               ; using "packed bcd" format
        addwf   tens,W          ; bcd "digit carry"?
        skpndc                  ; no, skip, else
        movwf   tens            ; fix 'tens'
        movlw   10              ; ones = ones - 10
        subwf   ones,W          ; borrow?
        bc      div10           ; no, branch, else
PrintIt
        movlw   " "             ; prep leading zero suppression
        movwf   mask            ; mask = 0x20 = " " (space char)
        swapf   tens,W          ; get hundreds, 0..2
        call    PutDigit        ; print " " or "1".."2"
        movf    tens,W          ; get tens, 0..9
        call    PutDigit        ; print " " or "0".."9"
        movf    ones,W          ; get ones, 0..9
        goto    PutNumber       ; always print "0".."9"
PutDigit
        andlw   0x0F            ;
        skpz                    ;
PutNumber
        bsf     mask,4          ; mask = 0x30 = "0"
        iorwf   mask,W          ; wreg = " " or "0".."9"
        goto    Put232          ; Put232 or PutLCD

I realize it is Assembly for an MCU, but thought the approach may help you with discrete devices.

John
 
This article discusses BCD and how to do BCD addition and subtraction which may be helpful to you.
 
I'm not having an issue adding and subtracting though. I'm having trouble converting 2 Digital inputs into one Binary output. On some level I guess you are adding them together but as far as the heart of addition on the calculator goes, that is fine. If I were to simply have the inputs be set in Binary, I would have this project complete but I'm missing the link between my Decimal Number Pad and my Registers.
 
Hi Mike,
You are certainly doing this the hard way in the days of micro controllers. As you do not say what your input device is I will assume it is two 10 position rotary switches. I think using the idea of a look up table in a ROM is the easiest way. As you plan on using the most basic devices I think you will rule out using a UV erasable ROM chip. I would suggest doing this with an array of diodes On the output side you would have 8 lines 2^0 (1s), 2^1 (2s), 2^2 (4s) up to 2^7 (128s) So dealing with the output from the rotary switches. First the units switch. From the "0" position there would be no diodes connected to any of the binary output lines. For the "1" position you would have a single diode connected to the 2^0 output line (Diode output to the binary output assuming positive logic.). For the "2" position You would again have one diode from the switch to the 2^1 binary output. For the "3" position of the switch you would have two diodes. One connected to 2^0 and one connected to 2^1 this would continue up to the "9" position when the diode would go to the 2^0 and the other other to the 2^3 . (The "7" position would have three diodes.) The 10s switch would be wired in a similar way but to a separate 8 binary lines. (As the value on these lines needs to be added to the value on the 8 lines from the units switch) On the 10s switch again position "0" would have no diodes. Position "1" would have 2 diodes. One to the 2^1 and one to the 2^3 output (Adds up to 10 decimal) The "2" position (20 decimal ) would have 2 diodes. One to the 2^4 output and one to the 2^2 output. (16 + 4 = 20) and so on.
I used this method many yeas ago to convert 5 bit teleprinter code to binary values. This was for a system to make the antenna system track one of the "Oscar" amateur radio satellites using information punched onto paper tape. (Before the days of micro controllers) I think there as about 100 ICs in the system.) I hope this makes sense.

Les.
 
It's been hard putting this all into words, but it has somewhat helped me solve a bit of this a narrow down the question actually.
I have a number pad 0-9 with a +, - and reset button. Much like you would see on a standard calculator.
I believe I am trying to go from "BCD to Binary". Two "4 digit Binary inputs" and would like them converted into a 1's and 10's place 2 digit number. I want 4 pressed then 5 to = 45 not 4 and 5. 0100 and 0101 (a BCD number) after going through a process would represent 101101 (a Binary number).
I am not using Micro Controllers as this is more of a learning experiment. It may seem a bit silly but I have learned so much from it. Binary, Hex, BCD, what a register is, what a decoder is, history of computers and calculators, a much deeper understanding of how logic gates are used, and so much more. If I wanted to short cut the process I would just go buy a full calculator IC as all of the circuits are combined onto one chip but I would learn far less.

I appreciate the help so far guys. I know I didn't make this easy or word it so nicely. If it wasn't for helpful people like you guys, far less people would ever make it into the world of electronics.
 
To ask Wade's question again. (In a slightly different way.) Does you main add/subtract unit work on binary numbers (You would be dealing with 7 bits.) or is it adding two digits of BCD numbers. (8 bits. Each BCD digit requiring 4 bits.) This is to find out if enter data as BCD then convert it to binary. You then calculate in binary and finally convert the binary back to BCD to display the result.

Les.
 
They are Binary adders. The only parts of my calculator that is not in Binary is the Inputs, the junction from the input to the adders (or the registers linked to the adders, really) and the junction to the 7 segment displays. All of the core operations are done in Binary.
I will get a picture of what I have designed out later today when I get the chance. That might clear up a bit of the confusion.
 
I want 4 pressed then 5 to = 45 not 4 and 5. 0100 and 0101 (a BCD number) after going through a process would represent 101101 (a Binary number).
Then there is no conversion needed... If the first keypad is connected to a low nibble and the second to the high nibble..

keypad 1 = 4 or..... 0100
keypad 2 = 5 or..... 0101

Result = 01010100.... 45 BCD or binary 84..
 
I've drawn out a diagram that shows the issue. Now you can better see the issue and what inputs are coming in and need to come out.

In the picture, the example input is "45". 4 is pressed first, saving it to the Registers first section. 5 is then pressed shifting the 4 over and 5 is now saved to the first byte and 4 in the second. This is where they are considered "BCD 4 and 5" They are then sent to the "?" IC to be interpreted as 45. The question is; What is the "?" IC? Since the input to said chip is BCD and the output is Binary I would expect it to be a "BCD to Binary" IC but I can't seem to find such a thing. I have 8 BCD data lines in, 4 for each digit 0-9 and need 7 output lines for my Binary number 0-127(Only using 0-99 in this project though).

After this the data is sent to the first leg of my Full Adders (In Binary). The "+" button is then hit and the first process is repeated on a second Register, formatted and sent to the Full Adders other input leg and added together with the first input giving me a Binary answer to export out to a decoder after the = button is pressed and sent to my two 7-Segment Displays.

Here is the link to the Diagram I drew up. I color coded the path the 4 and 5 button presses take to make it easier to follow as well.
https://imgur.com/VtuZ6BF
 
Then there is no conversion needed... If the first keypad is connected to a low nibble and the second to the high nibble..

keypad 1 = 4 or..... 0100
keypad 2 = 5 or..... 0101

Result = 01010100.... 45 BCD or binary 84..
But he wants to do the arithmitic in binary, not BCD.
 
Your decimal to binary input block needs to deal with decimal position differently. (As I described when I guessed that you might be using rotary switches for input.) For the first key press (10s) You either have to add the binary value of the key ten times or encode the keys to output ten times their binary value. If you want to allow for the input being just one key press (Values less than 10) then you would need logic to shift the first key press four bits to the left if a second digit is entered. (You may also want to add logic to deal with a false entry of more than two decimal digits.) Another way would be to assemble the two BCD nibbles and use them to address a lookup table in a ROM You would need a ROM with 256 locations and 8 bits wide. As I suspect you would not want to use a real ROM you would have to use two 4 to 10 (or 16 ) line decoder chips and gates to create 100 lines which would then be encoded to straight binary values with a diode matrix (Or an array of gates.) You seem to contradict yourself when you you say the input is the only part that is not straight binary. You say elsewhere that that the output display is in decimal format.

Les.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top