teaching machines

CS 352 Lecture 8 – 7-Segment Logic

September 22, 2016 by . Filed under cs352, fall 2016, lectures.

Dear students,

Today we’re round out our understanding of Karnaugh maps in the ultimate test. We’re going to design the logic to drive a 7-segment display! What is that, you ask? A 7-segment display is one of these:

Image courtesy Peter Halasz.

It’s called “7-segment” because it’s decomposed into seven independent LEDs, each identified by a letter, A through G:

Image courtesy h2g2bob.

The big question that the circuit must answer is, “Do I light segment X?” In pseudocode, we might craft the logic like so:

if segment A should be on
  power segment A

if segment B should be on
  power segment B

if segment C should be on
  power segment C

...

What are the conditions for those if statements? We could write them sloppily:

if n == 2 || n == 3 || n == 5 || n == 6 || n == 7 || n == 8 || n == 9
  power segment A

...

But the logic would not be minimal. Instead, we can look at the underlying bit patterns for numbers 0 through 9. We need four bits to represent numbers in this range. We’ll call the most significant bit D3 and the least D0. We can then sketch out a truth table, where the inputs are D3, D2, D1, D0, and the outputs are whether or not segment X is illuminated for the given bits:

VALUE D3 D2 D1 D0 A B C D E F G
0 0 0 0 0 ? ? ? ? ? ? ?
1 0 0 0 1 ? ? ? ? ? ? ?
2 0 0 1 0 ? ? ? ? ? ? ?
3 0 0 1 1 ? ? ? ? ? ? ?
4 0 1 0 0 ? ? ? ? ? ? ?
5 0 1 0 1 ? ? ? ? ? ? ?
6 0 1 1 0 ? ? ? ? ? ? ?
7 0 1 1 1 ? ? ? ? ? ? ?
8 1 0 0 0 ? ? ? ? ? ? ?
9 1 0 0 1 ? ? ? ? ? ? ?

Let us now begin our challenge:

  1. Form a team of three.
  2. Await your segment assignment: a letter in [A, G].
  3. Complete your column of the truth table.
  4. Create a Karnaugh map with D3/D2 on one axis and D1/D0 on the other.
  5. Share the minimized expression with your instructor.

If the entire class gets the logic working by the end of our 50 minutes together, your instructor will award a participation point to all present! We will test on a real live Arduino board hooked up to a real live 7-segment display.

See you next class!

Sincerely,

P.S. It’s Haiku Friday!

My first watch display
1×2 + 3×7
Was good at the time

P.P.S. Here’s the code we wrote together in class…

segment.cpp

#include "Arduino.h"

void setup() {
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}

int i = 0;

void loop() {
  int d0 = (i >> 0) & 1;
  int d1 = (i >> 1) & 1;
  int d2 = (i >> 2) & 1;
  int d3 = (i >> 3) & 1;
  
  // A
  if (!d3 && d1 ||
      !d1 && !d0 && !d2 ||
      !d1 && !d2 && d3 ||
      !d3 && d2 && d0) {
    digitalWrite(0, HIGH);
  }

  // B
  if (!d3 && !d1 && !d0 ||
      !d2 && !d1 ||
      !d3 && d1 && d0 ||
      !d3 && !d2) {
    digitalWrite(1, HIGH);
  }

  // C
  if (!d3 && d2 ||
      !d3 && !d2 && !d1 ||
      d3 && !d2 && !d1 ||
      d0 && d1 && !d3) {
    digitalWrite(2, HIGH);
  }

  // D
  if (d1 && !d2 && !d3 ||
      !d0 && !d1 && !d2 ||
      !d0 && d1 && !d3 ||
      d0 && !d1 && d2 && !d3) {
    digitalWrite(3, HIGH);
  }

  // E
  if (d1 && !d0 && !d3 ||
      !d1 && !d0 && !d2) {
    digitalWrite(4, HIGH);
  }
 
  // F
  if (d3 && !d2 && !d1 ||
      !d3 && !d1 && !d0 ||
      !d3 && d2 && !d1 ||
      !d3 && d2 && !d0) {
    digitalWrite(5, HIGH);
  }
  
  // G
  if (d3 && !d2 && !d1 ||
      !d3 && d2 && !d1 ||
      !d3 && !d2 && d1 ||
      !d3 && d1 && !d0) {
    digitalWrite(6, HIGH);
  }

  delay(1000);

  digitalWrite(0, LOW);
  digitalWrite(1, LOW);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);

  i = (i + 1) % 10;
}