Arduino powered 7seg led display with Port Manipulation

Arduino led display (1)

Time for something a little more advanced. Direct Port Manipulation. Normally when using Arduino software, the actual logic behind changing the values in pins is abstracted away with digitalRead and digitalWrite. Now, for most people that’s just fine. But it has some limitations. For one, it is a little slower than might be practical for some situations and it can only change one pin at a time. But what if you wanted to change ALL the pins at the same time? Annoying with digitalWrite, but super easy with Port Manipulation.Arduino led display (1)

What we’re using today to demonstrate this is a 7 segmented display. There are seven separate segments, each with a single LED that is individually operated. When combinations are set, you get a number. Using digitalWrite, you would have to write out that command seven times for just one number and there are ten possible numbers for each digit! You can see how this can get really complicated for doing something as simple as counting up.

It’s easy to setup, but takes some planning in the code.

Just another one of the many things that I made at TechShop today.

http://techshop.ws

Step 1: Materials

Arduino Uno

2 digit 7 segmented display (common anode)

14 660ohm resistors

breadboard

wire

usb cable

Step 2: Circuit

This is the way a 2 digit common anode display would be attached.  Common anode means that all the led’s share a common power supply. So in order so turn one on, the pin will be set to LOW.

KEEP TRACK OF WHAT WIRES GO TO WHAT LED! You’re going to use that information to build the port mapping later.

A potentiometer is added just in case you want to get fancy and have the pot control the number instead of just counting up. This is not too much more difficult, so go ahead and try it.

Schematic Arduino led display (1)

Step 3: Port Manipulation Mapping

This is the pin mapping for the arduino uno. Notice that each port there is a designation right next to the pin that looks like “PX#”. This is the pin number for that letter bank. There are multiple banks in this chip. The ones we care about are bank B and D. They are the ones to be manipulated. Write down the pin and corresponding port bank id for all the leds. The next part will be to combine them.

Step 4: Code

The key here is to make a grid that maps the individual combinations of 8 bits to make one byte of data. On the paper, you can see that I made the combinations of letters that would make certain numbers. Then I took that and translated it to the banks. Each bank reads all 8 pins at the same time so we need to tell it which ones to turn on and off for each number. 1 for on, 0 for off. (This is inverted in the code) This data is represented by an 8 bit binary number. I mapped out all the combinations on a piece of paper and changed them to their hex values for simplicity.

This is the code to manipulate the ports for faster accessing and generally cleaner code. What this does is count from 0 to 99 and start over.

Read more: Arduino powered 7seg led display with Port Manipulation

Leave a Comment

Your email address will not be published. Required fields are marked *