void setup() {
chipName("SN74HC595"); // 8-bit shift register
logicFamily(TTL);
pin(1, "Q1", THREE_STATE);
pin(2, "Q2", THREE_STATE);
pin(3, "Q3", THREE_STATE);
pin(4, "Q4", THREE_STATE);
pin(5, "Q5", THREE_STATE);
pin(6, "Q6", THREE_STATE);
pin(7, "Q7", THREE_STATE);
pin(8, "GND", GROUND);
pin(9, "Q7S", THREE_STATE);
pin(10, "MR", INPUT, LINE_OVER);
pin(11, "SHCP");
pin(12, "STCP");
pin(13, "OE", OUTPUT, LINE_OVER);
pin(14, "DS");
pin(15, "Q0", THREE_STATE);
pin(16, "VCC", POWER);
}
bool q[8];
bool qs[8];
bool lastShcp = false;
bool lastStcp = false;
void loop() {
// Read the current states of control pins
// Read SHCP (shift clock)
bool shcp = digitalRead(11);
// Detect rising edge of SHCP
bool shcpRise = !lastShcp && shcp;
// Read STCP (storage clock)
bool stcp = digitalRead(12);
// Detect rising edge of STCP
bool stcpRise = !lastStcp && stcp;
// Read OE (output enable)
bool noe = digitalRead(13);
// Read MR (master reset)
bool nmr = digitalRead(10);
// Read DS (serial data input)
bool ds = digitalRead(14);
// Set the three-state mode based on OE (output enable) state
// Q7S oub
pinThreeStateMode(9, noe ? HIGH_Z : LOW_IMPEDANCE);
// Set the three-state mode for pins Q1 to Q7 based on OE (output enable) state
for (int i = 1; i <= 7; i++)
pinThreeStateMode(i, noe ? HIGH_Z : LOW_IMPEDANCE);
pinThreeStateMode(15, noe ? HIGH_Z : LOW_IMPEDANCE);
// Reset all outputs when Storage Clock (STCP) rises and Master Reset (MR) is not active
if (stcpRise && !nmr) {
// Clear Q7S
digitalWrite(9, LOW);
for (int i = 0; i < 8; i++)
// Clear both qs and q arrays
qs[i] = q[i] = false;
// Set Q1 to Q7 TO low
for (int i = 1; i <= 7; i++)
digitalWrite(i, LOW);
// Set Q0 to low
digitalWrite(15, LOW);
}
// Handle shift and storage of data when Shift Clock (SHCP) rises and Master Reset (MR) is active
if (shcpRise && nmr) {
// On STCP rise, copy shifted data into output array and update Q pins
if (stcpRise) {
for (int i = 0; i < 8; i++)
q[i] = qs[i];
// Update Q1 to Q7 pins
for (int i = 1; i <=7; i++)
digitalWrite(i, q[i]);
// Update Q0 pin
digitalWrite(15, q[0]);
}
// Update Q7S pin
digitalWrite(9, qs[7]);
// Shift the qs array
for (int i = 7; i >= 1; i--)
qs[i] = qs[i-1];
// Load the new serial data into the first position of qs array
qs[0] = ds;
log((int)qs[0]);
}
// Update output pins whe Storage Clock (STCP) rises and Master Reset (MR) is active
if (!shcpRise && stcpRise && nmr) {
for (int i = 0; i < 8; i++)
// Copy shifted data into output array
q[i] = qs[i];
// Update Q1 to Q7 pins
for (int i = 1; i <= 7; i++)
digitalWrite(i, q[i]);
// Update Q0 oub
digitalWrite(15, q[0]);
}
// Update the previous states of Shift Clock (SHCP) and Storage Clock (STCP)
lastShcp = shcp;
lastStcp = stcp;
}