• linux tmp75 /dev/i2c-* 获取数据 demo


    /**********************************************************************
     *             linux tmp75 /dev/i2c-* 获取数据 demo
     * 说明:
     *     之前尝试过一次用这种方式来读EEPROM,结果以失败告终,也没找到原因,
     * 今天定位到问题是由于I2C_SLAVE、I2C_SLAVE_FORCE导致的,之前一直尝试
     * I2C_SLAVE,今天定位到问题是I2C总线忙,改成用I2C_SLAVE_FORCE就解决。
     * 还有就是测试程序的时候,竟然把不小心tmp75的连续转换给关了,导致获取到
     * 的数据总是固定的,一度怀疑人生。
     *
     *                                     2016-3-26 深圳 南山平山村 曾剑锋
     *********************************************************************/
    
    
    // 参考文章:
    //     1. MX6 i2C linux driver
    //          https://community.freescale.com/thread/315690
    //     2. Linux内核学习:I2C_SLAVE_FORCE
    //          http://m.blog.csdn.net/article/details?id=8245226
    //
    
    #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>
    
    #define I2C_DEV "/dev/i2c-3"
    
    int main(void){
    
        int tmp75Fd;
        int ret;
        unsigned char slaveAddr = 0x4c;
        unsigned char buf[4] = {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, 0x4c) < 0 ) {
            perror("faile to set address.
    ");
            return -1;
          }
    
        // 配置tmp75控制器
        buf[0] = 0x01;
        buf[1] =  (1 << 6) | (1 << 5);
        if ( write(tmp75Fd, buf, 2) != 2 ) {
            perror("faile to write config.
    ");
            return -1;
        }    
    
        // 读取tmp75控制器中的值,保证配置正确
        buf[0] = 1;
        if ( write(tmp75Fd, buf, 1) != 1 ) {
            perror("faile to write Pointer register.
    ");
            return -1;
        }
        buf[0] = 0;
        if ( read(tmp75Fd, buf, 1) != 1 ) {
            perror("faile to read back configure data.
    ");
            return -1;
        }
        printf("tmp75 configure: 0x%x.
    ", buf[0]);
    
    
        // 将tmp75内的寄存器指针指向地址0
        buf[0] = 0;
        if ( write(tmp75Fd, buf, 1) != 1 ) {
            perror("faile to write Pointer register.
    ");
            return -1;
        }
    
        // 循环读取温度数据
        buf[0] = 0;
        buf[1] = 0;
        while ( 1 ) {
    
            if ( read(tmp75Fd, buf, 2) != 2 ) {
                perror("faile to read data.
    ");
                return -1;
            }
            printf("tmp75 temperature: 0x%x%x.
    ", buf[0], buf[1]);
    
            usleep(500000);
        }
        
        // 貌似是多余的
        close(tmp75Fd);
    
        return 0;
    }
  • 相关阅读:
    没有被实例化的类 中的 非static成员函数竟然也可以被调用。。。前提是该成员函数没有用到成员变量
    c++注意
    关于类大小的小试验
    C语言|博客作业02
    在C#中进行时间和时间戳的转换
    正则表达式中匹配中括号 [ ]
    在C#中将对象序列化成Json格式
    在MSSQL中的简单数据类型递归
    HTML中padding和margin的区别和用法
    C#中的对称加密
  • 原文地址:https://www.cnblogs.com/zengjfgit/p/5323338.html
Copyright © 2020-2023  润新知