About 1 week ago I posted a question asking how to use an SR, well having cracked one I am in the process of working out how cascading works. I built a small circuit on a breadboard for a 24 hour clock using two bytes of data and 16 LEDs (8 for hours, 8 for minutes, it is easier this way). I remade the scruffy test circuit tonight and added an extra byte of data so I have another 8 LEDs for the seconds. Now I test it the two less significant byte outputs are fading up to full power, this is acceptable for something that doesn't update often but for something like this that needs to update often it didn't seem right having 2/3 of the LEDs not working. I have shot a video which is currently uploading to youtube, I have a static (and higher quality) image of the breadboard here:
Wiring key:
- Green: Latch and hours
- Yellow: Clock and Minutes
- Blue: Data (Arduino>Secs>Q7'>Mins>Q7'>Hours
- White: Secs
- Orange: Reset (connected to +5v)
- Red: +5v Rail
- Black: Gnd Rail
... and the code...
//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;
//Pin connected to the reset of the devices.
int pinReset = 5;
int ranOnce = 0;
void setup() {
//Start Serial for debuging purposes
Serial.begin(9600);
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
pinMode(pinReset, OUTPUT);
digitalWrite(pinReset, HIGH);
}
void loop() {
//count up routine
for (int j = 0; j != 24; j++) {
for(int k = 0; k != 60; k++)
{
for(int s = 0; s != 60; s++)
{
if(ranOnce == 0){
ranOnce = 1;
j = 17;
k = 30;
}
digitalWrite(latchPin, 0); //Open the gate for the data
shiftOut(dataPin, clockPin, j); //Send the data through (first num) ie (10100000,x)
shiftOut(dataPin, clockPin, k); //Send the data through (2nd num) ie (x,11101101)
shiftOut(dataPin, clockPin, s);
// RESULT: 01010101,11101101
digitalWrite(latchPin, 1); //Shut the gare
delay(1000); //Dude, this happens too quick! WAIT 1 MINUTE
}
}
}
}
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);
}
I have checked the circuit myself a few times, and rebuilt it once (pictured is current) but I fail to see what is wrong. Video to follow.
Arduino /w ATMega328
Arduino Version 0016
74HC595 Shift Register