DS18b20 example
Dependencies: DS1820 mbed-STM32F103C8T6 mbed
main.cpp@5:0281a22f3cc7, 2018-11-09 (annotated)
- Committer:
- kezzab25
- Date:
- Fri Nov 09 04:28:18 2018 +0000
- Revision:
- 5:0281a22f3cc7
- Parent:
- 3:f483abe4bc57
Initial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 0:e069f9f26768 | 1 | #define MULTIPLE_PROBES |
Sissors | 2:d644c874ea0c | 2 | #define DATA_PIN A0 |
Sissors | 0:e069f9f26768 | 3 | |
Sissors | 0:e069f9f26768 | 4 | |
Sissors | 0:e069f9f26768 | 5 | #ifdef MULTIPLE_PROBES |
kezzab25 | 5:0281a22f3cc7 | 6 | #include "stm32f103c8t6.h" |
Sissors | 0:e069f9f26768 | 7 | #include "mbed.h" |
Sissors | 0:e069f9f26768 | 8 | #include "DS1820.h" |
Sissors | 0:e069f9f26768 | 9 | |
Sissors | 0:e069f9f26768 | 10 | #define MAX_PROBES 16 |
kezzab25 | 5:0281a22f3cc7 | 11 | |
Sissors | 0:e069f9f26768 | 12 | DS1820* probe[MAX_PROBES]; |
kezzab25 | 5:0281a22f3cc7 | 13 | |
kezzab25 | 5:0281a22f3cc7 | 14 | int main() |
kezzab25 | 5:0281a22f3cc7 | 15 | { |
Sissors | 0:e069f9f26768 | 16 | // Initialize the probe array to DS1820 objects |
Sissors | 0:e069f9f26768 | 17 | int num_devices = 0; |
Sissors | 0:e069f9f26768 | 18 | while(DS1820::unassignedProbe(DATA_PIN)) { |
Sissors | 0:e069f9f26768 | 19 | probe[num_devices] = new DS1820(DATA_PIN); |
Sissors | 0:e069f9f26768 | 20 | num_devices++; |
Sissors | 0:e069f9f26768 | 21 | if (num_devices == MAX_PROBES) |
Sissors | 0:e069f9f26768 | 22 | break; |
Sissors | 0:e069f9f26768 | 23 | } |
kezzab25 | 5:0281a22f3cc7 | 24 | |
Sissors | 0:e069f9f26768 | 25 | printf("Found %d device(s)\r\n\n", num_devices); |
Sissors | 0:e069f9f26768 | 26 | while(1) { |
Sissors | 0:e069f9f26768 | 27 | probe[0]->convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready |
Sissors | 0:e069f9f26768 | 28 | for (int i = 0; i<num_devices; i++) |
Sissors | 0:e069f9f26768 | 29 | printf("Device %d returns %3.1foC\r\n", i, probe[i]->temperature()); |
Sissors | 0:e069f9f26768 | 30 | printf("\r\n"); |
Sissors | 0:e069f9f26768 | 31 | wait(1); |
Sissors | 0:e069f9f26768 | 32 | } |
kezzab25 | 5:0281a22f3cc7 | 33 | |
Sissors | 0:e069f9f26768 | 34 | } |
Sissors | 0:e069f9f26768 | 35 | |
Sissors | 0:e069f9f26768 | 36 | #else |
kezzab25 | 5:0281a22f3cc7 | 37 | #include "stm32f103c8t6.h" |
Sissors | 0:e069f9f26768 | 38 | #include "mbed.h" |
Sissors | 0:e069f9f26768 | 39 | #include "DS1820.h" |
kezzab25 | 5:0281a22f3cc7 | 40 | |
Sissors | 0:e069f9f26768 | 41 | DS1820 probe(DATA_PIN); |
kezzab25 | 5:0281a22f3cc7 | 42 | |
kezzab25 | 5:0281a22f3cc7 | 43 | int main() |
kezzab25 | 5:0281a22f3cc7 | 44 | { |
Sissors | 0:e069f9f26768 | 45 | while(1) { |
Sissors | 0:e069f9f26768 | 46 | probe.convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready |
Sissors | 0:e069f9f26768 | 47 | printf("It is %3.1foC\r\n", probe.temperature()); |
Sissors | 0:e069f9f26768 | 48 | wait(1); |
Sissors | 0:e069f9f26768 | 49 | } |
Sissors | 0:e069f9f26768 | 50 | } |
Sissors | 0:e069f9f26768 | 51 | |
Sissors | 0:e069f9f26768 | 52 | #endif |