Imported Upstream version 2.9.0
[debian/cc1111] / device / examples / ds390 / ow390 / temp10.c
1 //---------------------------------------------------------------------------
2 // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
3 // 
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:
10 // 
11 // The above copyright notice and this permission notice shall be included 
12 // in all copies or substantial portions of the Software.
13 // 
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.
21 // 
22 // Except as contained in this notice, the name of Dallas Semiconductor 
23 // shall not be used except as stated in the Dallas Semiconductor 
24 // Branding Policy. 
25 // ---------------------------------------------------------------------------
26 //
27 //  temp10.C - Module to read the DS1920/DS1820 - temperature measurement.
28 //  
29 //  Version: 2.00
30 //             
31 // ---------------------------------------------------------------------------
32 // 
33 //
34 #include "ownet.h"
35 #include "temp10.h"
36
37 //----------------------------------------------------------------------
38 // Read the temperature of a DS1920/DS1820
39 //
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 
44 //                 returned
45 //
46 // Returns: TRUE(1)  temperature has been read and verified
47 //          FALSE(0) could not read the temperature, perhaps device is not
48 //                   in contact
49 //
50 int ReadTemperature(int portnum, uchar *SerialNum, float *Temp)
51 {
52    int rt=FALSE;
53    uchar send_block[30],lastcrc8;
54    int send_cnt=0, tsht, i, loop=0;
55    float tmp,cr,cpc;
56    
57    
58    setcrc8(portnum,0);
59
60    // set the device serial number to the counter device
61    owSerialNum(portnum,SerialNum,FALSE);
62
63    for (loop = 0; rt==FALSE && loop < 2; loop ++)
64    {
65       // access the device 
66       if (owAccess(portnum))
67       {
68          // send the convert temperature command
69          owTouchByte(portnum,0x44);
70
71          // set the 1-Wire Net to strong pull-up
72          if (owLevel(portnum,MODE_STRONG5) != MODE_STRONG5)
73             return FALSE;
74  
75          // sleep for 1 second
76          msDelay(1000);
77
78          // turn off the 1-Wire Net strong pull-up
79          if (owLevel(portnum,MODE_NORMAL) != MODE_NORMAL)
80             return FALSE;
81
82          // access the device 
83          if (owAccess(portnum))
84          {
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;
91
92             // now send the block
93             if (owBlock(portnum,FALSE,send_block,send_cnt))
94             {
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]);
98
99                // verify CRC8 is correct
100                if (lastcrc8 == 0x00)
101                {
102                   // calculate the high-res temperature
103                   tsht = send_block[1]/2;
104                   if (send_block[2] & 0x01)
105                      tsht |= -128;
106                   tmp = (float)(tsht);
107                   cr = send_block[7];
108                   cpc = send_block[8];
109                   if (((cpc - cr) == 1) && (loop == 0))
110                      continue;
111                   if (cpc == 0)
112                      return FALSE;   
113                   else
114                      tmp = tmp - (float)0.25 + (cpc - cr)/cpc;
115    
116                   *Temp = tmp;
117                   // success
118                   rt = TRUE;
119                }
120             }
121          }
122       }
123         
124    }
125    
126  // return the result flag rt
127       return rt;
128       
129 }