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