| 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 |
|
|
// temp10.C - Module to read the DS1920/DS1820 - temperature measurement.
|
| 28 |
|
|
//
|
| 29 |
|
|
// ---------------------------------------------------------------------------
|
| 30 |
|
|
//
|
| 31 |
|
|
//
|
| 32 |
|
|
#include "ownet.h"
|
| 33 |
|
|
#include "temp10.h"
|
| 34 |
|
|
|
| 35 |
|
|
//----------------------------------------------------------------------
|
| 36 |
|
|
// Read the temperature of a DS1920/DS1820 (family code 0x10)
|
| 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 DS1920/DS1820 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 ReadTemperature10(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 |
|
|
float tmp,cr,cpc;
|
| 54 |
|
|
int power;
|
| 55 |
|
|
|
| 56 |
|
|
// set the device serial number to the counter device
|
| 57 |
|
|
owSerialNum(portnum,SerialNum,FALSE);
|
| 58 |
|
|
|
| 59 |
|
|
for (loop = 0; loop < 2; loop ++)
|
| 60 |
|
|
{
|
| 61 |
|
|
// check if the chip is connected to VDD
|
| 62 |
|
|
if (owAccess(portnum))
|
| 63 |
|
|
{
|
| 64 |
|
|
owWriteByte(portnum,0xB4);
|
| 65 |
|
|
power = owReadByte(portnum);
|
| 66 |
|
|
}
|
| 67 |
|
|
|
| 68 |
|
|
// access the device
|
| 69 |
|
|
if (owAccess(portnum))
|
| 70 |
|
|
{
|
| 71 |
|
|
// send the convert command and if nesessary start power delivery
|
| 72 |
|
|
if (power) {
|
| 73 |
|
|
if (!owWriteBytePower(portnum,0x44))
|
| 74 |
|
|
return FALSE;
|
| 75 |
|
|
} else {
|
| 76 |
|
|
if (!owWriteByte(portnum,0x44))
|
| 77 |
|
|
return FALSE;
|
| 78 |
|
|
}
|
| 79 |
|
|
|
| 80 |
|
|
// sleep for 1 second
|
| 81 |
|
|
msDelay(1000);
|
| 82 |
|
|
|
| 83 |
|
|
// turn off the 1-Wire Net strong pull-up
|
| 84 |
|
|
if (power) {
|
| 85 |
|
|
if (owLevel(portnum,MODE_NORMAL) != MODE_NORMAL)
|
| 86 |
|
|
return FALSE;
|
| 87 |
|
|
}
|
| 88 |
|
|
|
| 89 |
|
|
// access the device
|
| 90 |
|
|
if (owAccess(portnum))
|
| 91 |
|
|
{
|
| 92 |
|
|
// create a block to send that reads the temperature
|
| 93 |
|
|
// read scratchpad command
|
| 94 |
|
|
send_cnt = 0;
|
| 95 |
|
|
send_block[send_cnt++] = 0xBE;
|
| 96 |
|
|
// now add the read bytes for data bytes and crc8
|
| 97 |
|
|
for (i = 0; i < 9; i++)
|
| 98 |
|
|
send_block[send_cnt++] = 0xFF;
|
| 99 |
|
|
|
| 100 |
|
|
// now send the block
|
| 101 |
|
|
if (owBlock(portnum,FALSE,send_block,send_cnt))
|
| 102 |
|
|
{
|
| 103 |
|
|
// initialize the CRC8
|
| 104 |
|
|
setcrc8(portnum,0);
|
| 105 |
|
|
// perform the CRC8 on the last 8 bytes of packet
|
| 106 |
|
|
for (i = send_cnt - 9; i < send_cnt; i++)
|
| 107 |
|
|
lastcrc8 = docrc8(portnum,send_block[i]);
|
| 108 |
|
|
|
| 109 |
|
|
// verify CRC8 is correct
|
| 110 |
|
|
if (lastcrc8 == 0x00)
|
| 111 |
|
|
{
|
| 112 |
|
|
// calculate the high-res temperature
|
| 113 |
|
|
tsht = send_block[1]/2;
|
| 114 |
|
|
if (send_block[2] & 0x01)
|
| 115 |
|
|
tsht |= -128;
|
| 116 |
|
|
tmp = (float)(tsht);
|
| 117 |
|
|
cr = send_block[7];
|
| 118 |
|
|
cpc = send_block[8];
|
| 119 |
|
|
if (((cpc - cr) == 1) && (loop == 0))
|
| 120 |
|
|
continue;
|
| 121 |
|
|
if (cpc == 0)
|
| 122 |
|
|
return FALSE;
|
| 123 |
|
|
else
|
| 124 |
|
|
tmp = tmp - (float)0.25 + (cpc - cr)/cpc;
|
| 125 |
|
|
|
| 126 |
|
|
*Temp = tmp;
|
| 127 |
|
|
// success
|
| 128 |
|
|
rt = TRUE;
|
| 129 |
|
|
break;
|
| 130 |
|
|
}
|
| 131 |
|
|
}
|
| 132 |
|
|
}
|
| 133 |
|
|
}
|
| 134 |
|
|
|
| 135 |
|
|
}
|
| 136 |
|
|
|
| 137 |
|
|
// return the result flag rt
|
| 138 |
|
|
return rt;
|
| 139 |
|
|
}
|