one wire driver

Dependents:   09_PT1000 10_PT1000 11_PT1000

Files at this revision

API Documentation at this revision

Comitter:
rs27
Date:
Sat May 04 12:15:25 2013 +0000
Parent:
1:0950824b1ca3
Child:
3:e773b0d83471
Commit message:
test

Changed in this revision

DS2482.cpp Show annotated file Show diff for this revision Revisions of this file
DS2482.h Show annotated file Show diff for this revision Revisions of this file
--- a/DS2482.cpp	Sat May 04 10:11:42 2013 +0000
+++ b/DS2482.cpp	Sat May 04 12:15:25 2013 +0000
@@ -963,8 +963,7 @@
     
     //--------------------------------------------------------------
     // die Bausteine am Bus suchen
-
-    ow.bus = 0;     
+    
     //pc.printf("\n -- detect -- \n");
     //wait(0.5);    
     detect();    // reset and init the 1-wire master
@@ -1024,8 +1023,6 @@
     //--------------------------------------------------------------
     // alle Bausteine an bus 0 starten
 
-    ow.bus = 0;
-
     // find a DS18S20
     OWFirst();
     OWReset();
@@ -1044,8 +1041,6 @@
     //--------------------------------------------------------------
     // alle Bausteine an bus 1 starten
     
-    ow.bus = 1;
-    
     // find a DS18S20
     OWFirst();
     OWReset();
@@ -1068,11 +1063,6 @@
 bool DS2482::ow_read_rom(void)
 {
   uint8_t n;
-   
-  if ((ow.device_table[ow.device_table_index].status & 0x80) == 0) 
-    ow.bus = 0;
-  else                       
-    ow.bus = 1;
 
   // Write Read ROM Code command
   OWWriteByte(OW_CMD_READ_ROM);
@@ -1102,11 +1092,6 @@
 
     ow_flags = 0;
 
-    if ((ow.device_table[ow.device_table_index].status & 0x80) == 0) 
-        ow.bus = 0;
-    else                         
-        ow.bus = 1;
-
     if (!OWReset()) return false;
 
     // select the device
@@ -1151,11 +1136,6 @@
     uint8_t n, crc;
 
     ow_flags = 0;
-    
-    if ((ow.device_table[ow.device_table_index].status & 0x80) == 0) 
-        ow.bus = 0;
-    else                         
-        ow.bus = 1;
 
     if (!OWReset()) return false;
 
@@ -1203,11 +1183,6 @@
 bool DS2482::ow_write_scratchpad_ds18x20 (uint8_t th, uint8_t tl)
 {
     uint8_t i;
-
-    if ((ow.device_table[ow.device_table_index].status & 0x80) == 0) 
-        ow.bus = 0;
-    else                         
-        ow.bus = 1;
             
     // Do bus reset
     if (!OWReset()) return false;
@@ -1245,11 +1220,6 @@
 {
 
     uint8_t i;
-
-    if ((ow.device_table[ow.device_table_index].status & 0x80) == 0) 
-        ow.bus = 0;
-    else                         
-        ow.bus = 1;
             
     // Do bus reset
     if (!OWReset()) return false;
@@ -1281,12 +1251,7 @@
 bool DS2482::ow_write_eeprom_ds18x20 (void)
 {
 
-    uint8_t i;
-
-    if ((ow.device_table[ow.device_table_index].status & 0x80) == 0) 
-        ow.bus = 0;
-    else                         
-        ow.bus = 1;                 
+    uint8_t i;                
                               
     // Do bus reset
     if (!OWReset()) return false;
@@ -1312,11 +1277,6 @@
 {
     uint8_t i;
     
-    if ((ow.device_table[ow.device_table_index].status & 0x80) == 0) 
-        ow.bus = 0;
-    else                         
-        ow.bus = 1;
-    
     // Do bus reset
     if (!OWReset()) return false;
 
@@ -1351,11 +1311,6 @@
             // printf_P(PSTR("\n device table %d"),n);
             ow.device_table_index = n; 
 
-            if ((ow.device_table[ow.device_table_index].status & 0x80) == 0) 
-                ow.bus = 0;
-             else                        
-                ow.bus = 1;
-
             if ((ow.device_table[n].rom[0] == 0x10) || (ow.device_table[n].rom[0] == 0x28))
             {
                 if (ow_read_scratchpad())
@@ -1378,3 +1333,95 @@
     } // end for(...
 }
 
+//-----------------------------------------------------------------------------
+
+bool DS2482::ds1820_start_conversion(uint8_t command)
+{
+    uint8_t i;
+
+    // Do bus reset
+    if (!OWReset()) return false;
+
+    // Check if a match ROM code must be done or just skip ROM code (0xFF)
+    if (command > 0xFE)
+    {
+        OWWriteByte(OW_CMD_SKIP_ROM); // Send Skip ROM Code command
+    }
+    else
+    {
+        // select the device
+        // den Baustein wird über den ROM Code adressiert
+        OWWriteByte(OW_CMD_MATCH_ROM); // 0x55 match command
+
+        for (i = 0; i < 8; i++)
+        {
+            OWWriteByte(ow.device_table[ow.device_table_index].rom[i]);
+        }
+    }
+
+    // Send the "Convert T" command to start conversion
+    OWWriteByte(OW_CMD_CONVERT_T);
+    
+    // Set flag to mark a measurement in progress
+    ds1820_request_pending = TRUE;
+
+    return TRUE;
+}
+
+
+//-----------------------------------------------------------------------------
+
+bool DS2482::ds18B20_read_hrtemp(void)
+{
+  
+    union ds18B20_temp_union
+    {
+        int16_t word;
+        uint8_t  byte[2];
+    } ds18B20_temp;
+    
+    // uint8_t temp_lo;
+    // uint8_t temp_hi;
+    // int8_t digit;
+    float ds1820_float_result; 
+
+    // Preset float result to zero in case of function termination (return false)
+    //ds1820_float_result = 0.0;
+    ow.device_table[ow.device_table_index].result = 0;
+
+    // Return false if reading scratchpad memory fails
+    if (!ow_read_scratchpad())
+    {
+        ow.device_table[ow.device_table_index].status &= 0xf0;
+        ow.device_table[ow.device_table_index].status |= 4;
+        return FALSE;
+    }
+    
+    ds18B20_temp.byte[0] = ow_scratchpad[DS1820_LSB];
+    ds18B20_temp.byte[1] = ow_scratchpad[DS1820_MSB];
+
+    //ds18B20_temp.word <<= 4;  // Vorzeichenbit auf MSB Bit schieben
+    //ds18B20_temp.word /= 16;  // die letzten 4 Stellen wieder löschen
+
+    ds1820_float_result = ds18B20_temp.word * 0.0625;
+    
+    // printf_P(PSTR("\nDS18B20 T-Kanal: %d = %2.2f "),device_index,ds1820_float_result);
+    
+    ow.device_table[ow.device_table_index].result = (uint16_t)(ds1820_float_result * 10);  
+    ow.device_table[ow.device_table_index].result += 300;
+
+    pc.printf("\nTemperatur Kanal: %d = %d "),device_index,ow.device_table[device_index].result);
+  
+    // Clear flag to mark that no measurement is in progress
+    ds1820_request_pending = FALSE;
+  
+    // Flag für erfolgreiches Lesen setzen
+    ow.device_table[ow.device_table_index].status &= 0xf0;
+    ow.device_table[ow.device_table_index].status |= 3;
+  
+    return TRUE;
+}
+
+
+//-----------------------------------------------------------------------------
+
--- a/DS2482.h	Sat May 04 10:11:42 2013 +0000
+++ b/DS2482.h	Sat May 04 12:15:25 2013 +0000
@@ -113,34 +113,31 @@
     #define OW_RD_SCRATCHPAD    0xBE
     #define OW_SEARCH_ROM       0xF0
 
-    // ROM code structure
-    typedef struct sOW_ROM_CODE_ITEM
-    {
-        uint8_t adr;        // Adresse für den Baustein
-        uint8_t status;     // Status für den Wandler
-                            // bit 8 ist für Buskennung
-        uint16_t result;    // Ablage für Temperaturwert
-        uint16_t value_1;   // Ablage für Stromwert
-        uint16_t value_2;   // Ablage für Spannungwert
-        uint8_t rom[8];     // 8 Bytes for ROM code
-    } tOW_ROM_CODE_ITEM;
-
-
-    typedef struct sOW
-    {
-        uint8_t devices;    // Number of devices
-        uint8_t bus;        // Bus 0 oder 1
-        uint8_t device_table_index;
-        tOW_ROM_CODE_ITEM device_table[OW_MAX_DEVICES]; // OW-Device data table
-    } tOW;
-
     // ============================================================================
     
     class DS2482
     {
+      public:
+     
+        // ROM code structure
+        typedef struct sOW_ROM_CODE_ITEM
+        {
+            uint8_t adr;        // Adresse für den Baustein
+            uint8_t status;     // Status für den Wandler
+                                // bit 8 ist für Buskennung
+            uint16_t result;    // Ablage für Temperaturwert
+            uint16_t value_1;   // Ablage für Stromwert
+            uint16_t value_2;   // Ablage für Spannungwert
+            uint8_t rom[8];     // 8 Bytes for ROM code
+        } tOW_ROM_CODE_ITEM;
+ 
+        typedef struct sOW
+        {
+            uint8_t devices;    // Number of devices
+            uint8_t device_table_index;
+            tOW_ROM_CODE_ITEM device_table[OW_MAX_DEVICES]; // OW-Device data table
+        } tOW;
 
-      public:
-        
         // global vars
         tOW ow;
          
@@ -187,6 +184,9 @@
        
         void DS18XX_Read_Address(void);
         void start_conversion(void);
+        
+        bool ds1820_start_conversion(uint8_t command);
+        bool ds18B20_read_hrtemp(void);
  
       protected: