[c]代码库
#include<reg52.h>
#define ui unsigned int
#define uc unsigned char
sbit clk=P3^6;// ds1302 端口
sbit io=P3^4;
sbit rst=P3^5;
sbit rs=P2^6;//1602液晶 端口
sbit en=P2^7;
sbit rw=P2^5;
sbit DQ=P3^7;//ds18b20 端口
uc code table[]= "20 - -";
uc code table1[]=" : : . c";
uc shi,fen,miao,shi1,shi2,fen1,fen2,nian,yue,ri,count,flag,flag1;
uc a,b,c,d,e,m,n,s,num,nling,count;
int t1,t2;
float tt;
uint ;temph=30;
uint ;templ=0;
///////////////////**延时部分**////////////////////
void delay1(ui i) //us延时函数
{
while(i--);
}
void delay(ui z) //ms延时函数
{
ui x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
///////////////////**1602液晶**////////////////////
void write_1602(uc d,uc date)
{
rs=d; //d=0写命令 d=1写数据
P0=date;
delay(2);
en=1;
delay(2);
en=0;
}
void wadd_1602s(uc add,uc date)//往lcd指定地址写两位数据
{
uc shi,ge;
shi=date/10;
ge=date%10;
write_1602(0,add);
write_1602(1,0x30+shi);
write_1602(1,0x30+ge);
}
void wadd_1602z(uc add1,uc date1,uc date2,uc date3) // 指定地址写三位字符
{
write_1602(0,add1);
write_1602(1,date1);
write_1602(1,date2);
write_1602(1,date3);
}
void init_1602() //1602初始化
{
uc a;
rw=0;
write_1602(0,0x38);
write_1602(0,0x0c);
write_1602(0,0x06);
write_1602(0,0x01);
for(a=0;a<=7;a++)
{
write_1602(1,table[a]);
}
write_1602(0,0x80+0x40);
for(a=0;a<=16;a++)
{
write_1602(1,table1[a]);
}
}
////////////////////**ds1302时钟**////////////////////
void write_1302(uc date) //写数据
{
ui i,k;
k=date;
for(i=0;i<8;i++)
{
io=k&0x01;
clk=1;
clk=0;
k=k>>1;
}
}
uc read_1302() //读数据
{
ui i,k;
for(i=0;i<8;i++)
{
k=k>>1;
if(io==1)k=k+0x80;
clk=1;
clk=0;
}
return k;
}
void wadd_1302(uc add,uc date) //指定地址写数据
{
rst=0;
clk=0;
rst=1;
write_1302(add);
write_1302(date);
clk=1;
rst=0;
}
uc radd_1302(uc add) //指定地址读数据
{
uc d;
rst=0;
clk=0;
rst=1;
write_1302(add|0x01);
d=read_1302();
clk=1;
rst=0;
return d;
}
void Initial_DS1302(void) //时钟芯片初始化
{
wadd_1302(0x8e,0x00);
wadd_1302(0x80,0x00);
wadd_1302(0x82,0x45);
wadd_1302(0x84,0x16);
wadd_1302(0x86,0x20);
wadd_1302(0x88,0x07);
wadd_1302(0x8c,0x12);
wadd_1302(0x8e,0x80);
}
////////////////////**18b20测温**//////////////////
void init_ds18b20() //18b20初始化函数
{
uc x=0;
DQ = 1; //DQ复位
delay1(8); //稍做延时
DQ = 0; //单片机将DQ拉低
delay1(80); //精确延时 大于 480us
DQ = 1; //拉高总线
delay1(10);
x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
delay1(5);
}
uc ReadOneChar() //读一个字节
{
uc i;
uc dat=0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
delay1(5);
}
return(dat);
}
void WriteOneChar(uc dat)//写一个字节
{
uc i;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay1(5);
DQ = 1;
dat>>=1;
}
delay1(5);
}
void ReadTem()//读取温度
{
init_ds18b20();
WriteOneChar(0xcc); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
delay1(200);
init_ds18b20();
WriteOneChar(0xcc); //跳过读序号列号的操作
WriteOneChar(0xbe); //读取温度寄存器等
a=ReadOneChar();
b=ReadOneChar();
t1=b;
t1<<=8;
t1=t1|a;
if(tt>=35)
nling=1;
else if(tt<=10)
nling=1;
}
///////////////////////显示部分////////////////////////
void display()
{
ReadTem(); //温度
if(t1<0){t2=~t1+1; write_1602(0,0x80+0x40+9);write_1602(1,'-');}
else {t2=t1;write_1602(0,0x80+0x40+9);write_1602(1,' ');}
tt=t2*0.0625; //将温度的高位与低位合并
t2=tt*10+0.5; //对结果进行4舍5入
write_1602(0,0x80+0x40+10);
write_1602(1,0x30+(t2/100)%10);
write_1602(1,0x30+(t2/10)%10);
write_1602(1,'.');
write_1602(1,0x30+(t2%10));
write_1602(1,0xdf);
write_1602(1,'C');
shi=(radd_1302(0x84)>>4)*10+(radd_1302(0x84)&0x0f);
fen=(radd_1302(0x82)>>4)*10+(radd_1302(0x82)&0x0f);
miao=(radd_1302(0x80)>>4)*10+(radd_1302(0x80)&0x0f);
wadd_1602s(0x80+0x40,shi);
wadd_1602s(0x80+0x43,fen);
wadd_1602s(0x80+0x46,miao); //时间
nian=(radd_1302(0x8c)>>4)*10+(radd_1302(0x8c)&0x0f);
yue=(radd_1302(0x88)>>4)*10+(radd_1302(0x88)&0x0f);
ri=(radd_1302(0x86)>>4)*10+(radd_1302(0x86)&0x0f);
wadd_1602s(0x82,nian);
wadd_1602s(0x85,yue);
wadd_1602s(0x88,ri); //日期
}
//*******************主函数******************
void main()
{
init_1602();
init_ds18b20();
Initial_DS1302();
while(1)
{
if(flag1==0)
display();
delay(100);
}
}