Category Archives: Bin to Hex 7 Segment Decoder

Binary to 7 Segment Hex Decoder

While working on the Retro Computer Project, I was looking to build a small interface that would let me connect to the address and data bus of the Micro to provide a method to manually program the micro as well as read/write  data into memory.

Back in the last century there was a logic IC that provided this function, but I couldn’t find it anywhere. There were plenty of the BCD to 7 segment decoders, but nothing that would give values above 9 such as A, B, C, D, E & F.

Time for a quick project and to quickly put something together! So I looked at what I had available and had a boot load of PIC 16F628 Micro controllers. So if you can’t find what you are looking for, build it!

As my goal was to get something to put together in a couple of hours that I could use quickly, I was not thinking about how efficent the code would be, so there may be better ways of doing this. Please feel free to take the code and schematic for this Binary to Hex 7 Segment decoder and use it for your own projects. All I ask is that you link or reference back to me and this project.

With this project, the first step was to define my requirements, my outputs and inputs. Obviously I needed to drive a 7 Segment LED display (common cathode). I needed an input to latch data into the decoder and wanted to have a test function as well as a blanking input.

The Hardware

I used the internal oscillator on the PIC as the clock, set to 4MHz. The main loop in the code checked to see if any of the control lines were active. If any of these control lines were active (low) the PIC microcontroller would perform the necessary action. The data for the decoder was latched into the buffer prior to decoding.

Bin2Hex_Schematic (PDF)

Hex27Seg (KiCAD Project)

You can ignore the switches to the left of the PIC in the schematic, as there were just used to simulate the control lines and the data input. The resistors to the right of the PIC were just current limiting resistors for each of the 7 segments in the LED display.

The Code

The code was fairly simple and can be broken down it to a few sections. The first being the port setup code. As the PIC has many multi-function pins, its very important to set these correctly at the start. One mistake that I made was to forget to turn off the analogue comparators on PORT A, ended up wasting an hour looking at this, until the penny dropped.

In the main loop of the program, I started by reading the data on PORT_A and then performed a test to see if any of the control lines had gone active low.

You will notice in the code, that the first test performed is for the Blanking Input – if this was not active then the code would check for the Test Display Input. As this was nested inside the if-else statement for the Blanking Input, the Blanking Input had priority over the Test Display. This was one simple way to implement such logic.

When the control lines were active, I wanted to ignore any update from the Latch line, so a simple flag was used to achieve this.

The code for then managing the  control lines was very simple. It could well have been included in the main. I have found by experience, it is always better to have separate function code block for every action. This makes the main code loop easier to read and when you’re troubleshooting, you’re not finding yourself going back over blocks of code again.

In this screen shot you can see the first few lines of the code for the section that converted the binary data into values for the 7 Segment display. In the first line of code I was performing a logical AND to clear out the upper bits of the data, as these bits were the control lines on PORT_A.

Next I compared the data with values from 0 to F inclusive. Based on the value I switched on the different segments of the LED display. I left the formatting of this in binary as I thought this might make it easier for other to read.

You will notice in the code segment above, there is a line that is commented out. This was my debug function, which was used to flash the LED decimal point x number of times, to indicate the value of the data input. This is a good point to bring up. When building any system large or small, it’s always handy to have some form of debugging build in. Just having a simple LED to flash, can save a engineers time, as there is nothing better than a visual indicator to let the person know that they have reached that point in the code.

The source code for the project which was developed in MPLABX is included below. Please feel free to copy and modify the code as you need for your project. I’m just going to program 6 of these for the Retro Computer Project and build the interface board next.

Project C Source Code (Zipped)