66 lines
1.5 KiB
C
66 lines
1.5 KiB
C
|
#include <fcntl.h>
|
|||
|
#include <stdio.h>
|
|||
|
#include <stdlib.h>
|
|||
|
#include <string.h>
|
|||
|
#include <unistd.h>
|
|||
|
#include <sys/select.h>
|
|||
|
|
|||
|
float getTempByDev(char *devName);
|
|||
|
|
|||
|
int main(int argc, char *argv[])
|
|||
|
{
|
|||
|
if (argc == 2)
|
|||
|
{
|
|||
|
char * ch = argv[1];
|
|||
|
// int gpio = atoi(ch);
|
|||
|
char * name = "/dev/ds18b20-";
|
|||
|
char devName[16];
|
|||
|
strcpy(devName, name);
|
|||
|
strcat(devName, ch);
|
|||
|
float t223 = getTempByDev(devName);
|
|||
|
printf("%.4f", t223);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
printf("-1");
|
|||
|
}
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
float getTempByDev(char *devName)
|
|||
|
{
|
|||
|
int fd;
|
|||
|
unsigned char result[2] = {0};
|
|||
|
unsigned int hightBitValue = 0;
|
|||
|
unsigned int lowBitValue = 0;
|
|||
|
float p = 0.0625;
|
|||
|
float value = 1024.0f;
|
|||
|
|
|||
|
fd = open(devName, 0);
|
|||
|
if (fd >= 0)
|
|||
|
{
|
|||
|
int i = read(fd, &result, sizeof(&result));
|
|||
|
if (i >= 0)
|
|||
|
{
|
|||
|
// printf("%xH-%xH\n", result[1], result[0]);
|
|||
|
hightBitValue = result[1];
|
|||
|
lowBitValue = result[0];
|
|||
|
hightBitValue <<= 8;
|
|||
|
hightBitValue = hightBitValue + lowBitValue;
|
|||
|
if ((result[1] & 0xf8))
|
|||
|
{
|
|||
|
// printf("aaa\n");
|
|||
|
hightBitValue = ~hightBitValue + 1;
|
|||
|
value = hightBitValue * p * -1;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
value = hightBitValue * p;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
close(fd);
|
|||
|
}
|
|||
|
return value;
|
|||
|
}
|