从多路复用器中选择通道以在c编程中显示温度值

从多路复用器中选择通道以在c编程中显示温度值,c,C,我试图从共享同一地址的两个传感器接收一些温度读数,当我使用一个传感器时,它可以完美工作。然而,当我试图将第二个传感器安装到电路上并编辑程序abit时,它不起作用 以下是传感器读数时的代码: void readSensor(void) { int ch; TWI_init_master(); //TWI_start(); for (ch=0; ch<2; ch++) { TWI_start(); TWI_write_address(0xE0); // set multip

我试图从共享同一地址的两个传感器接收一些温度读数,当我使用一个传感器时,它可以完美工作。然而,当我试图将第二个传感器安装到电路上并编辑程序abit时,它不起作用

以下是传感器读数时的代码:

void readSensor(void) {
int ch;
TWI_init_master();
//TWI_start();
for (ch=0; ch<2; ch++) 
{

    TWI_start();
    TWI_write_address(0xE0); // set multiplixer
    TWI_write_data(0x00); // disable all ch
    TWI_write_data(0x01); // enable  ch-ch+1
    TWI_repeated_start();// restart
    TWI_write_address(0x14);// writing to sensor
    TWI_write_data(0x4C);// asking for data
    TWI_repeated_start();// restart
    TWI_read_address(0x15);// read

    /*For ch 2 to read
    TWI_write_data(ch+1); // enable  ch
    TWI_repeated_start();// restart
    TWI_write_data (0xE1); // enable read
    TWI_write_address(0x14);// writing to sensor
    TWI_write_data(0x4C);// asking for data
    TWI_repeated_start(); //restart
    TWI_read_address(0x15);// read
    */


    if (ch==0)
    {
        for(i = 0; i < 35; i++) {
        TWI_read_data();//geting data
        readbuff[i] = recv_data;
        }

        if(!(D6T_checkPEC(readbuff, 34))) {
            writeChar(0x50, USB);
            writeChar(0x50, USB);
            writeChar(0x50, USB);
        }
        else
        {
            for(i = 2; i < 34; i++) 
            {

                temp = readbuff[i];
                writeChar(temp, USB);
                delay10ms(10);
            }                       
        }
    }
    else if (ch==1) 
    {

        for(j = 36; j < 67; j++) {
            TWI_read_data();//geting data
            readbuff[j] = recv_data;
            }

        if(!D6T_checkPEC(readbuff, 66)) {
            writeChar(0x50, USB);
            writeChar(0x50, USB);
            writeChar(0x50, USB);
        }
        else
        {
            for(j = 2; j < 66; j++) 
            {
                temp = readbuff[j];
                writeChar(temp, USB);
                delay10ms(10);  
            }
        }
    }
    TWI_stop();
    delay10ms(10);  
    //TWI_repeated_start();// restart
}
TWI_stop(); }
void读取传感器(void){
int-ch;
TWI_init_master();
//TWI_start();
对于(ch=0;ch引用数据表:“当选择通道时,在I2C总线上设置停止条件后,通道变为激活状态。”但是,在向mux发送命令后,似乎没有发送STOP,而是使用了重复启动。因此,mux直到调用
TWI\u STOP
的最后才切换

您可能应该将
TWI\u repeated\u start
(在mux写入之后)替换为
TWI\u stop
,然后是
TWI\u start
。在传感器读取之后,您还需要
TWI\u stop

编辑:这是您的代码和我建议的更改

void readSensor(void) {
int ch;
TWI_init_master();
//TWI_start();
for (ch=0; ch<2; ch++) 
{

    TWI_start();
    TWI_write_address(0xE0); // set multiplixer
    TWI_write_data(0x00); // disable all ch
    TWI_write_data(0x01); // enable  ch-ch+1
    // **REMOVED** TWI_repeated_start();// restart
    TWI_stop();  // **NEW** Mux switches on STOP condition
    TWI_start(); // **NEW**
    TWI_write_address(0x14);// writing to sensor
    TWI_write_data(0x4C);// asking for data
    TWI_repeated_start();// restart
    TWI_read_address(0x15);// read

    /*For ch 2 to read
    TWI_write_data(ch+1); // enable  ch
    TWI_repeated_start();// restart
    TWI_write_data (0xE1); // enable read
    TWI_write_address(0x14);// writing to sensor
    TWI_write_data(0x4C);// asking for data
    TWI_repeated_start(); //restart
    TWI_read_address(0x15);// read
    */


    if (ch==0)
    {
        for(i = 0; i < 35; i++) {
            TWI_read_data();//geting data
            readbuff[i] = recv_data;
        }

        if(!(D6T_checkPEC(readbuff, 34))) {
            writeChar(0x50, USB);
            writeChar(0x50, USB);
            writeChar(0x50, USB);
        }
        else
        {
            for(i = 2; i < 34; i++) 
            {

                temp = readbuff[i];
                writeChar(temp, USB);
                delay10ms(10);
            }                       
        }
    }
    else if (ch==1) 
    {

        for(j = 36; j < 67; j++) {
            TWI_read_data();//geting data
            readbuff[j] = recv_data;
            }

        if(!D6T_checkPEC(readbuff, 66)) {
            writeChar(0x50, USB);
            writeChar(0x50, USB);
            writeChar(0x50, USB);
        }
        else
        {
            for(j = 2; j < 66; j++) 
            {
                temp = readbuff[j];
                writeChar(temp, USB);
                delay10ms(10);  
            }
        }
    }
    TWI_stop();
    delay10ms(10);  
    //TWI_repeated_start();// restart
}
TWI_stop(); }
void读取传感器(void){
int-ch;
TWI_init_master();
//TWI_start();

对于(ch=0;ch![第一个4][1][1]的从属地址是固定的]:


我从这里看到了图表。1110,我将A2,A1,A0设置为低(0)。

你的意思是TWI_start();TWI_write_address(0xE0);//设置多路复用器TWI_write_data(0);//禁用所有ch_write_data(ch+1);//启用ch ch+1 TWI_stop();TWI_start()//TWI_repeated_start();//重新启动TWI_write_address(0x14);//向传感器写入两次写入数据(0x4C);//请求两次重复的数据\u start();//重新启动两次读取地址(0x15);//Read正如您所看到的,注释中的代码基本上是不可读的。我已经编辑了我的答案,用我的更改来显示代码。是的,我已经按照您所说的做了,但仍然无法看到结果。我也不知道为什么。因为我的数据将在实时程序中显示。我唯一能想到的是电气。例如,你确定地址管脚都接地以提供E0地址吗?我已经展示了说明地址管脚的图表,管脚连接也说明A0-2接地。对不起,我没有其他东西。假设你的TWI_u函数按照它们的名称进行操作,并且你的原理图是正确的,我认为它应该可以工作。我建议重新设置打开您的问题(链接回此问题),并包括示意图。