#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; TH0 = 0x3C; TL0 = 0xAF; EA = 1; ET0 = 1; TR0 = 1; } void Timer0_ISR() interrupt 1 { TH0 = 0x3C; TL0 = 0xAF; Count--; if(Count == 60) { Count = 0; } 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); }
时间: 2024-03-07 09:50:47 浏览: 200
duojikongzhi.rar_Unsigned_c++include <reg51>
这段程序与之前的程序相比,只有一个小错误。在定时器中断服务程序中,我们将`if(Count == 0)`修改为了`if(Count == 60)`,这样在计数器减到0时不会将计数器重新设置为60,而是一直保持在0。因此,需要将`if(Count == 60)`修改回`if(Count == 0)`。修改后的程序如下:
```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;
TH0 = 0x3C;
TL0 = 0xAF;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void Timer0_ISR() interrupt 1
{
TH0 = 0x3C;
TL0 = 0xAF;
Count--;
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的功能。程序中的`Timer0_Init()`函数用于初始化定时器0,`Timer0_ISR()`函数为定时器0的中断服务程序。在程序的`main()`函数中,我们调用了`Timer0_Init()`函数进行定时器0的初始化,并在无限循环中等待定时器0中断的触发。
阅读全文