用动态扫描方法和定时器0在数码管的前两位显示00-59秒的循环计时,每1秒加1,写出代码
时间: 2024-05-08 10:19:41 浏览: 17
好的,我可以回答这个问题。以下是代码示例:
```c
#include <reg52.h>
// 定义端口
sbit D1 = P2^2;
sbit D2 = P2^3;
// 定义计时变量
unsigned int count = 0;
// 定时器0中断服务程序
void timer0() interrupt 1 {
TH0 = (65536 - 50000) / 256; // 重新赋初值
TL0 = (65536 - 50000) % 256;
count++; // 计时加1
if (count >= 60) count = 0; // 计时归零
}
// 动态扫描函数
void display() {
unsigned char i;
for (i = 0; i < 50; i++) { // 50ms的扫描间隔
switch (i % 2) {
case 0:
D1 = 1; // 打开第一位数码管
P0 = count / 10; // 显示十位数
break;
case 1:
D2 = 1; // 打开第二位数码管
P0 = count % 10; // 显示个位数
break;
}
Delay(1); // 延时1ms
D1 = D2 = 0; // 关闭数码管
}
}
void main() {
TMOD = 0x01; // 定时器0,模式1
TH0 = (65536 - 50000) / 256; // 初值,50ms
TL0 = (65536 - 50000) % 256;
TR0 = 1; // 启动定时器0
ET0 = 1; // 启动定时器0中断
EA = 1; // 允许中断
while (1) {
display(); // 动态扫描
}
}
// 延时函数
void Delay(unsigned int t) {
unsigned int i, j;
for (i = 0; i < t; i++)
for (j = 0; j < 110; j++);
}
```
希望这个代码能帮到你,如果你还有什么问题,请继续问我。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)