18b20.c
来自「用51单片机做数字温度计PCB资料」· C语言 代码 · 共 79 行
C
79 行
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P1^0;
uchar temperature,tl,th;
void delay(uint n)
{
uint x,y;
for(x=n;x>0;x--)
for(y=112;y>0;y--)
;
}
void init()
{
uchar x=0;
DQ=1;
delay(5);
DQ=0;
delay(500);
DQ=1;
delay(15);
}
uchar read()
{
uchar i=0;
uchar dat=0;
for(i=8;i>0;i--)
{
DQ=1;
delay(2);
DQ=0;
dat>>=1;
DQ=1;
if(DQ!=0)
dat|=0x80;
delay(4);
}
return(dat);
}
void write(uchar dat)
{
uchar i=0;
for(i=8;i>0;i--)
{
DQ=0;
DQ=dat&0x01;
delay(5);
DQ=1;
dat>>=1;
}
delay(5);
}
uchar readtemperature()
{
init();
write(0xcc);
write(0x44);
delay(150);
init();
write(0xcc);
write(0xbe);
tl=read();
th=read();
temperature=(th*256+tl)*0.0625;
delay(200);
return (temperature);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?