问过很多人不知哪里错了,求助!
时间:10-02
整理:3721RD
点击:
数字温度计程序
#include<reg52.h>
#include<intrins.h>
typedef unsigned char uchar;
typedef unsigned int uint;
sbit DATA=P3^7;
uchar code table[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00,0x40};
uchar bai,shi,ge;
bit flag_Negative_number;
void delay(uint num)
{
while(num--);
}
{
uchar x=0;
DATA=1;
delay(80);
DATA=1;
delay(20);
x=DATA;
delay(30);
}
ReadOneChar(void)
{
uchar i=0;
uchar dat=0;
for(i=8;i>0;i--)
{
DATA=0;
dat>>=1;
if(DATA)
dat|=0x80;
delay(8);
}
return(dat);
}
WriteOneChar(uchar dat)
{
uchar i=0;
for(i=8;i>0;i--)
{
DATA=0;
DATA=dat&0x01;
delay(10);
DATA=1;
dat>>=1;
}
delay(8);
}
int ReadTemperature(void)
{
uchar a=0;
uchar b=0;
int t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0x44);
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0xBE);
a=ResdOneChar();
b=ReadOneChar();
t=b;
t<<=8;
t=t|a;
if(b&0x80)
{
t=~t+1;
flag_Negative_number=1;
}
else{flage_Negative_number=0;}
tt=t*0.0625;
t=tt*10+0.5;
return(t);
}
void display(int bai,int shi,int ge)
{
P0=0x00;
P2=0XFD;
P0=0X80;
delay(500);
P0=0x00;
P2=0xf7;
if(flag_Negative_number)
else{P0=table[16];}
delay(150);
P0=0x00;
P2=0xfb;
P0=table[bai];
delay(500);
P0=0x00;
P2=0xfd;
P0=table[shi];
delay(500);
P0=0x00;
P2=0xfe;
P0=table[ge];
delay(500);
}
void main()
{
int temp;
while(1)
{
temp=ReadTemperature();
bai=temp%1000/100;
shi=temp%100/10;
ge=tem%10;
diaplay(bai,shi,ge);
}
}