基于51数字时钟程序.docx
- 文档编号:20121444
- 上传时间:2023-04-25
- 格式:DOCX
- 页数:20
- 大小:17.87KB
基于51数字时钟程序.docx
《基于51数字时钟程序.docx》由会员分享,可在线阅读,更多相关《基于51数字时钟程序.docx(20页珍藏版)》请在冰豆网上搜索。
基于51数字时钟程序
基于51数字时钟程序
这部分是主程序:
main.c
#include
externvoidInitT0(void);
externunsignedcharDispBuf[6];
externbitxTim,xKey;
externvoidKeyPro(void);
externunsignedcharKeySts;
unsignedcharsec,min,hour,sel,almsec,almmin,almhor;
unsignedcharcodetable[16]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};//ÊýÂë¹Ü×ÖÐαí
voidTimUpdate(void)
{
if(++sec==60)
{
sec=0;
if(++min==60)
{
min=0;
if(sel==1)
{
if(++hour==24)hour=0;
}
else
if(++hour==12)hour=0;
}
}
}
voidAlarm()
{
if((almhor==hour)&&(almmin==min)&&(almsec==sec))P2=P2^16;
if((almhor==hour)&&(almmin==min)&&(almsec==(sec+10)))P2=P2^16;
if(sec==0&&min==0)
P2=P2^0x10;
if(sec==10&&min==0)
P2=P2^0x10;
}
voidDisplayTim()
{
DispBuf[0]=table[sec%10];
DispBuf[1]=table[sec/10];
DispBuf[2]=table[min%10];
DispBuf[3]=table[min/10];
DispBuf[4]=table[hour%10];
DispBuf[5]=table[hour/10];
}
voidmain(void)
{
unsignedcharx,i;
sel=1;
x=1;
for(i=0;i<6;i++)DispBuf[i]=0xFF;
sec=0;
min=0;
hour=0;
almsec=0;
almmin=0;
almhor=0;
P2=P2&&0xEF;
InitT0();
KeySts=0;//°´¼üµÄ״̬±äÁ¿
while
(1)
{
if(xTim&&(KeySts==0))
{
xTim=0;
TimUpdate();
DisplayTim();
Alarm();
P1=~x;
x<<=1;
if(x==0)x=1;
}
if(xKey)
{
xKey=0;
KeyPro();
}
}
}
按键程序和显示控制程序:
key.c
#defineSET0x20
#defineLEFT0x10
#defineRIGHT0x08
#defineUP0x04
#defineDOWN0x02
#defineOK0x01
#include
externunsignedcharsec,min,hour,sel,almsec,almmin,almhor;
externunsignedcharcodetable[16];//ÊýÂë¹Ü×ÖÐαí
externunsignedcharDispBuf[6];
unsignedcharEditSec,EditMin,EditHour,i;
externunsignedcharKeyVal;
unsignedcharCurKey=0,LastKey=0;
unsignedcharKeySts=0;
voidDisplayEdit()
{
if(KeySts<=6)
{
DispBuf[0]=table[EditSec%10];
DispBuf[1]=table[EditSec/10];
DispBuf[2]=table[EditMin%10];
DispBuf[3]=table[EditMin/10];
DispBuf[4]=table[EditHour%10];
DispBuf[5]=table[EditHour/10];
DispBuf[6-KeySts]&=0x7F;
}
if(KeySts==7)
DispBuf[4]&=0x7F;
if(KeySts==8)
{
for(i=2;i<=5;i++)DispBuf[i]=0xFF;
DispBuf[0]=table[(sel+1)*12%10];
DispBuf[1]=table[(sel+1)*12/10];
}
if((KeySts<=14)&&(KeySts>8))
{
DispBuf[0]=table[almsec%10];
DispBuf[1]=table[almsec/10];
DispBuf[2]=table[almmin%10];
DispBuf[3]=table[almmin/10];
DispBuf[4]=table[almhor%10];
DispBuf[5]=table[almhor/10];
DispBuf[14-KeySts]&=0x7F;
}
if(KeySts==15)
for(i=0;i<=5;i++)
DispBuf[i]&=0x7F;
}
voidCopy()
{
sec=EditSec;
min=EditMin;
hour=EditHour;
}
voidKeyPro(void)
{
unsignedcharKeyPressed;
LastKey=CurKey;
CurKey=KeyVal;
KeyPressed=CurKey^LastKey;
if(KeyPressed==0)return;//¶ÁÈëµÄ¼üÖµÏà¶ÔÓÚÉÏ´ÎûÓб仯
if(CurKey==0)return;//»òÓб仯£¬µ«ÊôÓÚÊͷŶ¯×÷£¬¾ùÍ˳ö
switch(KeySts)
{
case0:
//SetKeypressedwhilerunning
if(CurKey==SET)KeySts++;
EditSec=sec;
EditMin=min;
EditHour=hour;
break;
case1:
//hourHieditstatus;
switch(CurKey)
{
caseRIGHT:
KeySts++;
break;
caseLEFT:
KeySts=15;
break;
caseUP:
if(sel==1)
{
if(EditHour==23)EditHour=0;
elseif(EditHour<13)EditHour+=10;//¼ÓСʱ¸ßλ
elseEditHour=23;
}
else{
if(EditHour==11)EditHour=0;
elseif(EditHour<1)EditHour+=10;//¼ÓСʱ¸ßλ
elseEditHour=11;
}
break;
caseDOWN:
if(sel==1)
{
if(EditHour==0)EditHour=23;
elseif(EditHour>10)EditHour-=10;//¼õСʱµÍλ
elseEditHour=0;
}
else
{
if(EditHour==0)EditHour=11;
elseif(EditHour>10)EditHour-=10;//¼õСʱµÍλ
elseEditHour=0;
}
break;
caseOK:
Copy();
KeySts=0;
break;
}
break;
case2:
//hourLoeditstatus;
switch(CurKey)
{
caseRIGHT:
KeySts++;
break;
caseLEFT:
KeySts--;
break;
caseUP:
if(sel==1)
if(EditHour<23)EditHour++;
elseEditHour=0;
elseif(EditHour<12)EditHour++;
elseEditHour=0;
break;
caseDOWN:
if(sel==1)
if(EditHour>0)EditHour--;
elseEditHour=23;
elseif(EditHour>0)EditHour--;
elseEditHour=11;
break;
caseOK:
Copy();
KeySts=0;
break;
}
break;
case3:
//minHieditstatus;
switch(CurKey)
{
caseRIGHT:
KeySts++;
break;
caseLEFT:
KeySts--;
break;
caseUP:
if(EditMin<49)EditMin+=10;
elseEditMin-=50;
break;
caseDOWN:
if(EditMin>10)EditMin-=10;
elseEditMin+=50;
break;
caseOK:
Copy();
KeySts=0;
break;
}
break;
case4:
//minLoeditstatus;
switch(CurKey)
{
caseRIGHT:
KeySts++;
break;
caseLEFT:
KeySts--;
break;
caseUP:
if(EditMin<59)EditMin++;
elseEditMin=0;
break;
caseDOWN:
if(EditMin>0)EditMin--;
elseEditMin=59;
break;
caseOK:
Copy();
KeySts=0;
break;
}
break;
case5:
//secHieditstatus;
switch(CurKey)
{
caseRIGHT:
KeySts++;
break;
caseLEFT:
KeySts--;
break;
caseUP:
if(EditSec<49)EditSec+=10;
elseEditSec-=50;
break;
caseDOWN:
if(EditSec>10)EditSec-=10;
elseEditSec+=50;
break;
caseOK:
Copy();
KeySts=0;
break;
}
break;
case6:
//secLoeditstatus;
switch(CurKey)
{
caseRIGHT:
KeySts++;
break;
caseLEFT:
KeySts--;
break;
caseUP:
if(EditSec<59)EditSec++;
elseEditSec=0;
break;
caseDOWN:
if(EditSec>0)EditSec--;
elseEditSec=59;
break;
caseOK:
Copy();
KeySts=0;
break;
}
break;
case7:
switch(CurKey)
{
caseRIGHT:
KeySts++;
break;
caseLEFT:
KeySts--;
break;
caseUP:
if(EditHour<23)EditHour++;
elseEditHour=0;
break;
caseDOWN:
if(EditHour>0)EditHour--;
elseEditHour=23;
break;
caseOK:
Copy();
KeySts=0;
break;
}
break;
case8:
switch(CurKey)
{
caseRIGHT:
KeySts++;
break;
caseLEFT:
KeySts--;
break;
caseUP:
if(sel==1)
{
sel=0;
if(hour>=13)hour-=12;
}
else
{
sel=1;
if(hour<=12)hour+=12;
}
break;
caseDOWN:
if(sel==1)
{
sel=0;
if(hour>=13)hour-=12;
}
else
{
sel=1;
if(hour<=12)hour+=12;
}
break;
caseOK:
KeySts=0;
break;
}
break;
case9:
//hourHieditstatus;
switch(CurKey)
{
caseRIGHT:
KeySts++;
break;
caseLEFT:
KeySts=8;
break;
caseUP:
if(almhor==23)almhor=0;
elseif(almhor<13)almhor+=10;//¼ÓСʱ¸ßλ
elsealmhor=23;
break;
caseDOWN:
if(almhor==0)almhor=23;
elseif(almhor>10)almhor-=10;//¼õСʱµÍλ
elsealmhor=0;
break;
caseOK:
KeySts=0;
break;
}
break;
case10:
//hourLoeditstatus;
switch(CurKey)
{
caseRIGHT:
KeySts++;
break;
caseLEFT:
KeySts--;
break;
caseUP:
if(almmin<23)almmin++;
elsealmmin=0;
break;
caseDOWN:
if(almmin>0)almmin--;
elsealmmin=23;
break;
caseOK:
KeySts=0;
break;
}
break;
case11:
//minHieditstatus;
switch(CurKey)
{
caseRIGHT:
KeySts++;
break;
caseLEFT:
KeySts--;
break;
caseUP:
if(almmin<49)almmin+=10;
elsealmmin-=50;
break;
caseDOWN:
if(almmin>10)almmin-=10;
elsealmmin+=50;
break;
caseOK:
KeySts=0;
break;
}
break;
case12:
//minLoeditstatus;
switch(CurKey)
{
caseRIGHT:
KeySts++;
break;
caseLEFT:
KeySts--;
break;
caseUP:
if(almsec<59)almsec++;
elsealmsec=0;
break;
caseDOWN:
if(almsec>0)almsec--;
elsealmsec=59;
break;
caseOK:
KeySts=0;
break;
}
break;
case13:
//secHieditstatus;
switch(CurKey)
{
caseRIGHT:
KeySts++;
break;
caseLEFT:
KeySts--;
break;
caseUP:
if(almsec<49)almsec+=10;
elsealmsec-=50;
break;
caseDOWN:
if(almsec>10)almsec-=10;
elsealmsec+=50;
break;
caseOK:
KeySts=0;
break;
}
break;
case14:
//secLoeditstatus;
switch(CurKey)
{
caseRIGHT:
KeySts++;
break;
caseLEFT:
KeySts--;
break;
caseUP:
if(almsec<59)almsec++;
elsealmsec=0;
break;
caseDOWN:
if(almsec>0)almsec--;
elsealmsec=59;
break;
caseOK:
KeySts=0;
break;
}
break;
case15:
//reset
switch(CurKey)
{
caseRIGHT:
KeySts=1;
break;
caseLEFT:
KeySts--;
break;
caseUP:
EditHour=0;
EditMin=0;
EditSec=0;
almhor=0;
almmin=0;
almsec=0;
break;
caseOK:
Copy();
KeySts=0;
break;
}
break;
default:
KeySts=0;//´íÎó״̬ÎÞÌõ¼þ¸´Î»
}
DisplayEdit();
}
中断控制:
led.c
#include
sbitDAT=P2^7;
sbitPL=P2^6;
sbitCLK=P2^5;
unsignedcharpLED;
unsignedcharDispBuf[6];
unsignedcharcount;
bitxTim,xKey,xDat;
unsignedcharKeyVal;
voidInitT0(void)
{
pLED=0;
xTim=0;
count=0;
TMOD=(TMOD&0xF0)|0x01;
TH0=0xF1;//1/240s
TL0=0x00;
TR0=1;
ET0=1;
EA=1;
}
voidT0Srv(void)interrupt1using1
{
unsignedchari;
TH0=0xF1;//1/240s
P0=0xFF;
P2=0x08|pLED;
P0=DispBuf[pLED];
if(++pLED==6)pLED=0;
count++;
if((count&0x03)==0)//ÿ4´ÎÖжϣ¬Ô¼16ºÁÃë¼ì²éÒ»´Î°´¼ü
{
CLK=0;
PL=0;//LatchKey
PL=1;
KeyVal=0;
for(i=0;i<8;i++)
{
KeyVal<<=1;
DAT=1;
xDat=DAT;
if(!
xDat)KeyVal|=0x01;//ѹϵļü£¬¶ÔӦλÖÃ1
CLK=1;
CLK=0;
}
KeyVal&=0x3F;
xKey=1;
}
if(count==240)//ÿ240Öжϣ¬Ê±¼ä¸üÐÂ
{
count=0;
xTim=1;
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 基于 51 数字 时钟 程序