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.
owTemp.cpp@1:dcf2f8359398, 2010-12-08 (annotated)
- Committer:
- svlach
- Date:
- Wed Dec 08 21:23:21 2010 +0000
- Revision:
- 1:dcf2f8359398
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
svlach | 1:dcf2f8359398 | 1 | //--------------------------------------------------------------------------- |
svlach | 1:dcf2f8359398 | 2 | // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. |
svlach | 1:dcf2f8359398 | 3 | // |
svlach | 1:dcf2f8359398 | 4 | // |
svlach | 1:dcf2f8359398 | 5 | // Permission is hereby granted, free of charge, to any person obtaining a |
svlach | 1:dcf2f8359398 | 6 | // copy of this software and associated documentation files (the "Software"), |
svlach | 1:dcf2f8359398 | 7 | // to deal in the Software without restriction, including without limitation |
svlach | 1:dcf2f8359398 | 8 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
svlach | 1:dcf2f8359398 | 9 | // and/or sell copies of the Software, and to permit persons to whom the |
svlach | 1:dcf2f8359398 | 10 | // Software is furnished to do so, subject to the following conditions: |
svlach | 1:dcf2f8359398 | 11 | // |
svlach | 1:dcf2f8359398 | 12 | // The above copyright notice and this permission notice shall be included |
svlach | 1:dcf2f8359398 | 13 | // in all copies or substantial portions of the Software. |
svlach | 1:dcf2f8359398 | 14 | // |
svlach | 1:dcf2f8359398 | 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
svlach | 1:dcf2f8359398 | 16 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
svlach | 1:dcf2f8359398 | 17 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
svlach | 1:dcf2f8359398 | 18 | // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES |
svlach | 1:dcf2f8359398 | 19 | // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
svlach | 1:dcf2f8359398 | 20 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
svlach | 1:dcf2f8359398 | 21 | // OTHER DEALINGS IN THE SOFTWARE. |
svlach | 1:dcf2f8359398 | 22 | // |
svlach | 1:dcf2f8359398 | 23 | // Except as contained in this notice, the name of Dallas Semiconductor |
svlach | 1:dcf2f8359398 | 24 | // shall not be used except as stated in the Dallas Semiconductor |
svlach | 1:dcf2f8359398 | 25 | // Branding Policy. |
svlach | 1:dcf2f8359398 | 26 | //--------------------------------------------------------------------------- |
svlach | 1:dcf2f8359398 | 27 | // |
svlach | 1:dcf2f8359398 | 28 | // temp.c - Application to find and read the 1-Wire Net |
svlach | 1:dcf2f8359398 | 29 | // DS1920/DS1820/DS18S20 - temperature measurement. |
svlach | 1:dcf2f8359398 | 30 | // |
svlach | 1:dcf2f8359398 | 31 | // This application uses the files from the 'Public Domain' |
svlach | 1:dcf2f8359398 | 32 | // 1-Wire Net libraries ('general' and 'userial'). |
svlach | 1:dcf2f8359398 | 33 | // |
svlach | 1:dcf2f8359398 | 34 | // |
svlach | 1:dcf2f8359398 | 35 | // Version: 2.00 |
svlach | 1:dcf2f8359398 | 36 | // |
svlach | 1:dcf2f8359398 | 37 | |
svlach | 1:dcf2f8359398 | 38 | #include <stdlib.h> |
svlach | 1:dcf2f8359398 | 39 | #include <stdio.h> |
svlach | 1:dcf2f8359398 | 40 | #include "owTemp.h" |
svlach | 1:dcf2f8359398 | 41 | #include "ownet.h" |
svlach | 1:dcf2f8359398 | 42 | #include "readDS1920.h" |
svlach | 1:dcf2f8359398 | 43 | #include "findtype.h" |
svlach | 1:dcf2f8359398 | 44 | |
svlach | 1:dcf2f8359398 | 45 | // defines |
svlach | 1:dcf2f8359398 | 46 | #define MAXDEVICES 20 |
svlach | 1:dcf2f8359398 | 47 | |
svlach | 1:dcf2f8359398 | 48 | // global serial numbers |
svlach | 1:dcf2f8359398 | 49 | uchar FamilySN[MAXDEVICES][8]; |
svlach | 1:dcf2f8359398 | 50 | |
svlach | 1:dcf2f8359398 | 51 | // variables |
svlach | 1:dcf2f8359398 | 52 | int family_code; |
svlach | 1:dcf2f8359398 | 53 | |
svlach | 1:dcf2f8359398 | 54 | |
svlach | 1:dcf2f8359398 | 55 | |
svlach | 1:dcf2f8359398 | 56 | owTemp::owTemp () |
svlach | 1:dcf2f8359398 | 57 | { |
svlach | 1:dcf2f8359398 | 58 | |
svlach | 1:dcf2f8359398 | 59 | |
svlach | 1:dcf2f8359398 | 60 | } |
svlach | 1:dcf2f8359398 | 61 | |
svlach | 1:dcf2f8359398 | 62 | |
svlach | 1:dcf2f8359398 | 63 | void PrintSerialNum(uchar* buffer) |
svlach | 1:dcf2f8359398 | 64 | { |
svlach | 1:dcf2f8359398 | 65 | int i; |
svlach | 1:dcf2f8359398 | 66 | for(i=7; i>=0; i--) |
svlach | 1:dcf2f8359398 | 67 | { |
svlach | 1:dcf2f8359398 | 68 | printf("%02X", buffer[i]); |
svlach | 1:dcf2f8359398 | 69 | } |
svlach | 1:dcf2f8359398 | 70 | } |
svlach | 1:dcf2f8359398 | 71 | |
svlach | 1:dcf2f8359398 | 72 | |
svlach | 1:dcf2f8359398 | 73 | //---------------------------------------------------------------------- |
svlach | 1:dcf2f8359398 | 74 | // Main Test for DS1920/DS1820 temperature measurement |
svlach | 1:dcf2f8359398 | 75 | // |
svlach | 1:dcf2f8359398 | 76 | void owTemp::Maingettemp( void ) |
svlach | 1:dcf2f8359398 | 77 | { |
svlach | 1:dcf2f8359398 | 78 | float current_temp; |
svlach | 1:dcf2f8359398 | 79 | int i = 0; |
svlach | 1:dcf2f8359398 | 80 | int NumDevices=0; |
svlach | 1:dcf2f8359398 | 81 | int portnum = 0; |
svlach | 1:dcf2f8359398 | 82 | |
svlach | 1:dcf2f8359398 | 83 | |
svlach | 1:dcf2f8359398 | 84 | //---------------------------------------- |
svlach | 1:dcf2f8359398 | 85 | // Introduction header |
svlach | 1:dcf2f8359398 | 86 | printf("\r\n/---------------------------------------------\r\n"); |
svlach | 1:dcf2f8359398 | 87 | printf(" Temperature application DS1920/DS1820 - Version 1.00 \r\n" |
svlach | 1:dcf2f8359398 | 88 | " The following is a test to exercise a DS1920/DS1820.\r\n" |
svlach | 1:dcf2f8359398 | 89 | " Temperature Find and Read from a: \r\n" |
svlach | 1:dcf2f8359398 | 90 | " DS1920/DS1820 (at least 1)\r\n\r\n"); |
svlach | 1:dcf2f8359398 | 91 | |
svlach | 1:dcf2f8359398 | 92 | printf(" Press any CTRL-C to stop this program.\r\n\r\n"); |
svlach | 1:dcf2f8359398 | 93 | printf(" Output [Serial Number(s) ........ Temp1(F)] \r\n\r\n"); |
svlach | 1:dcf2f8359398 | 94 | |
svlach | 1:dcf2f8359398 | 95 | // attempt to acquire the 1-Wire Net |
svlach | 1:dcf2f8359398 | 96 | portnum = owAcquireEx("1"); |
svlach | 1:dcf2f8359398 | 97 | |
svlach | 1:dcf2f8359398 | 98 | // success |
svlach | 1:dcf2f8359398 | 99 | printf("Port opened: %i\r\n", portnum); |
svlach | 1:dcf2f8359398 | 100 | |
svlach | 1:dcf2f8359398 | 101 | // Find the device(s) |
svlach | 1:dcf2f8359398 | 102 | NumDevices = FindDevices(portnum, &FamilySN[0], 0x10, MAXDEVICES); |
svlach | 1:dcf2f8359398 | 103 | |
svlach | 1:dcf2f8359398 | 104 | if (NumDevices>0) |
svlach | 1:dcf2f8359398 | 105 | { |
svlach | 1:dcf2f8359398 | 106 | printf("\r\n"); |
svlach | 1:dcf2f8359398 | 107 | printf("Device(s) Found: \r\n"); |
svlach | 1:dcf2f8359398 | 108 | for (i = 0; i < NumDevices; i++) |
svlach | 1:dcf2f8359398 | 109 | { |
svlach | 1:dcf2f8359398 | 110 | PrintSerialNum(FamilySN[i]); |
svlach | 1:dcf2f8359398 | 111 | printf("\r\n"); |
svlach | 1:dcf2f8359398 | 112 | } |
svlach | 1:dcf2f8359398 | 113 | printf("\r\n\r\n"); |
svlach | 1:dcf2f8359398 | 114 | |
svlach | 1:dcf2f8359398 | 115 | while (1) |
svlach | 1:dcf2f8359398 | 116 | { |
svlach | 1:dcf2f8359398 | 117 | // read the temperature and print serial number and temperature |
svlach | 1:dcf2f8359398 | 118 | for (i = 0; i < NumDevices; i++) |
svlach | 1:dcf2f8359398 | 119 | { |
svlach | 1:dcf2f8359398 | 120 | if (ReadTemperature(portnum, FamilySN[i],¤t_temp)) |
svlach | 1:dcf2f8359398 | 121 | { |
svlach | 1:dcf2f8359398 | 122 | float far = current_temp * 9.0 / 5.0 + 32.0; |
svlach | 1:dcf2f8359398 | 123 | PrintSerialNum(FamilySN[i]); |
svlach | 1:dcf2f8359398 | 124 | printf(" %5.1f Degrees Farenheight\r\n", far); |
svlach | 1:dcf2f8359398 | 125 | // converting temperature from Celsius to Fahrenheit |
svlach | 1:dcf2f8359398 | 126 | } |
svlach | 1:dcf2f8359398 | 127 | else |
svlach | 1:dcf2f8359398 | 128 | printf(" Error reading temperature, verify device present:%d\r\n", |
svlach | 1:dcf2f8359398 | 129 | (int)owVerify(portnum, FALSE)); |
svlach | 1:dcf2f8359398 | 130 | } |
svlach | 1:dcf2f8359398 | 131 | printf("\r\n"); |
svlach | 1:dcf2f8359398 | 132 | } |
svlach | 1:dcf2f8359398 | 133 | |
svlach | 1:dcf2f8359398 | 134 | } |
svlach | 1:dcf2f8359398 | 135 | else |
svlach | 1:dcf2f8359398 | 136 | printf("\r\n\r\n\r\nERROR, device DS1920/DS1820 not found!\r\n"); |
svlach | 1:dcf2f8359398 | 137 | |
svlach | 1:dcf2f8359398 | 138 | // release the 1-Wire Net |
svlach | 1:dcf2f8359398 | 139 | owRelease(portnum); |
svlach | 1:dcf2f8359398 | 140 | } |
svlach | 1:dcf2f8359398 | 141 |