|
发表于 2011-10-30 21:36:47
|
显示全部楼层
温度传感器ds18b20:温度只能变化到15.9后又返回0???时序问题???
望指教:(附上程序)
#include<reg52.h>
#include<stdio.h>
#include<test12864.h>
#define uchar unsigned char
#define uint unsigned int
sbit dq=P1^7;
uchar d_temp,date;
uchar tempshi,tempge,tempxiaoshu;
void delayus(uchar time)
{
while(time--);
}
void reset()
{
dq=1;
delayus(4);
dq=0;
delayus(100);
dq=1;
delayus(40);
}
void writebyte(uint dat)
{
uint i;
for(i=0;i<8;i++)
{
dq=0;
dq=dat&0x01;
delayus(10);
dq=1;
dat>>=1;
}
delayus(10); //
}
uchar readchar()
{
uchar i,dat=0;
delayus(10); //
for(i=0;i<8;i++)
{
dq=0;
dat=dat>>1;
dq=1;
if(dq)
dat=dat|0x80;
delayus(10);
}
return dat;
}
void gettemperature()
{
uchar temp1,temp2,temp,tflag ;
reset();
writebyte(0xcc);
writebyte(0x44);
reset();
writebyte(0xcc);
writebyte(0xbe);
temp1=readchar();
temp2=readchar();
temp=(temp2<<8)|temp1;
if(temp<0x0fff)
tflag=1;
else
{
tflag=0;
temp=~temp+1;
}
d_temp=temp*(0.625);
tempshi=d_temp/100;
tempge=d_temp%100/10;
tempxiaoshu=d_temp%10;
}
//温度只能变化到15.9后又返回0???时序问题???
void main()
{
initial();
while(1) //进入大循环,使字符停住
{
i=0;
lcdposition(0,0); //设置显示位置为第一行的第一个字符
while(display1!='\0') //判断数组元素是否为空,为空则跳出循环
{
writedat(display1);
i++;
}
gettemperature();
lcdposition(3,0);
writedat('T');
writedat('=');
writedat(0x30+tempshi);
writedat(0x30+tempge);
writedat('.');
writedat(0x30+tempxiaoshu);
}
} |
|