|
這個(gè)程序顯示的溫度為什么不會(huì)變
#include<reg52.h>
#include<stdio.h>
#define uchar unsigned char
#define uint unsigned int
sbit ds=P2^2;
sbit duan=P2^6;
sbit wei=P2^7;
uint temp;
float f_temp;
uchar code shuzi[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code table[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,
0xfd,0x87,0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1};
void delay(xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void dsreset() //ds18b20初始化
{
uint i;
ds=0;
i=103;
while(i>0)i--;
ds=1;
i=4;
while(i>0)i--;
}
bit tempreadbit() //讀一位
{
uint i;
bit dat;
ds=0;i++;
ds=1;i++;i++;
dat=ds;
i=8;
while(i>0)i--;
return(dat);
}
uchar tempread() //讀一個(gè)字節(jié)
{
uchar i,j,dat;
dat=0;
for(i=1;i<=8;i++)
{
j=tempreadbit();
dat=(j<<7)|(dat>>1);
}
return(dat);
}
void tempwritebyte(uchar dat)
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb)
{
ds=0;
i++;
i++;
ds=1;
i=8;
while(i>0)i--;
}
else
{
ds=0;
i=8;
while(i>0)i--;
ds=1;
i++;
i++;
}
}
}
void tempchange()
{
dsreset();
delay(1);
tempwritebyte(0xcc);
tempwritebyte(0x44);
}
uint get_temp() //讀暫存器中的溫度數(shù)據(jù)
{
uchar a,b;
dsreset();
delay(1);
tempwritebyte(0xcc);
tempwritebyte(0xbe);
a=tempread();
b=tempread();
temp=b;
temp<<=8;
temp=temp|a;
f_temp=temp*0.625;
temp=f_temp*10+0.5; //四舍五入
f_temp=f_temp+0.05;
return temp;
} //temperature=get_temp
void display(uchar wendu)
{
uchar wendu1,wendu2,wendu3;
wendu1=wendu/100;
wendu2=wendu%100/10;
wendu3=wendu%100%10;
duan=1;
P0=shuzi[wendu1];
duan=0;
P0=0xff;
wei=1;
P0=0xfe;
wei=0;
delay(2);
duan=1;
P0=table[wendu2];
duan=0;
P0=0xff;
wei=1;
P0=0xfd;
wei=0;
delay(2);
duan=1;
P0=shuzi[wendu3];
duan=0;
P0=0xff;
wei=1;
P0=0xfb;
wei=0;
delay(2);
}
void main()
{
while(1)
{
tempchange();
display(get_temp);
}
}
|
|