设计一个基于51单片机的秒表计时器,实现如下功能: 计时器的计时精度为百分之一秒,即0.01秒。 计时时间显示格式:hh.mm.ss.SS。其中:hh表示小时数,mm表示分钟数,ss表示秒数,SS表示百分秒数。如“1.59.59.99”表示1小时59分59秒99分秒。 当计时时间小于1分钟时,只显示秒和分秒数,如“59.99”表示0小时0分59秒99分秒。 当计时时间小于1小时时,只显示分钟、秒和分秒数,如“59.59.99”表示0小时59分59秒99分秒。 计时器有两种工作状态:停止状态、计时状态。 停止状态下计时器停止计时,保持当前计时时间不变。开机时默认进入该状态并初始化当前计时时间为0(0小时0分0秒0分秒)。 计时状态下计时器进行计时,每隔百分之一秒更新当前显示的计时时间。 计时器有两个按钮:启动/停止按钮、复位按钮。 在停止状态下按下“启动/停止”按钮使计时器立即进入计时状态,在当前计时时间基础上开始计时。 在计时状态下按下“启动/停止”按钮使计时器立即进入停止状态。 在计时状态下按下“复位”按钮将计时器当前计时时间重置为0,使计时器重新开始计时。 在停止状态下按下“复位”按钮时,将计时器的当前计时时间重置为0,计时器仍然保持停止状态。
时间: 2024-02-06 22:13:06 浏览: 94
基于单片机控制的9999秒倒计时器的毕业设计设计
4星 · 用户满意度95%
以下是一个基于51单片机的秒表计时器的示例代码,实现了上述功能:
```c
#include <reg51.h>
// 数码管位选控制
#define LED_COM P0
// 数码管段选控制
sbit LED_A = P2^2;
sbit LED_B = P2^3;
sbit LED_C = P2^4;
sbit LED_D = P2^5;
sbit LED_E = P2^6;
sbit LED_F = P2^7;
sbit LED_G = P3^0;
sbit LED_DP = P3^1;
// 定义显示的数字和位置
unsigned char led[] = {0, 0, 0, 0, 0, 0, 0, 0};
unsigned char position[] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f};
// 定义计时器状态
#define STOP 0
#define RUNNING 1
unsigned char timer_state = STOP;
// 定义计时器时间
unsigned long timer_hour = 0;
unsigned long timer_minute = 0;
unsigned long timer_second = 0;
unsigned long timer_millisecond = 0;
// 延时函数
void delay(unsigned int t)
{
unsigned int i, j;
for (i = t; i > 0; i--)
{
for (j = 110; j > 0; j--);
}
}
// 数码管显示函数
void display()
{
unsigned char i;
for (i = 0; i < 8; i++)
{
LED_COM = position[i];
switch (led[i])
{
case 0:
LED_A = LED_B = LED_C = LED_D = LED_E = LED_F = 1;
LED_G = 0;
break;
case 1:
LED_B = LED_C = 1;
LED_A = LED_D = LED_E = LED_F = LED_G = 0;
break;
case 2:
LED_A = LED_B = LED_D = LED_E = LED_G = 1;
LED_C = LED_F = 0;
break;
case 3:
LED_A = LED_B = LED_C = LED_D = LED_G = 1;
LED_E = LED_F = 0;
break;
case 4:
LED_B = LED_C = LED_F = LED_G = 1;
LED_A = LED_D = LED_E = 0;
break;
case 5:
LED_A = LED_C = LED_D = LED_F = LED_G = 1;
LED_B = LED_E = 0;
break;
case 6:
LED_A = LED_C = LED_D = LED_E = LED_F = LED_G = 1;
LED_B = 0;
break;
case 7:
LED_A = LED_B = LED_C = 1;
LED_D = LED_E = LED_F = LED_G = 0;
break;
case 8:
LED_A = LED_B = LED_C = LED_D = LED_E = LED_F = LED_G = 1;
break;
case 9:
LED_A = LED_B = LED_C = LED_D = LED_F = LED_G = 1;
LED_E = LED_G = 0;
break;
default:
break;
}
delay(1); // 延时1ms
LED_A = LED_B = LED_C = LED_D = LED_E = LED_F = LED_G = LED_DP = 0;
}
}
// 计时器更新函数
void update_timer()
{
timer_millisecond++;
if (timer_millisecond >= 100)
{
timer_millisecond = 0;
timer_second++;
if (timer_second >= 60)
{
timer_second = 0;
timer_minute++;
if (timer_minute >= 60)
{
timer_minute = 0;
timer_hour++;
}
}
}
}
// 主函数
void main()
{
unsigned int i;
while (1)
{
// 获取当前时间
unsigned long hour = timer_hour;
unsigned long minute = timer_minute;
unsigned long second = timer_second;
unsigned long millisecond = timer_millisecond;
// 将时间转换为数码管显示的数字
if (hour >= 1)
{
led[0] = hour / 10;
led[1] = hour % 10;
led[2] = minute / 10;
led[3] = minute % 10;
led[4] = second / 10;
led[5] = second % 10;
led[6] = millisecond / 10;
led[7] = millisecond % 10;
}
else if (minute >= 1)
{
led[0] = minute / 10;
led[1] = minute % 10;
led[2] = second / 10;
led[3] = second % 10;
led[4] = millisecond / 10;
led[5] = millisecond % 10;
led[6] = led[7] = 0;
}
else
{
led[0] = led[1] = 0;
led[2] = second / 10;
led[3] = second % 10;
led[4] = millisecond / 10;
led[5] = millisecond % 10;
led[6] = led[7] = 0;
}
// 数码管显示
display();
// 更新计时器
if (timer_state == RUNNING)
{
update_timer();
}
// 检测按键
if (P1^0 == 0) // 启动/停止按钮
{
if (timer_state == STOP)
{
timer_state = RUNNING;
}
else
{
timer_state = STOP;
}
while (P1^0 == 0);
}
if (P1^1 == 0) // 复位按钮
{
timer_hour = timer_minute = timer_second = timer_millisecond = 0;
while (P1^1 == 0);
}
// 延时
for (i = 0; i < 10; i++)
{
delay(100); // 延时0.01s
}
}
}
```
需要注意的是,这段代码中的按键检测需要根据具体的硬件平台进行实现。另外,为了实现百分之一秒的计时精度,延时函数的延时时间需要调整为10,即延时0.01秒。
阅读全文