c51数码管计时59分59秒
时间: 2023-08-10 18:07:22 浏览: 40
这是一个计时器的应用,可以使用 C51 单片机和数码管来实现。以下是一个简单的代码示例:
```c
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit dula = P2^6;
sbit wela = P2^7;
uchar code table[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
uchar seconds = 0; // 秒
uchar minutes = 0; // 分
uint count = 0; // 计数器
void delay(uint t)
{
while(t--);
}
void display(uchar num, uchar pos)
{
dula = 1;
P0 = table[num];
dula = 0;
wela = 1;
P0 = 0xfe << pos;
wela = 0;
}
void main()
{
TMOD = 0x01; // 定时器0,模式1
TH0 = 0x4c; // 定时器初值,1ms
TL0 = 0x00;
TR0 = 1; // 启动定时器0
ET0 = 1; // 开启定时器0中断
EA = 1; // 开启总中断
while(1)
{
display(seconds % 10, 0); // 显示秒个位
display(seconds / 10, 1); // 显示秒十位
display(minutes % 10, 2); // 显示分个位
display(minutes / 10, 3); // 显示分十位
}
}
void timer0() interrupt 1
{
TH0 = 0x4c; // 定时器初值,1ms
TL0 = 0x00;
count++;
if(count == 1000) // 1s
{
count = 0;
seconds++;
if(seconds == 60)
{
seconds = 0;
minutes++;
if(minutes == 60)
{
minutes = 0;
}
}
}
}
```
这段代码利用定时器0来计时,每隔1ms产生一次中断,在中断处理函数中实现计时器的逻辑。在主函数中通过显示函数将计时器的时间显示在数码管上。