OpenCores
URL https://opencores.org/ocsvn/sockit_owm/sockit_owm/trunk

Subversion Repositories sockit_owm

[/] [sockit_owm/] [trunk/] [HAL/] [src/] [temp28.c] - Blame information for rev 3

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 iztok
//---------------------------------------------------------------------------
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
//  temp28.C - Module to read the DS18B20 - temperature measurement.
28
//
29
// ---------------------------------------------------------------------------
30
//
31
//
32
#include "ownet.h"
33
#include "temp28.h"
34
 
35
//----------------------------------------------------------------------
36
// Read the temperature of a DS18B20 (family code 0x28)
37
//
38
// 'portnum'     - number 0 to MAX_PORTNUM-1.  This number was provided to
39
//                 OpenCOM to indicate the port number.
40
// 'SerialNum'   - Serial Number of DS18B20 to read temperature from
41
// 'Temp '       - pointer to variable where that temperature will be
42
//                 returned
43
//
44
// Returns: TRUE(1)  temperature has been read and verified
45
//          FALSE(0) could not read the temperature, perhaps device is not
46
//                   in contact
47
//
48
int ReadTemperature28(int portnum, uchar *SerialNum, float *Temp)
49
{
50
   uchar rt=FALSE;
51
   uchar send_block[30],lastcrc8;
52
   int send_cnt, tsht, i, loop=0;
53
   int power;
54
 
55
   // set the device serial number to the counter device
56
   owSerialNum(portnum,SerialNum,FALSE);
57
 
58
   for (loop = 0; loop < 2; loop ++)
59
   {
60
      // check if the chip is connected to VDD
61
      if (owAccess(portnum))
62
      {
63
         owWriteByte(portnum,0xB4);
64
         power = owReadByte(portnum);
65
      }
66
 
67
      // access the device
68
      if (owAccess(portnum))
69
      {
70
         // send the convert command and if nesessary start power delivery
71
         if (power) {
72
            if (!owWriteBytePower(portnum,0x44))
73
               return FALSE;
74
         } else {
75
            if (!owWriteByte(portnum,0x44))
76
               return FALSE;
77
         }
78
 
79
         // sleep for 1 second
80
         msDelay(1000);
81
 
82
         // turn off the 1-Wire Net strong pull-up
83
         if (power) {
84
            if (owLevel(portnum,MODE_NORMAL) != MODE_NORMAL)
85
               return FALSE;
86
         }
87
 
88
         // access the device
89
         if (owAccess(portnum))
90
         {
91
            // create a block to send that reads the temperature
92
            // read scratchpad command
93
            send_cnt = 0;
94
            send_block[send_cnt++] = 0xBE;
95
            // now add the read bytes for data bytes and crc8
96
            for (i = 0; i < 9; i++)
97
               send_block[send_cnt++] = 0xFF;
98
 
99
            // now send the block
100
            if (owBlock(portnum,FALSE,send_block,send_cnt))
101
            {
102
               // initialize the CRC8
103
               setcrc8(portnum,0);
104
               // perform the CRC8 on the last 8 bytes of packet
105
               for (i = send_cnt - 9; i < send_cnt; i++)
106
                  lastcrc8 = docrc8(portnum,send_block[i]);
107
 
108
               // verify CRC8 is correct
109
               if (lastcrc8 == 0x00)
110
               {
111
                  // calculate the high-res temperature
112
                  tsht =        send_block[2] << 8;
113
                  tsht = tsht | send_block[1];
114
                  if (tsht & 0x00001000)
115
                          tsht = tsht | 0xffff0000;
116
                  *Temp = ((float) tsht)/16;
117
                  // success
118
                  rt = TRUE;
119
                  break;
120
               }
121
            }
122
         }
123
      }
124
 
125
   }
126
 
127
   // return the result flag rt
128
   return rt;
129
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.