SHT30 Linux标准 i2c-dev 读取程序

    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <string.h>
    #include <linux/i2c.h>
    #include <linux/i2c-dev.h>
    #include <unistd.h>
    #include <sys/time.h>
    #include <sys/ioctl.h>

    #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;
    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值