返回信息流rt 数字温湿度计的那个... 不知道怎么把DS18B20串行的数据读成并行...求大牛帮忙..其实估计也没人选了这个...我nc了...
这是一条镜像帖。来源:北邮人论坛 / circuit / #12335同步于 2009/11/7
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Circuit机器人发帖
快崩溃了...DS18B20数据不能读....
mastering
2009/11/7镜像同步7 回复
订阅后,新回复会通过你的通知中心匿名送达。
7 条回复
lz看看这个
//DS18B20温度传感器驱动(显示0至60度)
/******************************************************************************/
void Delay(int num){//延时函数
while(num--) ;
}
/******************************************************************************/
void Init_DS18B20(void){//初始化ds1820
unsigned char x=0;
DQ = 1; //DQ复位
Delay(8); //稍做延时
DQ = 0; //单片机将DQ拉低
Delay(80); //精确延时 大于 480us
DQ = 1; //拉高总线
Delay(14);
x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
Delay(20);
}
/******************************************************************************/
unsigned char ReadOneChar(void){//读一个字节
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--){
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
Delay(4);
}
return(dat);
}
/******************************************************************************/
void WriteOneChar(unsigned char dat){//写一个字节
unsigned char i=0;
for (i=8; i>0; i--){
DQ = 0;
DQ = dat&0x01;
Delay(5);
DQ = 1;
dat>>=1;
}
}
/******************************************************************************/
unsigned int ReadTemperature(void){//读取温度
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器
a=ReadOneChar(); //读低8位
b=ReadOneChar(); //读高8位
t=b;
t<<=8;
t=t|a;
tt=t*0.0625;
t= tt*10+0.5; //放大10倍输出并四舍五入
return(t);
}
http://blog.chinaunix.net/u2/66435/showart_2087212.html
看datasheet,
然后us级别的延迟一定要保证精确,
肯定能读出来