Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
readDS1920.cpp@0:70c3bea805ee, 2010-11-30 (annotated)
- Committer:
- svlach
- Date:
- Tue Nov 30 22:11:18 2010 +0000
- Revision:
- 0:70c3bea805ee
- Child:
- 1:dcf2f8359398
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
svlach | 0:70c3bea805ee | 1 | //--------------------------------------------------------------------------- |
svlach | 0:70c3bea805ee | 2 | // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. |
svlach | 0:70c3bea805ee | 3 | // |
svlach | 0:70c3bea805ee | 4 | // Permission is hereby granted, free of charge, to any person obtaining a |
svlach | 0:70c3bea805ee | 5 | // copy of this software and associated documentation files (the "Software"), |
svlach | 0:70c3bea805ee | 6 | // to deal in the Software without restriction, including without limitation |
svlach | 0:70c3bea805ee | 7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
svlach | 0:70c3bea805ee | 8 | // and/or sell copies of the Software, and to permit persons to whom the |
svlach | 0:70c3bea805ee | 9 | // Software is furnished to do so, subject to the following conditions: |
svlach | 0:70c3bea805ee | 10 | // |
svlach | 0:70c3bea805ee | 11 | // The above copyright notice and this permission notice shall be included |
svlach | 0:70c3bea805ee | 12 | // in all copies or substantial portions of the Software. |
svlach | 0:70c3bea805ee | 13 | // |
svlach | 0:70c3bea805ee | 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
svlach | 0:70c3bea805ee | 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
svlach | 0:70c3bea805ee | 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
svlach | 0:70c3bea805ee | 17 | // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES |
svlach | 0:70c3bea805ee | 18 | // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
svlach | 0:70c3bea805ee | 19 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
svlach | 0:70c3bea805ee | 20 | // OTHER DEALINGS IN THE SOFTWARE. |
svlach | 0:70c3bea805ee | 21 | // |
svlach | 0:70c3bea805ee | 22 | // Except as contained in this notice, the name of Dallas Semiconductor |
svlach | 0:70c3bea805ee | 23 | // shall not be used except as stated in the Dallas Semiconductor |
svlach | 0:70c3bea805ee | 24 | // Branding Policy. |
svlach | 0:70c3bea805ee | 25 | // --------------------------------------------------------------------------- |
svlach | 0:70c3bea805ee | 26 | // |
svlach | 0:70c3bea805ee | 27 | // temp10.C - Module to read the DS1920/DS1820 - temperature measurement. |
svlach | 0:70c3bea805ee | 28 | // |
svlach | 0:70c3bea805ee | 29 | // Version: 2.00 |
svlach | 0:70c3bea805ee | 30 | // |
svlach | 0:70c3bea805ee | 31 | // --------------------------------------------------------------------------- |
svlach | 0:70c3bea805ee | 32 | // |
svlach | 0:70c3bea805ee | 33 | // |
svlach | 0:70c3bea805ee | 34 | #include "./Headers/ownet.h" |
svlach | 0:70c3bea805ee | 35 | #include "./Headers/readDS1920.h" |
svlach | 0:70c3bea805ee | 36 | |
svlach | 0:70c3bea805ee | 37 | //---------------------------------------------------------------------- |
svlach | 0:70c3bea805ee | 38 | // Read the temperature of a DS1920/DS1820 |
svlach | 0:70c3bea805ee | 39 | // |
svlach | 0:70c3bea805ee | 40 | // 'portnum' - number 0 to MAX_PORTNUM-1. This number was provided to |
svlach | 0:70c3bea805ee | 41 | // OpenCOM to indicate the port number. |
svlach | 0:70c3bea805ee | 42 | // 'SerialNum' - Serial Number of DS1920/DS1820 to read temperature from |
svlach | 0:70c3bea805ee | 43 | // 'Temp ' - pointer to variable where that temperature will be |
svlach | 0:70c3bea805ee | 44 | // returned |
svlach | 0:70c3bea805ee | 45 | // |
svlach | 0:70c3bea805ee | 46 | // Returns: TRUE(1) temperature has been read and verified |
svlach | 0:70c3bea805ee | 47 | // FALSE(0) could not read the temperature, perhaps device is not |
svlach | 0:70c3bea805ee | 48 | // in contact |
svlach | 0:70c3bea805ee | 49 | // |
svlach | 0:70c3bea805ee | 50 | |
svlach | 0:70c3bea805ee | 51 | |
svlach | 0:70c3bea805ee | 52 | int ReadTemperature(int portnum, uchar *SerialNum, float *Temp) |
svlach | 0:70c3bea805ee | 53 | { |
svlach | 0:70c3bea805ee | 54 | uchar rt=FALSE; |
svlach | 0:70c3bea805ee | 55 | uchar send_block[30],lastcrc8; |
svlach | 0:70c3bea805ee | 56 | int send_cnt, tsht, i, loop=0; |
svlach | 0:70c3bea805ee | 57 | float tmp,cr,cpc; |
svlach | 0:70c3bea805ee | 58 | |
svlach | 0:70c3bea805ee | 59 | // set the device serial number to the counter device |
svlach | 0:70c3bea805ee | 60 | owSerialNum(portnum,SerialNum,FALSE); |
svlach | 0:70c3bea805ee | 61 | |
svlach | 0:70c3bea805ee | 62 | for (loop = 0; loop < 2; loop ++) |
svlach | 0:70c3bea805ee | 63 | { |
svlach | 0:70c3bea805ee | 64 | // access the device |
svlach | 0:70c3bea805ee | 65 | if (owAccess(portnum)) |
svlach | 0:70c3bea805ee | 66 | { |
svlach | 0:70c3bea805ee | 67 | // send the convert command and start power delivery |
svlach | 0:70c3bea805ee | 68 | if (!owWriteBytePower(portnum,0x44)) |
svlach | 0:70c3bea805ee | 69 | return FALSE; |
svlach | 0:70c3bea805ee | 70 | |
svlach | 0:70c3bea805ee | 71 | // sleep for 1 second |
svlach | 0:70c3bea805ee | 72 | msDelay(1000); |
svlach | 0:70c3bea805ee | 73 | |
svlach | 0:70c3bea805ee | 74 | // turn off the 1-Wire Net strong pull-up |
svlach | 0:70c3bea805ee | 75 | if (owLevel(portnum,MODE_NORMAL) != MODE_NORMAL) |
svlach | 0:70c3bea805ee | 76 | return FALSE; |
svlach | 0:70c3bea805ee | 77 | |
svlach | 0:70c3bea805ee | 78 | // access the device |
svlach | 0:70c3bea805ee | 79 | if (owAccess(portnum)) |
svlach | 0:70c3bea805ee | 80 | { |
svlach | 0:70c3bea805ee | 81 | // create a block to send that reads the temperature |
svlach | 0:70c3bea805ee | 82 | // read scratchpad command |
svlach | 0:70c3bea805ee | 83 | send_cnt = 0; |
svlach | 0:70c3bea805ee | 84 | send_block[send_cnt++] = 0xBE; |
svlach | 0:70c3bea805ee | 85 | // now add the read bytes for data bytes and crc8 |
svlach | 0:70c3bea805ee | 86 | for (i = 0; i < 9; i++) |
svlach | 0:70c3bea805ee | 87 | send_block[send_cnt++] = 0xFF; |
svlach | 0:70c3bea805ee | 88 | |
svlach | 0:70c3bea805ee | 89 | // now send the block |
svlach | 0:70c3bea805ee | 90 | if (owBlock(portnum,FALSE,send_block,send_cnt)) |
svlach | 0:70c3bea805ee | 91 | { |
svlach | 0:70c3bea805ee | 92 | // initialize the CRC8 |
svlach | 0:70c3bea805ee | 93 | setcrc8(portnum,0); |
svlach | 0:70c3bea805ee | 94 | // perform the CRC8 on the last 8 bytes of packet |
svlach | 0:70c3bea805ee | 95 | for (i = send_cnt - 9; i < send_cnt; i++) |
svlach | 0:70c3bea805ee | 96 | lastcrc8 = docrc8(portnum,send_block[i]); |
svlach | 0:70c3bea805ee | 97 | |
svlach | 0:70c3bea805ee | 98 | // verify CRC8 is correct |
svlach | 0:70c3bea805ee | 99 | if (lastcrc8 == 0x00) |
svlach | 0:70c3bea805ee | 100 | { |
svlach | 0:70c3bea805ee | 101 | // calculate the high-res temperature |
svlach | 0:70c3bea805ee | 102 | tsht = send_block[1]/2; |
svlach | 0:70c3bea805ee | 103 | if (send_block[2] & 0x01) |
svlach | 0:70c3bea805ee | 104 | tsht |= -128; |
svlach | 0:70c3bea805ee | 105 | tmp = (float)(tsht); |
svlach | 0:70c3bea805ee | 106 | cr = send_block[7]; |
svlach | 0:70c3bea805ee | 107 | cpc = send_block[8]; |
svlach | 0:70c3bea805ee | 108 | if (((cpc - cr) == 1) && (loop == 0)) |
svlach | 0:70c3bea805ee | 109 | continue; |
svlach | 0:70c3bea805ee | 110 | if (cpc == 0) |
svlach | 0:70c3bea805ee | 111 | return FALSE; |
svlach | 0:70c3bea805ee | 112 | else |
svlach | 0:70c3bea805ee | 113 | tmp = tmp - (float)0.25 + (cpc - cr)/cpc; |
svlach | 0:70c3bea805ee | 114 | |
svlach | 0:70c3bea805ee | 115 | *Temp = tmp; |
svlach | 0:70c3bea805ee | 116 | // success |
svlach | 0:70c3bea805ee | 117 | rt = TRUE; |
svlach | 0:70c3bea805ee | 118 | break; |
svlach | 0:70c3bea805ee | 119 | } |
svlach | 0:70c3bea805ee | 120 | } |
svlach | 0:70c3bea805ee | 121 | } |
svlach | 0:70c3bea805ee | 122 | } |
svlach | 0:70c3bea805ee | 123 | |
svlach | 0:70c3bea805ee | 124 | } |
svlach | 0:70c3bea805ee | 125 | |
svlach | 0:70c3bea805ee | 126 | // return the result flag rt |
svlach | 0:70c3bea805ee | 127 | return rt; |
svlach | 0:70c3bea805ee | 128 | } |