写一个51单片机数码管计时器时分秒,独立按键控制暂停开始清零。
时间: 2024-02-26 07:59:03 浏览: 56
好的,以下是一个简单的示例程序,实现了51单片机数码管计时器时分秒,独立按键控制暂停开始清零的功能:
```
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit key_start = P3^0; // 按键1
sbit key_pause = P3^1; // 按键2
sbit key_clear = P3^2; // 按键3
uchar code table[] = { // 数码管显示表
0x3f, 0x06, 0x5b, 0x4f, 0x66,
0x6d, 0x7d, 0x07, 0x7f, 0x6f
};
uchar second = 0; // 秒
uchar minute = 0; // 分
uchar hour = 0; // 时
uchar flag_pause = 0; // 暂停标志
uchar flag_clear = 0; // 清零标志
void delay(uint t) { // 延时函数
uint i, j;
for(i = t; i > 0; i--)
for(j = 110; j > 0; j--);
}
void display(uchar num, uchar pos) { // 在数码管上显示数字
uchar i, j;
uchar buf[8] = {0}; // 数组用于存储8位二进制数值
for(i = 0; i < 8; i++) {
buf[i] = num % 2;
num /= 2;
}
for(i = 0, j = pos; i < 8; i++, j++) {
P0 = 0xff;
P2 = 1 << j;
P0 = ~buf[i] << 4;
delay(1);
}
}
void timer0() interrupt 1 { // 定时器中断函数
TH0 = (65536 - 50000) / 256; // 0.5秒
TL0 = (65536 - 50000) % 256;
if(!flag_pause) { // 未暂停状态下计时
second++;
if(second == 60) {
second = 0;
minute++;
if(minute == 60) {
minute = 0;
hour++;
if(hour == 24) {
hour = 0;
}
}
}
}
}
void main() {
TMOD = 0x01; // 定时器0工作在模式1
TH0 = (65536 - 50000) / 256; // 初始值,0.5秒
TL0 = (65536 - 50000) % 256;
ET0 = 1; // 允许定时器0中断
EA = 1; // 允许总中断
TR0 = 1; // 启动定时器0
while(1) {
if(key_start == 0) { // 按键1,开始计时
flag_pause = 0;
}
if(key_pause == 0) { // 按键2,暂停计时
flag_pause = 1;
}
if(key_clear == 0) { // 按键3,清零计时
flag_clear = 1;
flag_pause = 1;
second = 0;
minute = 0;
hour = 0;
}
if(flag_clear) { // 清空数码管
display(0, 0);
display(0, 4);
display(0, 8);
flag_clear = 0;
} else { // 显示时间
display(table[second % 10], 0); // 秒的个位
display(table[second / 10], 1); // 秒的十位
display(table[minute % 10], 4); // 分的个位
display(table[minute / 10], 5); // 分的十位
display(table[hour % 10], 8); // 时的个位
display(table[hour / 10], 9); // 时的十位
}
}
}
```
在上面的示例程序中,我们使用定时器0来进行计时,并在定时器中断函数中更新时间变量。同时,我们使用三个独立的按键来控制计时器的暂停、开始和清零。在主程序中,我们不断地检测按键状态,并根据需要执行相应的操作。在显示时间时,我们使用了一个数码管显示表来将数字转换为对应的数码管段码,并使用`display()`函数在数码管上显示。最后,我们使用一个简单的延时函数`delay()`来控制数码管的刷新速度。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)