?? my_clock.c
字號:
}
//======================設(shè)置日期
setdate()
{
uchar cru=0,max=1,xp=2,key; //定義無符號字符類型
uchar year,month,day;
buf[0]=date.year/10; //設(shè)置年高位
buf[1]=date.year%10; //設(shè)置年底位
buf[2]=date.month/10; //設(shè)置月高位
buf[3]=date.month%10; //設(shè)置月底位
buf[4]=date.day/10; //設(shè)置日高位
buf[5]=date.day%10; //設(shè)置日高位
buf[6]='s'-48; //設(shè)置"確定"位
ClrLcd();
WriteString("Set Date",3,0); //各位顯示位置
LcdPos(0,1);
LcdWd('2');
LcdWd('0');
LcdWd(buf[0]+48);
LcdWd(buf[1]+48);
LcdPos(4,1);
LcdWd('-');
LcdWd(buf[2]+48);
LcdWd(buf[3]+48);
LcdPos(7,1);
LcdWd('-');
LcdWd(buf[4]+48);
LcdWd(buf[5]+48);
SetCur(CurNoFlash);
LcdPos(xp,1);
while(key=getkey())
{
if(key==1)
{
if(cru==6)
{
year=buf[0]*10+buf[1];
month=buf[2]*10+buf[3];
day=buf[4]*10+buf[5];
key=day;
if(0<month && month<13 && day!=0) //寫入日期
{
key=monthday[month];
if((year%4)==0 && month==2) key++;
if(day<=key)
{
date.year=year;
date.month=month;
date.day=day;
save();
ClrLcd();
SetCur(NoCur);
calweek();
return;
}
else
{
WriteString("Date Wrong! ",0,0); //寫入錯誤
cru=4;
max=2;
xp=8;
}
}
else
{
WriteString("Date Wrong! ",0,0); //寫入錯誤
cru=2;
max=1;
xp=5;
}
}
if((++buf[cru])>max)
{
buf[cru]=0;
}
LcdPos(xp,1); //清屏幕,待顯示
LcdWd(buf[cru]+48);
}
else if(key==2)
{
if(++cru>6) cru=0;
switch (cru)
{
case 0:{max=9;xp=2;} break;
case 1:{max=9;xp=3;} break;
case 2:{max=1;xp=5;} break;
case 3:{max=9;xp=6;} break;
case 4:{max=3;xp=8;} break;
case 5:{xp=9;max=9;} break;
case 6:{xp=10;} break;
}
}
else if(key==3)
{
if(cru==0) cru=6; else cru--;
switch (cru)
{
case 0:{max=9;xp=2;} break;
case 1:{max=9;xp=3;} break;
case 2:{max=1;xp=5;} break;
case 3:{max=9;xp=6;} break;
case 4:{max=3;xp=8;} break;
case 5:{xp=9;max=9;} break;
case 6:{xp=10;} break;
}
}
LcdPos(xp,1);
}
SetCur(NoCur);
}
///============================
menumode() //菜單修改
{ uchar num=0,key;
menudis(num);
while(key=getkey())
{
if(key==1)
{
switch(num)
{
case 0: {settime();ClrLcd(); return;}
break;
case 1: {setdate();ClrLcd();return;}
break;
case 2: {setbell();
ClrLcd();return;}
break;
case 3: {cancel();ClrLcd();return;}
break;
}
}
else if(key==2)
{
if(++num>3) num=0;
}
else if(key==3)
{
if(num) num--;
else num=3;
}
else
{
}
menudis(num);
}
ClrLcd();
}
calweek() //星期對應(yīng)日期子程序
{
uchar year;
year=date.year/4;
week=(weekmon[date.month]+date.day+6+year+date.year)%7;
}
weekdis() //寫星期
{
WriteString(&weekday[week][0],11,0);
}
dayinc() //日期約束子程序
{
uchar i,year;
i=monthday[date.month];
year=date.year & 0x03;
if(date.month==2)
{
if( year==0 && date.year ) i++;
}
date.day++;
if(date.day>i)
{
date.day=1;
if(++date.month>12)
{
date.month=1;
date.year++;
}
}
calweek();
}
///秒加一程序
secinc()
{
if(++time.sec>=60)
{
time.sec=0; //秒回零
bellfla=1;
if(++time.min>=60)
{
time.min=0; //分回零
if(++time.hour>=24)
{
time.hour=0;
dayinc();
}
}
}
}
///時間顯示程序
timedis()
{
LcdPos(3,1);
LcdWd(time.hour/10+48);
LcdWd(time.hour%10+48);
LcdPos(5,1);
LcdWd(':');
LcdWd(time.min/10+48);
LcdWd(time.min%10+48);
LcdPos(8,1);
LcdWd(':');
LcdWd(time.sec/10+48);
LcdWd(time.sec%10+48);
}
datedis() //日期顯示程序
{ LcdPos(0,0);
LcdWd('2');
LcdWd('0');
LcdWd(date.year/10+48);
LcdWd(date.year%10+48);
LcdPos(4,0);
LcdWd('-');
LcdWd(date.month/10+48);
LcdWd(date.month%10+48);
LcdPos(7,0);
LcdWd('-');
LcdWd(date.day/10+48);
LcdWd(date.day%10+48);
}
strbell() //響鈴子程序
{
reflash=0;
ClrLcd();
WriteString("Timer Over",0,0);
while(1)
{
if(reflash<5)
BELLING=0;
else if(reflash<7)
BELLING=1; //響鈴
else if(reflash<12)
{
BELLING=0 ;
if((P1&0x0f)!=0x0f)
{
BELLING=1; //響鈴
break;
}
}
else
{
BELLING=1; //響鈴
break;
}
timedis(); //返回
//datedis();
weekdis();
}
ClrLcd();
}
bell()
{ uchar i,hour;
for(i=0;i<40;i++)
{
if(belltime[i].hour>50)
{
hour=belltime[i].hour & 0x7f;
if(hour==time.hour & belltime[i].min==time.min) strbell();
}
}
}
timer2() interrupt 5 //中斷溢出
{
static uint ovtime;
if(++ovtime>=20)
{
ovtime=0;
secinc();
reflash++;
}
TF2=0;
}
uchar getkey() //聲明getkey函數(shù)
{
uchar key=0;
uint i;
reflash=0;
while(reflash<10)
{
if((P1&0x0f)!=0x0f)
{
mDelay(100);
if((P1&0x0f)!=0x0f)
{
if(!ESC) {key=0;break;}
else if(!EN) {key=1;break;}
else if(!UP) {key=2;break;}
else if(!DOWN) {key=3;break;}
else {key=0;break;}
}
}
}
return key;
}
initial() //學(xué)號各位數(shù)字對應(yīng)碼
{ if(
ID[0]==51 //"3"
&&ID[1]==49 //"1"
&&ID[2]==48 //"0"
&&ID[3]==53 //"5"
&&ID[4]==48 //"0"
&&ID[5]==48 //"0"
&&ID[6]==57 //"9"
&&ID[7]==55 //"7"
&&ID[8]==50 //"2"
&&ID[9]==48 //"0"
)
{
TL2=(0xffff-50000)%256; //裝初值底位
TH2=(0xffff-50000)/256; //裝初值高位
RCAP2L=(0xffff-50000)%256; //重裝初值底位
RCAP2H=(0xffff-50000)/256; //重裝初值高位
T2CON=0x04; //開中斷
IE=0xa0;
WriteString(&ID[0],3,2); //寫ID
}
else
while(1);
}
cancel() //清除函數(shù)
{
uchar i;
for(i=0;i<34;i++)
belltime[i].hour&=0x7f;
ClrLcd();
WriteString("Cancel alarm....",0,0); //清除報警時間
WriteString("done",4,1);
getkey();
}
void main() //主函數(shù)
{ //uchar xPos,yPos,i;
RstLcd();
ClrLcd();
SetCur(NoCur);
initial();
getkey();
ClrLcd();
//==================== //設(shè)置初始時間
time.hour=14;
time.min=28;
time.sec=00;
date.year=8;
date.month=5;
date.day=12;
calweek();
belltime[1].hour=0x01; //鬧鐘1
belltime[1].min=1;
//belltime[1].hour|=0x80;
belltime[2].hour=0x02; //鬧鐘2
belltime[2].min=2;
//belltime[2].hour|=0x80;
belltime[3].hour=0x03; //鬧鐘3
belltime[3].min=3;
// belltime[3].hour|=0x80;
//===================
// xPos=3;
// yPos=0;
//開光標(biāo)顯示、閃爍
while(1)
{
if(reflash)
{
timedis();
datedis();
weekdis();
reflash=0;
if(bellfla)
{
bell();
bellfla=0;
}
}
if(EN==0)
{
mDelay(100);
if(EN==0) menumode();
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -