I am designing a PIC18F4520 microcontroller based system. By using this system I want to multiply two numbers. Each number is a 2-digit BCD number. Such as 73*27 and I want to see results as 2715 at a 4 digit seven segment display. I have a problem creating algorithms while multiplying those numbers. How can we multiply those numbers?
Electrical – How to we multiply two 2-digit BCD numbers
7segmentdisplayassemblycodepic
Related Solutions
Usually 'A "counter"advances one count when it's clock input makes an active transition'.
Translated into people-speak that means that there is an input pin usually named "CLOCK" or "CLK". When this changes from low to high (or sometimes from high to low) the counter steps one count.
This COULD be done as easily as connecting the Clock pin to ground with a resistor and pulling it high with a pushbutton etc to cause a count to occur. BUT you will rapidly learn about debounce. A switch makes many open/close actions as it closes or open. One easy way to address this is to us capacitor to slow things down and eliminate spurious changes. There are more formal and properly defined ways of doing this.
Counte ICs typically have pins with some or all of these functions
Clock / Clk - as above
Reset - When "asserted" the counter resets to zero.
Asserted may mean set to high or low level depending on the device.Enable. When active the counter can be controlled by the clock.
When inactive the counter does not count.
May be high or low depending on counter IC.
Some have several.Set/Preset/ Load - SOME counters may have a value loaded from external pins. Rare.
Carry out - sends a signal to a following stage to allow a multi IC counter to be made.
- Carry in - accepts carry out from a previous stage.
More ...
Below is the logic diagram of the 74293 counter that you mentioned.
This is a very old IC using a technology (called "TTL") that is essentially obsolete for casual amateur use. They still work as well as ever and are find to learn with, but you will probably want to use a more modern equivalent for anything serious in due course.
The counter consists of 4 x "JK flipflops". They are wired so that a falling edge on cle CK (- CLK = Clock) line causes a flip flop to "toggle". That is if Q is high it goes low and if Q is low it goes high. The IC has two parts - a divide by 2 part controlled by input A and a divide by 8 part controlled by input B. To make a divide by 16 ( = 2 x 8) you connect input B to QA and use input A only. This then become a 4 stage counter that will divide by 2 x 2 x 2 x 2 = 2^4 = 16.
Looking at the diagram:
CK A and B on pins 10 & 11 are the clock lines mentioned above.
Ro(1) and R0(2) combine to provide a reset function.
When R0(1) and R0(2) are both high at the same time the counter rests so all x outputs are low.
When either or both the R0 lines are low the counter is free to count when clocked.
The following is what happens when you connecy input B to Qa and clock Input A. (Clocking input A consiss of lowering its level from high to low.
A full clock cycle consists if high/low change, pause, low/high change, pause.
QA QB QC QD are the outputs.
QA is the LSb or least significant bit of the output.
It "toggles" on each high to low clock transition.
It's state on each clock goes: 010101010101...Qb is the next most significant output bit. It toggles every second clock.
Its state goes 0011001100110011...Qc - you should get the picture by now
Toggles every 4 clocks
Sate goes 0000111100001111...Qd - most significant bit = MSb. Changes every 8 clocks.
State goes 00000000111111110000000011111111...
Diagram repeated
SO now you have a counter which counts each time input B is lowered.
BUT how do you convert the 4 outputs to a 7 segment display drive?
There are a number of ways. The simplest uses diodes and a few transistors!
You could use a binary to 7 segment decoder IC.
I'm not recommending you use it but an SN7447 BDC to 7 segment decoder will do the job up to a count of 9.
What I would recommend is that you look at the [74C925 / 926 / 927 / 928 family(http://media.digikey.com/pdf/Data%20Sheets/Fairchild%20PDFs/MM74C925-28.pdf) OR any of the many other ICs that do a similar job. This will drive up to 4 digits and get you to a practical solution much more quickly but still teach you quite a lot along the way.
When you are ready you can go BACK to the 74293 to learn the lower level lessons that it can teach you.
What you need to do is convert the binary into something called BCD - Binary Coded Decimal.
Basically BCD is binary, but only takes the values 0-9 for each digit.
If you can still get hold of them (no idea if you can), there was a 7400 series IC that does the conversion. As I recall it was 74185.
Alternatively, if they are not available, you could use a parallel EEPROM IC or something like that. If you store the BCD equivalent values in addresses 0-15 of the EEPROM, then the lower 4 address bits become your 4bit binary input, then 5 of the bits of the data outputs can be your BCD value (the 5th bit being carry - i.e. 10).
Thinking about it, you could even use an 8-bit EEPROM to do the whole thing (including 7-segment). If you use the lower 7 bits for the first 7-segment, and the other data bit can go to segments B&C on the second 7-segment display. Again by storing the equivalent values in addresses 0-15 you can make a lookup table to generate the mapping.
Related Topic
- Electronic – How to “unground” a low state of a not gate
- Electrical – How to convert a number to its 4-bit binary equivalent using logic gates
- Electrical – Detecting Switch input in ARM 7 and multiplexing two seven segment displays
- Electrical – How to convert 9 bit binary to BCD for 3 Seven Segment Displays
- Electrical – Building a BCD to 7-segment using 3×8 decoder
- Electrical – Displaying different numbers in 4-Digit 7-Segment Display using VHDL
Best Answer
The obvious answer is most certainly the most viable here
This is a pretty dated microcontroller, but it can multiply (binary) numbers.
There's really no reason to do anything but the obvious, trivial.
I'll be honest: that schools and universities still teach BCD is an anachronism. There's no practical reason. Conversion to BCD is such a marginal problem that there'd be so many better topics to teach in a basics of digital logic class... it always makes me a bit sad.
As @jonk correctly points out, you can do "hand multiplication", of course, with your digits, should you feel the desire to do this in decimal!
But it would teach you something that is not clever: doing decimal computation on a binary computer.
An incredibly easy way of converting a two-digit BCD to binary is putting the two 4-bit digits into one 8-bit Byte (
higher_bcd_digit<<4 | lower_bcd_digit
) and using that as index into a 256-entry-table containing the binary representation of the number. That is how I would implement the BCD-to-binary conversion.I think (not sure) on PIC18 that would result in a single-cycle 2-digit-BCD-to-binary conversion: Impossible to beat.
Sure, 156 of your 256 possible entries would be entries you don't care about. But you've got 32 kB of program memory, so that's not really a bottleneck.
It gets worse: because there's so much free space in that table, you can fill the larger "don't care" table "gaps" with your program code to save space. Just needs a bit of manual positioning of code, something you do anyway, according to your "assembly" tag.
So, there you go, maybe four cycles (i.e. about 0.4 µs) to convert your two BCD numbers to binary. A multiplication, and a conversion back to BCD.
That conversion would take longer (can't do the trick you can do for an 8 bit table on the harvard architecture of the PIC18, so can't simply store all possible products in a table in program memory), but again, the conversion is so simple on CPUs like this one, since they support multiplication and subtraction out of the box.