/*
Arduino Forum
Topic: Shift Register Code
Sub-Category: Programming Questions
Category: Using Arduino
Link: https://forum.arduino.cc/t/shift-register-code/1142032/9
*/
// Pin definitions
#define SRCLK 13 // Pin connected to SHCP (Shift Register Clock)
#define SER 11 // Pin connected to DS (Serial Input)
#define RCLK 9 // Pin connected to STCP (Storage Register Clock)
// Setup function
void setup() {
ShiftRegisterInit(); // Initialize shift register
}
// Main loop
void loop() {
// TODO: Add your desired functionality here
ShiftRegisterWrite(B10010011); // Write data to the shift register
}
// Function to initialize shift register pins
void ShiftRegisterInit() {
pinMode(RCLK, OUTPUT); // Set RCLK pin as output
pinMode(SER, OUTPUT); // Set SER pin as output
pinMode(SRCLK, OUTPUT); // Set SRCLK pin as output
digitalWrite(SRCLK, HIGH); // Set initial state of SRCLK pin to HIGH
digitalWrite(RCLK, HIGH); // Set initial state of RCLK pin to HIGH
}
// Function to write data to the shift register
void ShiftRegisterWrite(uint8_t data) {
digitalWrite(RCLK, LOW); // Set RCLK pin to LOW for data input
for (uint8_t index = 0; index < 8; index++) {
digitalWrite(SRCLK, LOW); // Set SRCLK pin to LOW for shifting in the next bit
digitalWrite(SER, bitRead(data, index)); // Write the current bit to SER pin
digitalWrite(SRCLK, HIGH); // Set SRCLK pin to HIGH to shift in the data bit
}
digitalWrite(RCLK, HIGH); // Set RCLK pin to HIGH to latch the data to the output pins
}
MSB
LSB