zoukankan      html  css  js  c++  java
  • 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.
    ", 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;
        }
    
  • 相关阅读:
    C# 从需要登录的网站上抓取数据
    mysql
    Fiddler抓取https设置详解(图文)
    Handlebars块级Helpers
    SQL优化技巧
    MyBatis持久层框架使用总结
    网页刷新页面方法小结
    史上最全的程序猿面试资料
    ActiveMQ开发与简介
    Lex使用指南
  • 原文地址:https://www.cnblogs.com/chenfulin5/p/8809846.html
Copyright © 2011-2022 走看看