bradsprojects has been created and maintained by me - Brad!

 

Tutorial 5 - Seven Segment Displays

Seven segment displays are extremely useful devices. We can quite easily drive one, two, three or more seven segment displays with just one microcontroller - we will start with just one so you can see how the circuit and code works, then we will build on that to get to four seven segment displays running from one 16f648a microcontroller.

 
 

If you have never worked with seven segment displays, they really are quite simple. Internally there are seven LED's arranged in a figure eight pattern. depending on which segments (or LED's) within the display that we turn on, will depend on what number or character is on the screen. You can purchase these displays in two different varieties - either common cathode or common anode.

This tutorial we will be using the common anode seven segment displays, this simply means that all the LED's anodes are connected to a common point. Then in order to turn on each LED, we simply connect ground to the corresponding pin. Lets have a quick look inside a common anode display:

 
 

Here you can see that there are seven inividual LED's. Internally all the anodes are connected together and all cathodes are seperate. If we connect +5v to the common anode connection, we can now turn on any segment we like, just by connecting ground to one of the seven cathodes (making sure to use a resistor of course!)

The image to the right shows the pinout for these displays

 
 

Now it is time to build our first circuit

 
 

Notice how there are two common anode connections? you don't need to connect both of them, either one will do. This just makes it handy for if you were designing a complex circuit board and it might be hard to get a conenction to the top common anode, you could alternatively use the bottom common anode etc... so for our excercises, just use which ever one you want. Also note the dot in the picture. If you were designing something like a calculator, you would use the dot as the decimal place. We won't be using it here though. 

Now before we build the circuit, have a think about this,

If you wanted to display the number five on the screen what would you do? what would you connect and where?

Well the answer is, you would connect +5v to the common anode, then ground (through resistors) to a, f, g, c and d just like this:

 
 

Once built, your circuit should look something similar to this

 
 

Sourcecode

Now it is time to download the first piece of sourcecode so we can make the display do something. You can download that here

 
 

What you can expect to learn in this tutorial

 
 
how to connect one seven segment display to the microcontroller
how to connect two seven segment displays to the microcontroller using transistors
A quick introduction to the retlw instruction
 

What you will need

 
 
pic16f628a or pic16f648a microcontroller
programmer
electronics breadboard
2x common anode seven segment displays
2x NPN small signal transistors (i use pn2222a but there are hundreds that will do the same job)
9x resistors (any resistor between 100 ohms to 1kohm will do)
hookup wire
 

Once built, your circuit should look something similar to this

 
 

Now if you open up the source code you will notice that this program will simply count from '0' to '9' and then repeat again in an infinite loop. Also notice that in order to display each number, we need to send a certain combination of 8-bits to PORTB.|

So having said all that - do you know why a logic zero will turn on an LED? Well the answer is simple. Remember that the seven segment displays are common anode - all the anodes are connected to +5v already, so in order to turn an LED on, we need to connect the cathode to ground (through a resistor) what logic is equal to ground? LOGIC ZERO OF COURSE!

Some things to experiment with in this sourcecode

Try and count through all the hexadecimal digits I.E 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F.

What combination of 1's and 0's do you need to make the extra digits?

 
 

Alright, now that we know how to make the different digits, now we can move onto adding more displays.

You can download the fullsize version of the schematic to the right by clicking here


You will notice that we have added an extra display, two resistors (also 150 ohm) and lastly, two NPN transistors.

We use the transistors to 'activate' each display at a certain time. I have added some colours to make it a bit easier for you to see where each connection needs to go. You can see that both displays are almost connected exactly the same.

I.E. they both get exactly the same information from PORTB of the microcontroller - this is why we need the transistors, so that even though both displays receive the same info at the same time, we can select which one the displays will be turned on. This will all make sense (hopefully) when we get into the source code.

 
 

Here is what the completed circuit looks like

 
 

You can now download the new sourcecode here

 
 

Okay if you run the code on your circuit you should be presented with the number '0' on one display and the number '7' on the other, but you only see one number at any one time (thats because we alternate between the two displays.)

You will notice that we control the first display through the transistor to PORTA pin 0. By sending a logic '1' to PORTA pin 0 we are turning the transistor ON if we are turning it on then we have now effectively connected +5v to the common anode pin of the first display.

If we send a logic '0' to PORTA pin 0 then we are turning the transistor OFF and therefor disconnecting +5v from the common anode pin of the display.

Likewise, the second display is controlled by sending a '1' or a '0' to PORTA pin 1. So the key thing to remember is:

PORTA pin 0 drives the first display
PORTA pin 1 drives the second display


Now you may ask "well what is the point if we can only see one number at a time?" I'm glad you asked, you can answer your own question by reducing the delay time. What happens when you reduce the delay from 255 to 100? or 50 or even lower?

At what number does the display stop flickering?
 
So there's your answer, even though we can only have one display on at anyone time, we speed up the rate at which they are turned on and to the human eye, it looks like one steady display! nifty = )
 
Can you think of something useful to do with a display like this? some sort of counter?

 
 

We'll before i leave you, i thought i'd do one last piece of sourcecode which is just a bit of fun (well i think so) basically this sourccode is going to make your display do this

You can download the code here

 
 

Maybe not the most exciting thing you have ever seen but it gives you a little insight as to how we can use data look-up tables to store information ready to be read and then written to our display. you may also notice that in order to achieve the above animation, we are going to need to switch between the left and right displays being activated.
 
I am not going to go into this piece of sourcecode in any detail because we will be dealing with look-up tables in a later module. But if you had a look at the code all we are doing is calling the table and as such, we increment the program counter by one more everytime we call it. this means that everytime we visit the lookup table, we grab the next retlw instruction - so what is a retlw instruction?
 
The retlw instruction is basically this - return to where we came from BUT also store this byte of information in the w register when we go back. So really it acts just like the return instruction but it stores a byte of data in w as we return.

As stated before, we will talk more about these instructions in a later module, but i thought i would throw it in here as an introduction.
 
Well that is all for this module, thanks for looking and God bless.