OneWire Library lets you access 1-wire devices made by Maxim/Dallas, such as temperature sensors

Dependents:   OneWireTest mbed_blinky Affich_Lum_Moist Projetv0 ... more

#include "mbed.h"
#include "OneWire.h"

OneWire owBus(p21);

int main()
{
    char _id[16];
    DeviceAddresses* devAddresses = owBus.getFoundDevAddresses();
    uint8_t foundNum = owBus.getFoundDevNum();
    printf("OneWire: found %d devices\r\n", foundNum);

    while(1) {
        OneWireDeviceTemperature::startConversationForAll(&owBus, OWTEMP_11_BIT);
        for (uint8_t i = 0; i < foundNum; i++) {
            OneWireDevice* owDevice = OneWireDeviceFactory::init(&owBus, (*devAddresses)[i]);
            
            if (owDevice->getFamily() != ONEWIRE_DS18B20_FAMILY)    // currently only DS18B20 supports
                continue;

            owDevice->generateId(_id);
            printf("OneWire: device #%s = %.4f*C\r\n", _id, (float) owDevice->sendGetCommand(GET_TEMPERATURE));
            delete owDevice;            
        }
        
        wait(5);
    }
 }
Revision:
2:07da4deb7135
Parent:
1:74dcd0c7d5d4
--- a/OneWire.cpp	Mon Dec 17 13:34:57 2012 +0000
+++ b/OneWire.cpp	Tue Dec 18 11:40:28 2012 +0000
@@ -17,7 +17,6 @@
  */
 
 #include "OneWire.h"
-#include "ds18b20.h"
 
 uint8_t const OneWire::dscrc_table[] = {
     0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
@@ -80,28 +79,27 @@
 {
     uint8_t i, value=0;
     for (i=0; i<8; i++) {
-        if (readBit()) value|=0x01<<i;
+        if (readBit())
+            value |= 0x01<<i;
     }
-    return(value);
+    return value;
 }
 
-
 void OneWire::writeBit(uint8_t bitval)
 {
     wait_us(1);
     DIO.output();
     DIO = 0;
     wait_us(10);
-    if (bitval==1) DIO = 1;
+    if (bitval)
+        DIO = 1;
     wait_us(50);
     DIO.input();
 }
 
-
 void OneWire::writeByte(uint8_t val)
 {
-    uint8_t i;
-    uint8_t temp;
+    uint8_t i, temp;
     for (i=0; i<8; i++) {
         temp = val>>i;
         temp &= 0x01;
@@ -119,7 +117,6 @@
     writeByte(0xcc);
 }
 
-
 void OneWire::select(uint8_t* devAddr)
 {
     writeByte(0x55);
@@ -135,7 +132,6 @@
     writeByte(cmd);
 }
 
-
 void OneWire::resetSearch(void)
 {
     lastDiscrep = 0;
@@ -165,9 +161,9 @@
     writeByte(0xF0); // send SearchROM command
     do { // for all eight bytes
         x = 0;
-        if (readBit()==1) x = 2;
+        if (readBit()) x = 2;
         wait_us(120);
-        if (readBit()==1) x |= 1; // and its complement
+        if (readBit()) x |= 1; // and its complement
         if (x ==3) // there are no devices on the 1-Wire
             break;
         else {
@@ -241,71 +237,4 @@
 uint8_t OneWire::getFoundDevNum()
 {
     return foundDevNum;
-}
-
-
-
-// ======================== OneWireDevice Class =============================
-OneWireDevice* OneWireDeviceFactory::init(OneWire* ow, DeviceAddress address)
-{
-    switch (address[0]) {
-        case ONEWIRE_DS18B20_FAMILY:
-            return new OneWireDeviceDS18B20(ow, address);
-    }
-
-    return NULL;
-}
-
-OneWireDevice::OneWireDevice(OneWire* ow, DeviceAddress devAddr)
-{
-    owBus = ow;
-    for (uint8_t i = 0; i < 8; i++)
-        address[i] = devAddr[i];
-
-
-}
-
-void OneWireDevice::generateId(char* id)
-{
-    sprintf(id, "%02X%02X%02X%02X%02X%02X%02X%02X", address[0], address[1],address[2],address[3],address[4],address[5],address[6],address[7]);
-}
-
-uint8_t OneWireDevice::getFamily()
-{
-    return address[0];
-}
-
-
-void OneWireDeviceTemperature::startConversationForAll(OneWire* owBus, uint8_t resolution)
-{
-    DeviceAddresses* devAddresses = owBus->getFoundDevAddresses();
-    for (uint8_t i = 0; i < owBus->getFoundDevNum(); i++) {
-        OneWireDevice* owDevice = OneWireDeviceFactory::init(owBus, (*devAddresses)[i]);
-        if (owDevice->getFamily() != ONEWIRE_DS18B20_FAMILY)
-            continue;
-
-        owDevice->sendSetCommand(SET_TEMPERATURE_RESOLUTION, resolution);
-        delete owDevice;
-    }
-    //delete devAddresses;
-
-//    owBus->sendGlobalCommand(0xcc); // Skip ROM command.
-    owBus->sendGlobalCommand(0x44);
-//    wait_us(750);
-
-    switch (resolution) {
-        case OWTEMP_9_BIT:
-            wait_us(94);
-            break;
-        case OWTEMP_10_BIT:
-            wait_us(188);
-            break;
-        case OWTEMP_11_BIT:
-            wait_us(375);
-            break;
-        case OWTEMP_12_BIT:
-        default:
-            wait_us(750);
-            break;
-    }
-};
\ No newline at end of file
+}
\ No newline at end of file