/****为了简单起见,调整时间是只添加了s1和s2,删除了s3的功能,若调过了可通过s2循环一圈 ****/
/****From Brandon 2016-09-17 ****/
/****关键点:按键扫描和定时器0的中断程序比较生疏,需再看 ****/
#include<reg51.h>
sbit LCD_rs=P1^0;
sbit LCD_rw=P1^1;
sbit LCD_en=P2^5;
sbit s1=P3^0;
sbit rd=P3^7;
sbit wr=P3^6;
sbit s2=P3^1;
sbit s3=P3^2;
unsigned char code table[]=" 2016-9-15 THU";
unsigned char code table1[]=" 00:00:00";
unsigned char num,count,s1num;
char miao,shi,fen; //
void Delay(unsigned char i)
{
unsigned char j,k;
for(j=i;j>0;j--)
for(k=110;k>0;k--);
}
void LCD_Write_com(unsigned char a)
{
LCD_rs=0;
Delay(5);
LCD_rw=0;
Delay(5);
P0=a;
Delay(5);
LCD_en=1;
Delay(5);
LCD_en=0;
}
void LCD_Write_data(unsigned char b)
{
LCD_rs=1;
Delay(5);
LCD_rw=0;
Delay(5);
P0=b;
LCD_en=1;
Delay(5);
LCD_en=0;
Delay(5);
}
void LCD_init()
{
Delay(15);
LCD_Write_com(0x38);
Delay(5);
LCD_Write_com(0x08);
Delay(5);
LCD_Write_com(0x01);
Delay(5);
LCD_Write_com(0x06);
Delay(5);
LCD_Write_com(0x0C);
Delay(5);
}
void Time0_init()
{
TMOD=0x01; //定时器0工作在模式1,
TH0=(65536-50000)/256; //高8位=60
TL0=(65536-50000)%256; //低8位
TR0=1; //开定时器0
ET0=1; //使能定时器0中断
EA=1; //开总中断
}
void Write_sfm(unsigned char add,unsigned char date)
{
unsigned char shi,ge;
shi=date/10;
ge=date%10;
LCD_Write_com(0x80+0x40+add);
LCD_Write_data(0x30+shi); //0x30表示的是对应数字的ASC码
LCD_Write_data(0x30+ge); //0x30表示的是对应数字的ASC码
}
void Keyscan()
{
rd=0;
if(s1==0) //s1摁下
{
Delay(5);
if(s1==0)
{
s1num++;
while(!s1);//等待s1松开
if(s1num==1)
{
TR0=0; //关定时器0
LCD_Write_com(0x80+0x40+10);//光标定位到1602第二行第11个字符处
LCD_Write_com(0x0f);//开显示,显示光标,光标闪烁
}
}
if(s1num==2)
{
TR0=0;
LCD_Write_com(0x80+0x40+7);
LCD_Write_com(0x0f);
}
if(s1num==3)
{
TR0=0;
LCD_Write_com(0x80+0x40+4);
LCD_Write_com(0x0f);
}
if(s1num==4)
{
s1num=0;
LCD_Write_com(0x0c); //开显示,不显示光标
TR0=1; //开定时器
}
}
if(s1num!=0) //若s1num不等于0
{
if(s2==0) //s2摁下
{
Delay(5);
if(s2==0)
{
while(!s2); //s2松开后while循环结束
if(s1num==1)
{
miao++;
if(miao==60)
miao=0;
Write_sfm(10,miao);
LCD_Write_com(0x80+0x40+10);
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
Write_sfm(7,fen);
LCD_Write_com(0x80+0x40+7);
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
Write_sfm(4,shi);
LCD_Write_com(0x80+0x40+4);
}
}
}
}
}
void main()
{
wr=0;
LCD_init();
LCD_Write_com(0x80); //不能写到for函数里面,否则显示不正常
for(num=0;num<15;num++)
{
LCD_Write_data(table[num]);
Delay(5);
}
LCD_Write_com(0x80+0x40); //不能写到for函数里面,否则显示不正常
for(num=0;num<12;num++)
{
LCD_Write_data(table1[num]);
Delay(5);
}
Time0_init();
while(1) //试试此处加一个冒号,提示什么警告,感悟:调试时要会对警告进行分析
{
Keyscan();
}
}
//以下程序待自己编写
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256; //50000*(11.0592/12)ms定时,若晶振为12MHz,则为50ms
count++;
if(count==18) //此处表示18个以上的数字,即1s,注意有误差
{
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);
}
}