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