上传者: congciqibu
|
上传时间: 2019-12-21 19:35:37
|
文件大小: 2KB
|
文件类型: C
#include
#define uchar unsigned char
#define uint unsigned int
sbit led=P2^5;
sbit wei=P2^7;
sbit duan=P2^6;
sbit DQ=P2^2;
uchar mazhi_duan[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x00};
uchar mazhi_wei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xff};
void delayl(uint n)
{
uint i,j;
for(i=n;i>0;i--)
for(j=114;j>0;j--);
}
void delays(uchar i)
{
while(i--);
}
bit init_DS18B20() //DS8B20初始化
{
bit x;
DQ=1; //DQ复位
delays(8);
DQ=0; //单片机将DQ拉低
delays(75);
DQ=1; //拉高总线
delays(15);
x=DQ; //延时过后 若x=0则初始化成功 若x=1则初始化失败
delays(5);
return x;
}
void write_data(uchar dat)
{
uchar i,temp;
temp=dat;
DQ=1;
for(i=0;i>=1;
}
}
uchar read_data()
{
uchar i,dat;
DQ=1;
for(i=0;i>=1;
DQ=1;//配置为输入
if(DQ)
dat|=0x80;
delays(4);
}
return dat;
}
uint readtemp()
{
uchar temph,templ;
uint temp;
float wendu;
init_DS18B20();
write_data(0xcc);//跳过ROM
write_data(0x44);//启动温度转换
//delayl(100);
init_DS18B20();
write_data(0xcc);//跳过ROM
write_data(0xBE);//读温度
//以下读温度,低八位在前
//高8位在后
templ=read_data();
temph=read_data();
temp = (temph<<8)|templ;
wendu = temp*0.625+0.5;//温度扩大10倍,四舍五入
temp = wendu;//10倍温度
return temp;
}
void STC_init()
{
P1=0x00;//关闭led
led=0; //锁存
wei=0;
duan=0;
}
void display(uchar weil,uchar duanl,bit dp)
{
wei=1;
P0=mazhi_wei[weil-1];
wei=0;
duan=1;
if(dp==1)
P0=(mazhi_duan[duanl]|0x80);
else
P0=mazhi_duan[duanl];
duan=0;
}
void main()
{
uchar i;
uint wendu;
STC_init();
wendu=readtemp();
delayl(500);
wendu=readtemp();
delayl(500);
while(1)
{
wendu=readtemp();
for(i=0;i<80;i++)
{
display(1,wendu/100,0);
delayl(3);
display(2,wendu0/10,1);
delayl(3);
display(3,wendu,0);
delayl(3);
}
}
}