#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#define DS18B20 "/dev/ds18b20"
int main()
{
int fd;
char buf[2],temp[7];
unsigned char a=0;
unsigned int b=0;
unsigned int t=0;
unsigned char TempH,TempL;
if((fd = open(DS18B20, O_RDONLY)) < 0){
printf("error in open %s",DS18B20);
return -1;
}
read(fd,buf,0);
a=buf[0];
b=buf[1];
b <<= 8;
t = a + b;
if(t&0x8000) {
t = ~t;
t = t+1;
temp[0] = '-';
} else {
temp[0] = ' ';
}
TempH = t >> 4;
TempL = t & 0x0F;
TempL = TempL*6/10;
temp[1] = TempH/100 + '0';
temp[2] = (TempH%100)/10 + '0';
temp[3] = (TempH%100)%10 + '0';
temp[4] = '.';
temp[5] = (TempL & 0x0F)*6/10 + '0';
temp[6] = 0;
/*printf("%d %d\n",buf[1],buf[0]);*/
printf("the temprature is %s C.\n",temp);
close(fd);
}
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#define DS18B20 "/dev/ds18b20"
int main()
{
int fd;
char buf[2],temp[7];
unsigned char a=0;
unsigned int b=0;
unsigned int t=0;
unsigned char TempH,TempL;
if((fd = open(DS18B20, O_RDONLY)) < 0){
printf("error in open %s",DS18B20);
return -1;
}
read(fd,buf,0);
a=buf[0];
b=buf[1];
b <<= 8;
t = a + b;
if(t&0x8000) {
t = ~t;
t = t+1;
temp[0] = '-';
} else {
temp[0] = ' ';
}
TempH = t >> 4;
TempL = t & 0x0F;
TempL = TempL*6/10;
temp[1] = TempH/100 + '0';
temp[2] = (TempH%100)/10 + '0';
temp[3] = (TempH%100)%10 + '0';
temp[4] = '.';
temp[5] = (TempL & 0x0F)*6/10 + '0';
temp[6] = 0;
/*printf("%d %d\n",buf[1],buf[0]);*/
printf("the temprature is %s C.\n",temp);
close(fd);
}