Page 12 of 21
Chapter 15
Page 246
function only - needs a main program to call it
int oneWireScan(uint8_t pin, uint64_t serial[])
{
static int bitcount = 0;
static int deviceCount = 0;
if (bitcount > 63)
{
bitcount = 0;
deviceCount++;
return deviceCount;
}
if (bitcount == 0)
{
if (presence(pin) == 1)
{
bitcount = 0;
return deviceCount;
}
deviceCount = 0;
serial[deviceCount] = 0;
writeByte(pin, 0xF0);
};
int b1 = readBit(pin);
int b2 = readBit(pin);
if (b1 == 0 && b2 == 1)
{
serial[deviceCount] >>= 1;
writeBit(pin, 0);
bitcount++;
oneWireScan(pin, serial);
};
if (b1 == 1 && b2 == 0)
{
serial[deviceCount] >>= 1;
serial[deviceCount] |= 0x8000000000000000LL;
writeBit(pin, 1);
bitcount++;
oneWireScan(pin, serial);
};
if (b1 == 1 && b2 == 1)
{
bitcount = 0;
return deviceCount;
};
if (b1 == 0 && b2 == 0)
{
serial[deviceCount] >>= 1;
writeBit(pin, 0);
int bitposition = bitcount;
bitcount++;
oneWireScan(pin, serial);
bitcount = bitposition;
if (presence(pin) == 1)
{
bitposition = 0;
return 0;
}
writeByte(pin, 0xF0);
uint64_t temp = serial[deviceCount - 1] | (0x1LL << (bitcount));
int i;
uint64_t bit;
for (i = 0; i < bitcount + 1; i++)
{
bit = temp & 0x01LL;
temp >>= 1;
b1 = readBit(pin);
b2 = readBit(pin);
writeBit(pin, bit);
serial[deviceCount] >>= 1;
serial[deviceCount] |= (bit << 63);
}
bitcount++;
oneWireScan(pin, serial);
};
return deviceCount;
}