利用一个数码管和两个开关实现秒表0-9功能
#include<reg52.h> #define LED_ALL P0 sbit LED = P0^0; sbit key1 = P1^0; sbit key2 = P1^1; unsigned int i = 0; unsigned n = 0; unsigned char num[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff}; void INT_T0(void) { TMOD = 0X01; //打开定时器0 ,使用模式1定时 TH0 = 0XFC; TL0 = 0X18; //初始值 EA = 1;//打开总开关 ET0 = 1;//打开中断T0开关 } void main(void) { INT_T0(); TR0 = 1; //定时功能打开 while(1) { if(key1==0) TR0 = 1; else TR0 = 0; if(key2==0) n=0; LED_ALL=num[n]; } } void time0(void) interrupt 1 { TH0 = 0XFC; TL0 = 0X18; //初始值 if(i < 1000) { i++; } else { P0 = num[n]; i = 0; if(n<9) n++; else n=0; } } 来源: https:/