SHT 30 Linux標準i 2 c-devリーダ

2079 ワード

    #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.
", I2C_DEV); return -1; } // 7 if ( ioctl(tmp75Fd, I2C_TENBIT, 0) < 0) { printf("faile to set bits.
"); return -1; } // //if ( ioctl(tmp75Fd, I2C_SLAVE, 0x4c) < 0 ) { if ( ioctl(tmp75Fd, I2C_SLAVE_FORCE, slaveAddr) < 0 ) { perror("faile to set address.
"); return -1; } while ( 1 ) { // tmp75 buf[0] = 0x2C; buf[1] = 0x0D; if ( write(tmp75Fd, buf, 2) != 2 ) { perror("faile to write config.
"); return -1; } //buf[0] = 0xE0; //buf[1] = 0x00; //if ( write(tmp75Fd, buf, 2) != 2 ) { // perror("faile to write config.
"); // return -1; //} buf[0] = 0; buf[1] = 0; if ( read(tmp75Fd, buf, 8) != 8 ) { perror("faile to read back configure data.
"); return -1; } printf("tmp75 configure: 0x%x.0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x
", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7] ); usleep(500000); } // close(tmp75Fd); return 0; }