读取DS18B20温度,通过LCD1602显示出来,并输出控制汇总 下载本文

内容发布更新时间 : 2024/5/23 21:29:54星期一 下面是文章的全部内容请认真阅读。

}

LcdWriteCom(0x80);

#endif 5.temp.c #include\

void Delay1ms(unsigned int y) //延时 { }

unsigned char Ds18b20Init() //初始化 {

unsigned int i; DSPORT=0; i=70; while(i--); DSPORT=1; i=0;

while(DSPORT)

unsigned int x; for(y;y>0;y--)

for(x=110;x>0;x--);

}

{ }

return 1;//成功 i++; if(i>5000)

return 0;//失败

void Ds18b20WriteByte(unsigned char dat) //写字节 {

unsigned int i,j; for(j=0;j<8;j++) {

DSPORT=0;

//每写入一位数据之前先把总线拉

低1us } }

(数据手册上模糊)

i++;

DSPORT=dat&0x01; i=6; while(i--); DSPORT=1; dat>>=1;

unsigned char Ds18b20ReadByte() { }

void Ds18b20ChangTemp() //温度转换 {

Ds18b20Init(); Delay1ms(1); unsigned char byte,bi; unsigned int i,j; for(j=8;j>0;j--) {

DSPORT=0; i++; DSPORT=1; i++; i++;

bi=DSPORT; byte=(byte>>1)|(bi<<7); i=4; while(i--);

}

return byte;

}

Ds18b20WriteByte(0xcc);

Ds18b20WriteByte(0x44);

void Ds18b20ReadTempCom() //读取温度命令 { }

int Ds18b20ReadTemp() //读取温度 {

int temp=0;

unsigned char tmh,tml; Ds18b20ChangTemp();

Ds18b20Init(); Delay1ms(1);

Ds18b20WriteByte(0xcc); Ds18b20WriteByte(0xbe);

Ds18b20ReadTempCom(); tml=Ds18b20ReadByte();

tmh=Ds18b20ReadByte(); temp=tmh; temp<<=8; temp|=tml;

}

return temp;