Line No. | Rev | Author | Line |
---|---|---|---|
1 | 6 | kaklik | //***************************************************************************** |
2 | // File Name : ds18b20.c |
||
3 | // Title : Dallas 1-Wire DS18B20 Temperature Sensor Library |
||
4 | // Revision : 3 |
||
5 | // Notes : |
||
6 | // Target MCU : Atmel AVR series |
||
7 | // Editor Tabs : 4 |
||
8 | // |
||
9 | //***************************************************************************** |
||
10 | |||
11 | //----- Include Files --------------------------------------------------------- |
||
12 | #include "rprintf.h" // include rprintf function library |
||
13 | #include "dallas.h" // include dallas support |
||
14 | #include "ds18b20.h" // include ds18b20 support |
||
15 | |||
16 | void ds18b20Init(void) |
||
17 | { |
||
18 | // initialize the 1-wire |
||
19 | dallasInit(); |
||
20 | } |
||
21 | |||
22 | u08 ds18b20Setup(dallas_rom_id_T* rom_id, u08 resolution, s08 alarm_low, s08 alarm_high) |
||
23 | { |
||
24 | u08 error; |
||
25 | |||
26 | // check resolution |
||
27 | if ((resolution < DS18B20_RES_MIN) || (resolution > DS18B20_RES_MAX)) |
||
28 | return DALLAS_RESOLUTION_ERROR; |
||
29 | |||
30 | // check address |
||
31 | error = dallasAddressCheck(rom_id, DS18B20_FAMILY); |
||
32 | if (error != DALLAS_NO_ERROR) |
||
33 | return error; |
||
34 | |||
35 | // reset and select node |
||
36 | error = dallasMatchROM(rom_id); |
||
37 | if (error != DALLAS_NO_ERROR) |
||
38 | return error; |
||
39 | |||
40 | // starts writting at address 0x02, T_H |
||
41 | dallasWriteByte(DS18B20_WRITE_SCRATCHPAD); |
||
42 | dallasWriteByte(alarm_high); |
||
43 | dallasWriteByte(alarm_low); |
||
44 | |||
45 | // convert resolution to bitmask |
||
46 | // valid value are 9-12 encoded as 0-4, resolution stored in bits 5&6 and bits 0-4 are always one |
||
47 | resolution = ((resolution - 9) << 5) | 0x60; |
||
48 | dallasWriteByte(resolution); |
||
49 | |||
50 | // reset and select node |
||
51 | error = dallasMatchROM(rom_id); |
||
52 | if (error != DALLAS_NO_ERROR) |
||
53 | return error; |
||
54 | |||
55 | // start reading at address 0x00 |
||
56 | dallasWriteByte(DS18B20_READ_SCRATCHPAD); |
||
57 | |||
58 | dallasReadByte(); // 0x00, temp lsb |
||
59 | dallasReadByte(); // 0x01, temp msb |
||
60 | |||
61 | // verify the data |
||
62 | if ((s08)dallasReadByte() != alarm_high) // 0x02, alarm high |
||
63 | return DALLAS_VERIFY_ERROR; |
||
64 | if ((s08)dallasReadByte() != alarm_low) // 0x03, alarm low |
||
65 | return DALLAS_VERIFY_ERROR; |
||
66 | if (dallasReadByte() != resolution) // 0x04, resolution |
||
67 | return DALLAS_VERIFY_ERROR; |
||
68 | |||
69 | return DALLAS_NO_ERROR; |
||
70 | } |
||
71 | |||
72 | u08 ds18b20Start(dallas_rom_id_T* rom_id) |
||
73 | { |
||
74 | u08 error; |
||
75 | |||
76 | // check address |
||
77 | error = dallasAddressCheck(rom_id, DS18B20_FAMILY); |
||
78 | if (error != DALLAS_NO_ERROR) |
||
79 | return error; |
||
80 | |||
81 | // reset and select node |
||
82 | error = dallasMatchROM(rom_id); |
||
83 | if (error != DALLAS_NO_ERROR) |
||
84 | return error; |
||
85 | |||
86 | // send convert command |
||
87 | dallasWriteByte(DS18B20_CONVERT_TEMP); |
||
88 | |||
89 | return DALLAS_NO_ERROR; |
||
90 | } |
||
91 | |||
92 | /*------ DallasTempGetResult ------*/ |
||
93 | u08 ds18b20Result(dallas_rom_id_T* rom_id, u16 *result) |
||
94 | { |
||
95 | u08 error; |
||
96 | |||
97 | union int16_var_U |
||
98 | { |
||
99 | u16 i16; |
||
100 | u08 i08[2]; |
||
101 | } int16_var; |
||
102 | |||
103 | // check address |
||
104 | error = dallasAddressCheck(rom_id, DS18B20_FAMILY); |
||
105 | if (error != DALLAS_NO_ERROR) |
||
106 | return error; |
||
107 | |||
108 | // reset and select node |
||
109 | error = dallasMatchROM(rom_id); |
||
110 | if (error != DALLAS_NO_ERROR) |
||
111 | return error; |
||
112 | |||
113 | // send command |
||
114 | dallasWriteByte(DS18B20_READ_SCRATCHPAD); |
||
115 | |||
116 | // read results 1 byte at a time |
||
117 | int16_var.i08[0] = dallasReadByte(); |
||
118 | int16_var.i08[1] = dallasReadByte(); |
||
119 | *result = int16_var.i16; |
||
120 | |||
121 | return DALLAS_NO_ERROR; |
||
122 | } |
||
123 | |||
124 | u08 ds18b20StartAndResult(dallas_rom_id_T* rom_id, u16 *result) |
||
125 | { |
||
126 | u08 error; |
||
127 | |||
128 | // start |
||
129 | error = ds18b20Start(rom_id); |
||
130 | if(error != DALLAS_NO_ERROR) |
||
131 | return error; |
||
132 | |||
133 | // wait |
||
134 | dallasWaitUntilDone(); |
||
135 | |||
136 | // return any errors - results passed by reference |
||
137 | return ds18b20Result(rom_id,result); |
||
138 | } |
||
139 | |||
140 | void ds18b20Print(u16 result, u08 resolution) |
||
141 | { |
||
142 | // print raw value |
||
143 | rprintfProgStrM(" 0x"); |
||
144 | rprintfu16(result); |
||
145 | rprintfChar(' '); |
||
146 | |||
147 | // print real temp |
||
148 | rprintfNum(10, 4, TRUE , ' ', result>>4); |
||
149 | rprintf("."); |
||
150 | rprintfNum(10, 4, FALSE, '0', (10000*((u32)(result&0x000F)))/16 ); |
||
151 | rprintfProgStrM(" C"); |
||
152 | } |
||
153 | |||
154 | /* OLD VERSION |
||
155 | |||
156 | void ds18b20Print(u16 result, u08 resolution) |
||
157 | { |
||
158 | u08 numerator; |
||
159 | |||
160 | // print header |
||
161 | rprintfProgStrM(" 0x"); |
||
162 | rprintfu16(result); |
||
163 | rprintfChar(' '); |
||
164 | |||
165 | // extract fractional part |
||
166 | numerator = result & 0x000F; |
||
167 | |||
168 | // extract integer part |
||
169 | result >>= 4; |
||
170 | |||
171 | // deal with negative numbers |
||
172 | if ((s08)result < 0) |
||
173 | { |
||
174 | // for negative fractions |
||
175 | if (numerator) |
||
176 | { |
||
177 | numerator = 16-numerator; // fractions are reversed |
||
178 | ++result; // the integer is one more than 2's complement |
||
179 | if (!(s08)result) // between -1 and 0 |
||
180 | rprintfChar('-'); // we have to take care of the negative sign |
||
181 | } |
||
182 | } |
||
183 | |||
184 | // print integer part (signed) |
||
185 | rprintfNum(10,4,1,'0',(s08)result); |
||
186 | rprintfChar(' '); |
||
187 | |||
188 | //Print fractional part. use numerator for numerator, resolution for denomiator |
||
189 | // bit resolution setup mask degree resolution |
||
190 | // -------------- ---------- ----------------- |
||
191 | // 9 0x00 1/2 |
||
192 | // 10 0x20 1/4 |
||
193 | // 11 0x40 1/8 |
||
194 | // 12 0x60 1/16 |
||
195 | numerator >>= (12-resolution); // scale fractional part of result to resolution |
||
196 | resolution = 2 << (resolution - 9); // create denominator as 2^(res-9) |
||
197 | rprintfNum(10,3,0,0,numerator); |
||
198 | rprintfChar('/'); |
||
199 | rprintfNum(10,3,0,0,resolution); |
||
200 | } |
||
201 | */ |
Powered by WebSVN v2.8.3