#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define NORMAL_MODE 0
#define HOUR_SET 1
#define MINTUE_SET 2
#define SECOND_SET 3
sbit lcden=P3^4;
sbit lcdrs=P3^5;
sbit KEY_HOUR_UP=P2^0;
sbit KEY_HOUR_DN=P2^1;
sbit KEY_MIN_UP=P2^2;
sbit KEY_MIN_DN=P2^3;
sbit KEY_SEC_UP=P2^4;
sbit KEY_SEC_DN=P2^5;
uchar hour,min,sec,num,count;
uchar code table[]="Time";
//uchar code table1[]="12:00:00"; //默认显示状态
//void write_date(uchar date);
void init();
void keycontrol();
void delayms(uint xms) //延时函数
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void write_com(uchar com) //液晶写命令函数
{
lcdrs=0;
lcden=0;
P0=com;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
void write_date(uchar date) //液晶写数据函数
{
lcdrs=1;
lcden=0;
P0=date;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
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 keycontrol()
{
if(KEY_HOUR_UP==0)
{
hour++;
if(hour==24)
{
hour=0;
}
while(!KEY_HOUR_UP);
write_sfm(5,hour);
}
if(KEY_HOUR_DN==0)
{
hour--;
if(hour==0)
{
hour=23;
}
while(!KEY_HOUR_DN);
write_sfm(5,hour);
}
if(KEY_MIN_UP==0)
{
min++;
if(min==60)
{
min=0;
}
while(!KEY_MIN_UP);
write_sfm(8,min);
}
if(KEY_MIN_DN==0)
{
min--;
if(min==0)
min=59;
while(!KEY_MIN_DN);
write_sfm(8,min);
}
if(KEY_SEC_UP==0)
{
sec++;
if(sec==60)
sec=0;
while(!KEY_SEC_UP);
write_sfm(11,sec);
}
if(KEY_SEC_DN==0)
{
sec--;
if(sec==0)
sec=59;
while(!KEY_SEC_DN);
write_sfm(11,sec);
}
}
void init() //初始化函数
{
hour=12;
min=0;
sec=0;
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80+0x40+7);
write_date(':');
delayms(5);
write_com(0x80+0x40+10);
write_date(':');
delayms(5);
TMOD=0X11;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
EA=1;
ET0=1;
TR0=1;
}
void main() //主函数
{
init();
while(1)
{
keycontrol();
write_com(0x80);
for(num=0;num<4;num++)
{
write_date(table[num]);
delayms(5);
if(count==20) //20次50毫秒为1秒
{
count=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
if(hour==24)
{
hour=0;
}
write_sfm(5,hour);
}
write_sfm(8,min);
}
write_sfm(11,sec);
}
}
}
}
void T0_time()interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
count++;
}
此帖出自
小平头技术问答
一周热门 更多>