SHT30 Linux标准 i2c-dev 读取程序

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 

    #define I2C_DEV "/dev/i2c-2"

    int main(void){

        int tmp75Fd;
        int ret;
        unsigned char slaveAddr = 0x45;
        unsigned char buf[8] = {0};

        // 打开设备
        tmp75Fd = open(I2C_DEV, O_RDWR);
        if ( tmp75Fd < 0 ){
            printf("faile to open the i2c bus: %s.\n", I2C_DEV);
            return -1;
        }

        // 设置7位地址
        if ( ioctl(tmp75Fd, I2C_TENBIT, 0) < 0) {
            printf("faile to set bits.\n");
            return -1;
        }
        // 强制设置地址
        //if ( ioctl(tmp75Fd, I2C_SLAVE, 0x4c) < 0 ) {
        if ( ioctl(tmp75Fd, I2C_SLAVE_FORCE, slaveAddr) < 0 ) {
            perror("faile to set address.\n");
            return -1;
        }

        while ( 1 ) {
            // 配置tmp75控制器
            buf[0] = 0x2C;
            buf[1] = 0x0D;
            if ( write(tmp75Fd, buf, 2) != 2 ) {
                perror("faile to write config.\n");
                return -1;
            }

            //buf[0] = 0xE0;
            //buf[1] = 0x00;
            //if ( write(tmp75Fd, buf, 2) != 2 ) {
            //    perror("faile to write config.\n");
            //    return -1;
            //}

            buf[0] = 0;
            buf[1] = 0;
            if ( read(tmp75Fd, buf, 8) != 8 ) {
                perror("faile to read back configure data.\n");
                return -1;
            }    
            printf("tmp75 configure: 0x%x.0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x \n", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7] );
            usleep(500000);
        }

       // 貌似是多余的
        close(tmp75Fd);

        return 0;
    }

你可能感兴趣的:(SHT30 Linux标准 i2c-dev 读取程序)