7 Segment Display using 4 displays program

Post Reply
tmccanna
Posts: 2
Joined: Wed Jun 27, 2012 8:38 pm

7 Segment Display using 4 displays program

Post by tmccanna »

I used the 2 - 7-segment display with 74HC595 modules program as my starting point for a 4 - 7-segment display. I used this program because I wanted a timed increment and it will count days so it will increment every 24 hrs. I can't figure out how to modify the 2 digit program to work with 4 digits. When it counts up and it gets to 100 the middle digit will be ramdom and when it counts up to 1000 the middle two digits are ramdom. The outside digits count with no issue. Here is what I have for code so far. Any help to get me pointed in the right direction would be great. Thanks.

Code: Select all

/*
Using 2 7-segment displays with the 74HC595 shift registers
CC by-sa-nc 3.0
http://tronixstuff.wordpress.com
 */
int latchpin = 8; // connect to pin 12 on the '595
int clockpin = 12; // connect to pin 11 on the '595
int datapin = 11; // connect to pin 14 on the '595
float b = 0;
int c = 0;
float d = 0;
int e = 0;
float f =0;
int g = 0;
float h=0;
int i = 0;
int speed = 300; // used to control speed of counting
int segdisp[10] = {
 3,159,37,13,153,73,65,27,1,9 };
void setup()
{
 pinMode(latchpin, OUTPUT);
 pinMode(clockpin, OUTPUT);
 pinMode(datapin, OUTPUT);
}
void loop()
{
 //  Count up
 for (int z=0; z<10000; z++)
 {
   digitalWrite(latchpin, LOW);
   shiftOut(datapin, clockpin, LSBFIRST, 0); // clears the right most display #4
   shiftOut(datapin, clockpin, LSBFIRST, 0); // clears display #3
   shiftOut(datapin, clockpin, LSBFIRST, 0); // clears display #2
   shiftOut(datapin, clockpin, LSBFIRST, 0); // clears the left most display #1
   digitalWrite(latchpin, HIGH);
   if (z<10)
   {
     digitalWrite(latchpin, LOW);
     shiftOut(datapin, clockpin, LSBFIRST, segdisp[z]); // sends the digit down the serial path
     shiftOut(datapin, clockpin, LSBFIRST, 255); // sends a blank down the serial path to push the digit to the right
     shiftOut(datapin, clockpin, LSBFIRST, 255); // sends a blank down the serial path to push the digit to the right
     shiftOut(datapin, clockpin, LSBFIRST, 255); // sends a blank down the serial path to push the digit to the right
     digitalWrite(latchpin, HIGH);
   }
   else if (z>=10)
   {
     d=z%10; // find the remainder of dividing z by 10, this will be the right-hand digit #4
     c=int(d); // make it an integer, c is the right hand digit #4
     b=z/10; // divide z by 10 - the whole number value will be the digit #3
     e = int(b); // e is the digit #3
     f=z/100;
     g = int(f);
     h=z/1000;
     i = int(h);
     digitalWrite(latchpin, LOW); // send the digits down to the shift registers!
     shiftOut(datapin, clockpin, LSBFIRST, segdisp[c]); 
     shiftOut(datapin, clockpin, LSBFIRST, segdisp[e]);
     shiftOut(datapin, clockpin, LSBFIRST, segdisp[g]);
     shiftOut(datapin, clockpin, LSBFIRST, segdisp[i]);
     digitalWrite(latchpin, HIGH);
   }
   delay(speed);
 }
 delay(2000);
User avatar
techsupport
Site Admin
Posts: 210
Joined: Sat Mar 03, 2012 2:32 pm

Re: 7 Segment Display using 4 displays program

Post by techsupport »

Hi friend,

It seems that you use two 74HC595 to control two 1-digit 7-segment display respectively in your original project according to your program.
The program to display 4 digits on four 1-digit 7-segment display is wrong, you need to consider the condition when 10=<z<=99(2 digits), 100=<z<=999 (3 digits) and 1000=<z<=9999 (4 digits)

Code: Select all

(10=<z<=99)
  rhd = subject % 10; /#1
  a = subject/10;
  lhd = int(a);           /#2

(100=<z<=999)
  a = subject/100;
  lhd = int(a);          /#3
  a = subject/10;
  b = int(a); 
  mhd = b % 10;       /#2
  b=subject%100;      
  rhd=b%10;            /#1

(1000=<z<=9999)
  a = subject/1000;
  lhd = int(a);
  b=lhd*1000;
  c=subject-b;
  a = c/100;
  mhd = int(a);
  a = c/10;
  b = int(a); 
  mhd2 = b % 10;
  b=subject%1000;
  c=b%100;
  rhd=c%10;           /#1

More details please refer to http://tronixstuff.wordpress.com/2010/0 ... apter-six/
Your real trustworthy partner in China!!
Email: forum@geeetech.com
tmccanna
Posts: 2
Joined: Wed Jun 27, 2012 8:38 pm

Re: 7 Segment Display using 4 displays program

Post by tmccanna »

Thanks for your help, I got it to work correctly and count from 0 up to 9999.
Post Reply