LibDriver DS18B20
Loading...
Searching...
No Matches
driver_ds18b20_read_test.c
Go to the documentation of this file.
1
37
39
40static ds18b20_handle_t gs_handle;
41
50uint8_t ds18b20_read_test(uint32_t times)
51{
52 uint8_t res;
53 uint32_t i;
54 int16_t raw;
55 float temperature;
56 uint8_t rom[8];
57 ds18b20_info_t info;
58
59 /* link interface function */
70
71 /* get ds18b20 info */
72 res = ds18b20_info(&info);
73 if (res != 0)
74 {
75 ds18b20_interface_debug_print("ds18b20: get info failed.\n");
76
77 return 1;
78 }
79 else
80 {
81 /* print ds18b20 info */
82 ds18b20_interface_debug_print("ds18b20: chip is %s.\n", info.chip_name);
83 ds18b20_interface_debug_print("ds18b20: manufacturer is %s.\n", info.manufacturer_name);
84 ds18b20_interface_debug_print("ds18b20: interface is %s.\n", info.interface);
85 ds18b20_interface_debug_print("ds18b20: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
86 ds18b20_interface_debug_print("ds18b20: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
87 ds18b20_interface_debug_print("ds18b20: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
88 ds18b20_interface_debug_print("ds18b20: max current is %0.2fmA.\n", info.max_current_ma);
89 ds18b20_interface_debug_print("ds18b20: max temperature is %0.1fC.\n", info.temperature_max);
90 ds18b20_interface_debug_print("ds18b20: min temperature is %0.1fC.\n", info.temperature_min);
91 }
92
93 /* ds18b20 init */
94 res = ds18b20_init(&gs_handle);
95 if (res != 0)
96 {
97 ds18b20_interface_debug_print("ds18b20: init failed.\n");
98
99 return 1;
100 }
101
102 /* start basic read test */
103 ds18b20_interface_debug_print("ds18b20: start basic read test.\n");
104
105 /* skip rom mode */
106 res = ds18b20_set_mode(&gs_handle, DS18B20_MODE_SKIP_ROM);
107 if (res != 0)
108 {
109 ds18b20_interface_debug_print("ds18b20: set mode failed.\n");
110 (void)ds18b20_deinit(&gs_handle);
111
112 return 1;
113 }
114 ds18b20_interface_debug_print("ds18b20: set skip rom.\n");
115
116 /* RESOLUTION_9BIT */
118 if (res != 0)
119 {
120 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution failed.\n");
121 (void)ds18b20_deinit(&gs_handle);
122
123 return 1;
124 }
125 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution 9bit.\n");
126 for (i = 0; i < times; i++)
127 {
128 res = ds18b20_read(&gs_handle, (int16_t *)&raw, (float *)&temperature);
129 if (res != 0)
130 {
131 ds18b20_interface_debug_print("ds18b20: read failed.\n");
132 (void)ds18b20_deinit(&gs_handle);
133
134 return 1;
135 }
136 ds18b20_interface_debug_print("ds18b20: temperature: %.01fC.\n", temperature);
138 }
139
140 /* RESOLUTION_10BIT */
142 if (res != 0)
143 {
144 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution failed.\n");
145 (void)ds18b20_deinit(&gs_handle);
146
147 return 1;
148 }
149 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution 10bit.\n");
150 for (i = 0; i < times; i++)
151 {
152 res = ds18b20_read(&gs_handle, (int16_t *)&raw, (float *)&temperature);
153 if (res != 0)
154 {
155 ds18b20_interface_debug_print("ds18b20: read failed.\n");
156 (void)ds18b20_deinit(&gs_handle);
157
158 return 1;
159 }
160 ds18b20_interface_debug_print("ds18b20: temperature: %.01fC.\n", temperature);
162 }
163
164 /* RESOLUTION_11BIT */
166 if (res != 0)
167 {
168 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution failed.\n");
169 (void)ds18b20_deinit(&gs_handle);
170
171 return 1;
172 }
173 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution 11bit.\n");
174 for (i = 0; i < times; i++)
175 {
176 res = ds18b20_read(&gs_handle, (int16_t *)&raw, (float *)&temperature);
177 if (res != 0)
178 {
179 ds18b20_interface_debug_print("ds18b20: read failed.\n");
180 (void)ds18b20_deinit(&gs_handle);
181
182 return 1;
183 }
184 ds18b20_interface_debug_print("ds18b20: temperature: %.01fC.\n", temperature);
186 }
187
188 /* RESOLUTION_12BIT */
190 if (res != 0)
191 {
192 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution failed.\n");
193 (void)ds18b20_deinit(&gs_handle);
194
195 return 1;
196 }
197 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution 12bit.\n");
198 for (i = 0; i < times; i++)
199 {
200 res = ds18b20_read(&gs_handle, (int16_t *)&raw, (float *)&temperature);
201 if (res != 0)
202 {
203 ds18b20_interface_debug_print("ds18b20: read failed.\n");
204 (void)ds18b20_deinit(&gs_handle);
205
206 return 1;
207 }
208 ds18b20_interface_debug_print("ds18b20: temperature: %.01fC.\n", temperature);
210 }
211
212 /* get rom */
213 res = ds18b20_get_rom(&gs_handle, (uint8_t *)rom);
214 if (res != 0)
215 {
216 ds18b20_interface_debug_print("ds18b20: get rom failed.\n");
217 (void)ds18b20_deinit(&gs_handle);
218
219 return 1;
220 }
221 res = ds18b20_set_rom(&gs_handle, (uint8_t *)rom);
222 if (res != 0)
223 {
224 ds18b20_interface_debug_print("ds18b20: set mode failed.\n");
225 (void)ds18b20_deinit(&gs_handle);
226
227 return 1;
228 }
229
230 /* set match rom mode */
231 res = ds18b20_set_mode(&gs_handle, DS18B20_MODE_MATCH_ROM);
232 if (res != 0)
233 {
234 ds18b20_interface_debug_print("ds18b20: set mode failed.\n");
235 (void)ds18b20_deinit(&gs_handle);
236
237 return 1;
238 }
239 ds18b20_interface_debug_print("ds18b20: set match rom.\n");
240
241 /* RESOLUTION_9BIT */
243 if (res != 0)
244 {
245 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution failed.\n");
246 (void)ds18b20_deinit(&gs_handle);
247
248 return 1;
249 }
250 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution 9bit.\n");
251 for (i = 0; i < times; i++)
252 {
253 res = ds18b20_read(&gs_handle, (int16_t *)&raw, (float *)&temperature);
254 if (res != 0)
255 {
256 ds18b20_interface_debug_print("ds18b20: read failed.\n");
257 (void)ds18b20_deinit(&gs_handle);
258
259 return 1;
260 }
261 ds18b20_interface_debug_print("ds18b20: temperature: %.01fC.\n", temperature);
263 }
264
265 /* RESOLUTION_10BIT */
267 if (res != 0)
268 {
269 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution failed.\n");
270 (void)ds18b20_deinit(&gs_handle);
271
272 return 1;
273 }
274 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution 10bit.\n");
275 for (i = 0; i < times; i++)
276 {
277 res = ds18b20_read(&gs_handle, (int16_t *)&raw, (float *)&temperature);
278 if (res != 0)
279 {
280 ds18b20_interface_debug_print("ds18b20: read failed.\n");
281 (void)ds18b20_deinit(&gs_handle);
282
283 return 1;
284 }
285 ds18b20_interface_debug_print("ds18b20: temperature: %.01fC.\n", temperature);
287 }
288
289 /* RESOLUTION_11BIT */
291 if (res != 0)
292 {
293 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution failed.\n");
294 (void)ds18b20_deinit(&gs_handle);
295
296 return 1;
297 }
298 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution 11bit.\n");
299 for (i = 0; i < times; i++)
300 {
301 res = ds18b20_read(&gs_handle, (int16_t *)&raw, (float *)&temperature);
302 if (res != 0)
303 {
304 ds18b20_interface_debug_print("ds18b20: read failed.\n");
305 (void)ds18b20_deinit(&gs_handle);
306
307 return 1;
308 }
309 ds18b20_interface_debug_print("ds18b20: temperature: %.01fC.\n", temperature);
311 }
312
313 /* RESOLUTION_12BIT */
315 if (res != 0)
316 {
317 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution failed.\n");
318 (void)ds18b20_deinit(&gs_handle);
319
320 return 1;
321 }
322 ds18b20_interface_debug_print("ds18b20: scratchpad set resolution 12bit.\n");
323 for (i = 0; i < times; i++)
324 {
325 res = ds18b20_read(&gs_handle, (int16_t *)&raw, (float *)&temperature);
326 if (res != 0)
327 {
328 ds18b20_interface_debug_print("ds18b20: read failed.\n");
329 (void)ds18b20_deinit(&gs_handle);
330
331 return 1;
332 }
333 ds18b20_interface_debug_print("ds18b20: temperature: %.01fC.\n", temperature);
335 }
336
337 /* finish read test */
338 ds18b20_interface_debug_print("ds18b20: finish read test.\n");
339 (void)ds18b20_deinit(&gs_handle);
340
341 return 0;
342}
driver ds18b20 read test header file
uint8_t ds18b20_init(ds18b20_handle_t *handle)
initialize the chip
uint8_t ds18b20_set_rom(ds18b20_handle_t *handle, uint8_t rom[8])
set the handle rom
uint8_t ds18b20_info(ds18b20_info_t *info)
get chip's information
uint8_t ds18b20_read(ds18b20_handle_t *handle, int16_t *raw, float *temp)
read data from the chip
uint8_t ds18b20_scratchpad_set_resolution(ds18b20_handle_t *handle, ds18b20_resolution_t resolution)
set the resolution in the scratchpad
struct ds18b20_handle_s ds18b20_handle_t
ds18b20 handle structure definition
uint8_t ds18b20_deinit(ds18b20_handle_t *handle)
close the chip
uint8_t ds18b20_set_mode(ds18b20_handle_t *handle, ds18b20_mode_t mode)
set the chip mode
struct ds18b20_info_s ds18b20_info_t
ds18b20 info structure definition
uint8_t ds18b20_get_rom(ds18b20_handle_t *handle, uint8_t rom[8])
get the chip rom
@ DS18B20_RESOLUTION_11BIT
@ DS18B20_RESOLUTION_9BIT
@ DS18B20_RESOLUTION_10BIT
@ DS18B20_RESOLUTION_12BIT
@ DS18B20_MODE_SKIP_ROM
@ DS18B20_MODE_MATCH_ROM
uint8_t ds18b20_interface_deinit(void)
interface bus deinit
void ds18b20_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t ds18b20_interface_write(uint8_t value)
interface bus write
uint8_t ds18b20_interface_init(void)
interface bus init
uint8_t ds18b20_interface_read(uint8_t *value)
interface bus read
void ds18b20_interface_debug_print(const char *const fmt,...)
interface print format data
void ds18b20_interface_disable_irq(void)
interface disable the interrupt
void ds18b20_interface_enable_irq(void)
interface enable the interrupt
void ds18b20_interface_delay_us(uint32_t us)
interface delay us
uint8_t ds18b20_read_test(uint32_t times)
read test
uint32_t driver_version
char manufacturer_name[32]