|
不改動(dòng)怎么加按鍵功能?給你改成單鍵60分鐘百分秒表。1ms數(shù)碼管動(dòng)態(tài)顯示。
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
#define ab P0
//uchar int_time;
uchar b,c;
uint a;
sbit LSA = P2^2;
sbit LSB = P2^3;
sbit LSC = P2^4;
sbit key1 = P3^0;
unsigned char code table[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x79,0x71};
uchar displaydata[8];
bit d;
void digdisplay();
void keyscan();
void clock(a,b)
{
displaydata[0] = table[c/10];
displaydata[1] = table[c%10];
displaydata[3] = table[b/10];
displaydata[4] = table[b%10];
displaydata[6] = table[a/100%10];
displaydata[7] = table[a/10%10];
}
/***********************************
* ?÷oˉêy
***********************************/
void main()
{
TMOD= 0x01; //設(shè)置定時(shí)器模式
TL0 = 0x66; //設(shè)置定時(shí)初值1ms
TH0 = 0xFC; //設(shè)置定時(shí)初值
EA = 1;
ET0 = 1;
TR0 = 1;
displaydata[2] = 0x40;
displaydata[5] = 0x40;
a =0;b = 0;
while(1)
{
keyscan();
clock(a,b);
}
}
void digdisplay()
{
static uchar i=0;
ab = 0x00;
switch(i)
{
case 0:
LSA = 0;LSB = 0;LSC = 0;ab = displaydata[i];i++;break;
case 1:
LSA = 1;LSB = 0;LSC = 0;ab = displaydata[i];i++;break;
case 2:
LSA = 0;LSB = 1;LSC = 0;ab = displaydata[i];i++;break;
case 3:
LSA = 1;LSB = 1;LSC = 0;ab = displaydata[i];i++;break;
case 4:
LSA = 0;LSB = 0;LSC = 1;ab = displaydata[i];i++;break;
case 5:
LSA = 1;LSB = 0;LSC = 1;ab = displaydata[i];i++;break;
case 6:
LSA = 0;LSB = 1;LSC = 1;ab = displaydata[i];i++;break;
case 7:
LSA = 1;LSB = 1;LSC = 1;ab = displaydata[i];i=0;break;
}
}
void keyscan()
{
static uchar count1=0,count2=0; //計(jì)數(shù)變量
static bit key_sign=0; //按鍵狀態(tài)標(biāo)志
if(key1==0) //檢測(cè)輸入如果為0
{
if((++count1>=100)&&(key_sign==0))
{
key_sign=1; //按鍵自鎖標(biāo)志置1,防止重復(fù)響應(yīng)
count2++;
if(count2>=3)
count2=0;
switch(count2)
{
case 0: a=0;
b=0;
c=0; break;//清0
case 1: d=1; break;//計(jì)時(shí)
case 2: d=0; break;//暫停
}
}
}
else
{
count1=0; //計(jì)數(shù)變量清0
key_sign=0; //按鍵狀態(tài)標(biāo)志清0
}
}
void TO_time() interrupt 1
{
TL0 = 0x66; //設(shè)置定時(shí)初值1ms
TH0 = 0xFC; //設(shè)置定時(shí)初值
if(d)
{
a++;
if(a == 1000)
{
b++;
a=0;
}
if(b == 60)
{
b = 0;
c++;
}
if(c == 60)
c = 0;
}
digdisplay();
} |
|