好資料 下面是樓主的源碼
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit KEY1 = P3^2;
sbit KEY2 = P3^3;
sbit KEY3 = P3^4;
uint hour=12,min=23,sec=33;
uint flag=1;
uint Count = 0;
uchar code table[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//數碼管段值
void Delay(uint del)
{ uchar i,j;
for(i=0; i<del; i++)
for(j=0; j<140; j++)
;}
void Time0_Init()
{ Count++;
TMOD=0x01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
EA=1;
ET0=1;}
void timer0_int(void)interrupt 1
{
Count++;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256; }
void time()
{ P2=7;
P0=table[sec%10];
Delay(1);
P2=6;
P0=table[sec/10];
Delay(1);
P2=5;
P0=0x40;
Delay(1);
P2=4;
P0=table[min%10];
Delay(1);
P2=3;
P0=table[min/10];
Delay(1);
P2=2;
P0=0x40;
Delay(1);
P2=1;
P0=table[hour%10];
Delay(1);
P2=0;
P0=table[hour/10];
Delay(1); }
void Key(void)
{ if(KEY1==0 ) //計時開始
{Delay(1); //20毫秒軟件防抖
if(KEY1==0)
TR0=1; }
if(KEY2==0 )
{ Delay(1);
if(KEY2 == 0)
TR0=0;
Delay(1);}
if(KEY3==0 ) //改變加減法
{Delay(1); //20毫秒軟件防抖
if(KEY3 == 0)
flag=!flag;
Delay(1);
}
}
void Main(void)
{ Time0_Init();
while(1)
{ time();
Key();
if(Count == 100)
{ Count=0;
if(flag==1) //type=1時為加法
{ sec++;
if(sec>59)
{ sec=0;
min++;
if(min>59)
{min=0;
hour++;
if(hour>23)
hour=0; }} }
else if(flag==0)
{sec--;
if(sec<1)
{sec=59;
min--;
if(min<1)
{min=59;
hour--;
if(hour<1)
hour=23;} } }
}
}}
|