simple library that can be used to write to a 595 shift register array

Files at this revision

API Documentation at this revision

Comitter:
henryeherman
Date:
Wed Oct 30 06:59:59 2013 +0000
Commit message:
Library to drive 595 shift registers

Changed in this revision

ShiftRegister.cpp Show annotated file Show diff for this revision Revisions of this file
ShiftRegister.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ShiftRegister.cpp	Wed Oct 30 06:59:59 2013 +0000
@@ -0,0 +1,34 @@
+#include "mbed.h"
+#include "ShiftRegister.h"
+
+using namespace mbed;
+
+ShiftRegister::ShiftRegister(DigitalOut &clk, DigitalOut &dat, 
+                DigitalOut &latch, DigitalOut &clr, 
+                DigitalOut &oe): _clk(clk), _dat(dat),
+                                    _latch(latch), _clr(clr),
+                                    _oe(oe) {
+                                    
+    _clk = 1;
+    _dat = 1;
+    _clr = 0;
+    _oe = 1;     
+    _clr = 1;
+    _oe = 0;   
+}
+
+ShiftRegister::~ShiftRegister() {
+    // Nothing to do!
+    clear();
+}
+
+
+
+void ShiftRegister::clear() {
+    _clk = 1;
+    _dat = 1;
+    _clr = 0;
+    _oe = 1;     
+    _clr = 1;
+    _oe = 0;  
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ShiftRegister.h	Wed Oct 30 06:59:59 2013 +0000
@@ -0,0 +1,56 @@
+#include "mbed.h"
+#ifndef SHIFTREGISTER_H
+#define SHIFTREGISTER_H
+
+
+class ShiftRegister {
+
+public:
+
+/*
+* Constructor
+*/
+ShiftRegister(DigitalOut &clk, DigitalOut &dat, 
+                DigitalOut &latch, DigitalOut &clr, 
+                DigitalOut &oe);
+
+/*
+* Destructor
+*/
+~ShiftRegister();
+
+/*
+* Write Value
+*/
+template <typename T>
+int write(T val, int bits);
+
+void clear();
+
+private:
+DigitalOut &_clk;
+DigitalOut &_dat;
+DigitalOut &_latch;
+DigitalOut &_clr;
+DigitalOut &_oe;
+
+}; // end class ShiftRegister
+
+
+template <typename T>
+int ShiftRegister::write(T val, int bits) {        
+    T tmpval = ~0;
+    int tmpsz = sizeof(val)*8;
+    val = tmpval & val;  
+    bits = tmpsz < bits ? tmpsz : bits;
+    for(char i = 0; i < bits; i++) {
+        _dat.write((val >> i) & 0x01);        
+        _clk = 0;        
+        _clk = 1;
+    }
+    _latch = 0; 
+    _latch =1;
+    return val;
+}
+
+#endif //ShiftRegister
\ No newline at end of file