zoukankan      html  css  js  c++  java
  • [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;
    }
  • 相关阅读:
    成功后的迷失
    让主参与的人生
    生命的见证
    谁偷走了我的安全感
    简单理解分页原理
    WebApp开发入门
    程序员必需知道的搜索引擎的搜索技巧
    提高网页打开速度技巧
    十分钟了解HTTP协议
    简单实现页面临摹设计图功能
  • 原文地址:https://www.cnblogs.com/aaronLinux/p/7009791.html
Copyright © 2011-2022 走看看