Tuesday, 30 January 2018

Arduino powered 7seg led display with Port Manipulation



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.

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.

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.


byte digitOne[10]= {0x6F, 0x09, 0x73, 0x3B, 0x1D, 0x3E, 0x7C, 0x0B, 0x7F, 0x1F}; //This is the hex value of each number stored in an array by index num
byte digitTwoB[10]= {0x3D,0x08,0x33,0x1B,0x0E,0x1F,0x3E,0x09,0x3F,0x0F};
byte digitTwoD[10]= {0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80};

void setup(){

  DDRD = 0xFF; //Set all the pins to output in bankD
  DDRB = 0x3F; //Same in bankB

}

void loop(){


  for(int i=0; i<10; i++){
    for(int j=0; j<10; j++){
      PORTD = ~digitOne[j]; //set pins low for digit one
      PORTD &= ~digitTwoD[i]; //AND with digit two pins for the odd pin
      PORTB = ~digitTwoB[i]; //set pins low for digit two
      delay(500);
    }
  }

}


And there you go. You can use port manipulation to change many of the pins very fast and for very specific reasons. If you have very time sensitive code or just want to change a whole bunch of things at once (like an array of leds) then port manipulation is your friend.

No comments:

Post a Comment