Files
a2bfc/ow-temp.c
(no author) d8a3fd2194 added RTC functions
git-svn-id: https://svn.salfter.gotdns.org/svn/a2bfc/trunk@89 1b90f75b-8b96-4784-87c0-14078182fce6
2007-09-24 05:41:06 +00:00

41 lines
821 B
C

#include "ow-temp.h"
#include <errno.h>
short ow_temp_read(unsigned char* devid, unsigned char format)
{
unsigned char data[8];
short rawtemp;
unsigned char i;
if (!ow_temp_idcheck(devid))
{
errno=EINVAL;
return -1;
}
ow_selectdevice(devid);
ow_writebyte(68); // ask for a temperature reading
ow_wait(); // wait for operation to complete
ow_selectdevice(devid);
ow_writebyte(190); // retrieve the reading
for (i=0; i<8; i++)
data[i]=ow_readbyte();
rawtemp=(short)(data[0]+((int)data[1]<<8));
errno=0;
switch (format)
{
case OW_TEMP_FAHRENHEIT:
return (rawtemp*9+8)/80+32;
case OW_TEMP_CELSIUS:
return (rawtemp+8)>>4;
case OW_TEMP_RAW:
default:
return rawtemp;
}
}
unsigned char ow_temp_idcheck(unsigned char* devid)
{
return (devid[0]==40)?1:0;
}