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