uint Read_temperature()
{
uint temt;
unsigned long int temperature = 0;
uchar dat_l = 0,dat_h = 0;
Init_da18b20();
//************开始转换**************
Writer_ds18b20(0xcc);//忽视r o m指令 后面跟44h可完成温度转换
Writer_ds18b20(0x44);
delay_ms(800);
//**********读暂存数据************
Init_da18b20();
Writer_ds18b20(0xcc);
Writer_ds18b20(0xbe);//一个从机只能跟一条读寄存器指令
dat_l = Read_ds18b20();
dat_h = Read_ds18b20();
Init_da18b20();//***********读取暂存结束*************
//temperature = (dat_h * 256 )+ dat_l;
temperature = dat_h;
temperature <<= 8;
temperature += dat_l;
temt =temperature * 0.0625;
return temt; //temperature;
}
我的ds18b20的温度采集函数是这样的 这是最终版 一开始时候显示00.00 后来发现是声明的问题 一开始我只有 uint temt的反回值 没有temperature这个中间变量 返回值都为00.00 是uint 即unsigned int的范围不够 溢出 。还发现temperature = (dat_h * 256 )+ dat_l;这样显示的温度是实际温度的2倍 不能使用()也很奇怪 想不通
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
//=====================================================
sbit DQ=P1^6;
//======================================================
sbit LED1=P1^3;
sbit LED2=P1^2;
sbit FMQ=P1^1;
//======================================================
unsigned int temp, temp1,temp2, xs;
//======================================================
void delay1(uint m)
{
uint i,j;
for(i=m;i>0;i--)
for(j=110;j>0;j--);
}
//======================================================
void delay(uint m)
{
while(m--);
}
//======================================================
void Init_DS18B20() //初始化18b20
{
unsigned char x=0;
DQ = 1;
delay(8);
DQ = 0;
delay(80); //>480us
DQ = 1;
delay(4);
x=DQ; //延时后 如果x=0则初始化成功 x=1则初始化失败
delay(20);
}
//=========================================================
uchar readchar() //读出一字节
{
uchar i=0;
uchar dat = 0;
for (i=0;i<8;i++)
{
DQ = 0;
dat>>=1;
DQ = 1;
if(DQ)
dat|=0x80;
delay(4);
}
return(dat);
}
//===================================================
writechar(unsigned char dat) //写入一字节
{
unsigned char i=0;
for (i=0; i<8; i++)
{
DQ = 0;
DQ = dat&0x01;
delay(5); //60us~120us
DQ = 1;
dat>>=1; //从最低位到最高位传入
}
}
//===========================================================
readtemperature() //读取温度
{
uchar a=0;
unsigned b=0;
unsigned t=0;
Init_DS18B20();
writechar(0xCC); // 跳过读序号列号的操作/
writechar(0x44); // 启动温度转换
delay(5); // 重要
Init_DS18B20();
writechar(0xCC); //跳过读序号列号的操作
writechar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度/
delay(3);
a=readchar(); //读取温度值低位 /
b=readchar(); //读取温度值高位 /
temp1=b<<4; //高8位中后三位数的值
temp1+=(a&0xf0)>>4; //低8位中的高4位值加上高8位中后三位数的值 temp1室温整数值
temp2=a&0x0f; //小数的值
temp=((b*256+a)>>4); //当前采集温度值除16得 实际温度值 zhenshu
xs=temp2*0.0625*10; //小数位,若为0.5则算为5来显示 xs小数 xiaoshu
//===============================================================================
if(temp>=30.0) //报警模块
FMQ=0;
else
FMQ=1;
if(temp>=25.0 && temp<30.0)
LED1=0,
LED2=1;
else
LED1=1;
}
//================================================================
void wenduxianshi() //数码管显示
{
int a,b,c;
a=(temp/10);
switch(a)
{
case 0 :P2=0x00 |0x04;
P3=0x28;
break;
case 1 :P2=0x18 |0x04;
P3=0xe8;
break;
case 2 :P2=0x10 |0x04;
P3=0x30;
break;
case 3 :P2=0x10 |0x04;
P3=0xa0;
break;
case 4 :P2=0x08 |0x04;
P3=0xe0;
break;
case 5 :P2=0x80 |0x04;
P3=0xa0;
break;
case 6 :P2=0x80 |0x04;
P3=0x20;
break;
case 7 :P2=0x10 |0x04;
P3=0xe8;
break;
case 8 :P2=0x00 |0x04;
P3=0x20;
break;
case 9 :P2=0x00 |0x04;
P3=0xa0;
break;
}
delay(800);
b=(temp%10);
switch(b)
{
case 0 :P2=0x00 |0x20;
P3=0x28 &0xdf;
break;
case 1 :P2=0x18 |0x20;
P3=0xe8 &0xdf;
break;
case 2 :P2=0x10 |0x20;
P3=0x30 &0xdf;
break;
case 3 :P2=0x10 |0x20;
P3=0xa0 &0xdf;
break;
case 4 :P2=0x08 |0x20;
P3=0xe0 &0xdf;
break;
case 5 :P2=0x80 |0x20;
P3=0xa0 &0xdf;
break;
case 6 :P2=0x80 |0x20;
P3=0x20 &0xdf;
break;
case 7 :P2=0x10 |0x20;
P3=0xe8 &0xdf;
break;
case 8 :P2=0x00 |0x20;
P3=0x20 &0xdf;
break;
case 9 :P2=0x00 |0x20;
P3=0xa0 &0xdf;
break;
}
delay(800);
c=(xs%10);
switch(c)
{
case 0 :P2=0x00 |0x40;
P3=0x28;
break;
case 1 :P2=0x18 |0x40;
P3=0xe8;
break;
case 2 :P2=0x10 |0x40;
P3=0x30;
break;
case 3 :P2=0x10 |0x40;
P3=0xa0;
break;
case 4 :P2=0x08 |0x40;
P3=0xe0;
break;
case 5 :P2=0x80 |0x40;
P3=0xa0;
break;
case 6 :P2=0x80 |0x40;
P3=0x20;
break;
case 7 :P2=0x10 |0x40;
P3=0xe8;
break;
case 8 :P2=0x00 |0x40;
P3=0x20;
break;
case 9 :P2=0x00 |0x40;
P3=0xa0;
break;
}
delay(800);
P2=0x80;
P3=0x38 |0x04;
}
//======================================================
main()
{
while(1)
{
readtemperature();
wenduxianshi();
}
}
//=======================================================
一周热门 更多>