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

时间:2014-03-08 02:42:46

标签: c

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

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

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 我希望有人可以帮助我看看它有什么问题。谢谢!

2 个答案:

答案 0 :(得分:1)

引用数据表:“选择通道后,在I2C总线上发出停止条件后,通道将变为活动状态。”但是,看起来您没有在命令发送到多路复用器之后发送STOP,而是使用重复START。因此,多路复用器在您调用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(); }

答案 1 :(得分:0)

![奴隶地址固定为前4个] [1] [1]:http://i.stack.imgur.com/aEplE.jpg

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