Exemple #1
0
void OneWireSearchRom(void) {

	unsigned char bit,bit_complementary,pozycja,buffer[NUM_OF_THERMS];
	for(unsigned char i=0;i < NUM_OF_THERMS;i++) {
		buffer[i]=0;
	}
	for(unsigned char i=0;i < NUM_OF_THERMS;i++) {
		pozycja=0;
		if (!OneWireReset()) return;
		OneWireWriteByte(0xF0);
		for(unsigned char bitNo=0;bitNo<64;bitNo++) {
		bit=OneWireReadBit();
		bit_complementary=OneWireReadBit();
		if ((!(bit|bit_complementary))&1) {
			if(buffer[pozycja+1]==0) {
			buffer[pozycja]++;
			}
		OneWireWriteBit((buffer[pozycja]-1));
		ROM[i][bitNo/8]|=((buffer[pozycja]-1))<<(bitNo%8);
		pozycja++;
		} 
		else {
			OneWireWriteBit(bit);
			ROM[i][bitNo/8]|=bit<<(bitNo%8);
		}
		}
		while(buffer[pozycja-1]==2) {
			buffer[pozycja]=0;
			pozycja--;
		}
	}
}
Exemple #2
0
	u8 DS18B20GetNext(u8 pin)
	{
		u8 m = 1;					// ROM Bit index
		u8 n = 0;					// ROM Byte index
		u8 k = 1;					// bit mask
		u8 x = 0;
		u8 discrepMarker = 0;				// discrepancy marker
		u8 g;						// Output bit
		u8 nxt;						// return value
		int flag;

		nxt = FALSE;					// set the next flag to false
		dowcrc = 0;					// reset the dowcrc

		flag = OneWireReset(pin);			// reset the 1-wire
		if(flag||doneFlag)				// no parts -> return false
		{
			lastDiscrep = 0;			// reset the search
			return FALSE;
		}
		// send SearchROM command for all eight bytes
		OneWireWrite(pin, SEARCHROM);
		do {
			x = 0;
			if(OneWireReadBit(pin) == 1) x = 2;
			//myDelay_10us(12);
			if(OneWireReadBit(pin) == 1 ) x |= 1;
			if(x == 3)
				break;
			else
			{
				if(x > 0)				// all devices coupled have 0 or 1
					g = x >> 1;			// bit write value for search
				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			// if equal to last pick 1
						g = (m == lastDiscrep);	// if not then pick 0
						// if 0 was picked then record position with mask k
						if (g == 0) discrepMarker = m;
				}
				if (g == 1)	// isolate bit in ROM[n] with mask k
					ROM[n] |= k;
				else
					ROM[n] &= ~k;
				OneWireWriteBit(pin, g);		// ROM search write
				m++;								// increment bit counter m
				k = k << 1;							// and shift the bit mask k
				if(k == 0)							// if the mask is 0 then go to new ROM
				{									// byte n and reset mask
					DS18B20_crc(ROM[n]);			// accumulate the CRC
					n++;
					k++;
				}
			}
		} while (n < 8);							// loop until through all ROM bytes 0-7
		if(m < 65 || dowcrc)						// if search was unsuccessful then
			lastDiscrep=0;							// reset the last discrepancy to 0
		else										// search was successful, so set lastDiscrep, lastOne, nxt
		{
			lastDiscrep = discrepMarker;
			doneFlag = (lastDiscrep == 0);
			nxt = TRUE;								// indicates search is not complete yet, more parts remain
		}
		return nxt;
	}