|
#include<at89x52.h>
#define uchar unsigned char
#define uint unsigned int
sbit RS=P2^0;
sbit RW=P2^1;
sbit E=P2^2;
sbit S1=P3^2;
sbit S2=P3^3;
sbit S3=P3^4;
uchar data hour=23,minute=59,second=50;
uchar count=0;
bit flag1s=0;
unsigned char v_time[9];
unsigned char disp[]="2021-09-07";
unsigned char v_key=0;
//函数声明
void LCD_init();
void Set_xy(unsigned char line,unsigned char col);
void Write_dat(unsigned char data_d);
void delay_nms(unsigned char t);
void Write_com(unsigned char com_d);
void Display(unsigned char line,unsigned char col,unsigned char *dlcd);
/*------------LCD初始化函数-------------*/
void LCD_init()
{
delay_nms(15);
Write_com(0x38);
delay_nms(5);
Write_com(0x0c);
delay_nms(5);
Write_com(0x06);
}
/*------------LCD写命令函数-------------*/
void Write_com(unsigned char com_d)
{
RS=0;
RW=0;
E=1;
P0=com_d;
E=0;
}
/*------------LCD设置显示位置函数-------------*/
void Set_xy(unsigned char line,unsigned char col)
{
unsigned char address;
if(line==0)
address=0x80+col;
else
address=0xc0+col;
delay_nms(5);
Write_com(address);
}
/*------------LCD写数据函数------------*/
void Write_dat(unsigned char data_d)
{
RS=1;
RW=0;
E=1;
P0=data_d;
E=0;
}
/*------------LCD固定字符串显示函数------------*/
void Display(unsigned char line,unsigned char col,unsigned char *dlcd)
{
Set_xy(line,col);
while(*dlcd)
{
delay_nms(5);
Write_dat(*dlcd);
dlcd++;
}
}
void delay_nms(unsigned char t)
{
unsigned int i, j;
for (i = t; i > 0; i--)
for (j = 110; j > 0; j--);
}
/*--------T0初始化函数---------*/
void T0_Init()
{
EA=1;
ET0=1;
TMOD=0x01;
TH0=0x3c;
TL0=0xb0;
TR0=1;
}
/*-------时钟函数--------*/
void timer_init()
{
if(flag1s)
{
flag1s=0;
if(++second==60)
{
second=0;
if(++minute==60)
{
minute=0;
if(++hour==24)
hour=0;
}
}
}
}
/*--------拆分处理函数---------*/
void convert()
{
v_time[0]=hour/10+0x30;
v_time[1]=hour%10+0x30;
v_time[2]=':';
v_time[3]=minute/10+0x30;
v_time[4]=minute%10+0x30;
v_time[5]=':';
v_time[6]=second/10+0x30;
v_time[7]=second%10+0x30;
v_time[8]=0;
}
/*---------按键扫描函数----------*/
void key_scan()
{
if(S1==0)
{
delay_nms(10);
if(S1==0)
{
v_key++; TR0=0;
if(v_key>=3)
{
v_key=0;
TR0=1;
}
while(S1==0);//按键S1按下处理函数
}
}
}
/*----------调分函数------------*/
void adjust_minute()
{
if(S2==0)
{
delay_nms(10);
if(S2==0)
{
minute++;
if(minute==60) minute=0;
while(S2==0);
}
}
if(S3==0)
{
delay_nms(10);
if(S3==0)
{
minute--;
if(minute==255) minute=59;
}
}
}
/*----------调时函数------------*/
void adjust_hour()
{
if(S2==0)
{
delay_nms(10);
if(S2==0)
{
hour++;
if(hour==24) hour=0;
while(S2==0);
}
}
if(S3==0)
{
delay_nms(10);
if(S3==0)
{
hour--;
if(hour==255) hour=23;
}
}
}
//主函数
main()
{
T0_Init();
LCD_init();
EA=1;
while(1)
{
key_scan();
switch(v_key)
{
case 0:timer_init();break;
case 1:adjust_minute();break;
case 2:adjust_hour();
}
convert();
Display(1,3,v_time);
Display(0,3,disp);
}
}
/*--------T0中断服务函数---------*/
void T0_ser() interrupt 1
{
TH0=0X3c;
TL0=0xb0;
if(++count==20)
{
flag1s=1;
count=0;
}
}
|
-
2.jpg
(57.5 KB, 下载次数: 90)
-
1.jpg
(55.61 KB, 下载次数: 87)
|