我试图从共享相同地址的两个传感器接收一些温度读数,而当我使用一个传感器时,它可以正常工作。但是,当我尝试将第二个传感器固定到电路上并重新编辑程序时,它不起作用。

这是传感器读取时的代码:

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(); }


多路复用器数据表:http://www.ti.com/lit/ds/symlink/pca9548a.pdf
我希望有人可以帮助我看看问题出在哪里。谢谢!

最佳答案

引用数据表:“当选择一个通道时,在I2C总线上设置了停止条件后,该通道变为活动状态。”但是,看来您没有在向多路复用器发送命令后发送STOP,而是使用了重复启动。因此,多路复用器直到您调用TWI_stop的末尾才切换。

您可能应该将TWI_repeated_start(在多路复用器写入之后)替换为TWI_stop,然后是TWI_start。读取传感器后,您还需要TWI_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(); }

09-29 21:16