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

Dependencies:   mbed

Committer:
pwheels
Date:
Thu Mar 24 17:21:29 2011 +0000
Revision:
0:1193dbfe28e2

        

Who changed what in which revision?

UserRevisionLine numberNew 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 int ReadDS18B20(int portnum, uchar *SerialNum, float *Temp)
pwheels 0:1193dbfe28e2 53 {
pwheels 0:1193dbfe28e2 54 uchar rt=FALSE;
pwheels 0:1193dbfe28e2 55 uchar send_block[30],lastcrc8;
pwheels 0:1193dbfe28e2 56 int send_cnt, tsht, i;
pwheels 0:1193dbfe28e2 57 float tmp;
pwheels 0:1193dbfe28e2 58
pwheels 0:1193dbfe28e2 59 // set the device serial number to the counter device
pwheels 0:1193dbfe28e2 60 owSerialNum(portnum,SerialNum,FALSE);
pwheels 0:1193dbfe28e2 61
pwheels 0:1193dbfe28e2 62 // access the device
pwheels 0:1193dbfe28e2 63 if (owAccess(portnum)) {
pwheels 0:1193dbfe28e2 64 // send the convert command and start power delivery
pwheels 0:1193dbfe28e2 65 if (!owWriteBytePower(portnum,0x44))
pwheels 0:1193dbfe28e2 66 return FALSE;
pwheels 0:1193dbfe28e2 67
pwheels 0:1193dbfe28e2 68 // sleep for 1 second
pwheels 0:1193dbfe28e2 69 msDelay(1000);
pwheels 0:1193dbfe28e2 70
pwheels 0:1193dbfe28e2 71 // turn off the 1-Wire Net strong pull-up
pwheels 0:1193dbfe28e2 72 if (owLevel(portnum,MODE_NORMAL) != MODE_NORMAL)
pwheels 0:1193dbfe28e2 73 return FALSE;
pwheels 0:1193dbfe28e2 74
pwheels 0:1193dbfe28e2 75 // access the device
pwheels 0:1193dbfe28e2 76 if (owAccess(portnum)) {
pwheels 0:1193dbfe28e2 77 // create a block to send that reads the temperature
pwheels 0:1193dbfe28e2 78 // read scratchpad command
pwheels 0:1193dbfe28e2 79 send_cnt = 0;
pwheels 0:1193dbfe28e2 80 send_block[send_cnt++] = 0xBE;
pwheels 0:1193dbfe28e2 81 // now add the read bytes for data bytes and crc8
pwheels 0:1193dbfe28e2 82 for (i = 0; i < 9; i++)
pwheels 0:1193dbfe28e2 83 send_block[send_cnt++] = 0xFF;
pwheels 0:1193dbfe28e2 84
pwheels 0:1193dbfe28e2 85 // now send the block
pwheels 0:1193dbfe28e2 86 if (owBlock(portnum,FALSE,send_block,send_cnt)) {
pwheels 0:1193dbfe28e2 87 // initialize the CRC8
pwheels 0:1193dbfe28e2 88 setcrc8(portnum,0);
pwheels 0:1193dbfe28e2 89 // perform the CRC8 on the last 8 bytes of packet
pwheels 0:1193dbfe28e2 90 for (i = send_cnt - 9; i < send_cnt; i++)
pwheels 0:1193dbfe28e2 91 lastcrc8 = docrc8(portnum,send_block[i]);
pwheels 0:1193dbfe28e2 92
pwheels 0:1193dbfe28e2 93 // verify CRC8 is correct
pwheels 0:1193dbfe28e2 94 if (lastcrc8 == 0x00) {
pwheels 0:1193dbfe28e2 95 // calculate the high-res temperature
pwheels 0:1193dbfe28e2 96 tsht = send_block[2]*256 + send_block[1];
pwheels 0:1193dbfe28e2 97 tmp = (float)(tsht);
pwheels 0:1193dbfe28e2 98 tmp = tmp / 16;
pwheels 0:1193dbfe28e2 99 *Temp = tmp;
pwheels 0:1193dbfe28e2 100 // success
pwheels 0:1193dbfe28e2 101 rt = TRUE;
pwheels 0:1193dbfe28e2 102 }
pwheels 0:1193dbfe28e2 103 }
pwheels 0:1193dbfe28e2 104 }
pwheels 0:1193dbfe28e2 105 }
pwheels 0:1193dbfe28e2 106 // return the result flag rt
pwheels 0:1193dbfe28e2 107 return rt;
pwheels 0:1193dbfe28e2 108 }