/*本程序实现的功能为:使用一个数字温度传感器ds18b20不断的检测当前温度,
同时将所测温度值用数码管显示出来,可以设法改变18b20的表面温度
来观察改程序所实现的功能。*/
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
uchar Init18b20(void);
void Write18b20(uchar com);
int Read18b20(void);
void delay(int time);
float Convert(int temperature);
void display(int disdate);//数码管显示函数
sbit B20 = P2^7;
sbit M_LE = P3^6;
sbit M_OE = P3^7;
sbit WEI_LE = P2^0;
sbit DUAN_LE= P2^1;
char code smdate[11] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
//数码管显示所需数据表0,1,2,3,4,5,6,7,8,9,"-"
void main()
{
int temdata = 0x0000;
float temperature18b20 = 0.0;
uchar i = 0;
WEI_LE = 1; //关数码管
DUAN_LE= 1;
P0 = 0x00;
WEI_LE = 0;
DUAN_LE= 0;
M_OE = 0;
M_LE = 1; //打开管理锁存器
P2 = 0x08; //关不必要资源
M_LE = 0;
while(1)
{
B20 = 1;
Init18b20();//初始化18b20
Write18b20(0xcc);//跳过ROM操作,因为我们只使用了一个18b20
Write18b20(0x44);//让18b20启动数据转换
for(i=200;i>0;i--)
{
display(temperature18b20);//将温度显示到液晶上
delay(5);
} //延时1秒,等待18b20转换数据
Init18b20();//再次初始化
Write18b20(0xcc);//跳过ROM操作
Write18b20(0xbe);//发送读取温度命令
temdata = Read18b20();//读取温度数值
temperature18b20 = Convert(temdata);//数值转换
}
}
void display(int disdate)//数码管显示函数
{
uchar i,t;
uchar dis[3] = {0};
dis[2] = disdate/100;
dis[1] = (disdate%100)/10;
dis[0] = disdate%10;
for(i=3,t=0x01; i>0; i--)//数码管动态显示
{
DUAN_LE = 1;
P0 = smdate[dis[i-1]];
DUAN_LE = 0;
WEI_LE = 1;
P0 = t;
WEI_LE = 0;
t <<= 1;
delay(1);
WEI_LE = 1;
P0 = 0x00;
WEI_LE = 0; //消隐
}
}
uchar Init18b20(void)
{
uchar tt;
int con;
B20 = 0;
for(con = 0; con<70; con++)
{
_nop_();
}
B20 = 1;
for(con = 0; con<10; con++)
{
_nop_();
}
if(0 == B20)
{
tt = 0xff;
}
else
{
tt = 0x00;
}
for(con = 0; con<90; con++)
{
_nop_();
}
return tt;
}
void Write18b20(uchar com)//向18b20写数据函数,具体操作时序请参阅18b20数据手册
{
uchar mask = 0x01;
uchar icon;
uchar jcon;
B20 = 1;
for(icon = 0; icon<8; icon++)//从低位开始,依次将数据写入18b20中
{
if(mask&com)
{
B20 = 0;
_nop_();
_nop_();
B20 = 1;
}
else
{
B20 = 0;
}
for(jcon = 0; jcon<18; jcon++)
{
_nop_();
}
B20 = 1;
mask <<= 1;
_nop_();
}
}
int Read18b20(void)//度温度函数,此函数实现从18b20中读出16位温度值,具体操作时序见18b20使用手册
{
uchar rcom;
uchar dtim;
uchar tt;
unsigned int date = 0x0000;
B20 = 1;
for(rcom = 0; rcom<16; rcom++)
{
date >>= 1;
B20 = 0;
_nop_();
_nop_();
B20 = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
tt = B20;
if(1 == tt)
{
date |= 0x8000;
}
for(dtim = 0; dtim<18; dtim++)
{
_nop_();
}
}
return date;
}
void delay(int time)//延时一毫秒程序
{
int i,j;
for(i = 0; i<time; i++)
{
for(j = 125; j>0; j--);
}
}
float Convert(int temperature)//数值转换函数,将从18b20中读出的二进制数转换为浮点型数据
{
float temperdate = 0.0;
if(0x8000&temperature)
{
temperature =~temperature + 0x0001;//对于负温度的处理,如果是负温度,先将数据逐位取反再加1
}
if(0x0001&temperature)
temperdate += 0.0625;
if(0x0002&temperature)
temperdate += 0.125;
if(0x0004&temperature)
temperdate += 0.25;
if(0x0008&temperature)
temperdate += 0.5;
if(0x0010&temperature)
temperdate += 1;
if(0x0020&temperature)
temperdate += 2;
if(0x0040&temperature)
temperdate += 4;
if(0x0080&temperature)
temperdate += 8;
if(0x0100&temperature)
temperdate += 16;
if(0x0200&temperature)
temperdate += 32;
if(0x0400&temperature)
temperdate += 64;
return temperdate;
}
评论0