作业帮 > 综合 > 作业

ds18b20 温度读回程序

来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/10/04 06:33:26
ds18b20 温度读回程序
我看到郭天祥基于TX 51D中的ds18b20程序,所以抽取了它的读取程序 ,程序如下
声明硬件ds18b20电路是VCC供电的,数据传输口即2口连接P2^2口,那么,只有基本的复位和时钟,我可以读出一次温度数吗?如果我改成90C52 单片机,就是程序储存大小可以达到64k的单片机中,可以读出温度吗?
sbit DS=P2^2; //define interface
uint temp; // variable of temperature
uchar flag1; // sign of the result positive or negative
void delay(uint count) //delay
{
uint i;
while(count)
{
i=200;
while(i>0)
i--;
count--;
}
}
void dsreset(void) //send reset and initialization command
{
uint i;
DS=0;
i=103;
while(i>0)i--;
DS=1;
i=4;
while(i>0)i--;
}
bit tmpreadbit(void) //read a bit
{
uint i;
bit dat;
DS=0;i++; //i++ for delay
DS=1;i++;i++;
dat=DS;
i=8;while(i>0)i--;
return (dat);
}
uchar tmpread(void) //read a byte date
{
uchar i,j,dat;
dat=0;
for(i=1;i1;
if(testb) //write 1
{
DS=0;
i++;i++;
DS=1;
i=8;while(i>0)i--;
}
else
{
DS=0; //write 0
i=8;while(i>0)i--;
DS=1;
i++;i++;
}
}
}
void tmpchange(void) //DS18B20 begin change
{
dsreset();
delay(1);
tmpwritebyte(0xcc); // address all drivers on bus
tmpwritebyte(0x44); // initiates a single temperature conversion
}
uint tmp() //返回值为带3位的温度
{
float tt;
uchar a,b;
dsreset();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0xbe);
a=tmpread();
b=tmpread();
temp=b;
temp
ds18b20 温度读回程序
我个人觉得是时序没有搞好,因为不同的单片机的速度,也就是晶振不一样,也就导致了延时不同