?? to.c
字號:
#include <AT89X51.H>
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,};
unsigned char sec; //定義計數值,每過1/10秒,sec加一
unsigned char keycnt=0;
unsigned int tcnt; //鍵值判斷
sbit gewei=P2^0; //個位選通定義
sbit shiwei=P2^1; //十位選通定義
void Delay(unsigned int tc) //延時程序
{
while( tc != 0 )
{
unsigned int i;
for(i=0; i<100; i++);
tc--;
}
}
void LED() //LED顯示函數
{
shiwei=0;
P0=table[sec/10];
Delay(8);
shiwei=1;
gewei=0;
P0=table[sec%10];
Delay(5);
gewei=1;
}
void KEY() //按鍵掃描程序
{
unsigned char i,j;
if(P3_0==0)
{
for(i=20;i>0;i--)
for(j=248;j>0;j--);
if(P3_0==0)
{
keycnt++;
switch(keycnt) //按下次數判斷
{
case 1: //第一次按下
TH0=0x06; //對TH0 TL0賦值
TL0=0x06;
TR0=1; //開始定時
break;
case 2: //第二次按下
TR0=0; //定時結束
break;
case 3: //第三次按下
keycnt=0; //重新開始判斷鍵值
sec=0; //計數重新從零開始
break;
}
while(P3_0==0);
}
}
}
void t0(void) interrupt 1 using 0 //定時中斷服務函數
{
tcnt++; //每過250ust tcnt加一
if(tcnt==400) //計滿400次(1/10秒)時
{
tcnt=0; //重新再計
sec++;
if(sec==100) //定時10秒,在從零開始計時
{
sec=0;
}
}
}
void main(void)
{
TMOD=0x02; //定時器工作在方式2
ET0=1;
EA=1;
sec=0;
while(1)
{
KEY();
LED();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -