#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit lcdrs=P2^6;
sbit lcden=P2^7;
sbit beep=P2^0;
sbit k5=P2^1;
sbit k4=P2^2;
sbit k3=P2^3;
sbit k2=P2^4;
sbit k1=P2^5;
sbit dsirq=P3^3;
sbit dscs=P3^4;
sbit dsas=P3^5;
sbit dsrw=P3^6;
sbit dsds=P3^7;
bit flag1,flag_alarm;
unsigned char hours,minutes,seconds,flag,amiao,afen,ashi,day,date,month,year,temp,temp1;
uchar code table1[]=" 20 - - ";
uchar code table2[]=" : : ";
void delay(uint z )
{
uint i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
/*lcd1602*/
void write_com(uchar com)/*写指令*/
{
lcdrs=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date)/*写数据*/
{
lcdrs=1;
lcden=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_hms(uchar add,uchar date)/*在lcd1062的相应地址显示data数据*/
{
uchar shi,ge;
/* ge=date&0x0F;
shi=(date>>4)&0x0F;//BCD码格式*/
ge=date%10;
shi=date/10;
write_com(add);
write_date(0x30+shi);
write_date(0x30+ge);
}
/*ds12c887*/
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)/*读走数据*/
{
uint redate;
dsas=1;
dsds=1;
dsrw=1;
dscs=0;
P0=add;
dsas=0;
dsds=0;
P0=0xff;
redate=P0;
dsds=1;
dsas=1;
dscs=1;
return redate;
}
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_alarm()
{
amiao=read_ds(1);
afen=read_ds(3);
ashi=read_ds(5);
}
void write_day(uchar week)//液晶星期显示函数
{
write_com(0x80+0x0d);
switch(week)
{
case 1: write_date('M');
delay(5);
write_date('O');
delay(5);
write_date('N');
break;
case 2: write_date('T');
delay(5);
write_date('U');
delay(5);
write_date('E');
break;
case 3: write_date('W');
delay(5);
write_date('E');
delay(5);
write_date('D');
break;
case 4: write_date('T');
delay(5);
write_date('H');
delay(5);
write_date('U');
break;
case 5: write_date('F');
delay(5);
write_date('R');
delay(5);
write_date('I');
break;
case 6: write_date('S');
delay(5);
write_date('A');
delay(5);
write_date('T');
break;
case 7: write_date('S');
delay(5);
write_date('U');
delay(5);
write_date('N');
break;
}
}
void key()/*检测按键*/
{
uchar num1;
if(flag_alarm==1)
{
if((k1==0)||(k2==0)||(k3==0)||(k4==0)||(k5==0))
{
delay(5);
if((k1==0)||(k2==0)||(k3==0)||(k4==0)||(k5==0))
{
while(!(k1&&k2&&k3&&k4&&k5));
beep=1;
flag_alarm=0;
P1=0x01;
TR0=1;
}
}
}
if(k1==0&&flag_alarm==0)/*最左边按键*/
{
delay(5);
if(k1==0)
{
while(!k1);
num1++;
flag=0;
P1=0x00;
TR0=0;
if(flag1==1&&num1>3)
num1=1;
if(num1==1)
{
write_com(0x80+0x40+0x0a);
write_com(0x0f);
}
if(num1==2)
{
write_com(0x80+0x40+7);
}
if(num1==3)
{
write_com(0x80+0x40+4);
}
if(num1==4)
{
write_com(0x80+0x0d);
}
if(num1==5)
{
write_com(0x80+0x0a);
}
if(num1==6)
{
write_com(0x80+7);
}
if(num1==7)
{
write_com(0x80+4);
}
if(num1==8)
{
write_com(0x80+0x40+0x0a);
num1=1;
write_com(0x0f);
}
}
}
if(num1!=0&&flag_alarm==0)
{
if(k2==0)/*左二按键*/
{
delay(5);
if(k2==0)
{
while(!k2);
if(num1==1)
{
seconds++;
if(seconds==60)
seconds=0;
write_hms(0x80+0x40+10,seconds);
write_com(0x80+0x40+10);
}
if(num1==2)
{
minutes++;
if(minutes==60)
minutes=0;
write_hms(0x80+0x40+7,minutes);
write_com(0x80+0x40+7);
}
if(num1==3)
{
hours++;
if(hours==24)
hours=0;
write_hms(0x80+0x40+4,hours);
write_com(0x80+0x40+4);
}
if(num1==4)
{
day++;
if(day==8)
day=1;
write_day(day);
}
if(num1==5)
{
date++;
if(date==32)
date=1;
write_hms(0x80+0x0a,date);
write_com(0x80+0x0a);
}
if(num1==6)
{
month++;
if(month==13)
month=1;
write_hms(0x80+7,month);
write_com(0x80+7);
}
if(num1==7)
{
year++;
if(year==100)
year=0;
write_hms(0x80+4,year);
write_com(0x80+4);
}
}
}
}
if(num1!=0&&flag_alarm==0)
{
if(k3==0)/*左三按键*/
{
delay(5);
if(k3==0)
{
while(!k3);
if(num1==1)
{
if(seconds==0)
seconds=60;
seconds--;
write_hms(0x80+0x40+10,seconds);
write_com(0x80+0x40+10);
}
if(num1==2)
{
if(minutes==0)
minutes=60;
minutes--;
write_hms(0x80+0x40+7,minutes);
write_com(0x80+0x40+7);
}
if(num1==3)
{
if(hours==0)
hours=24;
hours--;
write_hms(0x80+0x40+4,hours);
write_com(0x80+0x40+4);
}
if(num1==4)
{
day--;
if(day==0)
day=7;
write_day(day);
}
if(num1==5)
{
date--;
if(date==0)
date=31;
write_hms(0x80+0x0a,date);
write_com(0x80+0x0a);
}
if(num1==6)
{
month--;
if(month==0)
month=12;
write_hms(0x80+7,month);
write_com(0x80+7);
}
if(num1==7)
{
year--;
if(year==0)
year=99;
write_hms(0x80+4,year);
write_com(0x80+4);
}
}
}
}
if(num1!=0&&flag_alarm==0)
{
if(k4==0)/*右二建*/
{
delay(5);
if(k4==0)
{
while(!k4);
write_com(0x0c);
num1=0;
write_ds(0,seconds);
write_ds(2,minutes);
write_ds(4,hours);
write_ds(6,day);
write_ds(7,date);
write_ds(8,month);
write_ds(9,year);
P1=0x01;
flag=1;
TR0=1;
}
}
}
if(k5==0&&flag_alarm==0)
{
delay(5);
if(k5==0)
{
while(!k5);
TR0=0;
flag1=~flag1;
if(flag1==0)
{
flag=1;
write_com(0x80+0x40);
write_date(' ');
write_date(' ');
write_com(0x0c); //清屏下,否则光标乱跳(闹铃调时间时)
write_ds(1,seconds);
write_ds(3,minutes);
write_ds(5,hours);
P1=0x10;
TR0=1;
}
else
{
read_alarm(); //读原始数据
seconds=amiao;
minutes=afen;
hours=ashi;
write_com(0xc0);
write_date('n');
write_date('z');
write_hms(0x80+0x40+10,seconds);
write_hms(0x80+0x40+7,minutes);
write_hms(0x80+0x40+4,hours);
}
}
}
}
void init()/*初始化*/
{
uchar i;
lcden=0;
temp1=0;
flag=1;
P1=0x01;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(i=0;i<12;i++)
{
write_date(table1[i]);
delay(5);
}
write_com(0x80+0x40);
for(i=0;i<12;i++)
{
write_date(table2[i]);
delay(5);
}
TMOD=0x01;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
EA=1;
ET0=1;
TR0=1;
EX1=1;
IT1=1;
/* write_ds(0x0a,0x20); //控制寄存器A 计时开始,12c887初次使用需要赋值
write_ds(0x0b,0x26);//控制寄存器B 设置时钟显示方式,数据输入模式
read_ds(0x0c);//控制寄存器C 读寄存器C,使得寄存器C的IRQ置零
set_time();*/
}
void clock()
{
beep=0;
delay(100);
beep=1;
}
void main()
{
init();
while(1)
{
key();
if(flag_alarm==1) //如果闹铃时间到
{
temp1++;
if(temp1==1)
{
write_com(0x80+0x40+6);
write_date(':');
write_com(0x80+0x40+9);
write_date(':');
}
clock();
delay(100);
clock();
delay(500);
TR0=0;
P1=0xff;
del