#include<reg52.h>

typedef unsigned char uchar;
typedef unsigned int uint;

//按键定义
sbit key_stop = P1 ^ 0;
sbit key_star = P1 ^ 1;
sbit key_fmq = P1 ^ 7;

//0~9的十六进制数码
uchar code seg[10] = { 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90 };  // 0~9

//秒、分、时标志
uchar miao = 0, fen = 0, shi = 0;

//秒、分、时高位低位
uchar miao_L, miao_H, fen_L, fen_H, shi_L, shi_H;

//计数
uint counter = 0, sw_counter = 0;

//模式
uint moshi = -1;

//秒表是否打开
uint sw = 0;

//重置
uint cz = 0;

//秒表显示
uint sw_show = 0;

//秒表 秒、分、时标志
uchar sw_miao = 0, sw_fen = 1, sw_shi = 0;

//秒表 秒、分、时高位低位
uchar sw_fen_L, sw_fen_H, sw_miao_L, sw_miao_H;


void delay(uint x) {  //延时函数
	while (x--)
		;
}//delay

void key_delay(int xms) {  //按键延时函数 x ms
	unsigned int i, j;
	for (i = 0; i < xms; ++i)
		for (j = 0; j < 110; ++j)
			;
}//key_delay

void T0_Init() {  //定时器初始化

	TMOD = 0x01;
	TH0 = 0x3c;
	TL0 = 0xb0;
	EA = 1;  //开总中断
	ET0 = 1;
	TR0 = 1;
}//T0_Init

void display_shi(){
	P2 = 0x02;	// 0000 0010
	P0 = seg[shi_L];
	delay(100);
	P2 = 0x01;	// 0000 0001
	P0 = seg[shi_H];
	delay(100);
}
void display_fen(){
	P2 = 0x08;	// 0001 0000
	P0 = seg[fen_L];
	delay(100);
	P2 = 0x04;	// 0000 1000
	P0 = seg[fen_H];
	delay(100);
}

void display() {  //显示函数
	if (moshi != 1 && moshi != 2) {
			display_fen();
			display_shi();
	}
	if (moshi == 1) {
		display_shi();
		delay(3000);
		display_fen();
	}
	if (moshi == 2) {
		display_fen();
		delay(3000);
		display_shi();
	}

}//display

void stopwatch() {
	P2 = 0x08;	// 0001 0000
	P0 = seg[sw_miao_L];
	delay(50);

	P2 = 0x04;	// 0000 1000
	P0 = seg[sw_miao_H];
	delay(50);

	P2 = 0x02;	// 0000 0010
	P0 = seg[sw_fen_L];
	delay(50);

	P2 = 0x01;	// 0000 0001
	P0 = seg[sw_fen_H];
	delay(50);
}

void keyscan() {  //按键函数
	if (key_stop == 0) {
		key_delay(20);
		moshi++;
		if (moshi == 4) {
			sw_show = 0;
			moshi = 0;
			sw = 0;
			sw_miao = 0;
			sw_fen = 1;
			key_fmq = 1;
			cz = 0;
		}
		if (moshi == 0) {
			EA = 1;
		}
		if (moshi == 3)
			sw_show = 1;
		while (!key_stop);
	}
	if (key_star == 0) {
		if (moshi == 1) {
			key_delay(20);
			if (key_star == 0) {
				fen++;
				if (fen == 60) {
					fen = 0; // 将小时数重置为0					
				}
			}
		}
		if (moshi == 2) {
			key_delay(20);
			if (key_star == 0) {
				shi++;
				if (shi == 24) {
					shi = 0; // 将小时数重置为0
				}
			}
		}
		if (moshi == 3) {
			key_delay(20);
			if (key_star == 0) {
				cz = ~cz;
				sw = 1;
			}
		}
	}
	while (!key_star);
}


void main() {

	T0_Init();

	while (1) {
		if (sw_show == 0) {
			display();
		}
		if (sw_show == 1)
		{
			stopwatch();
		}
		keyscan();
	}

}//main

void timer0_Init() interrupt 1{  //中断函数 

	counter++;
	if (counter == 20) {
		counter = 0;
		miao++;	//i = 100 

		if (miao == 60) {
			miao = 0;
			fen++;
			if (fen == 60) {
				fen = 0;
				shi++;
				if (shi == 24) {
					shi = 0;
					fen = 0;
					miao = 0;
				}
			}
		}
		if (fen == 59) {
			if (miao >= 50 && miao % 2 == 0) {
				key_fmq = 0;
			}
			else {
				key_fmq = 1;
			}
		}
		miao_L = miao % 10;
		miao_H = miao / 10;

		fen_L = fen % 10;
		fen_H = fen / 10;

		shi_L = shi % 10;
		shi_H = shi / 10;
	}
	if (sw == 1) {
		if (sw_miao != 0 || sw_fen == 1) {
			sw_counter++;
			if (sw_counter == 20) {
				sw_counter = 0;

				if (sw_miao == 0) {
					sw_miao = 60;
					sw_fen = 0;
				}
				sw_miao--;

			}
		}
		if (sw_miao == 0 && sw_fen == 0)
		{
			key_fmq = 0;

		}if (cz) {
			sw_miao = 0;
			sw_fen = 1;
			sw = 0;
			key_fmq = 1;

		}

	}
	sw_miao_L = sw_miao % 10;
	sw_miao_H = sw_miao / 10;

	sw_fen_L = sw_fen % 10;
	sw_fen_H = sw_fen / 10;
}//timer0_Init
根据代码可以，写系统设计