极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 25454|回复: 2

CD74HC4067 模块的使用

[复制链接]
发表于 2020-5-20 21:46:13 | 显示全部楼层 |阅读模式
本帖最后由 swhl 于 2020-5-20 21:47 编辑


  CD74HC4067 作用是选通一路对十六路模拟信号,就像一个单刀多掷开关,根据芯片上 S0-S3 四个不同管脚的组合,让SIG管脚和C0-C15导通(每次只能连接一个),它适用于数字和模拟信号,可以只用5针最多连接16传感器系统,也可以用它来管理多个设备。
//测量换算每个Pin的电压
这个芯片的使用方法非常简单,例如: S0-S3 分别是 0 0 0 0时,SIG就和 C0是导通的。因此,这里我做一个实验,将一些电阻串联起来,分别接在C1 C3 C5 C9 C11 上面,然后测量换算每个Pin的电压.
[kenrobot_code]//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, muxChannel[channel]);
        }

        //read the value at the SIG pin
        int val = analogRead(SIG_pin);

        //return the value
        return val;
}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-28 16:54 , Processed in 0.040900 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表