Working version for L-tek FF1705

Dependencies:   OneWire

Dependents:   FROST

Files at this revision

API Documentation at this revision

Comitter:
hudakz
Date:
Sun Mar 10 13:19:03 2019 +0000
Parent:
19:74d2a4ac7f32
Child:
21:96ec0fecd7f8
Commit message:
Added multidrop capability.

Changed in this revision

DS1820.cpp Show annotated file Show diff for this revision Revisions of this file
DS1820.h Show annotated file Show diff for this revision Revisions of this file
OneWire.lib Show annotated file Show diff for this revision Revisions of this file
--- a/DS1820.cpp	Sun Jan 27 18:02:01 2019 +0000
+++ b/DS1820.cpp	Sun Mar 10 13:19:03 2019 +0000
@@ -5,34 +5,114 @@
  *
  * Example of use:
  * 
- * #include "DS1820.h"
+ * Single sensor.
  *
- * Serial serial(USBTX, USBRX);
+ * #include "mbed.h"
+ * #include "DS1820.h"
+ * 
+ * Serial      pc(USBTX, USBRX);
+ * DigitalOut  led(LED1);
+ * OneWire     oneWire(D8);    // substitute D8 with actual mbed pin name connected 1-wire bus
+ * float       temp = 0;
+ * int         result = 0;
+ * 
+ * int main()
+ * {
+ *     pc.printf("\r\n--Starting--\r\n");
+ *     if (ds1820.begin()) {
+ *         while (1) {
+ *             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
+ *         pc.printf("No DS1820 sensor found!\r\n");
+ * }
+ * 
+ * 
+ * More sensors connected to the same 1-wire bus.
+ * 
+ * #include "mbed.h"
+ * #include "DS1820.h"
+ * 
+ * #define     SENSORS_COUNT   64      // number of DS1820 sensors to be connected to the 1-wire bus (max 256)
+ * 
+ * Serial      pc(USBTX, USBRX);
+ * DigitalOut  led(LED1);
+ * OneWire     oneWire(D8);            // substitute D8 with actual mbed pin name connected to the DS1820 data pin
+ * DS1820*     ds1820[SENSORS_COUNT];
+ * int         sensors_found = 0;      // counts the actually found DS1820 sensors
+ * 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();
- *        wait(1.0);
- *        while(1) {
- *            printf("temp = %3.1f\r\n", ds1820.read());     // read temperature
- *            ds1820.startConversion();     // start temperature conversion
- *            wait(1.0);                    // let DS1820 complete the temperature conversion
- *        }
- *    } else
- *        printf("No DS1820 sensor found!\r\n");
+ *     int i = 0;
+ *     
+ *     pc.printf("\r\n Starting \r\n");
+ *     //Enumerate (i.e. detect) DS1820 sensors on the 1-wire bus
+ *     for(i = 0; i < SENSORS_COUNT; i++) {
+ *         ds1820[i] = new DS1820(&oneWire);
+ *         if(!ds1820[i]->begin()) {
+ *             delete ds1820[i];
+ *             break;
+ *         }
+ *     }
+ *     
+ *     sensors_found = i;
+ *     
+ *     if (sensors_found == 0) {
+ *         pc.printf("No DS1820 sensor found!\r\n");
+ *         return -1;
+ *     }
+ *     else
+ *         pc.printf("Found %d sensors.\r\n", sensors_found);
+ *     
+ *     while(1) {
+ *         pc.printf("-------------------\r\n");
+ *         for(i = 0; i < sensors_found; i++)
+ *             ds1820[i]->startConversion();   // start temperature conversion from analog to digital       
+ *         wait(1.0);                          // let DS1820s complete the temperature conversion
+ *         for(int i = 0; i < sensors_found; i++) {
+ *             if(ds1820[i]->isPresent())
+ *                 pc.printf("temp[%d] = %3.1f%cC\r\n", i, ds1820[i]->read(), 176);     // read temperature
+ *         }
+ *     }
  * }
- *
  * 
- * Note: Don't forget to connect a 4.7k Ohm resistor 
- *       between the DS1820's data pin and the +3.3V pin
- *
  */
  
 #include "DS1820.h"
 
-//#define DEBUG 1
+#define DEBUG 0
+
+//* Initializing static members
+uint8_t DS1820::lastAddr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
+/**
+ * @brief   Constructs a generic DS1820 sensor
+ * @note    begin() must be called to detect and initialize the actual model
+ * @param   pin: Name of data pin
+ * @retval
+ */
+DS1820::DS1820(PinName pin) {
+    oneWire = new OneWire(pin);
+    present = false;
+    model_s = false;
+}
 
 /**
  * @brief   Constructs a generic DS1820 sensor
@@ -40,34 +120,13 @@
  * @param   pin: Name of data pin
  * @retval
  */
-DS1820::DS1820(PinName pin) :
-    oneWire(pin) {
+DS1820::DS1820(OneWire* wire) :
+    oneWire(wire) {
     present = false;
     model_s = false;
 }
 
 /**
- * @brief   Constructs a specific model
- * @note    No need to call begin() to detect and initialize the model
- * @param   model:  One character model name: 'S', 's', 'B' or 'b'
- *          pin:    Name of data pin
- * @retval
- */
-DS1820::DS1820(char model, PinName pin) :
-    oneWire(pin) {
-    if((model == 'S') or (model == 's')) {
-        present = true;
-        model_s = true;
-    }
-    else if((model == 'B') or (model == 'b')) {
-        present = true;
-        model_s = false;
-    }
-    else
-        present = false;
-}
-
-/**
  * @brief   Detects and initializes the actual DS1820 model
  * @note
  * @param
@@ -75,16 +134,24 @@
             false:  otherwise
  */
 bool DS1820::begin(void) {
-    oneWire.reset_search();
-    wait_ms(250);
-    if(!oneWire.search(addr)) {
+#if DEBUG
+    printf("lastAddr =");
+    for(uint8_t i = 0; i < 8; i++) {
+        printf(" %x", lastAddr[i]);
+    }
+    printf("\r\n");
+#endif
+    if(!oneWire->search(lastAddr)) {
 #if DEBUG
         printf("No addresses.\r\n");
 #endif
-        oneWire.reset_search();
+        oneWire->reset_search();
         wait_ms(250);
         return false;
     }
+    
+    for (int i = 0; i < 8; i++)
+        addr[i] = lastAddr[i];
 
 #if DEBUG
     printf("ROM =");
@@ -168,18 +235,18 @@
     if(model_s)
         res = 9;
        
-    oneWire.reset();
-    oneWire.skip();
-    oneWire.write_byte(0xBE);            // to read Scratchpad
+    oneWire->reset();
+    oneWire->select(addr);
+    oneWire->write_byte(0xBE);            // to read Scratchpad
     for(uint8_t i = 0; i < 9; i++)  // read Scratchpad bytes
-        data[i] = oneWire.read_byte();   
+        data[i] = oneWire->read_byte();   
 
     data[4] |= (res - 9) << 5;      // update configuration byte (set resolution)  
-    oneWire.reset();
-    oneWire.skip();
-    oneWire.write_byte(0x4E);            // to write into Scratchpad
+    oneWire->reset();
+    oneWire->select(addr);
+    oneWire->write_byte(0x4E);            // to write into Scratchpad
     for(uint8_t i = 2; i < 5; i++)  // write three bytes (2nd, 3rd, 4th) into Scratchpad
-        oneWire.write_byte(data[i]);
+        oneWire->write_byte(data[i]);
 }
 
 /**
@@ -194,9 +261,9 @@
  */
 void DS1820::startConversion(void) {
     if(present) {
-        oneWire.reset();
-        oneWire.skip();
-        oneWire.write_byte(0x44);    //start temperature conversion
+        oneWire->reset();
+        oneWire->select(addr);
+        oneWire->write_byte(0x44);    //start temperature conversion
     }
 }
 
@@ -208,11 +275,11 @@
  */
 float DS1820::read(void) {
     if(present) {
-        oneWire.reset();
-        oneWire.skip();
-        oneWire.write_byte(0xBE);           // to read Scratchpad
+        oneWire->reset();
+        oneWire->select(addr);
+        oneWire->write_byte(0xBE);           // to read Scratchpad
         for(uint8_t i = 0; i < 9; i++)      // reading scratchpad registers
-            data[i] = oneWire.read_byte();
+            data[i] = oneWire->read_byte();
 
         // Convert the raw bytes to a 16-bit unsigned value
         uint16_t*   p_word = reinterpret_cast < uint16_t * > (&data[0]);
@@ -270,13 +337,13 @@
  */
 uint8_t DS1820::read(float& temp) {
     if(present) {
-        oneWire.reset();
-        oneWire.skip();
-        oneWire.write_byte(0xBE);               // to read Scratchpad
+        oneWire->reset();
+        oneWire->select(addr);
+        oneWire->write_byte(0xBE);               // to read Scratchpad
         for(uint8_t i = 0; i < 9; i++)          // reading scratchpad registers
-            data[i] = oneWire.read_byte();
+            data[i] = oneWire->read_byte();
 
-        if(oneWire.crc8(data, 8) != data[8])    // if calculated CRC does not match the stored one
+        if(oneWire->crc8(data, 8) != data[8])    // if calculated CRC does not match the stored one
         {
 #if DEBUG
             for(uint8_t i = 0; i < 9; i++)
--- a/DS1820.h	Sun Jan 27 18:02:01 2019 +0000
+++ b/DS1820.h	Sun Mar 10 13:19:03 2019 +0000
@@ -11,25 +11,96 @@
  * Example of use:
  * 
  * @code
- * #include "DS1820.h"
+ * 
+ * Single sensor.
  *
- * Serial serial(USBTX, USBRX);
+ * #include "mbed.h"
+ * #include "DS1820.h"
+ * 
+ * Serial      pc(USBTX, USBRX);
+ * DigitalOut  led(LED1);
+ * DS1820      ds1820(D8);  // substitute D8 with actual mbed pin name connected to 1-wire bus
+ * float       temp = 0;
+ * int         result = 0;
+ * 
+ * int main()
+ * {
+ *     pc.printf("\r\n--Starting--\r\n");
+ *     if (ds1820.begin()) {
+ *         while (1) {
+ *             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
+ *         pc.printf("No DS1820 sensor found!\r\n");
+ * }
+ * 
+ * 
+ * More sensors connected to the same 1-wire bus.
+ * 
+ * #include "mbed.h"
+ * #include "DS1820.h"
+ * 
+ * #define     SENSORS_COUNT   64      // number of DS1820 sensors to be connected to the 1-wire bus (max 256)
+ * 
+ * Serial      pc(USBTX, USBRX);
+ * DigitalOut  led(LED1);
+ * OneWire     oneWire(D8);            // substitute D8 with actual mbed pin name connected to the DS1820 data pin
+ * DS1820*     ds1820[SENSORS_COUNT];
+ * int         sensors_found = 0;      // counts the actually found DS1820 sensors
+ * 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();
- *        wait(1.0);
- *        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
- *        }
- *    } else
- *        serial.printf("No DS1820 sensor found!\r\n");
+ *     int i = 0;
+ *     
+ *     pc.printf("\r\n Starting \r\n");
+ *     //Enumerate (i.e. detect) DS1820 sensors on the 1-wire bus
+ *     for(i = 0; i < SENSORS_COUNT; i++) {
+ *         ds1820[i] = new DS1820(&oneWire);
+ *         if(!ds1820[i]->begin()) {
+ *             delete ds1820[i];
+ *             break;
+ *         }
+ *     }
+ *     
+ *     sensors_found = i;
+ *     
+ *     if (sensors_found == 0) {
+ *         pc.printf("No DS1820 sensor found!\r\n");
+ *         return -1;
+ *     }
+ *     else
+ *         pc.printf("Found %d sensors.\r\n", sensors_found);
+ *     
+ *     while(1) {
+ *         pc.printf("-------------------\r\n");
+ *         for(i = 0; i < sensors_found; i++)
+ *             ds1820[i]->startConversion();   // start temperature conversion from analog to digital       
+ *         wait(1.0);                          // let DS1820s complete the temperature conversion
+ *         for(int i = 0; i < sensors_found; i++) {
+ *             if(ds1820[i]->isPresent())
+ *                 pc.printf("temp[%d] = %3.1f%cC\r\n", i, ds1820[i]->read(), 176);     // read temperature
+ *         }
+ *     }
  * }
- *
+ * 
  * @endcode
  * 
  * Note: Don't forget to connect a 4.7k Ohm resistor 
@@ -38,16 +109,18 @@
  */
 class   DS1820
 {
-    OneWire oneWire;
+    OneWire *oneWire;
     bool    present;    
     bool    model_s;
     uint8_t data[12];
     uint8_t addr[8];
     float   toFloat(uint16_t word);
+    static  uint8_t lastAddr[8];
+    
 public:
-
     DS1820(PinName pin);
-    DS1820(char model, PinName pin);
+//    DS1820(char model, PinName pin);
+    DS1820(OneWire* wire);
     bool   begin(void);
     bool   isPresent();
     void   setResolution(uint8_t res);
--- a/OneWire.lib	Sun Jan 27 18:02:01 2019 +0000
+++ b/OneWire.lib	Sun Mar 10 13:19:03 2019 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/users/hudakz/code/OneWire/#bc8ed7280966
+http://developer.mbed.org/users/hudakz/code/OneWire/#27a1b359b95c