您的位置:首页 > 移动开发

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

2017-06-14 16:56 615 查看
刚开始去读/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\r\n", fd);
printf("open i2c device-lm75 failed!\r\n");
exit(1);
}

ret = read(fd, buffer, 10);
if(ret < 0)
{
printf("read error\r\n");
}
value = atoi(buffer);
memset(buffer, 0, strlen(buffer));
temp = value / 1000.0;
printf("environment temperature is %.2lfC\r\n", temp);
sleep(2);

close(fd);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: