本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有账号?注册会员
×
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar temp,num;
uchar code table1[]=" 18b20 " ;
uchar code table2[]=" wendu . " ;
sbit DQ=P0^0; //18b20数据
sbit RS=P0^1; //1602数据命令选择端
sbit E=P0^2; //1602使能端
void delay(uchar z) //延时函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=19;y>0;y--);
}
void write_com(uchar com) //1602写命令
{
RS=0;
P1=com;
delay(30);
E=1;
delay(30);
E=0 ;
}
void write_date(uchar date)//1602写数据
{
RS=1;
P1=date;
delay(30);
E=1;
delay(30);
E=0;
}
void init_1602() //1602初始化
{
E=0;
write_com(0x38);
write_com(0x0c);
write_com(0x04);
write_com(0x01);
write_com(0x80);
}
void xianshi ()
{
for(num=0;num<16;num++)
{
write_date(table1[num]);
delay(30);
}
write_com(0x80+0x40);
for(num=0;num<16;num++)
{
write_date(table2[num]);
delay(30);
}
}
void write_wendu(uchar add,uchar date)
{
write_com(0x80+0x40+add);
write_date(0x30+date);
}
void delay1(uint m)
{
while(m--);
}
void init_18b20() //18b20初始化
{
uchar n;
DQ=1;
delay1(8);
DQ=0;
delay1(80);
DQ=1;
delay1(8);
n=DQ ;
delay1(4);
}
void write_byte(uchar dat) //写一个字节
{
uchar i;
for(i=0;i<8;i++)
{
DQ=0;
dat=dat&0x01;
delay1(4);
DQ=1;
dat>>=1;
}
delay1(4);
}
uchar read_byte(void) //读一个字节
{
uchar i,value;
for(i=0;i<8;i++)
{
DQ=0;
value>>=1;
DQ=1;
if(DQ)
value|=0x80;
delay1(4);
}
return value;
}
uchar reantemper(void)
{
uchar a,b;
float tt;
init_18b20();
write_byte(0xcc); //跳过ROM
write_byte(0x44);//启动温度测量
delay1(300);
init_18b20();
write_byte(0xcc);
write_byte(0xbe);
a=read_byte();
b=read_byte();
temp=b;
temp<<=8;
temp=temp|a; //两个字节组成一个字
// b<<=4;
// b+=(a&0xf0)>>4;
// return b;
tt=temp*0.625; //温度在寄存器中为12位,最小为0.0625
temp=tt*10+0.5; //乘十表示小数点后只保留一位,加0.5是四舍五入
return temp;
}
void main()
{
init_1602();
xianshi();
while(1)
{
temp=reantemper();
write_wendu(10,temp/100);
write_wendu(11,temp%100/10);
write_wendu(13,temp%100%10);
}
} |