/*
##########################################################
|| ||
|| Title: Binary Counter with a 74HC595 Shift Register ||
|| Name: GC1CEO ||
|| Date: 09/17/2024 ||
|| Attribution: ||
|| Based on shiftOutCode, Hello World ||
|| by Carlyn Maw,Tom Igoe, David A. Mellis ||
|| Date: 10/25/2006 Modified: 3/23/2010 ||
|| ||
|| Description: ||
||This counts from 0 to 255 on a 75HC595 Shift Register ||
|| and outputs the results on 8 segments of an LED bar ||
|| graph. ||
|| ||
|| ||
##########################################################
This version has an attached logic analyzer which has lines
to DA/DATA, LATCH/RCLK/STCP, and CLOCK/CLK/SHCP. The delays
after each number in the program appear as DS/DATA being
held LOW and HIGH alternating with the actual byte being
shifted out at the beginning and end of each of these periods.
The DATA pulse isn't precisely the same width as the CLOCK pulse
but whatever the DATA is set to at the time when the CLOCK is
pulsed (and the LATCH is low) will be put into shift register
and when the LATCH is set to HIGH it'll move the state of the shift
register into the storage register and if OE is enabled it'll
output those contents.
*/
//Pin connected to ST_CP (Storage Clock / Latch) of 74HC595
int latchPin = 8;
//Pin connected to SH_CP (Shift Clock) of 74HC595
int clockPin = 12;
////Pin connected to DS / SER (Data/Serial Input) of 74HC595
int dataPin = 11;
////Pin connected to inverted MR / SRCLR (Master Reset / Serial Register Clear) of 74HC595
int resetPin = 10;
////Pin connected to inverted OE (Output Enable) of 74HC595
int oePin = 9;
// delay between each number (in ms)
int delayTime = 1000;
void setup() {
Serial.begin(9600);
//set pins to output so you can control the shift register and setting button and pot pins to INPUT
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(resetPin, OUTPUT);
pinMode(oePin, OUTPUT);
// Setting reset to HIGH to stop reset and setting output enable to LOW to always enable output
digitalWrite(resetPin, HIGH);
digitalWrite(oePin, LOW);
// Cleaning up serial monitor and announcing program has initalized and going to loop
for (int a = 0 ; a < 25 ; a++) {
Serial.println();
}
}
void loop() {
// count from 0 to 255 and display the number
// on the LEDs
Serial.print("Counting from 0 to 255!");
Serial.println();
// for loop to count from 0 to 255
for (int numberToDisplay = 0; numberToDisplay < 255; numberToDisplay++) {
// set the latch pin to low so LEDs dont change during shift
digitalWrite(latchPin, LOW);
// shift out the bits:
shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay);
//take the latch pin high so the LEDs will light up:
digitalWrite(latchPin, HIGH);
//print the number to serial
Serial.print(String(numberToDisplay) + " ");
// new line every 5 counts (and after the initial 0)
if (numberToDisplay % 5 == 0) {
Serial.println("");
}
// pause before next value
delay(delayTime);
}
// Announcement that count has finished and is now restarting
Serial.print("Count finished! Restarting!");
Serial.println();
}