📅  最后修改于: 2021-01-01 02:55:51             🧑  作者: Mango
在应用软件中,延迟是影响软件开发过程的重要因素之一。然而,正常延迟将不会提供特定的结果来克服实现计时器延迟的问题。
计数器和计时器是微控制器的硬件组件,在许多应用中使用它们来提供带有计数脉冲的宝贵时间延迟。计数器和计时器都通过使用软件技术来实现。
让我们看一下区分计时器和计数器的几点,如下所示:
Counter | Timer |
---|---|
The register is incremented considering 1 to 0 transitions at its corresponding to an external input pin (T0, T1). | The register incremented for every machine cycle. |
A counter uses an external signal to count pulses. | A timer uses the frequency of the internal clock signal, and generates delay. |
Maximum count rate is 1/24 of the oscillator frequency. | Maximum count rate is 1/12 of the oscillator frequency. |
让我们看一下使用timer1和mode2(T1M2)生成时间延迟的程序:
#include
void main()
{
unsigned char j;
TMOD=0x20; //set the timer mode//
for(j=0;j<2;j++) //double the time delay//
{
TL1=0x19; //set the time delay//
TH1=0x00;
TR1=1; //timer is on//
While(TF1==0); //check the flag bit//
TF1=0;
}
TR1=0; //timer is off//
}
void delay()
{
unsigned int j;
for(j=0;j<30000;j++);
}