基于MSP430单片机的DS18B20的通用程序

作者: 佚名   发布日期:2006-04-22 10:42   查看数:0   出自:互联网
DS18B20
高精度、1-Wire数字温度计
The DS18B20 is a digital thermometer featuring ±0.5°C accuracy over a -10°C to +85°C range. Data is read out over a 1-Wire® serial bus in 2's complement format with 9 to 12 bits of resolution (user-programmable).

The DS18B20 offers thermostatic functionality with over-temperature (TH) and under-temperature (TL) user-programmable setpoints stored in on-chip EEPROM. An internal flag is set when the measured temperature is greater than TH or less than TL. If thermostatic operation is not required, the two bytes of EEPROM reserved for TH and TL may be used for general-purpose nonvolatile storage.

Each DS18B20 features a unique and unchangeable 64-bit silicon serial number, which serves as the bus address for the sensor. This allows multiple DS18B20 devices to coexist on the same 1-Wire bus. The DS18B20 may be locally powered via a 3.0V to 5.5V supply, or power can be derived from the 1-Wire data line ("parasite power").
这是利用MSP430控制的DS18B20程序

#include
#include

#define Seck 20 //1秒中的主程序的系数
#define NOP_1uS _nop_();_nop_();_nop_();_nop_();_nop_();_nop_()

//***********************************
sbit DQ=P2^0;
//***********************************
static unsigned char Power=0;
unsigned char Temper=0;
//----------------------------------
//功能:10us 级别延时
//----------------------------------
void Delay10us(unsigned char n)
{
while(n--)
{
NOP_1uS;NOP_1uS;NOP_1uS;
NOP_1uS;NOP_1uS;NOP_1uS;
NOP_1uS;
}
}
//-----------------------------------
//功能:写18B20
//-----------------------------------
void Write_18B20(unsigned char n)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ=0;
Delay10us(1);//延时13us 左右
DQ=n & 0x01;
n=n>>1;
Delay10us(5);//延时50us 以上
DQ=1;
}
}
//------------------------------------
//功能:读取18B20
//------------------------------------
unsigned char Read_18B20(void)
{
unsigned char i;
unsigned char temp;
for(i=0;i<8;i++)
{
temp=temp>>1;
DQ=0;
NOP_1uS;//延时1us
DQ=1;
NOP_1uS;NOP_1uS;//延时5us
NOP_1uS;NOP_1uS;NOP_1uS;
if(DQ==0)
{
temp=temp&0x7F;
}else
{
temp=temp|0x80;
}
Delay10us(5);//延时40us
DQ=1;
}
return temp;
}
//-----------------------------------
void Init (void)
{
DQ=0;
Delay10us(50);//延时500us
DQ=1;
Delay10us(9);//延时90us
if(DQ==1) //0001 1111b=1f
{
Power =0; //失败0
}else
{
Power++;
DQ=1;
}
}
//----------------------------------
void Skip(void)
{
Write_18B20(0xcc);
Power++;
}
//----------------------------------
void Convert (void)
{
Write_18B20(0x44);
Power++;
}
//----------------------------------
void Delay1S (void)
{
static unsigned int i=0;

if (i++==Seck) {i=0;Power++;}
}
//----------------------------------
void ReadDo (void)
{
Write_18B20(0xbe);
Power++;
}
//----------------------------------

void ReadTemp (void)
{
union
{
unsigned char Temp[2]; //单字节温度
int Tt; //2字节温度
}T;
T.Temp[1]=Read_18B20(); //读低位
T.Temp[0]=Read_18B20(); //读高位
//-----------------------------------
Power=0;
T.Tt <<= 4;
Temper=T.Temp[0];
}
/**********************************
函数指针定义
***********************************/
code void (code *SubTemp[])()=
{
Init,Skip,Convert,Delay1S,Init,Skip,ReadDo,ReadTemp
};
//*****************************
void GetTemp(void)
{
(*SubTemp[Power])();

}
//**************************************
相关文章(最新&最热)