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);
    }
 }
Committer:
ivank
Date:
Tue Dec 18 12:12:21 2012 +0000
Revision:
3:7bd102cb73e0
Parent:
OneWire.h@2:07da4deb7135
- moved .h files to '_headers' folders; - added MIT License to all files

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ivank 3:7bd102cb73e0 1 /* Copyright (c) 2012, Ivan Kravets <me@ikravets.com>, www.ikravets.com. MIT License
ivank 3:7bd102cb73e0 2 *
ivank 3:7bd102cb73e0 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ivank 3:7bd102cb73e0 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
ivank 3:7bd102cb73e0 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ivank 3:7bd102cb73e0 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ivank 3:7bd102cb73e0 7 * furnished to do so, subject to the following conditions:
ivank 3:7bd102cb73e0 8 *
ivank 3:7bd102cb73e0 9 * The above copyright notice and this permission notice shall be included in all copies or
ivank 3:7bd102cb73e0 10 * substantial portions of the Software.
ivank 3:7bd102cb73e0 11 *
ivank 3:7bd102cb73e0 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ivank 3:7bd102cb73e0 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ivank 3:7bd102cb73e0 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ivank 3:7bd102cb73e0 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ivank 3:7bd102cb73e0 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ivank 3:7bd102cb73e0 17 */
ivank 3:7bd102cb73e0 18
ivank 0:b9cd1d4dcc1f 19 #ifndef ONEWIRE_H
ivank 0:b9cd1d4dcc1f 20 #define ONEWIRE_H
ivank 0:b9cd1d4dcc1f 21
ivank 0:b9cd1d4dcc1f 22 #include "mbed.h"
ivank 0:b9cd1d4dcc1f 23
ivank 0:b9cd1d4dcc1f 24 #define FALSE 0
ivank 0:b9cd1d4dcc1f 25 #define TRUE 1
ivank 0:b9cd1d4dcc1f 26
ivank 0:b9cd1d4dcc1f 27 #define ONEWIRE_DS18B20_FAMILY 0x28
ivank 0:b9cd1d4dcc1f 28
ivank 0:b9cd1d4dcc1f 29 enum OneWireDeviceCmd {
ivank 0:b9cd1d4dcc1f 30 SET_TEMPERATURE_RESOLUTION = 0,
ivank 0:b9cd1d4dcc1f 31 GET_TEMPERATURE
ivank 0:b9cd1d4dcc1f 32 };
ivank 0:b9cd1d4dcc1f 33
ivank 0:b9cd1d4dcc1f 34 typedef uint8_t DeviceAddress[8];
ivank 0:b9cd1d4dcc1f 35 typedef DeviceAddress* DeviceAddresses;
ivank 0:b9cd1d4dcc1f 36
ivank 0:b9cd1d4dcc1f 37 class OneWire {
ivank 0:b9cd1d4dcc1f 38 public:
ivank 0:b9cd1d4dcc1f 39 OneWire(PinName pin);
ivank 0:b9cd1d4dcc1f 40
ivank 0:b9cd1d4dcc1f 41 uint8_t reset(void);
ivank 0:b9cd1d4dcc1f 42 uint8_t readByte(void);
ivank 0:b9cd1d4dcc1f 43 void writeByte(uint8_t val);
ivank 0:b9cd1d4dcc1f 44 void select(uint8_t* devAddr);
ivank 0:b9cd1d4dcc1f 45 void resetSearch(void);
ivank 0:b9cd1d4dcc1f 46
ivank 0:b9cd1d4dcc1f 47 uint8_t search(uint8_t* devAddr);
ivank 0:b9cd1d4dcc1f 48 void sendGlobalCommand(uint8_t);
ivank 0:b9cd1d4dcc1f 49 DeviceAddresses* getFoundDevAddresses();
ivank 0:b9cd1d4dcc1f 50 uint8_t getFoundDevNum();
ivank 0:b9cd1d4dcc1f 51
ivank 0:b9cd1d4dcc1f 52 private:
ivank 0:b9cd1d4dcc1f 53 DigitalInOut DIO;
ivank 0:b9cd1d4dcc1f 54 static const uint8_t dscrc_table[];
ivank 0:b9cd1d4dcc1f 55
ivank 0:b9cd1d4dcc1f 56 DeviceAddresses* foundDevAdresses;
ivank 0:b9cd1d4dcc1f 57 uint8_t foundDevNum;
ivank 0:b9cd1d4dcc1f 58
ivank 0:b9cd1d4dcc1f 59 uint8_t lastDiscrep;
ivank 0:b9cd1d4dcc1f 60 uint8_t doneFlag;
ivank 0:b9cd1d4dcc1f 61 uint8_t dowcrc;
ivank 0:b9cd1d4dcc1f 62
ivank 0:b9cd1d4dcc1f 63 uint8_t readBit(void);
ivank 0:b9cd1d4dcc1f 64 void writeBit(uint8_t bitval);
ivank 0:b9cd1d4dcc1f 65
ivank 0:b9cd1d4dcc1f 66 void skip();
ivank 0:b9cd1d4dcc1f 67 uint8_t calcCRC(uint8_t x);
ivank 0:b9cd1d4dcc1f 68 void findDevAddresses(void);
ivank 0:b9cd1d4dcc1f 69 };
ivank 0:b9cd1d4dcc1f 70
ivank 2:07da4deb7135 71 #include "OneWireDevice.h"
ivank 0:b9cd1d4dcc1f 72
ivank 0:b9cd1d4dcc1f 73 #endif