计时表量程:00:00:00--59:59:99(min:s:10ms)
可存储和回查5组数据。
代码:
#include<reg52.h>
#include<string.h>
/***********************定义管脚**********************/
#define seg P0
#define bitt P2typedef unsigned char u8;
typedef unsigned int u16;sbit start = P3^1;
sbit store = P3^0;
sbit playback = P3^2;
sbit reset = P3^3;
sbit beep = P1^5;/***********************定义变量**********************/
u8 code display_bit[]={0x00,0x04,0x08,0x0c,0x10,0x14,0x18,0x1c};
u8 code display_seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00,0x40};
u8 time_store[5][3] = {0};
u8 i,j,k,flag; //*******全局变量*********//
u8 n_10ms;
u8 n_s;
u8 n_min;/***********************延时函数**********************/
void delay(u8 ms)
{u16 x,y;for(x=ms;x>0;x--)for(y=110;y>0;y--);
}/**********************嗡鸣器函数*********************/
void beep_vibrate()
{beep = ~beep;delay(1); beep = ~beep;delay(1);
}/*********************数码管显示**********************/
void led_display(u8 num_seg,u8 num_bit)
{ bitt = display_bit[num_bit];seg = display_seg[num_seg];
}/****************选择时间显示位置*********************/
void time_display(u8 time_bit, u8 time_num)
{u8 x,y,s;s = 1;x = time_num/10; //分离十位y = time_num%10; //分离个位if(time_bit == 1){led_display(y,0);delay(s);led_display(x,1);delay(s); }else if(time_bit == 2){led_display(y,3);delay(s); led_display(x,4);delay(s); }else if(time_bit == 3){led_display(y,6);delay(s);led_display(x,7);delay(s);}else{led_display(17,2);delay(s);led_display(17,5);delay(s);}
}/********************不断刷新三组数字****************/
void reflash()
{time_display(1,n_10ms);time_display(2,n_s);time_display(3,n_min);time_display(4,0);
}/**********************定时器T0预设******************/
void Init_timer0()
{IE = 0X82; //中断允许TMOD = 0x01;TH0 = 0x0D8;TL0 = 0x0F0;
}/************************开始函数*********************/
void start_k1()
{if(start == 0){delay(5);if(start == 0){flag = ~flag;}while(!start);}
}/************************存储函数*********************/
void store_k2()
{if(store == 0){delay(5);if(store == 0){if(i < 5){time_store[i][0] = n_10ms;time_store[i][1] = n_s;time_store[i][2] = n_min;i++;}while(!store);}}
}/************************回显函数*********************/
void playback_k3()
{if(playback == 0){delay(5);if(playback == 0){if(i >= 0){i = i>4?4:i;n_10ms = time_store[i][0];n_s = time_store[i][1];n_min = time_store[i][2];i--;}while(!playback);}}
}/************************清0函数*********************/
void reset_k4()
{if(reset == 0){delay(5);if(reset == 0){TR0 = 0;Init_timer0();flag = 0;i = 0;memset(time_store,0,sizeof(time_store));//将time_store[][]二维数组的元素全部设为0n_10ms = 0;n_s = 0;n_min = 0;while(!reset);}}
}/************************主函数**********************/
void main()
{seg = 0x00;Init_timer0();n_10ms = 0; //10毫秒n_s = 0; //秒n_min = 0; //分钟beep = 1; //嗡鸣器初始化flag = 0; //flag=1时,定时器打开i = 0; while(1) //大循环{start_k1(); //开始if(flag){TR0 = 1; //打开定时器store_k2(); //存储 }else{TR0 = 0; //关闭定时器playback_k3(); //回显} reset_k4(); //清0reflash(); //刷新一次}
}/*******************定时器服务函数*******************/
void timer0_service() interrupt 1
{TH0 = 0x0D8;TL0 = 0x0F0;if(n_10ms < 99){n_10ms++;}else{n_10ms = 0;if(n_s < 59){n_s++; }else{n_s = 0;if(n_min < 59){n_min++;}else{reset_k4(); } }}
}
仿真图
51单片机DIY