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

Dependencies:   DS1820

Revision:
4:d263a2860484
Parent:
3:eb195375cde7
Child:
5:4f7330bb1b64
--- a/main.cpp	Fri Mar 27 12:37:34 2015 +0000
+++ b/main.cpp	Sun Jan 20 10:13:38 2019 +0000
@@ -9,25 +9,36 @@
 #include "mbed.h"
 #include "DS1820.h"
 
-Serial serial(USBTX, USBRX);
+Serial      pc(USBTX, USBRX);
+DigitalOut  led(LED1);
+DS1820      ds1820(p8);    // substitute p8 with actual mbed pin name connected to the DS1820 data pin    
+float       temp = 0;
+int         result = 0; 
  
 int main() {
-    DS1820  ds1820(PA_9);    // substitute PA_9 with actual mbed pin name connected to the DS1820 data pin    
                              
     if(ds1820.begin()) {
-        ds1820.startConversion();   // start temperature conversion
-        wait(1.0);                  // let DS1820 complete the temperature conversion
         while(1) {
-            serial.printf("temp = %3.1f\r\n", ds1820.read());     // read temperature
-            ds1820.startConversion();     // start temperature conversion
-            wait(1.0);                    // let DS1820 complete the temperature conversion
+            ds1820.startConversion();  // start temperature conversion from analog to digital
+            wait(1.0);                 // let DS1820 complete the temperature conversion 
+            result = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC)
+            switch(result) {
+            case 0:    // no errors -> 'temp' contains the value of measured temperature
+                pc.printf("temp = %3.1f%cC\r\n", temp, 176);
+                break;
+            case 1:    // no sensor present -> 'temp' is not updated
+                pc.printf("no sensor present\n\r");
+                break;
+            case 2:    // CRC error -> 'temp' is not updated
+                pc.printf("CRC error\r\n");
+            } 
+            led = !led;
         }
     } else
-        serial.printf("No DS1820 sensor found!\r\n");
+        pc.printf("No DS1820 sensor found!\r\n");
 }
 
-
-// You can create an array of sensors:
+// Array of sensors:
 
 //int main() {
 //    DS1820  ds1820[3] = {DS1820(PA_8), DS1820(PA_9), DS1820(PC_7)};