one wire driver

Dependents:   09_PT1000 10_PT1000 11_PT1000

Revision:
2:e9048135901b
Parent:
1:0950824b1ca3
Child:
3:e773b0d83471
--- 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;
+}
+
+
+//-----------------------------------------------------------------------------
+