74HC595 Shift register with Arduino

191 views Asked by At

I am using shift register with Arduino to increase my Output pins. Here I want to edit each serial bit individualy and store it. In Short I want to control each pin of register as we use Arduino I/O pins. I try to do it with the following programe but it is not working.

#include<ShiftOutX.h>
 #include<ShiftPinNo.h>
// Define Connections to 74HC595
 
// ST_CP pin 12
const int latchPin = 10;
// SH_CP pin 11
const int clockPin = 11;
// DS pin 14
const int dataPin = 12;


String S1 = "1";
String   S2 = "0";
String   S3 = "0";
String  S4 = "0";
String   S5 = "0";
String   S6 = "0";
String   S7 = "0";
String   S8 = "0";
String   S9 = "0";
String   S10 = "1";
String  S11 = "0";
String   S12 = "0";
String   S13 = "0";
String  S14 = "0";
String   S15 = "0";
String   S16 = "0";
String  S17 = "0";
String   S18 = "0";
String  S19 = "0";
String   S20 = "1";
String  S21 = "0";
String  S22 = "0";
String   S23 = "0";
String  S24 = "0";
String  S25 = "0";
String  SB = "b";
String  S = "0";


shiftOutX reg(latchPin, dataPin, clockPin, LSBFIRST, 3);


void setup ()
{
  // Setup pins as Outputs
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{

    digitalWrite(latchPin, HIGH);

    String  one =S+SB+ S1+ S2+ S3+ S4+ S5+ S6+ S7+ S8+ S9+ S10+ S11+ S12+ S13+ S14+ S15+ S16+ S17+ S18+ S19+ S20+ S21+ S22+ S23+ S24+ S25;
    Serial.println(one);
    char buf[27];
    one.toCharArray(buf,one.length());
    Serial.println(buf);
    shiftOut_24(dataPin, clockPin, LSBFIRST, buf);
    digitalWrite(latchPin, LOW);
    digitalWrite(latchPin, HIGH);
    delay(1000);

}

"0b1000000001000000000100000 0b100000000100000000010000" I get the above out put on serial screen but output of the register is different. Please give me some solution.

2

There are 2 answers

0
datafiddler On

I understand you want to chain 3 shift registers

byte data[3] = {0b01000000, 0b00100000, 0b00100000 };

void shiftOut24(byte* data) {
   digitalWrite(latchPin, 0);
   shiftOut(dataPin, clockPin,MSBFIRST,data[2]);
   shiftOut(dataPin, clockPin,MSBFIRST,data[1]);
   shiftOut(dataPin, clockPin,MSBFIRST,data[0]);
   digitalWrite(latchPin, 1);
}
void debugPrint24(byte* data) {
// TODO print more nicely
// leading zeros by adding bit 24 currently
long out = 1L<<24;
out |= (long)data[0] | (long)data[1] << 8 | (long)data[2] << 16;
Serial.println(out,BIN); 
}

byte pos = 0;  // 0 .. 23  for test data
void loop() {
   debugPrint24(data); 
   shiftOut24(data);
   // adding test data for fun
   byte b = pos / 8;
   if (b > 2) b = pos = data[0] = data[1] = data[2] = 0;  
   data[b] |= 1 << (pos++ & 7); // set another bit for next round  
   delay(1000);    
}

Just some demo code to set single bits.

0
Gaurav Gund On

I got a code from the net and I think it could work well. It is easy to use and I was able to control each pin individualy. Below is the code:

// Arduino-Pin verbunden mit SH_CP des 74HC595
int shiftPin = 11;
// Arduino-Pin verbunden mit ST_CP des 74HC595
int storePin = 10;
// Arduino-Pin verbunden mit DS des 74HC595
int dataPin = 12;
 
// Dieses Muster soll ausgegeben werden
int muster[24] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

void Send_Data(){
 // storePin sicherheitshalber auf LOW
 digitalWrite(storePin, LOW); 
 
 for (int i=0; i<24; i++) {
 // Zuerst immer alle 3 Pins auf LOW
 // Aktion passiert bei Wechsel von LOW auf HIGH
 digitalWrite(shiftPin, LOW);
 // Jetzt den Wert der aktuellen Stelle ans Datenpin DS anlegen 
 digitalWrite(dataPin, muster[i]);
 // Dann ShiftPin SHCP von LOW auf HIGH, damit wird der Wert
 // am Datenpin ins Register geschoben. 
 digitalWrite(shiftPin, HIGH);
  }
  // Wenn alle 8 Stellen im Register sind, jetzt das StorePin STCP
 // von LOW auf HIGH, damit wird Registerinhalt an Ausgabepins
 // kopiert und der Zustand an den LEDs sichtbar
 
 digitalWrite(storePin, HIGH);
}
 


void setup() {
 
 // Pins 8,9,10 auf Ausgabe
 pinMode(storePin, OUTPUT);
 pinMode(shiftPin, OUTPUT);
 pinMode(dataPin, OUTPUT);
}
 

void loop () {
 muster[0] = 1;
 Send_Data();
 delay(1000);
 
 muster[15] = 1;
 Send_Data();
 delay(1000);
 
 muster[23] = 1;
 Send_Data();
 delay(1000);
 
 muster[0] = 0;
 Send_Data();
 delay(1000);
 
 muster[15] = 0;
 Send_Data();
 delay(1000);
 
 muster[23] = 0;
 Send_Data();
 delay(1000);
}

Please keep me updating if any one come across more easy way to do it.