1 //---------------------------------------------------------------------------
2 // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
18 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
22 // Except as contained in this notice, the name of Dallas Semiconductor
23 // shall not be used except as stated in the Dallas Semiconductor
25 // ---------------------------------------------------------------------------
27 // temp10.C - Module to read the DS1920/DS1820 - temperature measurement.
31 // ---------------------------------------------------------------------------
37 //----------------------------------------------------------------------
38 // Read the temperature of a DS1920/DS1820
40 // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to
41 // OpenCOM to indicate the port number.
42 // 'SerialNum' - Serial Number of DS1920/DS1820 to read temperature from
43 // 'Temp ' - pointer to variable where that temperature will be
46 // Returns: TRUE(1) temperature has been read and verified
47 // FALSE(0) could not read the temperature, perhaps device is not
50 int ReadTemperature(int portnum, uchar *SerialNum, float *Temp)
53 uchar send_block[30],lastcrc8;
54 int send_cnt=0, tsht, i, loop=0;
60 // set the device serial number to the counter device
61 owSerialNum(portnum,SerialNum,FALSE);
63 for (loop = 0; rt==FALSE && loop < 2; loop ++)
66 if (owAccess(portnum))
68 // send the convert temperature command
69 owTouchByte(portnum,0x44);
71 // set the 1-Wire Net to strong pull-up
72 if (owLevel(portnum,MODE_STRONG5) != MODE_STRONG5)
78 // turn off the 1-Wire Net strong pull-up
79 if (owLevel(portnum,MODE_NORMAL) != MODE_NORMAL)
83 if (owAccess(portnum))
85 // create a block to send that reads the temperature
86 // read scratchpad command
87 send_block[send_cnt++] = 0xBE;
88 // now add the read bytes for data bytes and crc8
89 for (i = 0; i < 9; i++)
90 send_block[send_cnt++] = 0xFF;
93 if (owBlock(portnum,FALSE,send_block,send_cnt))
95 // perform the CRC8 on the last 8 bytes of packet
96 for (i = send_cnt - 9; i < send_cnt; i++)
97 lastcrc8 = docrc8(portnum,send_block[i]);
99 // verify CRC8 is correct
100 if (lastcrc8 == 0x00)
102 // calculate the high-res temperature
103 tsht = send_block[1]/2;
104 if (send_block[2] & 0x01)
109 if (((cpc - cr) == 1) && (loop == 0))
114 tmp = tmp - (float)0.25 + (cpc - cr)/cpc;
126 // return the result flag rt