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.
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
- electronics breadboard
- 2x common anode seven segment displays
- 2x NPN small signal transistors (i use pn2222a)
- 9x resistors (any resistor between 100 ohms to 1kohm will do)
- hookup wire
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 individual LED’s. Internally all the anodes are connected together and all cathodes are separate. 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!)
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 connection to the top common anode, you could alternatively use the bottom common anode etc… so for our exercises, 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?
You can download all source code files via the link at the bottom of this page.
Now if you open up the first source code file 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.
If you run the second piece of code from the download 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!
Can you think of something useful to do with a display like this? some sort of counter?
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.