CD74HC4067 作用是选通一路对十六路模拟信号,更详细的说,根据芯片上 S0-S3 四个不同管脚的组合,让SIG管脚和C0-C15导通。因此,最常见的用法是用来测试模拟信号。比如,Arduino Uno上面只有6个模拟输入,用一个CD74HC4067可以多扩展出来16个,于是可以支持 6+16-1=21个模拟引脚。
这个芯片的使用方法非常简单,例如: S0-S3 分别是 0 0 0 0时,SIG就和 C0是导通的。因此,这里我做一个实验,将一些电阻串联起来,分别接在 C1 C3 C5 C9 C11 上面,然后测量换算每个Pin的电压.
//Mux control pins int s0 = 7; int s1 = 6; int s2 = 5; int s3 = 4; //Mux in "SIG" pin int SIG_pin = 0; void setup(){ pinMode(s0, OUTPUT); pinMode(s1, OUTPUT); pinMode(s2, OUTPUT); pinMode(s3, OUTPUT); digitalWrite(s0, LOW); digitalWrite(s1, LOW); digitalWrite(s2, LOW); digitalWrite(s3, LOW); Serial.begin(9600); } void loop(){ int v; //Loop through and read all 16 values //Reports back Value at channel 6 is: 346 for(int i = 0; i < 16; i ++){ Serial.print("Value at channel "); Serial.print(i); Serial.print(" is : "); v=readMux(i); Serial.println(v * 5.0 / 1024); } Serial.println(" "); delay(3000); } int readMux(int channel){ int controlPin[] = {s0, s1, s2, s3}; int 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(int i = 0; i < 4; i ++){ digitalWrite(controlPin[i], muxChannel[channel][i]); } //read the value at the SIG pin int val = analogRead(SIG_pin); //return the value return val; }
最终运行结果如下: 同时我使用万用表测量,两者相差在 0.02V左右,证明还是非常准确的.
除了当作模拟的扩展之外,这个芯片还可以用来控制 LED。在电路的世界里,即便最简单的芯片都可以玩出让人匪夷所思的效果。
参考:
1. http://bildr.org/2011/02/cd74hc4067-arduino/#
2. http://www.tigoe.com/pcomp/code/arduinowiring/540/