CD74HC4067 library ported for mbed.

Files at this revision

API Documentation at this revision

Comitter:
scottohair
Date:
Sat Jan 20 10:52:09 2018 +0000
Commit message:
Library for the CDHC4067. Tested on NUCLEO-F401RE.

Changed in this revision

CD74HC4067.cpp Show annotated file Show diff for this revision Revisions of this file
CD74HC4067.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 7f158d69af5e CD74HC4067.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CD74HC4067.cpp	Sat Jan 20 10:52:09 2018 +0000
@@ -0,0 +1,61 @@
+//CD74HC4067 Library based on bildr code: "http://bildr.org/2011/02/cd74hc4067-arduino/"
+//Ported to mbed by Scott M. O'Hair
+
+#include "CD74HC4067.h"
+#include "mbed.h"
+
+
+CD74HC4067::CD74HC4067(PinName S1, PinName S2, PinName S3, PinName S4, PinName SIG) : _S0(S1), _S1(S2), _S2(S3), _S3(S4), _SIG(SIG)
+{
+    //initialize port state to zero
+    for (uint8_t i = 0; i < 15; i++) {
+        portState[i] = 0.0;
+    }
+
+    //pull all pins low
+    _S0 = 0;
+    _S1 = 0;
+    _S2 = 0;
+    _S3 = 0;
+
+}
+
+void CD74HC4067::saveState(uint8_t port, float state)
+{
+    this->portState[port] = state;
+}
+
+float CD74HC4067::readMux(uint8_t channel)
+{
+    DigitalOut controlPin[] = {_S0, _S1, _S2, _S3};
+
+    uint8_t muxChannel[16][4]= {
+        {0,0,0,0}, //channel 0
+        {1,0,0,0}, //channel 1
+        {0,1,0,0}, //channel 2
+        {1,1,0,0}, //channel 3
+        {0,0,1,0}, //channel 4
+        {1,0,1,0}, //channel 5
+        {0,1,1,0}, //channel 6
+        {1,1,1,0}, //channel 7
+        {0,0,0,1}, //channel 8
+        {1,0,0,1}, //channel 9
+        {0,1,0,1}, //channel 10
+        {1,1,0,1}, //channel 11
+        {0,0,1,1}, //channel 12
+        {1,0,1,1}, //channel 13
+        {0,1,1,1}, //channel 14
+        {1,1,1,1}  //channel 15
+    };
+
+    //loop through the 4 sig
+    for(uint8_t i = 0; i < 4; i++) {
+        controlPin[i] =  muxChannel[channel][i];
+    }
+
+    //read the value at the SIG pin
+    float val = _SIG.read();
+
+    //return the value
+    return val;
+}
diff -r 000000000000 -r 7f158d69af5e CD74HC4067.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CD74HC4067.h	Sat Jan 20 10:52:09 2018 +0000
@@ -0,0 +1,56 @@
+//CD74HC4067 Library based on bildr code: "http://bildr.org/2011/02/cd74hc4067-arduino/"
+//Ported to mbed by Scott M. O'Hair
+//
+//sample code:
+/*
+#include "mbed.h"
+#include "CD74HC4067.h"
+
+Serial pc(PB_6, PA_10, 115200); //tx, rx 
+
+CD74HC4067 mux1(D8, D9, D12, D11, A0); //S0, S1, S2, S3, SIG
+ 
+int main()
+{
+    pc.printf("Running MUX test\n");
+    while(1) {
+        //Loop through and read all 16 values
+        for(int i = 0; i < 15; i++) {    
+            mux1.currentState = mux1.readMux(i);
+            if (fabs(mux1.portState[i] - mux1.currentState) > BTN_NOISE_THRES) { 
+                pc.printf("Value at channel %i is %f\n", i, mux1.currentState);
+                mux1.saveState(i, mux1.currentState);
+            }
+        }
+
+    }
+}
+*/
+
+
+
+#ifndef MBED_CD74HC4067_H
+#define MBED_CD74HC4067_H
+ 
+#include "mbed.h"
+ 
+#define BTN_NOISE_THRES   0.018f            //Define a threshold for the amount of analog change required
+
+class CD74HC4067 {
+public:
+    CD74HC4067(PinName S1, PinName S2, PinName S3, PinName S4, PinName SIG);
+    void saveState(uint8_t port, float state);
+    float readMux(uint8_t channel);
+    float currentState ;
+    float portState[16];
+  
+private:  
+    DigitalOut _S0;
+    DigitalOut _S1;
+    DigitalOut _S2;
+    DigitalOut _S3;
+    AnalogIn _SIG;    
+
+};
+ 
+#endif
\ No newline at end of file