Simple DS1820 sensor demo showing how to use the DS1820 library [https://developer.mbed.org/users/hudakz/code/DS1820/]

Dependencies:   DS1820

Committer:
hudakz
Date:
Sun Jan 27 14:12:49 2019 +0000
Revision:
7:8beaacebc82d
Parent:
6:d68db387a734
Child:
9:1d1c57840c7e
Updated.

Who changed what in which revision?

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