⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tempture.c

📁 温度采集程序。在keil51下经过调试
💻 C
字号:

#include <includes.h>

unsigned char	temp_lsb, temp_msb;
extern   char	Temperature;

void delay1(unsigned int i);
unsigned char ow_reset(void);
unsigned char read_bit(void);
void write_bit(char bitval);
unsigned char read_byte(void);
void write_byte(char val);
/****************************************************************************/
//
// DELAY - with an 22MHz crystal.
// Calling the routine takes about 10us, and then
// each count takes another 7us.
//
/****************************************************************************/
void delay1(unsigned int i)			// 每一个延时为:7us
{
	int s;
	for(s = 0; s < i; s++);
}

/****************************************************************************/
//
// OW_RESET - performs a reset on the one-wire bus and
// returns the presence detect. Reset is 480us, so delay
// value is (480-10)/7 = 70. Presence checked
// another 70us later, so delay is (70-10)/7 = 9.
//
/****************************************************************************/
unsigned char ow_reset(void)
{
	unsigned char presence;

	DQ = 0;						// pull DQ line low

	delay1(70);					// leave it low for 480us

	DQ = 1;						// allow line to return high

	delay1(9);					// wait for presence

	presence = DQ;				// get presence signal

	delay1(52);					// wait for end of timeslot

	return(presence);			// presence signal returned
}								// 0 = presence, 1 = no part
/****************************************************************************/
//
// READ_BIT - reads a bit from the one-wire bus. The delay
// required for a read is 15us, so the DELAY routine won't work.
// We put our own delay function in this routine in the form of a
// for() loop.
//
/****************************************************************************/
unsigned char read_bit(void)
{
	unsigned char i;

	DQ = 0;						// pull DQ low to start timeslot
	DQ = 1;						// then return high

	for (i = 0; i < 3; i++);	// delay 15us from start of timeslot

	return(DQ);					// return value of DQ line
}
/****************************************************************************/
//
// WRITE_BIT - writes a bit to the one-wire bus, passed in bitval.
//
/****************************************************************************/
void write_bit(char bitval)
{
	DQ = 0;						// pull DQ low to start timeslot
	if(bitval == 1)
		DQ = 1;					// return DQ high if write 1

	delay1(14);					// hold value for remainder of timeslot

	DQ = 1;
}								// Delay provides 7us per loop, plus 10us. Therefore delay(14) = 108us
/****************************************************************************/
//
// READ_BYTE - reads a byte from the one-wire bus.
//
/****************************************************************************/
unsigned char read_byte(void)
{
	unsigned char i;
	unsigned char value = 0;

	for (i = 0; i < 8; i++)
	{
		if(read_bit())
			value |= 0x01 << i;		// reads byte in, one byte at a time and then
									// shifts it left
		delay1(15);					// wait for rest of timeslot
	}

	return(value);
}
/****************************************************************************/
//
// WRITE_BYTE - writes a byte to the one-wire bus.
//
/****************************************************************************/
void write_byte(char val)
{
	unsigned char i;
	unsigned char temp;

	for (i = 0; i < 8; i++)		// writes byte, one bit at a time
	{
		temp = val >> i;		// shifts val right 'i' spaces
		temp &= 0x01;			// copy that bit to temp
		write_bit(temp);		// write bit in temp into
	}

	delay1(14);
}

/****************************************************************************/
//
// 读温度函数
//
/****************************************************************************/
void Read_Temperature(void)
{
	char get[10];
	unsigned char tempH, tempL;
	int  k;

	ow_reset();

	write_byte(0xCC);					// Skip ROM
	write_byte(0x44);					// Start Conversion

	delay1(14);

	ow_reset();

	write_byte(0xCC);					// Skip ROM
	write_byte(0xBE);					// Read Scratch Pad

	for (k = 0; k < 9; k++)
		get[k] = read_byte();

	tempH = get[1];						// Sign byte + lsbit
	tempL = get[0];						// Temp data plus lsb

	temp_lsb = tempL;
	temp_msb = tempH;

	if (tempH < 0x80)
		tempL >>= 1;					// shift to get whole degree

	if (tempH >= 0x80)
	{
		tempL = (~tempL) + 1;			// twos complement

		tempL >>= 1;					// shift to get whole degree

		tempL = ((-1)*tempL);			// add sign bit
	}

	Temperature = tempL;				// ready for conversion to Fahrenheit
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -