[app]温度传感器测试程序

刚开始去读/dev/i2c-1, 但是在设置地址的时候,始终错误,返回-1, 所以最后还是用了sys接口

/*******************************************
 * Description: Read temperature sensor from LM75
 * Author:        Aaron.gao
 * Data:        20170524
 * Histroy:
 *                1. NULL
 * ****************************************/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include <stdlib.h>
#include <fcntl.h>
#include <termios.h>


/* main func */
int main(int argc, char **argv)
{
    int ret, fd;
    char buffer[10];
    int value;
    float temp = 0.0;

    while(1)
    {
        /* open device */
        fd = open("/sys/bus/i2c/devices/i2c-1/1-004f/hwmon/hwmon0/temp1_input", O_RDONLY);
        if( fd < 0)
        {
            printf("fd is %d
", fd);
            printf("open i2c device-lm75 failed!
");
            exit(1);
        }

        ret = read(fd, buffer, 10);
        if(ret < 0)
        {
            printf("read error
");
        }
        value = atoi(buffer);
        memset(buffer, 0, strlen(buffer));
        temp = value / 1000.0;
        printf("environment temperature is %.2lfC
", temp);
        sleep(2);
        
        close(fd);
    }
    
    return 0;
}
原文地址:https://www.cnblogs.com/aaronLinux/p/7009791.html