BCD to 7-segment issues

# Thread: BCD to 7-segment issues

1. ## BCD to 7-segment issues

I am driving a 7-segment display with an HC4511 BCD to 7-segment decoder.

The input of the 4511 is connected to PORTB 0-3.

I have a byte value that contains the number I want to display.

Would something like this work?

Code:
```portB.0 = MyByte.Bit0
portB.1 = MyByte.Bit1
PortB.2 = MyByte.Bit2
PortB.3 = MyByte.Bit3```
In other words, would the lower 4 bits of PORTB follow what my byte is doing? When I update my byte, would PORTB then change to reflect the new value? Or am I on the wrong track here?

If I am on the wrong track, how do I make the lower 4 bits of PORTB mirror the lower 4 bits of my byte, without affecting the upper 4 bits of PORTB? The upper 4 are being used for other things.

Thanks,

Andy

2. Code:
`PORTB = (PORTB & \$F0) | (MyByte & \$0F)`

3. ## Re: BCD to 7-segment issues

Originally Posted by Darrel Taylor
PORTB = (PORTB & \$F0) | (MyByte & \$0F)
Wow! So simple. I played with it on paper and that works great. I did have the idea of an OR in my head, but couldn't figure out how a bit that was set to a '1' could change back to a '0'.

Actually, now that I think of it, since MyByte is only going to be zero to nine, I think I could get away with this:

Code:
`PORTB = (PORTB & \$F0) | MyByte`
Thanks!
Last edited by andywpg; - 9th November 2012 at 20:56.

4. ## Re: BCD to 7-segment issues

It would be great, if someone with better PBP knowledge can explain, what that code actually does (how)

5. ## Re: BCD to 7-segment issues

Let us suppose bits 5 and 6 of portb are set and 4 is displayed on the 7 seg display and we want to display 5
portb will be %01100100
(portb & \$F0) will clear the lower 4 bits of portb leaving bits 5 and 6 unchanged.

01100100 &
11110000
========
01100000 result A

(mybyte & \$0F) will clear the top 4 bits of mybyte
11000101 &
00001111
========
00000101 result B

we now OR ( | ) result A with result B

01100000 OR
00000101
========
01100101

Bits 5and 6 of portb are unchanged and the lower 4 bits are set to 5
If mybyte is never going to be greater than 9, (mybyte & \$0F) can be replaced by mybyte

See bitwise operators in the manual

6. ## Re: BCD to 7-segment issues

Thanks! So it is logical operation with pre-defined masks.

#### Posting Permissions

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