GSOE Belueftung DS18B20
Dependencies: LCD_i2c_GSOE DS1820
main.cpp@12:34123e51ac35, 2020-07-19 (annotated)
- Committer:
- hudakz
- Date:
- Sun Jul 19 19:29:57 2020 +0000
- Revision:
- 12:34123e51ac35
- Parent:
- 10:739d1a7a4b1b
- Child:
- 14:7d305c5c0364
Simple DS1820 sensor demo.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:77a366f9ba45 | 1 | /* |
hudakz | 10:739d1a7a4b1b | 2 | * Simple example program |
hudakz | 1:fe12bf2ad337 | 3 | * |
hudakz | 1:fe12bf2ad337 | 4 | * Note: Don't forget to connect a 4.7k Ohm resistor |
hudakz | 1:fe12bf2ad337 | 5 | * between the DS1820's data pin and the +3.3V pin |
hudakz | 1:fe12bf2ad337 | 6 | * |
hudakz | 0:77a366f9ba45 | 7 | */ |
hudakz | 10:739d1a7a4b1b | 8 | |
hudakz | 10:739d1a7a4b1b | 9 | /* Single DS1820 sensor: */ |
hudakz | 9:1d1c57840c7e | 10 | /* |
hudakz | 0:77a366f9ba45 | 11 | #include "mbed.h" |
hudakz | 0:77a366f9ba45 | 12 | #include "DS1820.h" |
hudakz | 0:77a366f9ba45 | 13 | |
hudakz | 4:d263a2860484 | 14 | Serial pc(USBTX, USBRX); |
hudakz | 4:d263a2860484 | 15 | DigitalOut led(LED1); |
hudakz | 10:739d1a7a4b1b | 16 | DS1820 ds1820(D8); // substitute D8 with the actual pin name connected to the DS1820 sensor |
hudakz | 4:d263a2860484 | 17 | float temp = 0; |
hudakz | 7:8beaacebc82d | 18 | int result = 0; |
hudakz | 7:8beaacebc82d | 19 | |
hudakz | 7:8beaacebc82d | 20 | int main() |
hudakz | 7:8beaacebc82d | 21 | { |
hudakz | 7:8beaacebc82d | 22 | if (ds1820.begin()) { |
hudakz | 7:8beaacebc82d | 23 | while (1) { |
hudakz | 7:8beaacebc82d | 24 | ds1820.startConversion(); // start temperature conversion from analog to digital |
hudakz | 12:34123e51ac35 | 25 | ThisThread::sleep_for(1000);// let DS1820 complete the temperature conversion |
hudakz | 4:d263a2860484 | 26 | result = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC) |
hudakz | 7:8beaacebc82d | 27 | switch (result) { |
hudakz | 7:8beaacebc82d | 28 | case 0: // no errors -> 'temp' contains the value of measured temperature |
hudakz | 7:8beaacebc82d | 29 | pc.printf("temp = %3.1f%cC\r\n", temp, 176); |
hudakz | 7:8beaacebc82d | 30 | break; |
hudakz | 7:8beaacebc82d | 31 | |
hudakz | 10:739d1a7a4b1b | 32 | case 1: // no sensor present -> 'temp' was not updated |
hudakz | 7:8beaacebc82d | 33 | pc.printf("no sensor present\n\r"); |
hudakz | 7:8beaacebc82d | 34 | break; |
hudakz | 7:8beaacebc82d | 35 | |
hudakz | 10:739d1a7a4b1b | 36 | case 2: // CRC error -> 'temp' was not updated |
hudakz | 7:8beaacebc82d | 37 | pc.printf("CRC error\r\n"); |
hudakz | 7:8beaacebc82d | 38 | } |
hudakz | 7:8beaacebc82d | 39 | |
hudakz | 4:d263a2860484 | 40 | led = !led; |
hudakz | 0:77a366f9ba45 | 41 | } |
hudakz | 7:8beaacebc82d | 42 | } |
hudakz | 7:8beaacebc82d | 43 | else |
hudakz | 4:d263a2860484 | 44 | pc.printf("No DS1820 sensor found!\r\n"); |
hudakz | 0:77a366f9ba45 | 45 | } |
hudakz | 9:1d1c57840c7e | 46 | */ |
hudakz | 1:fe12bf2ad337 | 47 | |
hudakz | 9:1d1c57840c7e | 48 | |
hudakz | 10:739d1a7a4b1b | 49 | /*Several DS1820 sensors connected to the 1-wire bus:*/ |
hudakz | 9:1d1c57840c7e | 50 | #include "mbed.h" |
hudakz | 9:1d1c57840c7e | 51 | #include "DS1820.h" |
hudakz | 9:1d1c57840c7e | 52 | |
hudakz | 10:739d1a7a4b1b | 53 | #define MAX_SENSOSRS 32 // max number of DS1820 sensors to be connected to the 1-wire bus (max 256) |
hudakz | 9:1d1c57840c7e | 54 | |
hudakz | 10:739d1a7a4b1b | 55 | DS1820* ds1820[MAX_SENSOSRS]; |
hudakz | 9:1d1c57840c7e | 56 | Serial pc(USBTX, USBRX); |
hudakz | 9:1d1c57840c7e | 57 | DigitalOut led(LED1); |
hudakz | 10:739d1a7a4b1b | 58 | OneWire oneWire(D8); // substitute D8 with the actual pin name connected to the 1-wire bus |
hudakz | 10:739d1a7a4b1b | 59 | int sensorsFound = 0; // counts the actually found DS1820 sensors |
hudakz | 9:1d1c57840c7e | 60 | |
hudakz | 10:739d1a7a4b1b | 61 | int main() |
hudakz | 10:739d1a7a4b1b | 62 | { |
hudakz | 10:739d1a7a4b1b | 63 | pc.printf("\r\n--Starting--\r\n"); |
hudakz | 9:1d1c57840c7e | 64 | |
hudakz | 9:1d1c57840c7e | 65 | //Enumerate (i.e. detect) DS1820 sensors on the 1-wire bus |
hudakz | 10:739d1a7a4b1b | 66 | for (sensorsFound = 0; sensorsFound < MAX_SENSOSRS; sensorsFound++) { |
hudakz | 10:739d1a7a4b1b | 67 | ds1820[sensorsFound] = new DS1820(&oneWire); |
hudakz | 10:739d1a7a4b1b | 68 | if (!ds1820[sensorsFound]->begin()) { |
hudakz | 10:739d1a7a4b1b | 69 | delete ds1820[sensorsFound]; |
hudakz | 9:1d1c57840c7e | 70 | break; |
hudakz | 9:1d1c57840c7e | 71 | } |
hudakz | 9:1d1c57840c7e | 72 | } |
hudakz | 10:739d1a7a4b1b | 73 | |
hudakz | 10:739d1a7a4b1b | 74 | switch (sensorsFound) { |
hudakz | 10:739d1a7a4b1b | 75 | case 0: |
hudakz | 10:739d1a7a4b1b | 76 | pc.printf("No DS1820 sensor found!\r\n"); |
hudakz | 10:739d1a7a4b1b | 77 | return -1; |
hudakz | 10:739d1a7a4b1b | 78 | |
hudakz | 10:739d1a7a4b1b | 79 | case 1: |
hudakz | 10:739d1a7a4b1b | 80 | pc.printf("One DS1820 sensor found.\r\n"); |
hudakz | 10:739d1a7a4b1b | 81 | break; |
hudakz | 10:739d1a7a4b1b | 82 | |
hudakz | 10:739d1a7a4b1b | 83 | default: |
hudakz | 10:739d1a7a4b1b | 84 | pc.printf("Found %d DS1820 sensors.\r\n", sensorsFound); |
hudakz | 9:1d1c57840c7e | 85 | } |
hudakz | 10:739d1a7a4b1b | 86 | |
hudakz | 10:739d1a7a4b1b | 87 | while (1) { |
hudakz | 10:739d1a7a4b1b | 88 | pc.printf("----------------\r\n"); |
hudakz | 10:739d1a7a4b1b | 89 | for (int i = 0; i < sensorsFound; i++) |
hudakz | 10:739d1a7a4b1b | 90 | ds1820[i]->startConversion(); // start temperature conversion from analog to digital |
hudakz | 12:34123e51ac35 | 91 | ThisThread::sleep_for(1000); // let DS1820 sensors complete the temperature conversion |
hudakz | 10:739d1a7a4b1b | 92 | for (int i = 0; i < sensorsFound; i++) { |
hudakz | 10:739d1a7a4b1b | 93 | if (ds1820[i]->isPresent()) |
hudakz | 10:739d1a7a4b1b | 94 | pc.printf("temp[%d] = %3.1f%cC\r\n", i, ds1820[i]->read(), 176); // read temperature |
hudakz | 9:1d1c57840c7e | 95 | } |
hudakz | 9:1d1c57840c7e | 96 | } |
hudakz | 9:1d1c57840c7e | 97 | } |