代码如下:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define LCD_data P0 //定义液晶数据口
sbit LCD_RS=P1^0; //寄存器选择输入
sbit LCD_RW=P1^1; //液晶读写控制
sbit LCD_EN=P1^2; //液晶使能控制
sbit LCD_PSB=P1^3; //串并控制方式
sbit s1=P2^0;
sbit s2=P2^1;
sbit s3=P2^2;
sbit s4=P2^3;
sbit rd=P3^7;
sbit beep=P3^4;
sbit dscs=P1^4;
sbit dsas=P1^5;
sbit dsrw=P1^6;
sbit dsds=P1^7;
sbit dsirq=P3^3;
bit flag1,flag_ri;
uchar count,s1num,flag,t0_num;
char miao,shi,fen,year,month,day,week,amiao,afen,ashi;
uchar code table[20]={"室内现有 "};
uchar code table0[]= {"20 -- -- "};
uchar code table1[]= {" : : "};
uchar code table2[]= {" "};
uchar code table3[]= {" "};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void di()
{
beep=0;
delay(100);
beep=1;
}
void write_cmd(uchar cmd)
{
LCD_RS=0;
LCD_RW=0;
LCD_EN=0;
P0=cmd;
delay(5);
LCD_EN=1;
delay(5);
LCD_EN=0;
}
void write_dat(uchar dat)
{
LCD_RS=1;
LCD_RW=0;
LCD_EN=0;
P0=dat;
delay(5);
LCD_EN=1;
delay(5);
LCD_EN=0;
}
void lcd_pos(uchar X,uchar Y)
{
uchar pos;
if(X==0)
{
X=0x80;
}
else if(X==1)
{
X=0x90;
}
else if(X==2)
{
X=0x88;
}
else if(X==3)
{
X=0x98;
}
pos=X+Y;
write_cmd(pos);
}
void displaygd()
{
uchar i;
lcd_pos(0,0);
for(i=0;i<8;i++)
{
write_dat(table0[i]);
delay(2);
}
lcd_pos(1,0);
for(i=0;i<8;i++)
{
write_dat(table1[i]);
delay(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 set_
time()
{
write_ds(0,0);
write_ds(1,0);
write_ds(2,0);
write_ds(3,0);
write_ds(4,0);
write_ds(5,0);
write_ds(6,0);
write_ds(7,0);
write_ds(8,0);
write_ds(9,0);
}
void read_al
ARM()
{
amiao=read_ds(1);
afen=read_ds(3);
ashi=read_ds(5);
}
void init()
{
uchar i;
EA=1;
EX1=1;
IT1=1;
flag1=0;
t0_num=0;
s1num=0;
week=1;
rd=0;
write_ds(0x0A,0x20);
write_ds(0x0B,0x26);
set_time();
LCD_PSB=1; //并口方式
write_cmd(0x30); //基本指令操作
delay(5);
write_cmd(0x0C);
delay(5);
write_cmd(0x01);
delay(5);
displaygd();
}
void write_sfm(uchar add,uchar date) //计算add为多少时对应时分秒 add=4时 7分 10秒
{
char shi,ge;
shi=date/10;
ge=date%10;
lcd_pos(1,add);
write_dat(0x30+shi);
write_dat(0x30+ge);
}
void write_nyr(uchar add,uchar date) //计算add为多少时对应年月日 add=3年 6月 9日
{
char shi,ge;
shi=date/10;
ge=date%10;
lcd_pos(0,add);
write_dat(0x30+shi);
write_dat(0x30+ge);
}
void write_week(uchar we)
{
lcd_pos(2,0);
switch(we)
{
case 1: write_dat('M');delay(5);
write_dat('O');delay(5);
write_dat('N');break;
case 2: write_dat('T');delay(5);
write_dat('U');delay(5);
write_dat('E');break;
case 3: write_dat('W');delay(5);
write_dat('E');delay(5);
write_dat('D');break;
case 4: write_dat('T');delay(5);
write_dat('H');delay(5);
write_dat('U');break;
case 5: write_dat('F');delay(5);
write_dat('R');delay(5);
write_dat('I');break;
case 6: write_dat('S');delay(5);
write_dat('A');delay(5);
write_dat('T');break;
case 7: write_dat('S');delay(5);
write_dat('U');delay(5);
write_dat('N');break;
}
}
void keyscan()
{
if(flag_ri==1)
{
if((s1==0)||(s2==0)||(s3==0)||(s4==0))
{
delay(5);
if((s1==0)||(s2==0)||(s3==0)||(s4==0))
{
while(!(s1&s2&s3&s4));di();
flag_ri=0;
}
}
}
if(s1==0)
{
delay(5);
if(s1==0)
{
s1num++;
if(flag1==1)
{
if(s1num==4)
s1num=1;
}
flag=1;
while(!s1);
di();
switch(s1num)
{
case 1: lcd_pos(1,4);write_cmd(0x0f);
break;
case 2: lcd_pos(1,2);break;
case 3: lcd_pos(1,0);break;
case 5: lcd_pos(0,5);break;
case 6: lcd_pos(0,3);break;
case 7: lcd_pos(0,1);break;
case 4: lcd_pos(2,0);break;
case 8: s1num=0;
write_cmd(0x0c);
flag=0;
write_ds(0,miao);
write_ds(2,fen);
write_ds(4,shi);
write_ds(6,week);
write_ds(7,day);
write_ds(8,month);
write_ds(9,year);
break;
}
}
}
if(s1num!=0)
{
if(s2==0)
{
delay(1);
if(s2==0)
{
while(!s2);
di();
switch(s1num)
{
case 1: miao++;
if(miao==60)
miao=0;
write_sfm(4,miao);
lcd_pos(1,4);
break;
case 2: fen++;
if(fen==60)
fen=0;
write_sfm(2,fen);
lcd_pos(1,2);
break;
case 3: shi++;
if(shi==24)
shi=0;
write_sfm(0,shi);
lcd_pos(1,0);
break;
case 4: week++;
if(week==8)
week=1;
write_week(week);
lcd_pos(2,0);
break;
case 5: day++;
if(day==32)
day=1;
write_nyr(5,day);
lcd_pos(0,5);
break;
case 6: month++;
if(month==13)
month=1;
write_nyr(3,month);
lcd_pos(0,3);
break;
case 7: year++;
if(year==100)
year=0;
write_nyr(1,year);
lcd_pos(0,1);
break;
}
}
}
if(s3==0)
{
delay(1);
if(s3==0)
{
while(!s3);
di();
switch(s1num)
{
case 1: miao--;
if(miao==-1)
miao=59;
write_sfm(4,miao);
lcd_pos(1,4);
break;
case 2: fen--;
if(fen==-1)
fen=59;
write_sfm(2,fen);
lcd_pos(1,2);
break;
case 3: shi--;
if(shi==-1)
shi=23;
write_sfm(0,shi);
lcd_pos(1,0);
break;
case 4: week--;
if(week==-1)
week=7;
write_week(week);
lcd_pos(2,0);
break;
case 5: day--;
if(day==-1)
day=31;
write_sfm(5,day);
lcd_pos(0,5);
break;
case 6: month--;
if(month==0)
month=12;
write_sfm(3,month);
lcd_pos(0,3);
break;
case 7: year--;
if(year==-1)
year=99;
write_sfm(1,year);
lcd_pos(0,1);
break;
}
}
}
}
if(s4==0)
{
delay(5);
if(s4==0)
{
flag1=~flag1;
while(!s4);
di();
if(flag1==0)
{
flag=0;
lcd_pos(1,0);
write_dat(' ');
write_dat(' ');
write_cmd(0x0c);
write_ds(1,miao);
write_ds(3,fen);
write_ds(5,shi);
}
else
{
read_alarm();
miao=amiao;
fen=afen;
shi=ashi;
lcd_pos(1,0);
write_dat('R');
write_dat('i');
lcd_pos(1,0);
write_sfm(0,ashi);
write_sfm(2,afen);
write_sfm(4,amiao);
}
}
}
}
void main()
{
init();
while(1)
{
keyscan();
if(flag_ri==1)
{
di();
delay(100);
di();
delay(500);
}
if(flag==0&&flag1==0)
{
keyscan();
year=read_ds(9);
month=read_ds(8);
day=read_ds(7);
week=read_ds(6);
shi=read_ds(4);
fen=read_ds(2);
miao=read_ds(0);
write_sfm(4,miao);
write_sfm(2,fen);
write_sfm(0,shi);
write_week(week);
write_nyr(1,year);
write_nyr(3,month);
write_nyr(5,day);
}
}
}
void exter() interrupt 2
{
uchar c;
flag_ri=1;
c=read_ds(0x0c);
}
一周热门 更多>