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.
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.
Step 1: Materials
2 digit 7 segmented display (common anode)
14 660ohm resistors
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.
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
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.