代码:
#include<reg52.h>
typedef unsigned char u8;
typedef unsigned int u16;
/********端口定义*********/
#define DATA P0
sbit seg_sel = P1^4;
sbit bit_sel = P1^5;
sbit start = P3^2;
sbit store = P3^3;
sbit diskey = P3^4;
sbit reset = P3^5;
sbit led = P1^0;
/********变量定义*********/
u16 tem_ms;
u8 n_ten_ms,n_sec,n_min;
u8 run_flag;//当run_flag为1,代表开始计数
u8 temp_buf[5][3];
u8 arr_pt;
u8 code seg_tab[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e
};
u8 code bit_tab[]={
0x10,0x20,0x40,0x80,
0x01,0x02,0x04,0x08
};
void delay(unsigned int y)
{
unsigned int x,z;
for(x=y; x>0; x--)
for(z=110; z>0; z--);
}
void display_led(u8 which_bit, u8 which_number)
{
bit_sel = 1;//Q[7..0]=D[7..0]
DATA = bit_tab[which_bit];
bit_sel = 0;//BIT[7..0]=0x80
seg_sel = 1;//Q[7..0]=D[7..0]
DATA = seg_tab[which_number];
seg_sel = 0;
}
void display(u8 which_bit, u8 which_number)
{
u8 x,y;
x = which_number/10;//分离十位
y = which_number%10;//分离个位
if (which_bit == 1)
{
display_led(1, x);
delay(2);
display_led(0, y);
delay(2);
}
if (which_bit == 2)
{
display_led(4, x);
delay(2);
display_led(3, y);
delay(2);
}
if (which_bit == 3)
{
display_led(7, x);
delay(2);
display_led(6, y);
delay(2);
}
}
void reflash (void)
{
display(1, n_ten_ms);
display(2, n_sec);
display(3, n_min);
}
void main (void)
{
TMOD = 0x01;//设置定时/计数器T0
TH0 = (65535-10000) / 256;
TL0 = (65535-10000) % 256;
EA = 1;//开放总中断
ET0 = 1;//开放定时器0的溢出中断
//TR0 = 1;
while (1)
{
if (start == 0)
{
delay(5);
if (start == 0)
{
run_flag = ~run_flag;
} while(!start);
}
if (run_flag)
{
TR0 = 1; //开始计数
}
else if (!run_flag)
{
TR0 = 0;
if (diskey == 0)
{
delay (5);
if (diskey == 0)
{
arr_pt --;
n_ten_ms = temp_buf[arr_pt][0];
n_sec = temp_buf[arr_pt][1];
n_min = temp_buf[arr_pt][2];
} while (!diskey);
}
}
if (reset == 0)
{
delay(5);
if (reset == 0)
{
n_ten_ms = 0;
n_sec = 0;
n_min = 0;
} while(!reset);
}
if (store == 0)
{
delay(5);
if (store == 0)
{
if ((arr_pt >=0) && (arr_pt< 5))
{
temp_buf[arr_pt][0] = n_ten_ms;
temp_buf[arr_pt][1] = n_sec;
temp_buf[arr_pt][2] = n_min;
arr_pt++;
}
} while(!store);
}
reflash();
}
}
void t0_server(void) interrupt 1
{
TH0 = (65535-10000) / 256;
TL0 = (65535-10000) % 256;
n_ten_ms++;
if (n_ten_ms >= 100)
{
n_ten_ms = 0;
n_sec++;
if (n_sec >= 60)
{
n_sec = 0;
n_min++;
if (n_min >= 60)
n_min = 0;
}
}
}
电路图:
实验效果: