static int ow_check_crc(romid_t romid) { return ow_crc(NULL, NULL, (uint8_t *)&romid, sizeof(romid)) == 0; }
// NEXT // The Next function searches for the next device on the 1-wire bus. If // there are no more devices on the 1-wire then false is returned. // unsigned char Next(void) { unsigned char m = 1; // ROM Bit index unsigned char n = 0; // ROM Byte index unsigned char k = 1; // bit mask unsigned char x = 0; unsigned char discrepMarker = 0; // discrepancy marker unsigned char g; // Output bit unsigned char nxt = FALSE; // return value int flag; // reset the 1-wire flag = ow_reset(); // reset the dowcrc dowcrc = 0; // no parts -> return false if(flag||doneFlag) { // reset the search lastDiscrep = 0; return FALSE; } // send SearchROM command write_byte(0xF0); // for all eight bytes do { x = 0; if(read_bit()==1) x = 2; uDelay(120); if(read_bit()==1) x |= 1; uDelay(120); if(x ==3) break; else { // all devices coupled have 0 or 1 if(x>0) // bit write value for search g = x>>1; else { // if this discrepancy is before the last // discrepancy on a previous Next then pick // the same as last time if(m<lastDiscrep) g = ((ROM[n]&k)>0); else g = (m==lastDiscrep); // if equal to last pick 1 // if not then pick 0 // if 0 was picked then record // position with mask k if (g==0) discrepMarker = m; } // isolate bit in ROM[n] with mask k if(g==1) ROM[n] |= k; else ROM[n] &= ~k; // ROM search write write_bit(g); // increment bit counter m m++; // and shift the bit mask k k = k<<1; // if the mask is 0 then go to new ROM if(k==0) { // accumulate the CRC ow_crc(ROM[n]); // byte n and reset mask n++; k++; } } } while(n<8); // if search was unsuccessful then // reset the last discrepancy to 0 if(m<65||dowcrc) lastDiscrep=0; else { // search was successful, so set lastDiscrep, // lastOne, nxt; lastDiscrep = discrepMarker; doneFlag = (lastDiscrep==0); // indicates search is not complete yet, more parts remain nxt = TRUE; } return nxt; }