#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcdrs=P1^0;
sbit lcdrw=P1^1;
sbit lcden=P2^5;
sbit s1=P3^0;//功能键
sbit s2=P3^1; //增大键
sbit s3=P3^2;//减小键
sbit rd=P3^7;
sbit beep=P2^3;
uchar count,s1num;
uchar miao,shi,fen;
uchar code table[]=" 2015-10-24 sat";
uchar code table1[]=" 00:00:00";
void delay(uint z) //延时函数
{
uint y;
for(;z>0;z--)
for(y=110;y>0;y--)
;
}
void di() //蜂鸣器发声函数
{
beep=0;
delay(100);
beep=1;
}
uchar busy_check(void)//判忙
{
uchar lcd_status;
lcdrs=0;
lcdrw=1;
lcden=1;
delay(5);
lcd_status=P0&0x80;
lcden=0;
return lcd_status;
}
void write_com(uchar com)//写指令
{
while(busy_check());//判忙
lcdrs=0;
lcdrw=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date) //写数据
{
while(busy_check());//判忙
lcdrs=1;
lcdrw=0;
lcden=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init()
{
uchar num;
lcden=0;
lcdrw=0;
write_com(0x38); //初始化1602液晶
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);//设置显示初始目标
for(num=0;num<15;num++) // 显示年月日星期
{
write_date(table[num]);
delay(5);
}
write_com(0x80+0x40);
for(num=0;num<12;num++)
{
write_date(table1[num]);
delay(5);
}
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
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(s1==0)
{
delay(5);
if(s1==0) //确认功能键被按下
{ s1num++;//功能键按下次数记录
while(!s1);//释放确认
if(s1num==1) //第一次被按下时
{
TR0=0; //关闭定时器
write_com(0x80+0x40+11); //光标定位到秒处
write_com(0x0f); //光标开始闪烁
}
}
if(s1num==2) //第二次按下光标闪烁定位到分钟位置
{
write_com(0x80+0x40+8);//光标定位到分处
}
if(s1num==3) //第二次按下光标闪烁定位到小时位置
{
write_com(0x80+0x40+5);//光标定位到小时处
}
if(s1num==4) //
{
s1num=0; //记录按键数清零
write_com(0x0c); //光标取消闪烁
TR0=1; //启动定时器使时钟走起
}
}
if(s1num!=0) //
{
if(s2==0)//增大键按下
{
delay(5);
if(s2==0)
{
while(!s2);
if(s1num==1)
{
miao++;//秒加1
if(miao==60)//秒到了60,重新设置为0
miao=0;
write_sfm(10,miao); //每调节一次送液晶显示一下
write_com(0x80+0x40+10);//显示位置
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
write_com(0x80+0x40+7);
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(4,shi);
write_com(0x80+0x40+4);
}
}
}
if(s3==0)
{
delay(5);
if(s3==0) //确认减小键被按下
{
while(!s3);//按键释放
di();
if(s1num==1)
{
/* if(miao==0)
{
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}*/
miao--; //则调整秒减1
if(miao==-1)
miao=59;//若减到负数时,重新设置为59
write_sfm(10,miao); //每调节一次,重新送回液晶显示
write_com(0x80+0x40+7); //显示位置重新回到调节处
}
if(s1num==2)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(7,fen);
write_com(0x80+0x40+4);
}
if(s1num==3)
{
shi--;
if(shi==-1)
shi=23;
write_sfm(4,shi);
write_com(0x80+0x40+2);
}
}
}
}
}
void main()
{
init();
while(1)
{
keyscan();
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==20) //1秒
{
count=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}
write_sfm(4,shi); //小时变化重新写入
}
write_sfm(7,fen); //分变化重新写入
}
write_sfm(10,miao); //秒变化重新写入
}
}
|