Hello:
I'm working with a bit of code that I found earlier trying to drive a single 7 segment display with a 74HC595 using some code that the forum user "Spiked Cola" mentioned in the forum post http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1247631566.
I have modified the array to contain the decimal values of the segments I want to light up and it appears to work but only for a couple of iterations before the LED display becomes corrupted (pixels don't light as expected or unexpected pixels light). I'm not entirely sure why this is not working. The general idea is that the code should be able to display 0 through 9 and loop indefinitely.
Admittedly, I'm still learning this code and am not sure if I'm doing the shift out properly. Please feel free to advise if I've messed up something somewhere.
Thank you.
//**************************************************************//
// Name : shiftOutCode, Predefined Array Style //
// Author : Carlyn Maw, Tom Igoe //
// Date : 25 Oct, 2006 //
// Version : 1.0 //
// Notes : Code for using a 74HC595 Shift Register //
// : to count from 0 to 255 //
//****************************************************************
//
// This code is modified from it's original design by FIRESTORM_v1
// to store the predefined pixels to light in a byte array.
//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;
//holders for infromation you're going to pass to shifting function
byte data;
byte dataArray[8];
void setup() {
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
Serial.begin(9600);
/*
The seven segment display is set up as follows:
A
#####
F # # B
# G #
#####
E # # C
# #
#####
D
Each element is attached to the output pun of the 74HC595, A to Q1, B to Q2, C to
Q3 and so on.. That being said, each element also has a decimal value assigned to
it for the purpose of calculating the decimal version of the needed byte to turn on
that segment and is computed below:
# segments Byte values = Decimal Values
1 B,C 2, 4 = 6
2 A,B,D,E,G 1,2,8,16,64 = 91
3 A,B,C,D,G 1,2,4,8,64 = 79
4 B,C,F,G 2,4,32,64 = 102
5 A,C,D,F,G 1,4,8,32,64 = 109
6 A,C,D,E,F,G 1,4,8,16,32,64 = 125
7 A,B,C 1,2,4 = 7
8 A,B,C,D,E,F,G 1,2,4,8,16,32,64= 127
9 A,B,C,D,F,G 1,2,4,8,32,64 = 111
0 A,B,C,D,E,F 1,2,4,8,16,32 = 63
These values are stored in an array with the index set to the number that should
appear on the seven segment LED display.
*/
dataArray[0] = 63;
dataArray[1] = 6;
dataArray[2] = 91;
dataArray[3] = 79;
dataArray[4] = 102;
dataArray[5] = 109;
dataArray[6] = 125;
dataArray[7] = 7;
dataArray[8] = 127;
dataArray[9] = 111;
}
void loop() {
// This loop will run indefinitely, counting from 0 to 9 and
// displays 0 through 9 on the seven segment LED display.
for (int j = 0; j < 10; j++) {
//load the light sequence you want from array
data = dataArray[j];
//ground latchPin and hold low for as long as you are transmitting
digitalWrite(latchPin, 0);
//move 'em out
shiftOut(dataPin, clockPin, data);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
digitalWrite(latchPin, 1);
delay(1000);
}
}
// the heart of the program
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
// This shifts 8 bits out MSB first,
//on the rising edge of the clock,
//clock idles low
//internal function setup
int i=0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
//clear everything out just in case to
//prepare shift register for bit shifting
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);
//for each bit in the byte myDataOut
//NOTICE THAT WE ARE COUNTING DOWN in our for loop
//This means that %00000001 or "1" will go through such
//that it will be pin Q0 that lights.
for (i=7; i>=0; i--) {
digitalWrite(myClockPin, 0);
//if the value passed to myDataOut and a bitmask result
// true then... so if we are at i=6 and our value is
// %11010100 it would the code compares it to %01000000
// and proceeds to set pinState to 1.
if ( myDataOut & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}
//Sets the pin to HIGH or LOW depending on pinState
digitalWrite(myDataPin, pinState);
//register shifts bits on upstroke of clock pin
digitalWrite(myClockPin, 1);
//zero the data pin after shift to prevent bleed through
digitalWrite(myDataPin, 0);
}
//stop shifting
digitalWrite(myClockPin, 0);
}