#include <reg51.h> unsigned char code table[]={0xFC,0x60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0, 0xFE,0xF6,0xEE,0x3E,0x9C,0x7A,0x9E,0x8E}; unsigned char Count,i,j; void Mydelay10ms(void) { unsigned char i,j; for(i=250;i>0;i--) for(j=248;j>0;j--); } void putch(unsigned char n) { SBUF=n; while(TI==0); TI=0; } void main(void) { Count=60; i=table[Count/10]; j=table[Count%10]; putch(i); putch(j); Mydelay10ms(); Mydelay10ms(); Mydelay10ms(); Mydelay10ms(); while(1) { Mydelay10ms(); Count--; if(Count==0) { Count=60; } i=table[Count/10]; j=table[Count%10]; putch(i); putch(j); Mydelay10ms(); Mydelay10ms(); Mydelay10ms(); Mydelay10ms(); } }上述程序修改为用定时中断,定时1s,实现0到60的计数,60清零
时间: 2023-12-09 10:02:49 浏览: 56
下面是修改后的程序:
```c
#include <reg51.h>
unsigned char code table[]={0xFC,0x60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0,0xFE,0xF6,0xEE,0x3E,0x9C,0x7A,0x9E,0x8E};
unsigned char Count,i,j;
void Timer0_Init()
{
TMOD |= 0x01; // 设置定时器0为模式1
TH0 = 0x3C; // 设置定时器0初值为0x3C
TL0 = 0xAF; // 设置定时器0初值为0xAF
EA = 1; // 打开总中断开关
ET0 = 1; // 打开定时器0中断开关
TR0 = 1; // 启动定时器0
}
void Timer0_ISR() interrupt 1 // 定时器0中断服务程序
{
TH0 = 0x3C; // 重新设置定时器0初值
TL0 = 0xAF;
Count--; // 计数器减1
if(Count == 0)
{
Count = 60; // 计数器清零
}
i = table[Count / 10];
j = table[Count % 10];
SBUF = i; // 发送高位数码
while(TI == 0);
TI = 0;
SBUF = j; // 发送低位数码
while(TI == 0);
TI = 0;
}
void main(void)
{
Count = 60;
Timer0_Init();
while(1);
}
```
在修改后的程序中,我们使用了定时器0来实现每隔1秒钟计数器减1的功能。具体来说,我们设置定时器0为模式1,初值为0x3CAF,定时器溢出时触发定时器0中断服务程序,在其中让计数器减1并发送数码管显示的数码。程序中的`Timer0_Init()`函数用于初始化定时器0,`Timer0_ISR()`函数为定时器0的中断服务程序。程序中的`TR0`为定时器0的启动控制位,`ET0`为定时器0的中断允许控制位。在程序的`main()`函数中,我们调用了`Timer0_Init()`函数进行定时器0的初始化,并在无限循环中等待定时器0中断的触发。