GSOE Belueftung DS18B20
Dependencies: LCD_i2c_GSOE DS1820
main.cpp@4:d263a2860484, 2019-01-20 (annotated)
- Committer:
- hudakz
- Date:
- Sun Jan 20 10:13:38 2019 +0000
- Revision:
- 4:d263a2860484
- Parent:
- 3:eb195375cde7
- Child:
- 5:4f7330bb1b64
Updated.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:77a366f9ba45 | 1 | /* |
hudakz | 0:77a366f9ba45 | 2 | * Simple DS1820 sensor demo |
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 | 0:77a366f9ba45 | 8 | |
hudakz | 0:77a366f9ba45 | 9 | #include "mbed.h" |
hudakz | 0:77a366f9ba45 | 10 | #include "DS1820.h" |
hudakz | 0:77a366f9ba45 | 11 | |
hudakz | 4:d263a2860484 | 12 | Serial pc(USBTX, USBRX); |
hudakz | 4:d263a2860484 | 13 | DigitalOut led(LED1); |
hudakz | 4:d263a2860484 | 14 | DS1820 ds1820(p8); // substitute p8 with actual mbed pin name connected to the DS1820 data pin |
hudakz | 4:d263a2860484 | 15 | float temp = 0; |
hudakz | 4:d263a2860484 | 16 | int result = 0; |
hudakz | 1:fe12bf2ad337 | 17 | |
hudakz | 0:77a366f9ba45 | 18 | int main() { |
hudakz | 1:fe12bf2ad337 | 19 | |
hudakz | 0:77a366f9ba45 | 20 | if(ds1820.begin()) { |
hudakz | 0:77a366f9ba45 | 21 | while(1) { |
hudakz | 4:d263a2860484 | 22 | ds1820.startConversion(); // start temperature conversion from analog to digital |
hudakz | 4:d263a2860484 | 23 | wait(1.0); // let DS1820 complete the temperature conversion |
hudakz | 4:d263a2860484 | 24 | result = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC) |
hudakz | 4:d263a2860484 | 25 | switch(result) { |
hudakz | 4:d263a2860484 | 26 | case 0: // no errors -> 'temp' contains the value of measured temperature |
hudakz | 4:d263a2860484 | 27 | pc.printf("temp = %3.1f%cC\r\n", temp, 176); |
hudakz | 4:d263a2860484 | 28 | break; |
hudakz | 4:d263a2860484 | 29 | case 1: // no sensor present -> 'temp' is not updated |
hudakz | 4:d263a2860484 | 30 | pc.printf("no sensor present\n\r"); |
hudakz | 4:d263a2860484 | 31 | break; |
hudakz | 4:d263a2860484 | 32 | case 2: // CRC error -> 'temp' is not updated |
hudakz | 4:d263a2860484 | 33 | pc.printf("CRC error\r\n"); |
hudakz | 4:d263a2860484 | 34 | } |
hudakz | 4:d263a2860484 | 35 | led = !led; |
hudakz | 0:77a366f9ba45 | 36 | } |
hudakz | 0:77a366f9ba45 | 37 | } else |
hudakz | 4:d263a2860484 | 38 | pc.printf("No DS1820 sensor found!\r\n"); |
hudakz | 0:77a366f9ba45 | 39 | } |
hudakz | 1:fe12bf2ad337 | 40 | |
hudakz | 4:d263a2860484 | 41 | // Array of sensors: |
hudakz | 1:fe12bf2ad337 | 42 | |
hudakz | 1:fe12bf2ad337 | 43 | //int main() { |
hudakz | 1:fe12bf2ad337 | 44 | // DS1820 ds1820[3] = {DS1820(PA_8), DS1820(PA_9), DS1820(PC_7)}; |
hudakz | 1:fe12bf2ad337 | 45 | // |
hudakz | 1:fe12bf2ad337 | 46 | // for(int i = 0; i < 3; i++) { |
hudakz | 1:fe12bf2ad337 | 47 | // if(!ds1820[i].begin()) { |
hudakz | 3:eb195375cde7 | 48 | // serial.printf("Cannot find sensor %d", i); |
hudakz | 1:fe12bf2ad337 | 49 | // } else |
hudakz | 1:fe12bf2ad337 | 50 | // ds1820[i].startConversion(); |
hudakz | 2:a680194ce6df | 51 | // } |
hudakz | 2:a680194ce6df | 52 | // wait(1.0); // let DS1820s complete the temperature conversion |
hudakz | 1:fe12bf2ad337 | 53 | // while(1) { |
hudakz | 1:fe12bf2ad337 | 54 | // for(int i = 0; i < 3; i++) { |
hudakz | 3:eb195375cde7 | 55 | // if(ds1820[i].isPresent() { |
hudakz | 2:a680194ce6df | 56 | // serial.printf("temp%d = %3.1f\r\n", i, ds1820[i].read()); // read temperature |
hudakz | 2:a680194ce6df | 57 | // ds1820[i].startConversion(); // start temperature conversion |
hudakz | 2:a680194ce6df | 58 | // } |
hudakz | 1:fe12bf2ad337 | 59 | // } |
hudakz | 1:fe12bf2ad337 | 60 | // wait(1.0); // let DS1820s complete the temperature conversion |
hudakz | 1:fe12bf2ad337 | 61 | // } |
hudakz | 1:fe12bf2ad337 | 62 | //} |
hudakz | 3:eb195375cde7 | 63 | // |
hudakz | 1:fe12bf2ad337 | 64 | |
hudakz | 1:fe12bf2ad337 | 65 | |
hudakz | 1:fe12bf2ad337 | 66 | |
hudakz | 1:fe12bf2ad337 | 67 |