Decoding variable into 4 digits of BCD, are there simple ways to this?

# Thread: Decoding variable into 4 digits of BCD, are there simple ways to this?

1. ## Decoding variable into 4 digits of BCD, are there simple ways to this?

Hello. I have giant 7 segment 4 digit display, which is driven by 4 CD4543BE (Inputs tied together, latches driven separately).

Say I connected it to MCU, with 1 digit display I can do something like this

Code:
```IF A=1 THEN HIGH PINA: LOW PINB: LOW PINC: LOW PIND
IF A=2 THEN HIGH PINB: LOW PINA: LOW PINC: LOW PIND```
And so on. There will be 10 lines of code in case of 1 digit display. But with 2-3-4 digits same approach will generate a huge code. One way I see is to use DIG statement and loop it 4 times to get all digits decoded. Maybe there are other, more elegant ways to do this?

2. ## Re: Decoding variable into 4 digits of BCD, are there simple ways to this?

It all depends on how the hardware is configured. Is the design optimized for minimal code? With out a schematic it's hard to say...

3. ## Re: Decoding variable into 4 digits of BCD, are there simple ways to this?

Well, there is no hardware yet I plan to use PIC16F887. The modules itself have A-B-C-D inputs of CD4543BE connected in paralel, and latch pins are also available on connector.

4. ## Re: Decoding variable into 4 digits of BCD, are there simple ways to this?

If you mean hooking up A to PORTC.0, B to PORTC.1, C to PORTC.2 and D to PORTC.3, to be able to directly address them, then yes, I can do that.

5. ## Re: Decoding variable into 4 digits of BCD, are there simple ways to this?

Schematic...... Not some wild a** guess... Definitive schematic Please...

6. ## Re: Decoding variable into 4 digits of BCD, are there simple ways to this?

Schematic of what?

7. ## Re: Decoding variable into 4 digits of BCD, are there simple ways to this?

Here is simple counter code I've made. It just counts from 0 to 99. But it is far from perfection.

Code:
```marcato:
tato=0
FOR KAKO=0 TO 99
XAXO=KAKO DIG 0
high latch 'enable write ones
low latch2 'disable write tens
gosub decoder 'display ones
if xaxo=0 and kako dig 1<>0 then 'if count is above 10
low latch 'disable ones
high latch2 'enable tens
'tato=tato+1
xaxo=kako dig 1
gosub decoder
xaxo=0
endif

if xaxo=0 and kako dig 1=0 then     'reset leading 0
low latch
high latch2
xaxo=0
gosub decoder
endif

PAUSE 300
NEXT
goto marcato
END

decoder:
IF XAXO=0 THEN GOSUB ZERO
IF XAXO=1 THEN GOSUB ONE
IF XAXO=2 THEN GOSUB TWO
IF XAXO=3 THEN GOSUB THREE
IF XAXO=4 THEN GOSUB FOUR
IF XAXO=5 THEN GOSUB FIVE
IF XAXO=6 THEN GOSUB SIX
IF XAXO=7 THEN GOSUB SEVEN
IF XAXO=8 THEN GOSUB EIGHT
IF XAXO=9 THEN GOSUB NINE
return

ZERO:
LOW A: LOW B: LOW C: LOW D
RETURN
ONE:
HIGH A: LOW B: LOW C: LOW D
RETURN
TWO:
LOW A: HIGH B: LOW C: LOW D
RETURN
THREE:
HIGH A: HIGH B: LOW C: LOW D
RETURN
FOUR:
HIGH C: LOW A: LOW B: LOW D
RETURN
FIVE:
HIGH C: HIGH A: LOW B: LOW D
RETURN
SIX:
HIGH C: HIGH B: LOW A: LOW D
RETURN
SEVEN:
HIGH A: HIGH B: HIGH C: LOW D
RETURN
EIGHT:
HIGH D: LOW A: LOW B: LOW C
RETURN
NINE:
HIGH D: HIGH A: LOW B: LOW C
RETURN```

8. ## Re: Decoding variable into 4 digits of BCD, are there simple ways to this?

Choose a port (A,B, or C). Connect the low 4 pins of the port to carry the number you want to display and connect those to the data pins of the CD4543BE. Then connect the 4 pins of the high byte to the latches. to write "2" to the first digit, send "12" to the port. to write 9 to the second digit send "29" to the port. Write 6 to the 3rd port with "46" and so on.

9. ## Re: Decoding variable into 4 digits of BCD, are there simple ways to this?

Excellent, thank you! will give it a try

So as I understand, this should display "2" in 1st digit from left?

PORTC=%10000010

or

PORTC=52

?

10. ## Re: Decoding variable into 4 digits of BCD, are there simple ways to this?

The binary code you displayed is correct, however that is 82, not 52,
52 would put "2" on the 4th digit AND the first digit as well. (the numbers are hex, by the way - not decimal)
Last edited by Charlie; - 7th March 2020 at 11:52.

11. ## Re: Decoding variable into 4 digits of BCD, are there simple ways to this?

Yes I see, is there a number to directly use decimal variable, or I need to convert it to hex?

And by the way, since I will have 16 total of such modules, to save number of MCU pins used, I come up with idea of using CD4514BE - 4 to 16 decoder, this should allow me to by using only 5 pins from MCU, drive up to 16 modules.
Last edited by CuriousOne; - 8th March 2020 at 04:19.

12. ## Re: Decoding variable into 4 digits of BCD, are there simple ways to this?

from 0-9 hex and decimal are exactly the same so no "conversion" is required. The high nibble is really decoding the device since you need a pin for each device so you want binary 0001, 0010, 0100, 1000. This is hex 1,2,4,8. It is simplest to treat the byte as 2 nibbles, the low one with the number to display, the other with the address.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts