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