shift register

Dependents:   Enrico_newproject

Files at this revision

API Documentation at this revision

Comitter:
billycorgan123
Date:
Wed Feb 21 19:37:59 2018 +0000
Commit message:
rev1.0

Changed in this revision

Shifter.cpp Show annotated file Show diff for this revision Revisions of this file
Shifter.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 7af9b977e6e9 Shifter.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Shifter.cpp	Wed Feb 21 19:37:59 2018 +0000
@@ -0,0 +1,55 @@
+// Include the standard types
+#include "Shifter.h"
+
+#define LOW 0
+#define HIGH 1
+DigitalOut SER_Pin(D4);
+DigitalOut RCLK_Pin(D3);
+DigitalOut SRCLK_Pin(D2);
+int Number_of_Registers=2;
+
+void Shifter::write()
+{
+//Set and display registers
+//Only call AFTER all values are set how you would like (slow otherwise)
+RCLK_Pin.write(LOW);
+  
+  //iterate through the registers
+  for(int i = Number_of_Registers - 1; i >=  0; i--)
+  {
+    //iterate through the bits in each registers
+    for(int j = 8 - 1; j >=  0; j--)
+    {
+      SRCLK_Pin.write(LOW);   
+      wait_us(1);
+      int val = shiftRegisters[i] & (1 << j);
+      SER_Pin.write(val);
+      wait_us(1);
+      SRCLK_Pin.write(HIGH);
+    }
+   
+  }
+  RCLK_Pin.write(HIGH);
+}
+    
+void Shifter::setPin(int index, bool val)
+{
+    int byteIndex = index/8;
+    int bitIndex = index % 8;
+    char current = shiftRegisters[byteIndex];
+    current &= ~(1 << bitIndex); //clear the bit
+    current |= val << bitIndex; //set the bit
+    shiftRegisters[byteIndex] = current; //set the value
+}
+
+void Shifter::setAll(bool val)
+{
+//set all register pins to LOW  
+  for(int i = Number_of_Registers * 8 - 1; i >=  0; i--){  setPin(i, val); }
+}
+
+void Shifter::clear()
+{
+//set all register pins to LOW  
+  for(int i = Number_of_Registers * 8 - 1; i >=  0; i--){ setPin(i, LOW); }
+}
\ No newline at end of file
diff -r 000000000000 -r 7af9b977e6e9 Shifter.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Shifter.h	Wed Feb 21 19:37:59 2018 +0000
@@ -0,0 +1,19 @@
+#ifndef Shifter_h
+#define Shifter_h
+
+// Include the standard types
+#include "mbed.h"
+
+// Define the Shifter class
+class Shifter
+{
+  public:
+    void write();
+    void setPin(int, bool);
+    void setAll(bool);
+    void clear();
+  private:
+    char shiftRegisters[25];
+};
+
+#endif 
\ No newline at end of file