#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
sbit rs=P3^5;
sbit lcden=P3^4;
sbit s1=P3^0;//功能键选择
sbit s2=P3^1;//数值加1
sbit s3=P3^2;//数值减1
sbit s4=P3^6;//闹钟查看设置键
sbit rd=P3^7;//
sbit beep=P2^3;//蜂鸣器
sbit dscs=P1^4;
sbit dsas=P1^5;
sbit dsrw=P1^6;
sbit dsds=P1^7;
sbit dsirq=P3^3;
sbit DQ=P2^2; //温度数据通信线
uint temp,tplsb,tpmsb; //温度
uchar s1num,s4num,flag,flag1;
uchar miao,shi,fen,week,ri,yue,nian,amiao,afen,ashi;
uchar code table1[]=" - - ";
uchar code table2[]=" : : ";
// 周一周二 周三 周四 周五 周六 周日
uchar code table3[]={0xff,0x4d,0x54,0x57,0x54,0x46,0x53,0x53};
// M T W T F S S
uchar code table4[]={0xff,0x4f,0x55,0x45,0x48,0x52,0x41,0x55};
// O U E H R A U
uchar code table5[]={0xff,0x4e,0x45,0x44,0x55,0x49,0x54,0x4e};
// N E D U I T N
void write_ds(uchar,uchar);
void set_alarm(uchar,uchar,uchar);
uchar read_ds(uchar);
//void set_time();
void read_alarm();
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void Delay_DS18B20(uint num)
{
while(num--) ;
}
void write_com(uchar com)
{
rs=0;
lcden=0;
P0=com;
delay(3);
lcden=1;
delay(3);
lcden=0;
}
void write_date(uchar date)
{
rs=1;
lcden=0;
P0=date;
delay(3);
lcden=1;
delay(3);
lcden=0;
}
void init()
{
uchar num;
EA=1; //打开总中断
EX1=1;//开定时器0中断
IT1=1;//
flag1=0;//闹钟设置,0为不响
s1num=0;
dula=0;//关闭数码管
wela=0;//关闭数码管
lcden=0;
rd=0;
// set_time();
read_alarm();
write_ds(0x0a,0x20);//时钟芯片寄存器A
write_ds(0x0b,0x26);//时钟芯片寄存器B
// read_ds(0x0c); //时钟芯片寄存器C
write_com(0x38); //液晶显示模式
write_com(0x0c); //液晶显示、开关、光标打开
write_com(0x06); //液晶地址指针及光标移动情况
write_com(0x01); //清屏
write_com(0x80); //屏幕第一行
for(num=0;num<12;num++)
{
write_date(table1[num]);
delay(1);
}
write_com(0x80+0x40);//屏幕第二行
for(num=0;num<16;num++)
{
write_date(table2[num]);
delay(1);
}
}
void Init_DS18B20()
{
uchar x=0;
DQ = 1; //DQ复位
Delay_DS18B20(8);//稍做延时
DQ = 0; //单片机将DQ拉低
Delay_DS18B20(80);//精确延时,大于480us
DQ = 1; //拉高总线
Delay_DS18B20(14);
x = DQ; //稍做延时后,如果x=0则初始化成功,x=1则初始化失败
Delay_DS18B20(20);
}
uchar ReadOneChar()
{
uchar i=0;
uchar dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
Delay_DS18B20(4);
}
return(dat);
}
void WriteOneChar(uchar dat)
{
uchar i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
Delay_DS18B20(5);
DQ = 1;
dat>>=1;
}
}
uint Readtemp()
{
uchar a=0;
uchar b=0;
uint t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0x44); //启动温度转换
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器
a=ReadOneChar(); //读低8位
b=ReadOneChar(); //读高8位
t=b;
t<<=8;
t=t|a;
tt=t*0.0625;
t= tt*10+0.5; //放大10倍输出并四舍五入
return(t);
}
void display(uint temp)
{
uchar A1,A2,A3;
A1=temp/100;
A2=temp%100/10;
A3=temp%10;
write_com(0x80+0x40+10);
write_date(0x30+A1);//十位
write_date(0x30+A2);//个位
write_date(0x2e);//小数点
write_date(0x30+A3);//小数点后一位
write_date(0xdf);//
write_date(0x43);//C
}
void didi()
{
beep=0;
delay(100);
beep=1;
}
void write_nyrx(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void write_sfm(uchar add,uchar date)//
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void keyscan()
{
// rd=0;
if(flag1==1)//按s1 s4键消除报警
{
if((s1==0)||(s4==0))
{
delay(5);
if((s1==0)||(s4==0))
while(!(s1&&s4));
didi();
flag1=0;//消除报警标志
}
}
if(s1==0) //调时功能键
{
delay(5);
if(s1==0)
{
s1num++;
flag=1;//flag功能键标志位 0为显示,1为不显示
while(!s1);
didi();
if(s1num==1)
{
TR1=0;
write_com(0x80+0x40+8);
write_com(0x0f);
}
if(s1num==2)
{
write_com(0x80+0x40+5);
}
if(s1num==3)
{
write_com(0x80+0x40+2);
}
if(s1num==4)
{
write_com(0x80+9);
}
if(s1num==5)
{
write_com(0x80+6);
}
if(s1num==6)
{
write_com(0x80+3);
}
if(s1num==7)
{
write_com(0x80+0);
}
if(s1num==8)
{
s1num=0;
write_com(0x0c);
flag=0;
write_ds(0,miao);
write_ds(2,fen);
write_ds(4,shi);
write_ds(6,week);
write_ds(7,ri);
write_ds(8,yue);
write_ds(9,nian);
}
}
}
if(s1num!=0)
{
if(s2==0)//功能键加1
{
delay(5);
if(s2==0)
{
delay(5);
while(!s2);
didi();
if(s1num==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(7,miao);
write_com(0x80+0x40+8);
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(4,fen);
write_com(0x80+0x40+5);
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(1,shi);
write_com(0x80+0x40+2);
}
if(s1num==4)
{
week++;
if(week==8)
week=0;
write_com(0x80+9);
write_date(table3[week]);
write_date(table4[week]);
write_date(table5[week]);
}
if(s1num==5)
{
ri++;
if(ri==32)
ri=1;
write_nyrx(6,ri);
write_com(0x80+6);
}
if(s1num==6)
{
yue++;
if(yue==13)
yue=1;
write_nyrx(3,yue);
write_com(0x80+3);
}
if(s1num==7)
{
nian++;
if(nian==99)
nian=0;
write_nyrx(0,nian);
write_com(0x80+0);
}
}
}
if(s3==0)//功能键减1
{
delay(5);
if(s3==0)
{
delay(5);
while(!s3);
didi();
if(s1num==1)
{
miao--;
if(miao==-1)
miao=59;
write_sfm(7,miao);
write_com(0x80+0x40+8);
}
if(s1num==2)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(4,fen);
write_com(0x80+0x40+5);
}
if(s1num==3)
{
shi--;
if(shi==-1)
shi=23;
write_sfm(1,shi);
write_com(0x80+0x40+2);
}
if(s1num==4)
{
week--;
if(week==0)
week=7;
write_com(0x80+9);
write_date(table3[week]);
write_date(table4[week]);
write_date(table5[week]);
}
if(s1num==5)
{
ri--;
if(ri==0)
ri=31;
write_nyrx(6,ri);
write_com(0x80+6);
}
if(s1num==6)
{
yue--;
if(yue==0)
yue=12;
write_nyrx(3,yue);
write_com(0x80+3);
}
if(s1num==7)
{
nian--;
if(nian==-1)
nian=99;
write_nyrx(0,nian);
write_com(0x80+0);
}
}
}
}
if(s4==0)//闹钟设置键
{
delay(5);
if(s4==0)
{
s4num++;
flag=1; //
while(!s4);
didi();
if(s4num==1)
{
write_com(0x80);
write_date(' ');
write_date(' ');
write_date('S');
write_date('E');
write_date('T');
write_date(' ');
write_date(' ');
write_date('A');
write_date('L');
write_date('A');
write_date('R');
write_date('M');
write_com(0x80+0x40);
write_date(' ');
write_date('0');
write_date('0');
write_date(':');
write_date('0');
write_date('0');
write_date(':');
write_date('0');
write_date('0');
write_com(0x80+0x40+8);
write_com(0x0f);
}
if(s4num==2)
write_com(0x80+0x40+5);
if(s4num==3)
write_com(0x80+0x40+2);
if(s4num==4)
{
s4num=0;
write_com(0x0c);
flag=0;
write_com(0x80+2);
write_date('-');
write_com(0x80+5);
write_date('-');
write_com(0x80+8);
write_date(' ');
write_ds(0,miao);
write_ds(1,amiao);
write_ds(2,fen);
write_ds(3,afen);
write_ds(4,shi);
write_ds(5,ashi);
write_ds(6,week);
write_ds(7,ri);
write_ds(8,yue);
write_ds(9,nian);
}
}
}
if(s4num!=0)
{
if(s2==0)//加1
{
delay(5);
if(s2==0)
{
delay(5);
while(!s2);
didi();
if(s4num==1)
{
amiao++;
if(amiao==60)
amiao=0;
write_sfm(7,amiao);
write_com(0x80+0x40+8);
}
if(s4num==2)
{
afen++;
if(afen==60)
afen=0;
write_sfm(4,afen);
write_com(0x80+0x40+5);
}
if(s4num==3)
{
ashi++;
if(ashi==24)
ashi=0;
write_sfm(1,ashi);
write_com(0x80+0x40+2);
}
}
}
if(s3==0)//减1
{
delay(5);
if(s3==0)
{
delay(5);
while(!s3);
didi();
if(s4num==1)
{
amiao--;
if(amiao==-1)
amiao=59;
write_sfm(7,amiao);
write_com(0x80+0x40+8);
}
if(s4num==2)
{
afen--;
if(afen==-1)
afen=59;
write_sfm(4,afen);
write_com(0x80+0x40+5);
}
if(s4num==3)
{
ashi--;
if(ashi==-1)
ashi=23;
write_sfm(1,ashi);
write_com(0x80+0x40+2);
}
}
}
}
}
void write_ds(uchar add,uchar date)
{
dscs=0;
dsas=1;
dsds=1;
dsrw=1;
P0=add; //先写地址
dsas=0;
dsrw=0;
P0=date; //再写数据
dsrw=1;
dsas=1;
dscs=1;
}
uchar read_ds(uchar add)
{
uchar ds_date;
dsas=1;
dsds=1;
dsrw=1;
dscs=0;
P0=add; //先写地址
dsas=0;
dsds=0;
P0=0xff;
ds_date=P0; //再读数据
dsds=1;
dsas=1;
dscs=1;
return ds_date;
}
void read_alarm()
{
amiao=read_ds(1);
afen=read_ds(3);
ashi=read_ds(5);
}
void main()
{
Readtemp(); // 读温度
flag=0; //时钟芯片的显示标志
init(); //时钟芯片的初始化
while(1)
{
keyscan();
if(flag1==1)//闹钟设置
{
didi();
delay(100);
didi();
delay(500);
}
if(flag==0)//flag显示标志位 0为显示
{
keyscan();
miao=read_ds(0);//读12C887数据
fen=read_ds(2);
shi=read_ds(4);
week=read_ds(6);
ri =read_ds(7);
yue=read_ds(8);
nian=read_ds(9);
write_sfm(7,miao);//送液晶显示
write_sfm(4,fen);
write_sfm(1,shi);
write_nyrx(6,ri);
write_nyrx(3,yue);
write_nyrx(0,nian);
write_com(0x80+9);
write_date(table3[week]);
write_date(table4[week]);
write_date(table5[week]);
display(Readtemp());//显示温度
}
}
}
void exter() interrupt 2 //闹钟中断
{
uchar c;
flag1=1;
c=read_ds(0x0c);
}
|