|
程序如下:
#include <reg51.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;
u8 temp_buf[5][3];
u8 arr_pt;
u8 code seg_tab[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90
};
u8 code bit_tab[]={
0x80,0x40,0x20,0x10,
0x08,0x04,0x02,0x01
};
void delay(unsigned int num)
{
unsigned int x,y;
for(y=num;y>0;y--)
for(x=110;x>0;x--);
}
void display_led(u8 which_bit ,u8 which_number)
{
bit_sel=1;
DATA=bit_tab[which_bit];
bit_sel=0;
seg_sel=1;
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(3,y);
delay(2);
display_led(2,x);
delay(2);
}
}
void reflash (void)
{
display(1,n_ten_ms);
display(2,n_sec);
display(3,n_min);
}
int main(void)
{
TMOD=0X01;
TH0=(65535-10000)/256;//装初值
TL0=(65535-10000)%256;
EA=1;//开总中断
ET0=1;//开定时器0中断
TR0=1;//启动定时器
//TR0=1;
while(1)
{
if(start==0)
{
delay(5);
if(start==0)
{
run_flag=~run_flag;
}
while(!start);
}
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);
}
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);
}
}
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;
}
}
}
|
|