Example program for the DS1820 library. It searches for connected devices, reports their addresses, and then displays their temperatures.
Fork of DS1820_HelloWorld by
main.cpp@5:a870fb672587, 2017-03-09 (annotated)
- Committer:
- JLarkin
- Date:
- Thu Mar 09 04:04:16 2017 +0000
- Revision:
- 5:a870fb672587
- Parent:
- 3:f483abe4bc57
Example DS1820 program reports device IDs and then reports temps
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 0:e069f9f26768 | 1 | #define MULTIPLE_PROBES |
JLarkin | 5:a870fb672587 | 2 | #define DATA_PIN p25 |
Sissors | 0:e069f9f26768 | 3 | |
Sissors | 0:e069f9f26768 | 4 | |
Sissors | 0:e069f9f26768 | 5 | #ifdef MULTIPLE_PROBES |
Sissors | 0:e069f9f26768 | 6 | |
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 |
Sissors | 0:e069f9f26768 | 11 | |
Sissors | 0:e069f9f26768 | 12 | DS1820* probe[MAX_PROBES]; |
Sissors | 0:e069f9f26768 | 13 | |
Sissors | 0:e069f9f26768 | 14 | int main() { |
Sissors | 0:e069f9f26768 | 15 | // Initialize the probe array to DS1820 objects |
Sissors | 0:e069f9f26768 | 16 | int num_devices = 0; |
Sissors | 0:e069f9f26768 | 17 | while(DS1820::unassignedProbe(DATA_PIN)) { |
Sissors | 0:e069f9f26768 | 18 | probe[num_devices] = new DS1820(DATA_PIN); |
Sissors | 0:e069f9f26768 | 19 | num_devices++; |
Sissors | 0:e069f9f26768 | 20 | if (num_devices == MAX_PROBES) |
Sissors | 0:e069f9f26768 | 21 | break; |
Sissors | 0:e069f9f26768 | 22 | } |
Sissors | 0:e069f9f26768 | 23 | |
JLarkin | 5:a870fb672587 | 24 | printf("Found %d device(s)\r\n", num_devices); |
JLarkin | 5:a870fb672587 | 25 | unsigned long long fullName; |
JLarkin | 5:a870fb672587 | 26 | unsigned int firstName; |
JLarkin | 5:a870fb672587 | 27 | unsigned int lastName; |
JLarkin | 5:a870fb672587 | 28 | for (int i = 0; i<num_devices; i++) { |
JLarkin | 5:a870fb672587 | 29 | fullName = probe[i]->whoAmI(); |
JLarkin | 5:a870fb672587 | 30 | firstName = fullName >> 32; |
JLarkin | 5:a870fb672587 | 31 | lastName = fullName; |
JLarkin | 5:a870fb672587 | 32 | printf("\tID %d = %x%x\r\n", i, firstName, lastName); |
JLarkin | 5:a870fb672587 | 33 | } |
JLarkin | 5:a870fb672587 | 34 | printf("\n"); |
JLarkin | 5:a870fb672587 | 35 | |
Sissors | 0:e069f9f26768 | 36 | while(1) { |
Sissors | 0:e069f9f26768 | 37 | probe[0]->convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready |
Sissors | 0:e069f9f26768 | 38 | for (int i = 0; i<num_devices; i++) |
JLarkin | 5:a870fb672587 | 39 | printf("%3.1f C\t", i, probe[i]->temperature()); |
Sissors | 0:e069f9f26768 | 40 | printf("\r\n"); |
Sissors | 0:e069f9f26768 | 41 | wait(1); |
Sissors | 0:e069f9f26768 | 42 | } |
Sissors | 0:e069f9f26768 | 43 | |
Sissors | 0:e069f9f26768 | 44 | } |
Sissors | 0:e069f9f26768 | 45 | |
Sissors | 0:e069f9f26768 | 46 | #else |
Sissors | 0:e069f9f26768 | 47 | #include "mbed.h" |
Sissors | 0:e069f9f26768 | 48 | #include "DS1820.h" |
Sissors | 0:e069f9f26768 | 49 | |
Sissors | 0:e069f9f26768 | 50 | DS1820 probe(DATA_PIN); |
Sissors | 0:e069f9f26768 | 51 | |
Sissors | 0:e069f9f26768 | 52 | int main() { |
Sissors | 0:e069f9f26768 | 53 | while(1) { |
Sissors | 0:e069f9f26768 | 54 | probe.convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready |
Sissors | 0:e069f9f26768 | 55 | printf("It is %3.1foC\r\n", probe.temperature()); |
Sissors | 0:e069f9f26768 | 56 | wait(1); |
Sissors | 0:e069f9f26768 | 57 | } |
Sissors | 0:e069f9f26768 | 58 | } |
Sissors | 0:e069f9f26768 | 59 | |
Sissors | 0:e069f9f26768 | 60 | #endif |