Multiplex 3 digit 7 segments display using 74HC595

Hi guys;

I just finish my code to multiplex 3 digit 7 segments display. First, I built the circuit, test the circuit, install the 74HC595, re-test doing a test program and do a multiplex code do count up from 0 to 999. The display will stay for 1 second ( about ) and count up.

Anyway, here the test code.

/*
tested code for checking and display 0 to F 
on each digit at a time.

*/
const byte digitpin[3]={10,11,12};
const byte datapin=9;
const byte enablepin=8;
const byte latchpin=7;
const byte clockpin=6;

// 7 segment data to display 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
byte numbers[16]={252,96,218,242,102,182,190,224,254,246,238,62,156,122,158,142};

void setup()
{
  pinMode(datapin, OUTPUT);
  pinMode(enablepin, OUTPUT);
  pinMode(latchpin, OUTPUT);
  pinMode(clockpin, OUTPUT);
  for (int i=0;i<3;i++)
  {
    pinMode(digitpin[i],OUTPUT);
  }
  digitalWrite(enablepin, LOW);
  for (int i=0;i<3;i++)
  {
    digitalWrite(digitpin[i],HIGH);
  }
  delay(2000);
}

void loop()
{
  for (int j=0;j<3;j++)
  {
   digitalWrite(digitpin[j], LOW);
  for (byte i=0;i<16;i++)
  {
    displaynumbers(i);
    delay(1000);
  }
  digitalWrite(digitpin[j], HIGH);
  delay(2000);
  }
}  

void displaynumbers(byte data)
{
    digitalWrite(latchpin, LOW);
    shiftOut(datapin, clockpin, LSBFIRST, numbers[data]);   
    digitalWrite(latchpin, HIGH);
}

And here the multiplex code.

/*
   Byte : 1596
   
   Version 1.0

   Counting from 0 to 999 - Multiplex 7 segment display
   Using 3 digit 7 segment display
   Commun Anode using a ULN2804 driver
   with 3 PNP transistor for digit select
   and the use of 74HC595 serial-to-parallel
 
   Program by Serge J Desjardins
   aka techone / tech37
   
   Toronto, Ontario  Canada
   
   Compiled and Tested
   
*/

const byte digitpin[3]={10,11,12};
const byte datapin=9;
const byte enablepin=8;
const byte latchpin=7;
const byte clockpin=6;

byte storage;
byte dataone;
byte datatwo;
byte datathree;

// 7 segment data to display 0,1,2,3,4,5,6,7,8,9
byte numbers[10]={252,96,218,242,102,182,190,224,254,246};

void setup()
{
  pinMode(datapin, OUTPUT);
  pinMode(enablepin, OUTPUT);
  pinMode(latchpin, OUTPUT);
  pinMode(clockpin, OUTPUT);
  for (int i=0;i<3;i++)
  {
    pinMode(digitpin[i],OUTPUT);
  }
  digitalWrite(enablepin, LOW);
  for (int i=0;i<3;i++)
  {
    digitalWrite(digitpin[i],HIGH);
  }
  delay(2000);
}

void loop()
{
  for (int counting=0;counting<1000;counting++)
  {
   BCDconverter(counting);
   for (int looping=0;looping<100;looping++)
   { 
     storage=dataone;
     digitalWrite(digitpin[0], LOW);
     digitalWrite(digitpin[1], HIGH);
     digitalWrite(digitpin[2], HIGH);
     displaynumbers(storage);
     storage=datatwo;
     delay(5);
     digitalWrite(digitpin[0], HIGH);
     digitalWrite(digitpin[1], LOW);
     digitalWrite(digitpin[2], HIGH);
     displaynumbers(storage);
     delay(5);
     storage=datathree;
     digitalWrite(digitpin[0], HIGH);
     digitalWrite(digitpin[1], HIGH);
     digitalWrite(digitpin[2], LOW);
     displaynumbers(storage);
     delay(5);
   }
  } 
}

void BCDconverter(int todisplay)
{
// Isolated Hundrens, tens and units
  dataone=byte(todisplay % 10);
  datatwo=byte((todisplay%100)/10);
  datathree=byte(todisplay/100);
}

void displaynumbers(byte data)
{
    digitalWrite(latchpin, LOW);
    shiftOut(datapin, clockpin, LSBFIRST, numbers[data]);   
    digitalWrite(latchpin, HIGH);
}

And I want you critique my code and if you see room for improvement, please go ahead.
And I hope my code will help others.

Hello, im also trying myself with the 74hc595. I'm trying to Build a Countdown Display from 3 Minuten Down to 0. My Quentin, are you Unding 1 or 3 Chips (74hc595) for the 3 digit display's? Can you Maybe send me you schematic how you Connected every thing? I'm thankfull for any help.
Greetings Mischa

1 Like