Embedded Systems 简明教程

Embedded Systems - Timer/Counter

timer 是一种用于测量时间间隔的特殊时钟类型。经常被称为用于测量经过时间从零向上计数的计时器 stopwatch 。它是一种根据指定时间间隔倒数并用于产生时间延迟的设备,例如,沙漏是一种计时器。

A timer is a specialized type of clock which is used to measure time intervals. A timer that counts from zero upwards for measuring time elapsed is often called a stopwatch. It is a device that counts down from a specified time interval and used to generate a time delay, for example, an hourglass is a timer.

counter 是一种存储(有时显示)特定事件或过程相对于时钟信号发生次数的设备。它用于计算微控制器外部发生的事件。在电子领域,可以使用寄存器类型电路(例如触发器)轻松实现计数器。

A counter is a device that stores (and sometimes displays) the number of times a particular event or process occurred, with respect to a clock signal. It is used to count the events happening outside the microcontroller. In electronics, counters can be implemented quite easily using register-type circuits such as a flip-flop.

Difference between a Timer and a Counter

将计时器与计数器区分开来的点如下:

The points that differentiate a timer from a counter are as follows −

Timer

Counter

The register incremented for every machine cycle.

The register is incremented considering 1 to 0 transition at its corresponding to an external input pin (T0, T1).

Maximum count rate is 1/12 of the oscillator frequency.

Maximum count rate is 1/24 of the oscillator frequency.

A timer uses the frequency of the internal clock, and generates delay.

A counter uses an external signal to count pulses.

Timers of 8051 and their Associated Registers

8051 有两个定时器,Timer 0 和 Timer 1。它们可用作定时器或事件计数器。Timer 0 和 Timer 1 均为 16 位宽。由于 8051 遵循 8 位架构,因此每个 16 位可作为低字节和高字节的两个独立寄存器访问。

The 8051 has two timers, Timer 0 and Timer 1. They can be used as timers or as event counters. Both Timer 0 and Timer 1 are 16-bit wide. Since the 8051 follows an 8-bit architecture, each 16 bit is accessed as two separate registers of low-byte and high-byte.

Timer 0 Register

Timer 0 的 16 位寄存器以低字节和高字节的形式访问。低字节寄存器称为 TL0(Timer 0 低字节),高字节寄存器称为 TH0(Timer 0 高字节)。这些寄存器可像任何其他寄存器一样访问。例如,指令 MOV TL0, #4H 将值移入 Timer #0 的低字节。

The 16-bit register of Timer 0 is accessed as low- and high-byte. The low-byte register is called TL0 (Timer 0 low byte) and the high-byte register is called TH0 (Timer 0 high byte). These registers can be accessed like any other register. For example, the instruction MOV TL0, #4H moves the value into the low-byte of Timer #0.

timer0

Timer 1 Register

Timer 1 的 16 位寄存器以低字节和高字节的形式访问。低字节寄存器称为 TL1(Timer 1 低字节),高字节寄存器称为 TH1(Timer 1 高字节)。这些寄存器可像任何其他寄存器一样访问。例如,指令 MOV TL1, #4H 将值移入 Timer 1 的低字节。

The 16-bit register of Timer 1 is accessed as low- and high-byte. The low-byte register is called TL1 (Timer 1 low byte) and the high-byte register is called TH1 (Timer 1 high byte). These registers can be accessed like any other register. For example, the instruction MOV TL1, #4H moves the value into the low-byte of Timer 1.

timer1

TMOD (Timer Mode) Register

Timer 0 和 Timer 1 都使用相同的寄存器来设置各种定时器操作模式。这是一个 8 位寄存器,其中低 4 位留给 Timer 0,高 4 位留给定时器。在每种情况下,低 2 位用于预先设置定时器模式,而高 2 位用于指定位置。

Both Timer 0 and Timer 1 use the same register to set the various timer operation modes. It is an 8-bit register in which the lower 4 bits are set aside for Timer 0 and the upper four bits for Timers. In each case, the lower 2 bits are used to set the timer mode in advance and the upper 2 bits are used to specify the location.

tmod

Gate − 设置后,定时器仅在 INT(0,1) 为高电平时运行。

Gate − When set, the timer only runs while INT(0,1) is high.

C/T − 计数器/定时器选择位。

C/T − Counter/Timer select bit.

M1 − 模式位 1。

M1 − Mode bit 1.

M0 − 模式位 0。

M0 − Mode bit 0.

GATE

每个定时器都有启动和停止的方法。一些定时器通过软件执行此操作,一些通过硬件执行,还有一些同时具有软件和硬件控制。8051 定时器具有软件和硬件控制。定时器的启动和停止由软件使用指令 SETB TR1CLR TR1 (对于定时器 1)以及 SETB TR0CLR TR0 (对于定时器 0)控制。

Every timer has a means of starting and stopping. Some timers do this by software, some by hardware, and some have both software and hardware controls. 8051 timers have both software and hardware controls. The start and stop of a timer is controlled by software using the instruction SETB TR1 and CLR TR1 for timer 1, and SETB TR0 and CLR TR0 for timer 0.

SETB 指令用于启动它,而 CLR 指令用于停止它。只要 TMOD 寄存器中的 GATE = 0,这些指令就会启动和停止定时器。通过使 TMOD 寄存器中的 GATE = 1,定时器可以由外部源启动和停止。

The SETB instruction is used to start it and it is stopped by the CLR instruction. These instructions start and stop the timers as long as GATE = 0 in the TMOD register. Timers can be started and stopped by an external source by making GATE = 1 in the TMOD register.

C/T (CLOCK / TIMER)

TMOD 寄存器中的此位用于决定是否使用定时器作为 delay generatorevent manager 。如果 C/T = 0,则将其用作定时器延迟生成。用于创建时延的时钟源是 8051 的晶体频率。如果 C/T = 0,则附加到 8051 的晶体频率也决定了 8051 定时器以规则间隔发出的滴答声的速度。

This bit in the TMOD register is used to decide whether a timer is used as a delay generator or an event manager. If C/T = 0, it is used as a timer for timer delay generation. The clock source to create the time delay is the crystal frequency of the 8051. If C/T = 0, the crystal frequency attached to the 8051 also decides the speed at which the 8051 timer ticks at a regular interval.

定时器频率总是附加到 8051 的晶体频率的 1/12。尽管基于 8051 的各种系统具有 10 MHz 到 40 MHz 的 XTAL 频率,但我们通常使用 11.0592 MHz 的 XTAL 频率。这是因为 8051 的串行通信波特率。XTAL = 11.0592 允许 8051 系统与 PC 通信且无错误。

Timer frequency is always 1/12th of the frequency of the crystal attached to the 8051. Although various 8051 based systems have an XTAL frequency of 10 MHz to 40 MHz, we normally work with the XTAL frequency of 11.0592 MHz. It is because the baud rate for serial communication of the 8051.XTAL = 11.0592 allows the 8051 system to communicate with the PC with no errors.

M1 / M2

M1

M2

Mode

0

0

13-bit timer mode.

0

1

16-bit timer mode.

1

0

8-bit auto reload mode.

1

1

Spilt mode.

Different Modes of Timers

Mode 0 (13-Bit Timer Mode)

在模式 0 下的 Timer 1 和 Timer 0 均作为 8 位计数器运行(带 32 位预分频器)。定时器寄存器配置为一个 13 位寄存器,由 TH1 的所有 8 位和 TL1 的低 5 位组成。TL1 的高 3 位是不确定的,应该忽略。设置运行标志 (TR1) 不会清除寄存器。当计数从所有 1 到所有 0 时,将设置定时器中断标志 TF1。对于 Timer 0,模式 0 操作与 Timer 1 相同。

Both Timer 1 and Timer 0 in Mode 0 operate as 8-bit counters (with a divide-by-32 prescaler). Timer register is configured as a 13-bit register consisting of all the 8 bits of TH1 and the lower 5 bits of TL1. The upper 3 bits of TL1 are indeterminate and should be ignored. Setting the run flag (TR1) does not clear the register. The timer interrupt flag TF1 is set when the count rolls over from all 1s to all 0s. Mode 0 operation is the same for Timer 0 as it is for Timer 1.

Mode 1 (16-Bit Timer Mode)

定时器模式 "1" 是一个 16 位定时器,是一种常用的模式。它的工作方式与 13 位模式相同,只是使用了所有 16 位。TLx 从 0 开始递增,最大为 255。达到值 255 后,TLx 重置为 0,然后 THx 递增 1。作为一个完整的 16 位定时器,该定时器最多可以包含 65536 个不同的值,并且在 65536 个机器周期后会溢出回 0。

Timer mode "1" is a 16-bit timer and is a commonly used mode. It functions in the same way as 13-bit mode except that all 16 bits are used. TLx is incremented starting from 0 to a maximum 255. Once the value 255 is reached, TLx resets to 0 and then THx is incremented by 1. As being a full 16-bit timer, the timer may contain up to 65536 distinct values and it will overflow back to 0 after 65,536 machine cycles.

Mode 2 (8 Bit Auto Reload)

两个定时器寄存器均配置为具有自动重新加载的 8 位计数器(TL1 和 TL0)。TL1(TL0)溢出将设置 TF1(TF0),并使用软件预设的 Th1(TH0)的内容重新加载 TL1(TL0)。重新加载不会改变 TH1(TH0)。

Both the timer registers are configured as 8-bit counters (TL1 and TL0) with automatic reload. Overflow from TL1 (TL0) sets TF1 (TF0) and also reloads TL1 (TL0) with the contents of Th1 (TH0), which is preset by software. The reload leaves TH1 (TH0) unchanged.

自动重新加载模式的好处在于,你可以让定时器始终包含 200 到 255 的值。如果你使用模式 0 或 1,你必须在代码中检查溢出,然后在这种情况下,将定时器重置为 200。在这种情况下,宝贵的指令会检查值和/或重新加载。在模式 2 中,微控制器会处理这一切。一旦你在模式 2 中配置了一个定时器,你不必担心检查定时器是否溢出,也不必担心重置值,因为微控制器硬件会为你完成这一切。自动重新加载模式用于建立一个通用的波特率。

The benefit of auto-reload mode is that you can have the timer to always contain a value from 200 to 255. If you use mode 0 or 1, you would have to check in the code to see the overflow and, in that case, reset the timer to 200. In this case, precious instructions check the value and/or get reloaded. In mode 2, the microcontroller takes care of this. Once you have configured a timer in mode 2, you don’t have to worry about checking to see if the timer has overflowed, nor do you have to worry about resetting the value because the microcontroller hardware will do it all for you. The auto-reload mode is used for establishing a common baud rate.

Mode 3 (Split Timer Mode)

定时器模式 "3" 称为 split-timer mode 。将 Timer 0 设置为模式 3 时,它将变为两个独立的 8 位定时器。Timer 0 是 TL0,Timer 1 是 TH0。这两个定时器从 0 计数到 255,并在溢出的情况下重置回 0。Timer 1 的所有位现在都将链接到 TH0。

Timer mode "3" is known as split-timer mode. When Timer 0 is placed in mode 3, it becomes two separate 8-bit timers. Timer 0 is TL0 and Timer 1 is TH0. Both the timers count from 0 to 255 and in case of overflow, reset back to 0. All the bits that are of Timer 1 will now be tied to TH0.

当 Timer 0 处于分解模式时,真正的 Timer 1(即 TH1 和 TL1)可以设置为模式 0、1 或 2,但它不能启动/停止,因为执行此操作的位现在链接到 TH0。真正的定时器 1 将在每个机器周期内递增。

When Timer 0 is in split mode, the real Timer 1 (i.e. TH1 and TL1) can be set in modes 0, 1 or 2, but it cannot be started/stopped as the bits that do that are now linked to TH0. The real timer 1 will be incremented with every machine cycle.

Initializing a Timer

决定定时器模式。考虑运行持续且独立于任何外部管脚的 16 位定时器。

Decide the timer mode. Consider a 16-bit timer that runs continuously, and is independent of any external pins.

初始化 TMOD SFR。使用 TMOD 的最低 4 位,并考虑定时器 0。将两个位,GATE 0 和 C/T 0,保持为 0,因为我们想要定时器独立于外部管脚。由于 16 位模式是定时器模式 1,因此清除 T0M1,并设置 T0M0。实际上,唯一要开启的是 TMOD 的第 0 位。现在执行以下指令 −

Initialize the TMOD SFR. Use the lowest 4 bits of TMOD and consider Timer 0. Keep the two bits, GATE 0 and C/T 0, as 0, since we want the timer to be independent of the external pins. As 16-bit mode is timer mode 1, clear T0M1 and set T0M0. Effectively, the only bit to turn on is bit 0 of TMOD. Now execute the following instruction −

MOV TMOD,#01h

现在,定时器 0 处于 16 位定时器模式,但定时器未运行。要以运行模式启动定时器,通过执行以下指令设置 TR0 位 −

Now, Timer 0 is in 16-bit timer mode, but the timer is not running. To start the timer in running mode, set the TR0 bit by executing the following instruction −

SETB TR0

现在,定时器 0 将立即开始计数,每隔一个机器周期递增一次。

Now, Timer 0 will immediately start counting, being incremented once every machine cycle.

Reading a Timer

可以两种方式读取 16 位定时器。读取定时器的实际值作为 16 位数字,或者检测定时器什么时候溢出。

A 16-bit timer can be read in two ways. Either read the actual value of the timer as a 16-bit number, or you detect when the timer has overflowed.

Detecting Timer Overflow

当定时器从最高值溢出到 0 时,微控制器会自动在 TCON 寄存器中设置 TFx 位。因此,无需检查定时器的准确值,而是可以检查 TFx 位。如果设置了 TF0,则定时器 0 已溢出;如果设置了 TF1,则定时器 1 已溢出。

When a timer overflows from its highest value to 0, the microcontroller automatically sets the TFx bit in the TCON register. So instead of checking the exact value of the timer, the TFx bit can be checked. If TF0 is set, then Timer 0 has overflowed; if TF1 is set, then Timer 1 has overflowed.