LCD1602時鐘顯示時間日期帶鬧鈴可循環(huán)調(diào)節(jié)
#include
#define uchar unsigned char
#define uint unsigned int
sbit lcdrs=P2^6;
sbit lcden=P2^7;
sbit lcdrw=P2^5;
sbit key1=P2^0;//歡迎開屏/顯示時間日期/顯示鬧鈴切換
sbit key3=P2^1; //循環(huán)調(diào)節(jié)
sbit key4=P2^2;//加
sbit key5=P2^3;//減
uchar flag1,flag,flag3,num1,shi,fen,miao,nian,yue,ri,zhou;
uchar _fen,_shi;
uchar aa,bb;
uchar code table[]={0,1,2,3,4,5,6,7,8,9,-};
uchar code table1[]="wanglushan";
uchar code table2[]="welcome you!";
uchar code table3[]="time:";
uchar code table4[]="date:";
uchar code table5[]="alarm:";
void dispfen();
void dispshi();
void dispzhou();
void dispri();
void dispyue();
void dispnian();
void disp_fen();
void disp_shi();
void delayms(uchar z);
void disptime();
void dispword();
void displing();
void delay5us();
void lcdint();
void lcdcom(uchar com1);
void lcddate(uchar date1);
void intc();
keyscan();
//--------------
void main()
{
intc();
while(1)
{
keyscan();
if(flag1==0)//當(dāng)未按調(diào)節(jié)鍵時:
{//功能顯示鍵,按該鍵將循環(huán)顯示開機(jī)畫面/時間日期/鬧鈴
//lcdint();
//delayms(10);
if(flag==0)
{
dispword();
}
if(flag==1)
{
disptime();
}
if(flag==2)
{
displing();
}
}
else//當(dāng)按下調(diào)節(jié)鍵時,將單獨(dú)顯示要調(diào)節(jié)的部分
{ //lcdint();
//delayms(10);
switch(flag1)
{
case 1:
dispfen();break;
case 2:
dispshi();break;
case 3:
dispzhou();break;
case 4:
dispri();break;
case 5:
dispyue();break;
case 6:
dispnian();break;
case 7:
disp_fen();break;
case 8:
disp_shi();break;
}
}
}
}
//----------延時
void delayms(uint z)
{
uint x;
uchar y;
for(x=z;x>0;x--)
for(y=113;y>0;y--);
}
//-----------------
void delay5us()
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
//---------LCD初始化-------
void lcdint()
{
lcdcom(0x38);
delay5us();
lcdcom(0x0c);
delay5us();
lcdcom(0x06);
delay5us();
lcdcom(0x01);
delayms(10);
}
//------LCD寫命令-----------
void lcdcom(uchar com1)
{
lcdrs=0;
lcdrw=0;
lcden=0;
delay5us();
P0=com1;
delay5us();
lcden=1;
delay5us();
lcden=0;
}
//------LCD寫數(shù)據(jù)-------
void lcddate(uchar date1)
{
lcdrs=1;
lcdrw=0;
lcden=0;
delay5us();
P0=date1;
delay5us();
lcden=1;
delay5us();
lcden=0;
}
//-------鍵盤掃描-------
keyscan()
{
if(key1==0)
delayms(100);
if(key1==0)
{
while(!key1);
lcdcom(0x01);
delayms(10);
flag++;
if(flag>2)
flag=0;
}
if(key3==0)
delayms(100);
if(key3==0)
{
while(!key3);
lcdcom(0x01);
delayms(10);
flag1++;
if(flag1>8)
flag1=0;
}
if(key4==0)
delayms(100);
if(key4==0)
{
while(!key4);
switch(flag1)
{
case 1:
fen++;
if(fen==60)
fen=0;
break;
case 2:
shi++;
if(shi==24)
shi=0;
break;
case 3:
zhou++;
if(zhou>7)
zhou=1;
break;
case 4:
ri++;
if(ri>31)
ri=1;
break;
case 5:
yue++;
if(yue>12)
yue=1;
break;
case 6:
nian++;
if(nian>99)
nian=0;
break;
case 7:
_fen++;
if(_fen==60)
_fen=0;
break;
case 8:
_shi++;
if(_shi==24)
_shi=0;
break;
default: break;
}
}
if(key5==0)
delayms(100);
if(key5==0)
{
while(!key5);
switch(flag1)
{
case 1:
if(fen==0)
fen=0;
else
fen--;
break;
case 2:
if(shi==0)
shi=0;
else
shi--;
break;
case 3:
if(zhou==1)
zhou=1;
else
zhou--;
break;
case 4:
if(ri==1)
ri=1;
else
ri--;
break;
case 5:
if(yue==1)
yue=1;
else
yue--;
break;
case 6:
if(nian==0)
nian=0;
else
nian--;
break;
case 7:
if(_fen==0)
_fen=0;
else
_fen--;
case 8:
if(_shi==0)
_shi=0;
else
_shi--;
break;
default: break;
}
}
}
//---------------
void disptime()
{
lcdcom(0xc0);
num1=0;
while(table3[num1]!=