|
//Ds18b20初始化,
bit Ds18b20Init()
{
bit i;
DSPORT=1;
_nop_();
DSPORT=0;//拉低總線
Delaylms(500); //拉低500秒,拉低480秒~960秒即可
DSPORT=1;//釋放總線
Delaylms(68);//延時(shí)68秒。等待DS18b20響應(yīng)
i=DSPORT;
Delaylms(150);
DSPORT=1;
_nop_();
return (i);
}
//寫函數(shù)
void Ds18b20WriteByte(uchar dat)
{
uchar i,j;
for(i=0;j<8;j++)
{
DSPORT=0;
Delaylms(1); //延時(shí)1us
DSPORT=dat&0x01;
Delaylms(77);//延時(shí)77us
DSPORT=1;
_nop_();
dat>>=1;
}
}
//讀函數(shù)
uchar Ds18b20ReadByte()
{
uint i,j;
uchar bi,byte;
for(i=8;j>0;j--)
{
DSPORT=0;
i++;
DSPORT=1;
i++;
i++;
bi=DSPORT;
Delaylms(77);
DSPORT=1;
_nop_();
byte=(byte>>1)|(bi<<7);
}
return byte;
}
//溫度轉(zhuǎn)換指令
void Ds18b20Changetemp()
{
Ds18b20Init();
Delaylms(1);
Ds18b20WriteByte(0xcc);
Ds18b20WriteByte(0x44);
}
//溫度讀取指令
void Ds18b20ReadTempcom()
{
Ds18b20Init();
Delaylms(1);
Ds18b20WriteByte(0xcc);
Ds18b20WriteByte(0xbe);//讀取RAM
}
//讀取溫度數(shù)據(jù)
int Ds18b20ReadTemp()
{
int temp=0;
uchar tmh,tml;
Ds18b20Changetemp();
Ds18b20ReadTempcom();
tml= Ds18b20ReadByte();
tmh= Ds18b20ReadByte();
temp=tmh;
temp<<=8;
temp|=tml;
return temp;
}
|
-
捕獲.PNG
(65.14 KB, 下載次數(shù): 68)
下載附件
2019-12-24 20:15 上傳
-
-
程序和仿真.zip
2019-12-24 20:20 上傳
點(diǎn)擊文件名下載附件
143.48 KB, 下載次數(shù): 35
|