Example #1
0
	u8 DS18B20StartMeasure(u8 pin, u8 num, u8 resolution)
	{
		u8 	res, busy = LOW;
		u8 	temp_lsb, temp_msb;
		u16	temp;

		switch (resolution)
		{
			case RES12BIT:	res = 0b01100000;	break;	// 12-bit resolution
			case RES11BIT:	res = 0b01000000;	break;	// 11-bit resolution
			case RES10BIT:	res = 0b00100000;	break;	// 10-bit resolution
			case  RES9BIT:	res = 0b00000000;	break;	//  9-bit resolution
			default:		res = 0b00000000;	break;	//  9-bit resolution
			/// NB: The power-up default of these bits is R0 = 1 and R1 = 1 (12-bit resolution)
		}
		
		if (!DS18B20Configure(pin, num, 0, 0, res)) return FALSE; // no alarm

		if (OneWireReset(pin)) return FALSE;

		if (num == SKIPROM)
		{
			// Skip ROM, address all devices
			OneWireWrite(pin, SKIPROM);
		}
		else
		{
			// Talk to a particular device
			if (!DS18B20MatchRom(pin, num)) return FALSE;
		}

		OneWireWrite(pin, CONVERT_T);		// Start temperature conversion
		return TRUE;
	}
Example #2
0
	u8 DS18B20Configure(u8 pin, u8 num, u8 TH, u8 TL, u8 config)
	{
		if (OneWireReset(pin)) return FALSE;
		if (num == SKIPROM)
		{
			// Skip ROM, address all devices
			OneWireWrite(pin, SKIPROM);
		}
		else
		{
			// Talk to a particular device
			DS18B20MatchRom(pin, num);
		}
		OneWireWrite(pin, WRITE_SCRATCHPAD);	// Allows the master to write 3 bytes of data to the scratchpad
		OneWireWrite(pin, TH);				// The first data byte is written into the TH register (byte 2 of the scratchpad)
		OneWireWrite(pin, TL);				// The second byte is written into the TL register (byte 3)
		OneWireWrite(pin, config);			// The third byte is written into the configuration register (byte 4)
		return TRUE;
	}
Example #3
0
	u8 DS18B20Read(u8 pin, u8 num, u8 resolution, DS18B20_Temperature * t)
	{
		u8 	res, busy = LOW;
		u8 	temp_lsb, temp_msb;
		u16	temp;

		switch (resolution)
		{
			case RES12BIT:	res = 0b01100000;	break;	// 12-bit resolution
			case RES11BIT:	res = 0b01000000;	break;	// 11-bit resolution
			case RES10BIT:	res = 0b00100000;	break;	// 10-bit resolution
			case  RES9BIT:	res = 0b00000000;	break;	//  9-bit resolution
			default:		res = 0b00000000;	break;	//  9-bit resolution
			/// NB: The power-up default of these bits is R0 = 1 and R1 = 1 (12-bit resolution)
		}
		
		if (!DS18B20Configure(pin, num, 0, 0, res)) return FALSE; // no alarm

		if (OneWireReset(pin)) return FALSE;

		if (num == SKIPROM)
		{
			// Skip ROM, address all devices
			OneWireWrite(pin, SKIPROM);
		}
		else
		{
			// Talk to a particular device
			if (!DS18B20MatchRom(pin, num)) return FALSE;
		}

		OneWireWrite(pin, CONVERT_T);		// Start temperature conversion

		while (busy == LOW)					// Wait while busy ( = bus is low)
			busy = OneWireRead(pin);

		if (OneWireReset(pin)) return FALSE;

		if (num == SKIPROM)
		{
			// Skip ROM, address all devices
			OneWireWrite(pin, SKIPROM);
		}
		else
		{
			// Talk to a particular device
			if (!DS18B20MatchRom(pin, num)) return FALSE;
		}

		OneWireWrite(pin, READ_SCRATCHPAD);// Read scratchpad

		temp_lsb = OneWireRead(pin);		// byte 0 of scratchpad : temperature lsb
		temp_msb = OneWireRead(pin);		// byte 1 of scratchpad : temperature msb

		if (OneWireReset(pin)) return FALSE;

		// Calculation
		// ---------------------------------------------------------------------
		//	Temperature Register Format
		//			BIT7	BIT6	BIT5	BIT4	BIT3	BIT2	BIT1	BIT0
		//	LS BYTE 2^3		2^2		2^1		2^0		2^-1	2^-2	2^-3	2^-4
		//			BIT15	BIT14	BIT13	BIT12	BIT11	BIT10	BIT9	BIT8
		//	MS BYTE S		S		S		S		S		2^6		2^5 	2^4
		//	S = SIGN

		temp = temp_msb;				
		temp = (temp << 8) + temp_lsb;	// combine msb & lsb into 16 bit variable
		
		if (temp_msb & 0b11111000)		// test if sign is set, i.e. negative
		{		
			t->sign = 1;
			temp = (temp ^ 0xFFFF) + 1;	// 2's complement conversion
		}
		else
		{
			t->sign = 0;
		}

		t->integer = (temp >> 4) & 0x7F;	// fractional part is removed, leaving only integer part

/*	
		t->fraction = 0;					// fractional part
		if (BitRead(temp, 0)) t->fraction +=  625;
		if (BitRead(temp, 1)) t->fraction += 1250;
		if (BitRead(temp, 2)) t->fraction += 2500;
		if (BitRead(temp, 3)) t->fraction += 5000;
*/
		t->fraction = (temp & 0x0F) * 625;
		t->fraction /= 100;					// two digits after decimal 

		return TRUE;
	}
Example #4
0
	u8 DS18B20ReadMeasure(u8 pin, u8 num, DS18B20_Temperature * t)
	{
		u8 	res, busy = LOW;
		u8 	temp_lsb, temp_msb;
		u16	temp;

		if (OneWireReset(pin)) return FALSE;

		if (num == SKIPROM)
		{
			// Skip ROM, address all devices
			OneWireWrite(pin, SKIPROM);
		}
		else
		{
			// Talk to a particular device
			if (!DS18B20MatchRom(pin, num)) return FALSE;
		}

		OneWireWrite(pin, READ_SCRATCHPAD);// Read scratchpad

		temp_lsb = OneWireRead(pin);		// byte 0 of scratchpad : temperature lsb
		temp_msb = OneWireRead(pin);		// byte 1 of scratchpad : temperature msb

		if (OneWireReset(pin)) return FALSE;

		// Calculation
		// ---------------------------------------------------------------------
		//	Temperature Register Format
		//			BIT7	BIT6	BIT5	BIT4	BIT3	BIT2	BIT1	BIT0
		//	LS BYTE 2^3		2^2		2^1		2^0		2^-1	2^-2	2^-3	2^-4
		//			BIT15	BIT14	BIT13	BIT12	BIT11	BIT10	BIT9	BIT8
		//	MS BYTE S		S		S		S		S		2^6		2^5 	2^4
		//	S = SIGN

		temp = temp_msb;				
		temp = (temp << 8) + temp_lsb;	// combine msb & lsb into 16 bit variable
		
		if (temp_msb & 0b11111000)		// test if sign is set, i.e. negative
		{		
			t->sign = 1;
			temp = (temp ^ 0xFFFF) + 1;	// 2's complement conversion
		}
		else
		{
			t->sign = 0;
		}

		t->integer = (temp >> 4) & 0x7F;	// fractional part is removed, leaving only integer part

/*	
		t->fraction = 0;					// fractional part
		if (BitRead(temp, 0)) t->fraction +=  625;
		if (BitRead(temp, 1)) t->fraction += 1250;
		if (BitRead(temp, 2)) t->fraction += 2500;
		if (BitRead(temp, 3)) t->fraction += 5000;
*/
		t->fraction = (temp & 0x0F) * 625;
		t->fraction /= 100;					// two digits after decimal 

		return TRUE;
	}
Example #5
0
	u8 DS18B20Read(u8 pin, u8 num, u8 resolution, TEMPERATURE * t)
	{
		u8 res, busy = 0;
		u8 temp_lsb, temp_msb;

		switch (resolution)
		{
			case RES12BIT:	res = Bin(01100000);	break;	// 12-bit resolution
			case RES11BIT:	res = Bin(01000000);	break;	// 11-bit resolution
			case RES10BIT:	res = Bin(00100000);	break;	// 10-bit resolution
			case  RES9BIT:	res = Bin(00000000);	break;	//  9-bit resolution
			default:		res = Bin(00000000);	break;	//  9-bit resolution
			/// NB: The power-up default of these bits is R0 = 1 and R1 = 1 (12-bit resolution)
		}
		DS18B20Configure(pin, num, 0, 0, res); // no alarm

		if (OneWireReset(pin)) return FALSE;

		if (num == SKIPROM)
		{
			// Skip ROM, address all devices
			OneWireWrite(pin, SKIPROM);
		}
		else
		{
			// Talk to a particular device
			DS18B20MatchRom(pin, num);
		}

		OneWireWrite(pin, CONVERT_T);		// Start temperature conversion

		while (busy == 0)					// Wait while busy ( = bus is low)
			busy = OneWireRead(pin);

		if (OneWireReset(pin)) return FALSE;

		if (num == SKIPROM)
		{
			// Skip ROM, address all devices
			OneWireWrite(pin, SKIPROM);
		}
		else
		{
			// Talk to a particular device
			DS18B20MatchRom(pin, num);
		}

		OneWireWrite(pin, READ_SCRATCHPAD);// Read scratchpad

		temp_lsb = OneWireRead(pin);		// byte 0 of scratchpad : temperature lsb
		temp_msb = OneWireRead(pin);		// byte 1 of scratchpad : temperature msb
		OneWireReset(pin);

		// Calculation
		// ---------------------------------------------------------------------
		//	Temperature Register Format
		//			BIT7	BIT6	BIT5	BIT4	BIT3	BIT2	BIT1	BIT0
		//	LS BYTE 2^3		2^2		2^1		2^0		2^-1	2^-2	2^-3	2^-4
		//			BIT15	BIT14	BIT13	BIT12	BIT11	BIT10	BIT9	BIT8
		//	MS BYTE S		S		S		S		S		2^6		2^5 	2^4
		//	S = SIGN

		if (temp_msb >= Bin(11111000))		// test if temperature is negative
		{		
			t->sign = 1;
			temp_msb -= Bin(11111000);
		}
		else
		{
			t->sign = 0;
		}

		t->integer = temp_lsb >> 4;			// fractional part is removed, it remains only integer part
		t->integer |= (temp_msb << 4);		// integer part from temp_msb is added
	
		t->fraction = 0;					// fractional part (
		if (BitRead(temp_lsb, 0)) t->fraction +=  625;
		if (BitRead(temp_lsb, 1)) t->fraction += 1250;
		if (BitRead(temp_lsb, 2)) t->fraction += 2500;
		if (BitRead(temp_lsb, 3)) t->fraction += 5000;
		t->fraction /= 100;					// two digits after decimal 

		return TRUE;
	}