Monday, May 25, 2015

Multiplexing

Display Different Numbers

Previously we covered how to display single numbers but we want to be able to display more than single digit numbers.  To do this we are going to use something called multiplexing.  This involves quickly flickering each segment on to display a certain number.

Software

We need to create a method to select the correct segment to light up.  Before we start programming anything have another glance at the datasheet.  We are working on the COM end of the LEDs.  The code to select certain segments is below:

void displayNumber(int segment, int val){
  switch(segment){
    case 0: digitalWrite(seg_one,LOW);
            digitalWrite(seg_two,HIGH);
            digitalWrite(seg_three,HIGH);
            digitalWrite(seg_four,HIGH);
            break;
    case 1: digitalWrite(seg_one,HIGH);
            digitalWrite(seg_two,LOW);
            digitalWrite(seg_three,HIGH);
            digitalWrite(seg_four,HIGH);
            break;
    case 2: digitalWrite(seg_one,HIGH);
            digitalWrite(seg_two,HIGH);
            digitalWrite(seg_three,LOW);
            digitalWrite(seg_four,HIGH);
            break;
    case 3: digitalWrite(seg_one,HIGH);
            digitalWrite(seg_two,HIGH);
            digitalWrite(seg_three,HIGH);
            digitalWrite(seg_four,LOW);
            break;
    default:digitalWrite(seg_one,HIGH);
            digitalWrite(seg_two,HIGH);
            digitalWrite(seg_three,HIGH);
            digitalWrite(seg_four,HIGH);
            break;
  }
  digitSelect(val);
}


One thing that looks weird is how we are using digital write.  We assert only one segment low in each case.  This is because we are on the other end of the LED so to enable a segment we pull that digit low.

We also have been referencing the variables seg_one-seg_four, these are the pins that DIG1-DIG4 will be connected to.  In this demo we are using pins 13-10 for dig 1-4.

Hardware




The only difference in the wiring is that the DIG# pins are attached to I/O pins rather than directly to ground.  This allows us to only pick one segment at a time.

Testing

To test our program we will display 1234.  To do this we are going to put 4 statements in the main loop.
void loop(){
displayNumber(1,1);
delayMicroseconds(1250);
displayNumber(2,2);
delayMicroseconds(1250);
displayNumber(3,3);
delayMicroseconds(1250);
displayNumber(4,4);
delayMicroseconds(1250);
}

We need to delay by a little bit to give the current time to flow through the segment to light it up.
This should display 1234.  The delay is picked so that the total time to complete one loop is 5ms.  This will be useful when we want to create a timer.

No comments:

Post a Comment