CD74HC4067 library ported for mbed.

Committer:
scottohair
Date:
Sat Jan 20 10:52:09 2018 +0000
Revision:
0:7f158d69af5e
Library for the CDHC4067. Tested on NUCLEO-F401RE.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
scottohair 0:7f158d69af5e 1 //CD74HC4067 Library based on bildr code: "http://bildr.org/2011/02/cd74hc4067-arduino/"
scottohair 0:7f158d69af5e 2 //Ported to mbed by Scott M. O'Hair
scottohair 0:7f158d69af5e 3
scottohair 0:7f158d69af5e 4 #include "CD74HC4067.h"
scottohair 0:7f158d69af5e 5 #include "mbed.h"
scottohair 0:7f158d69af5e 6
scottohair 0:7f158d69af5e 7
scottohair 0:7f158d69af5e 8 CD74HC4067::CD74HC4067(PinName S1, PinName S2, PinName S3, PinName S4, PinName SIG) : _S0(S1), _S1(S2), _S2(S3), _S3(S4), _SIG(SIG)
scottohair 0:7f158d69af5e 9 {
scottohair 0:7f158d69af5e 10 //initialize port state to zero
scottohair 0:7f158d69af5e 11 for (uint8_t i = 0; i < 15; i++) {
scottohair 0:7f158d69af5e 12 portState[i] = 0.0;
scottohair 0:7f158d69af5e 13 }
scottohair 0:7f158d69af5e 14
scottohair 0:7f158d69af5e 15 //pull all pins low
scottohair 0:7f158d69af5e 16 _S0 = 0;
scottohair 0:7f158d69af5e 17 _S1 = 0;
scottohair 0:7f158d69af5e 18 _S2 = 0;
scottohair 0:7f158d69af5e 19 _S3 = 0;
scottohair 0:7f158d69af5e 20
scottohair 0:7f158d69af5e 21 }
scottohair 0:7f158d69af5e 22
scottohair 0:7f158d69af5e 23 void CD74HC4067::saveState(uint8_t port, float state)
scottohair 0:7f158d69af5e 24 {
scottohair 0:7f158d69af5e 25 this->portState[port] = state;
scottohair 0:7f158d69af5e 26 }
scottohair 0:7f158d69af5e 27
scottohair 0:7f158d69af5e 28 float CD74HC4067::readMux(uint8_t channel)
scottohair 0:7f158d69af5e 29 {
scottohair 0:7f158d69af5e 30 DigitalOut controlPin[] = {_S0, _S1, _S2, _S3};
scottohair 0:7f158d69af5e 31
scottohair 0:7f158d69af5e 32 uint8_t muxChannel[16][4]= {
scottohair 0:7f158d69af5e 33 {0,0,0,0}, //channel 0
scottohair 0:7f158d69af5e 34 {1,0,0,0}, //channel 1
scottohair 0:7f158d69af5e 35 {0,1,0,0}, //channel 2
scottohair 0:7f158d69af5e 36 {1,1,0,0}, //channel 3
scottohair 0:7f158d69af5e 37 {0,0,1,0}, //channel 4
scottohair 0:7f158d69af5e 38 {1,0,1,0}, //channel 5
scottohair 0:7f158d69af5e 39 {0,1,1,0}, //channel 6
scottohair 0:7f158d69af5e 40 {1,1,1,0}, //channel 7
scottohair 0:7f158d69af5e 41 {0,0,0,1}, //channel 8
scottohair 0:7f158d69af5e 42 {1,0,0,1}, //channel 9
scottohair 0:7f158d69af5e 43 {0,1,0,1}, //channel 10
scottohair 0:7f158d69af5e 44 {1,1,0,1}, //channel 11
scottohair 0:7f158d69af5e 45 {0,0,1,1}, //channel 12
scottohair 0:7f158d69af5e 46 {1,0,1,1}, //channel 13
scottohair 0:7f158d69af5e 47 {0,1,1,1}, //channel 14
scottohair 0:7f158d69af5e 48 {1,1,1,1} //channel 15
scottohair 0:7f158d69af5e 49 };
scottohair 0:7f158d69af5e 50
scottohair 0:7f158d69af5e 51 //loop through the 4 sig
scottohair 0:7f158d69af5e 52 for(uint8_t i = 0; i < 4; i++) {
scottohair 0:7f158d69af5e 53 controlPin[i] = muxChannel[channel][i];
scottohair 0:7f158d69af5e 54 }
scottohair 0:7f158d69af5e 55
scottohair 0:7f158d69af5e 56 //read the value at the SIG pin
scottohair 0:7f158d69af5e 57 float val = _SIG.read();
scottohair 0:7f158d69af5e 58
scottohair 0:7f158d69af5e 59 //return the value
scottohair 0:7f158d69af5e 60 return val;
scottohair 0:7f158d69af5e 61 }