Go Down

Topic: how to add a second 7 segment display using a 74HC595 (Read 882 times) previous topic - next topic

Midway

Oct 30, 2013, 10:06 pm Last Edit: Oct 30, 2013, 10:19 pm by Midway Reason: 1
Hi all,

I have create a Up & Down counter with one 7 segment display using the 74HC595.
The code (below) works fine with the 74HC595, but it count only from 0 till 9

I need some help to add a second 7 segment display so i can count from 0 till 99

this is the code for one 7 segment display
Code: [Select]
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);               // I2C initialize the library with the numbers of the interface pins

byte Up_Button = 2;                                                          // Up button to digital input pin 2 (see diagram how to connect!!)
byte Down_Button = 3;                                                        // Down button to digital input pin 3 (see diagram how to connect!!)

int speed = 1000;                                                            // used to control speed of counting

                                //Global variables
int Up_State = 0;                                                            // variable that will read the Up button (either HIGH or LOW)
int Down_State = 0;                                                          // variable that will read the Down button (either HIGH or LOW)
int counter = 0;                                                             // variable that will store the count
int last_Up_State = 0;
int last_Down_State = 0;

const int latchPin = 5;                                                      // Pin connected to Pin 12 of 74HC595 (Latch)
const int dataPin  = 6;                                                      // Pin connected to Pin 14 of 74HC595 (Data)
const int clockPin = 7;                                                      // Pin connected to Pin 11 of 74HC595 (Clock)

unsigned long t1;
unsigned long t2;

const byte numbers[10] = {
 0b11111100,  // 0
 0b01100000,  // 1
 0b11011010,  // 2
 0b11110010,  // 3
 0b01100110,  // 4
 0b10110110,  // 5
 0b10111110,  // 6
 0b11100000,  // 7
 0b11111110,  // 8
 0b11100110,  // 9
};

void setup()
{
 pinMode(Up_Button, INPUT);                                                 // set Up Button as INPUT
 pinMode(Down_Button, INPUT);                                               // set Down Button as INPUT

   t1 = millis();

 pinMode(latchPin, OUTPUT);                                                 // set pins to output
 pinMode(clockPin, OUTPUT);
 pinMode(dataPin, OUTPUT);

 lcd.begin(16, 2);                                                          // set up the LCD's number of columns and rows:
 lcd.print("     V1.0 ");                                             // print a info message on the LCD
}

void loop()
{
 t2 = millis();                                                             // update digit every second
 if(t2 - t1 > 1000)
 {
   //      counter++;                                                       // automatic counting UP 7 segment display test
   t1 = t2;
   if(counter > 9) {
     counter = 0;
   }
 }
 show(numbers[counter]);
}

void show( byte number)
{
 for(int j=0; j<=7; j++)                                                    // Loop over each segment in the "number" we're about to display, and illuminate only one segment at a time.
 {
   byte toWrite = number & (0b10000000 >> j);

   if(!toWrite) {
     continue;
   }                                                                        // If all bits are 0 then no point writing it to the shift register, so break out and move on to next segment.

   shiftIt(toWrite);                                                        // Otherwise shift it into the register
 }
}

void shiftIt (byte data)
{
 digitalWrite(latchPin, LOW);                                               // Set latchPin LOW while and clock these 8 bits in to the register to illuminate a single segment.

 for (int k=0; k<=7; k++)
 {
   digitalWrite(clockPin, LOW);                                             // clockPin LOW prior to sending bit

   if ( data & (1 << k) )                                                   // Do another bitwise AND against a mask to check the state of each bit as we clock it in.
   {
     digitalWrite(dataPin, LOW);                                            // turn "On"
   }
   else
   {
     digitalWrite(dataPin, HIGH);                                           // turn "Off"
   }
   digitalWrite(clockPin, HIGH);                                            // and clock the bit in
 }
 digitalWrite(clockPin, LOW);                                               //stop shifting out data

 digitalWrite(latchPin, HIGH);                                              //set latchPin to high to lock and send data

 //    delay(100);                                                           // put delay here to check the multiplexing in action!

 Up_State = digitalRead(Up_Button);                                         // read the Up button state
 Down_State = digitalRead(Down_Button);                                     // read the Down button state
 if(Up_State != last_Up_State)                                              // compare Up button state to its last state
 {
   if(Up_State == LOW)                                                      // Up button is pressed
   {
     counter = counter++;                                                   // increment the counter
     lcd.setCursor(0, 1);
     lcd.print("count: ");
     lcd.setCursor(7, 1);
     lcd.print(counter);                                                    // print it on LCD
     lcd.print("   ");                                                      // 3 blank spaces
     delay(speed);                                                          // debounce delay
   }
 }
 last_Up_State = Up_State;

 if(Down_State != last_Down_State)                                          // compare Down state to its lastState
 {
   if(Down_State == LOW)                                                    // Down button is pressed
   {
     counter = counter--;                                                   // decrement the counter
     lcd.setCursor(0, 1);
     lcd.print("count: ");
     lcd.setCursor(7, 1);
     lcd.print(counter);                                                    // print it on LCD
     lcd.print("   ");                                                      // 3 blank spaces
     delay(speed);                                                          // debounce delay
   }
 }
 last_Down_State = Down_State;
}

johnwasser

You can daisy-chain the two shift registers.  They share the Clock and Latch pins and the Data Out of the first connects to the Data In of the second.  You pull the Latch line low as before and then shiftOut() two times.  The second shiftOut() pushes the first byte into the second shift register.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Nick Gammon

Example here:

http://www.gammon.com.au/forum/?id=11518
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up