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:
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 1:74dcd0c7d5d4 1 /* Copyright (c) 2012, Ivan Kravets <me@ikravets.com>, www.ikravets.com. MIT License
ivank 1:74dcd0c7d5d4 2 *
ivank 1:74dcd0c7d5d4 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ivank 1:74dcd0c7d5d4 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
ivank 1:74dcd0c7d5d4 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ivank 1:74dcd0c7d5d4 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ivank 1:74dcd0c7d5d4 7 * furnished to do so, subject to the following conditions:
ivank 1:74dcd0c7d5d4 8 *
ivank 1:74dcd0c7d5d4 9 * The above copyright notice and this permission notice shall be included in all copies or
ivank 1:74dcd0c7d5d4 10 * substantial portions of the Software.
ivank 1:74dcd0c7d5d4 11 *
ivank 1:74dcd0c7d5d4 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ivank 1:74dcd0c7d5d4 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ivank 1:74dcd0c7d5d4 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ivank 1:74dcd0c7d5d4 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ivank 1:74dcd0c7d5d4 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ivank 1:74dcd0c7d5d4 17 */
ivank 1:74dcd0c7d5d4 18
ivank 1:74dcd0c7d5d4 19 #include "OneWire.h"
ivank 1:74dcd0c7d5d4 20
ivank 1:74dcd0c7d5d4 21 uint8_t const OneWire::dscrc_table[] = {
ivank 1:74dcd0c7d5d4 22 0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
ivank 1:74dcd0c7d5d4 23 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
ivank 1:74dcd0c7d5d4 24 35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
ivank 1:74dcd0c7d5d4 25 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
ivank 1:74dcd0c7d5d4 26 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
ivank 1:74dcd0c7d5d4 27 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
ivank 1:74dcd0c7d5d4 28 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
ivank 1:74dcd0c7d5d4 29 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
ivank 1:74dcd0c7d5d4 30 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
ivank 1:74dcd0c7d5d4 31 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
ivank 1:74dcd0c7d5d4 32 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
ivank 1:74dcd0c7d5d4 33 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
ivank 1:74dcd0c7d5d4 34 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
ivank 1:74dcd0c7d5d4 35 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
ivank 1:74dcd0c7d5d4 36 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
ivank 1:74dcd0c7d5d4 37 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53
ivank 1:74dcd0c7d5d4 38 };
ivank 1:74dcd0c7d5d4 39
ivank 1:74dcd0c7d5d4 40 OneWire::OneWire(PinName pin) : DIO(pin)
ivank 1:74dcd0c7d5d4 41 {
ivank 1:74dcd0c7d5d4 42 DIO.output();
ivank 1:74dcd0c7d5d4 43 DIO = 0;
ivank 1:74dcd0c7d5d4 44 DIO.input();
ivank 1:74dcd0c7d5d4 45
ivank 1:74dcd0c7d5d4 46 reset();
ivank 1:74dcd0c7d5d4 47 findDevAddresses();
ivank 1:74dcd0c7d5d4 48 }
ivank 1:74dcd0c7d5d4 49
ivank 1:74dcd0c7d5d4 50
ivank 1:74dcd0c7d5d4 51 uint8_t OneWire::reset(void)
ivank 1:74dcd0c7d5d4 52 {
ivank 1:74dcd0c7d5d4 53 uint8_t presence;
ivank 1:74dcd0c7d5d4 54 DIO.output();
ivank 1:74dcd0c7d5d4 55 DIO = 0;
ivank 1:74dcd0c7d5d4 56 wait_us(480);
ivank 1:74dcd0c7d5d4 57 DIO.input();
ivank 1:74dcd0c7d5d4 58 wait_us(70);
ivank 1:74dcd0c7d5d4 59 presence = DIO;
ivank 1:74dcd0c7d5d4 60 wait_us(410);
ivank 1:74dcd0c7d5d4 61 return(presence);
ivank 1:74dcd0c7d5d4 62 }
ivank 1:74dcd0c7d5d4 63
ivank 1:74dcd0c7d5d4 64 uint8_t OneWire::readBit(void)
ivank 1:74dcd0c7d5d4 65 {
ivank 1:74dcd0c7d5d4 66 uint8_t retval;
ivank 1:74dcd0c7d5d4 67 wait_us(1);
ivank 1:74dcd0c7d5d4 68 DIO.output();
ivank 1:74dcd0c7d5d4 69 DIO = 0;
ivank 1:74dcd0c7d5d4 70 wait_us(2);
ivank 1:74dcd0c7d5d4 71 DIO.input();
ivank 1:74dcd0c7d5d4 72 wait_us(10);
ivank 1:74dcd0c7d5d4 73 retval=DIO;
ivank 1:74dcd0c7d5d4 74 wait_us(48);
ivank 1:74dcd0c7d5d4 75 return(retval);
ivank 1:74dcd0c7d5d4 76 }
ivank 1:74dcd0c7d5d4 77
ivank 1:74dcd0c7d5d4 78 uint8_t OneWire::readByte(void)
ivank 1:74dcd0c7d5d4 79 {
ivank 1:74dcd0c7d5d4 80 uint8_t i, value=0;
ivank 1:74dcd0c7d5d4 81 for (i=0; i<8; i++) {
ivank 2:07da4deb7135 82 if (readBit())
ivank 2:07da4deb7135 83 value |= 0x01<<i;
ivank 1:74dcd0c7d5d4 84 }
ivank 2:07da4deb7135 85 return value;
ivank 1:74dcd0c7d5d4 86 }
ivank 1:74dcd0c7d5d4 87
ivank 1:74dcd0c7d5d4 88 void OneWire::writeBit(uint8_t bitval)
ivank 1:74dcd0c7d5d4 89 {
ivank 1:74dcd0c7d5d4 90 wait_us(1);
ivank 1:74dcd0c7d5d4 91 DIO.output();
ivank 1:74dcd0c7d5d4 92 DIO = 0;
ivank 1:74dcd0c7d5d4 93 wait_us(10);
ivank 2:07da4deb7135 94 if (bitval)
ivank 2:07da4deb7135 95 DIO = 1;
ivank 1:74dcd0c7d5d4 96 wait_us(50);
ivank 1:74dcd0c7d5d4 97 DIO.input();
ivank 1:74dcd0c7d5d4 98 }
ivank 1:74dcd0c7d5d4 99
ivank 1:74dcd0c7d5d4 100 void OneWire::writeByte(uint8_t val)
ivank 1:74dcd0c7d5d4 101 {
ivank 2:07da4deb7135 102 uint8_t i, temp;
ivank 1:74dcd0c7d5d4 103 for (i=0; i<8; i++) {
ivank 1:74dcd0c7d5d4 104 temp = val>>i;
ivank 1:74dcd0c7d5d4 105 temp &= 0x01;
ivank 1:74dcd0c7d5d4 106 writeBit(temp);
ivank 1:74dcd0c7d5d4 107 }
ivank 1:74dcd0c7d5d4 108 }
ivank 1:74dcd0c7d5d4 109
ivank 1:74dcd0c7d5d4 110 uint8_t OneWire::calcCRC( uint8_t x)
ivank 1:74dcd0c7d5d4 111 {
ivank 1:74dcd0c7d5d4 112 return dscrc_table[dowcrc^x];
ivank 1:74dcd0c7d5d4 113 }
ivank 1:74dcd0c7d5d4 114
ivank 1:74dcd0c7d5d4 115 void OneWire::skip()
ivank 1:74dcd0c7d5d4 116 {
ivank 1:74dcd0c7d5d4 117 writeByte(0xcc);
ivank 1:74dcd0c7d5d4 118 }
ivank 1:74dcd0c7d5d4 119
ivank 1:74dcd0c7d5d4 120 void OneWire::select(uint8_t* devAddr)
ivank 1:74dcd0c7d5d4 121 {
ivank 1:74dcd0c7d5d4 122 writeByte(0x55);
ivank 1:74dcd0c7d5d4 123 for (uint8_t i=0; i<8; i++) {
ivank 1:74dcd0c7d5d4 124 writeByte(devAddr[i]);
ivank 1:74dcd0c7d5d4 125 }
ivank 1:74dcd0c7d5d4 126 }
ivank 1:74dcd0c7d5d4 127
ivank 1:74dcd0c7d5d4 128 void OneWire::sendGlobalCommand(uint8_t cmd)
ivank 1:74dcd0c7d5d4 129 {
ivank 1:74dcd0c7d5d4 130 reset();
ivank 1:74dcd0c7d5d4 131 skip();
ivank 1:74dcd0c7d5d4 132 writeByte(cmd);
ivank 1:74dcd0c7d5d4 133 }
ivank 1:74dcd0c7d5d4 134
ivank 1:74dcd0c7d5d4 135 void OneWire::resetSearch(void)
ivank 1:74dcd0c7d5d4 136 {
ivank 1:74dcd0c7d5d4 137 lastDiscrep = 0;
ivank 1:74dcd0c7d5d4 138 doneFlag = FALSE;
ivank 1:74dcd0c7d5d4 139 }
ivank 1:74dcd0c7d5d4 140
ivank 1:74dcd0c7d5d4 141 uint8_t OneWire::search(uint8_t* devAddr)
ivank 1:74dcd0c7d5d4 142 {
ivank 1:74dcd0c7d5d4 143 uint8_t m = 1; // ROM Bit index
ivank 1:74dcd0c7d5d4 144 uint8_t n = 0; // ROM Byte index
ivank 1:74dcd0c7d5d4 145 uint8_t k = 1; // bit mask
ivank 1:74dcd0c7d5d4 146 uint8_t x = 0;
ivank 1:74dcd0c7d5d4 147 uint8_t discrepMarker = 0; // discrepancy marker
ivank 1:74dcd0c7d5d4 148 uint8_t g; // Output bit
ivank 1:74dcd0c7d5d4 149 uint8_t nxt; // return value
ivank 1:74dcd0c7d5d4 150 uint8_t flag;
ivank 1:74dcd0c7d5d4 151 nxt = FALSE; // set the next flag to false
ivank 1:74dcd0c7d5d4 152 dowcrc = 0; // reset the dowcrc
ivank 1:74dcd0c7d5d4 153 DeviceAddress ROM;
ivank 1:74dcd0c7d5d4 154
ivank 1:74dcd0c7d5d4 155 flag = reset(); // reset the 1-Wire
ivank 1:74dcd0c7d5d4 156 if (flag||doneFlag) { // no parts -> return false
ivank 1:74dcd0c7d5d4 157 lastDiscrep = 0; // reset the search
ivank 1:74dcd0c7d5d4 158 return FALSE;
ivank 1:74dcd0c7d5d4 159 }
ivank 1:74dcd0c7d5d4 160
ivank 1:74dcd0c7d5d4 161 writeByte(0xF0); // send SearchROM command
ivank 1:74dcd0c7d5d4 162 do { // for all eight bytes
ivank 1:74dcd0c7d5d4 163 x = 0;
ivank 2:07da4deb7135 164 if (readBit()) x = 2;
ivank 1:74dcd0c7d5d4 165 wait_us(120);
ivank 2:07da4deb7135 166 if (readBit()) x |= 1; // and its complement
ivank 1:74dcd0c7d5d4 167 if (x ==3) // there are no devices on the 1-Wire
ivank 1:74dcd0c7d5d4 168 break;
ivank 1:74dcd0c7d5d4 169 else {
ivank 1:74dcd0c7d5d4 170 if (x>0) // all devices coupled have 0 or 1
ivank 1:74dcd0c7d5d4 171 g = x>>1; // bit write value for search
ivank 1:74dcd0c7d5d4 172 else {
ivank 1:74dcd0c7d5d4 173 // if this discrepancy is before the last discrepancy on a previous Next then pick the same as last time
ivank 1:74dcd0c7d5d4 174 if (m<lastDiscrep)
ivank 1:74dcd0c7d5d4 175 g = ((ROM[n]&k)>0);
ivank 1:74dcd0c7d5d4 176 else // if equal to last pick 1
ivank 1:74dcd0c7d5d4 177 g = (m==lastDiscrep); // if not then pick 0
ivank 1:74dcd0c7d5d4 178 // if 0 was picked then record position with mask k
ivank 1:74dcd0c7d5d4 179 if (g==0) discrepMarker = m;
ivank 1:74dcd0c7d5d4 180 }
ivank 1:74dcd0c7d5d4 181 if (g==1) // isolate bit in ROM[n] with mask k
ivank 1:74dcd0c7d5d4 182 ROM[n] |= k;
ivank 1:74dcd0c7d5d4 183 else
ivank 1:74dcd0c7d5d4 184 ROM[n] &= ~k;
ivank 1:74dcd0c7d5d4 185 writeBit(g); // ROM search write
ivank 1:74dcd0c7d5d4 186 m++; // increment bit counter m
ivank 1:74dcd0c7d5d4 187 k = k<<1; // and shift the bit mask k
ivank 1:74dcd0c7d5d4 188 if (k==0) { // if the mask is 0 then go to new ROM // byte n and reset mask
ivank 1:74dcd0c7d5d4 189 dowcrc = calcCRC(ROM[n]); // accumulate the CRC
ivank 1:74dcd0c7d5d4 190 n++;
ivank 1:74dcd0c7d5d4 191 k++;
ivank 1:74dcd0c7d5d4 192 }
ivank 1:74dcd0c7d5d4 193 }
ivank 1:74dcd0c7d5d4 194 } while (n<8); //loop until through all ROM bytes 0-7
ivank 1:74dcd0c7d5d4 195 if (m<65||dowcrc) // if search was unsuccessful then
ivank 1:74dcd0c7d5d4 196 lastDiscrep=0; // reset the last discrepancy to 0
ivank 1:74dcd0c7d5d4 197 else { // search was successful, so set lastDiscrep, lastOne, nxt
ivank 1:74dcd0c7d5d4 198 lastDiscrep = discrepMarker;
ivank 1:74dcd0c7d5d4 199 doneFlag = (lastDiscrep==0);
ivank 1:74dcd0c7d5d4 200 nxt = TRUE; // indicates search is not complete yet, more parts remain
ivank 1:74dcd0c7d5d4 201 }
ivank 1:74dcd0c7d5d4 202
ivank 1:74dcd0c7d5d4 203 for (uint8_t i = 0; i < 8; i++)
ivank 1:74dcd0c7d5d4 204 devAddr[i] = ROM[i];
ivank 1:74dcd0c7d5d4 205
ivank 1:74dcd0c7d5d4 206 return nxt;
ivank 1:74dcd0c7d5d4 207 }
ivank 1:74dcd0c7d5d4 208
ivank 1:74dcd0c7d5d4 209 void OneWire::findDevAddresses(void)
ivank 1:74dcd0c7d5d4 210 {
ivank 1:74dcd0c7d5d4 211 foundDevNum = 0;
ivank 1:74dcd0c7d5d4 212 resetSearch();
ivank 1:74dcd0c7d5d4 213
ivank 1:74dcd0c7d5d4 214 DeviceAddress devAddr;
ivank 1:74dcd0c7d5d4 215 while (search(devAddr)) {
ivank 1:74dcd0c7d5d4 216 foundDevNum++;
ivank 1:74dcd0c7d5d4 217 }
ivank 1:74dcd0c7d5d4 218 delete devAddr;
ivank 1:74dcd0c7d5d4 219
ivank 1:74dcd0c7d5d4 220 wait_us(250);
ivank 1:74dcd0c7d5d4 221 resetSearch();
ivank 1:74dcd0c7d5d4 222
ivank 1:74dcd0c7d5d4 223 foundDevAdresses = (DeviceAddresses*) malloc(foundDevNum * sizeof(int));
ivank 1:74dcd0c7d5d4 224 for (uint8_t i = 0; i < foundDevNum; i++) {
ivank 1:74dcd0c7d5d4 225 foundDevAdresses[i] = (DeviceAddress*) malloc(sizeof(DeviceAddress));
ivank 1:74dcd0c7d5d4 226 search((*foundDevAdresses)[i]);
ivank 1:74dcd0c7d5d4 227 }
ivank 1:74dcd0c7d5d4 228
ivank 1:74dcd0c7d5d4 229 printf("OneWire Bus: Found %d devices\r\n", getFoundDevNum() );
ivank 1:74dcd0c7d5d4 230 }
ivank 1:74dcd0c7d5d4 231
ivank 1:74dcd0c7d5d4 232 DeviceAddresses* OneWire::getFoundDevAddresses()
ivank 1:74dcd0c7d5d4 233 {
ivank 1:74dcd0c7d5d4 234 return foundDevAdresses;
ivank 1:74dcd0c7d5d4 235 }
ivank 1:74dcd0c7d5d4 236
ivank 1:74dcd0c7d5d4 237 uint8_t OneWire::getFoundDevNum()
ivank 1:74dcd0c7d5d4 238 {
ivank 1:74dcd0c7d5d4 239 return foundDevNum;
ivank 2:07da4deb7135 240 }