#include <reg51.h>
#include "1602.h"
# include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit k0=P1^0;
sbit k1=P1^1;
sbit k2=P1^2;
sbit k3=P1^3;
sbit buzzer="P3"^6;
uint count;
bit choose="0";
char sec="59";
char min="59";
char hour;
uchar temp,key;
uchar code num1[]=" CLOCK TIMER ";
uchar code num2[]="12:00:00";
void LCD_xianshi() //初始化
{
uchar i;
write_com(0x81);
for(i=0;i<12;i++)
{
write_date(num1 ); // 显示" 2008-10-30 THU "
delay(5);
}
write_com(0x80+0x40+4); // 最高位D7恒定为1,第二行显示,第五个字符位置开始。
for(i=0;i<8;i++)
{
write_date(num2 ); //显示" 12:00:00 "
delay(5);
}
}
void t1_init ()
{
TMOD="0x20";
TH1=0x06;
TL1=0x06;
ET1=1;
EA=1;
TR1=1;
}
void write_sfm(uchar date,uchar add) //写时分秒子程序
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_date(shi+'0');
delay(7);
write_date(ge+'0');
}
void baojing ()
{
while (1)
{
buzzer="0";
delay(100);
buzzer=1;
}
}
void keyscan() //键盘扫描
{
if(k0==0) //对第一个按键扫描
{
delay(7);
if(k0==0)
{
temp++;
choose=1;
while(!k0);
}
}
if(temp==1) //第一个键按下时,切换到功能一:修改时间
{
if(key==0)
{
write_com(0x80+0x40+11);
write_com(0x0f);
}
if(k1==0) //修改光标位置
{
delay(7);
if(k1==0)
{
key++;
while(!k1);
if(key==1)
{
TR1=0;
write_com(0x80+0x40+8);
write_com(0x0f);
}
if(key==2)
{
write_com(0x80+0x40+5);
write_com(0x0f);
}
if(key==3)
{
choose=0;
TR1=1;
key=0;
temp=0;
write_com(0x0c);
}
}
}
}
if(k2==0) //修改时间渐减
{
delay(5);
if(k2==0)
{
while(!k2);
if(key==0)
{
sec--;
if(sec==-1)
sec=59;
write_sfm(sec,10);
}
if(key==1)
{
min--;
if(min==-1)
{
min=59;
}
write_sfm(min,7);
}
if(key==2)
{
hour--;
if(hour==-1)
{
hour=12;
}
write_sfm(hour,4);
}
}
}
}
void main()
{
LCD_init();
t1_init ();
LCD_xianshi() ;
while(1)
{
keyscan();
if(choose==0)
{
write_sfm(sec,10);
write_sfm(hour,4);
write_sfm(min,7);
}
if((hour==0)&&(min==0)&&(sec==0)) //整点“滴”一下
{
}
}
}
void timer1() interrupt 3
{
count++;
if(count==4000)
{
count=0;
sec--;
if(sec==-1)
{
sec=59;
min--;
if(min==-1)
{
min==59;
hour--;
if(hour==-1)
{
hour=0;
}
}
} }
}
用户182381 2009-5-23 15:10