Timer and Counter
Table of Contents
Timer and Counter
What is Timer and Counter
A timer is a specific kind of clock that is used to record durations of time. A stopwatch is a timer that counts up from zero to measure the passage of time. It is a tool that creates a time delay by counting down from a set time interval; an hourglass is an example of a timer.
A counter is a device that keeps track of (and occasionally displays) the frequency of a specific event or process in relation to a clock signal. It is employed to keep track of events taking place away from the microcontroller. Counters are quite simple to implement in electronics using register-type circuits, like a flip-flop.
Difference between Timer and Counter
The points that differentiate timer and counter are as follows −
Timer | Counter |
---|---|
Every time the machine cycled, the register was increased. | The register is increased while taking into account a 1 to 0 transition at the pins (T0, T1) that correspond to an external input. |
The highest count rate is one-twelfth of oscillator frequency. | The highest possible count rate is 1/24 of oscillator frequency. |
A timer creates delay by using the internal clock’s frequency. | To count pulses, a counter uses an outside signal. |
Timers of 8051 and their Associated Registers
Timer 0 and Timer 1 are the 8051’s two timers. They can serve as event counters or timers. The width of Timer 0 and Timer 1 are both 16 bits. Each 16 bit is accessed as two separate registers of low-byte and high-byte because the 8051 uses an 8-bit architecture.
Timer 0 Register
Timer 0’s 16-bit register is accessed using low- and high-byte operations. The names of the high-byte register are TH0 (Timer 0 high byte) and the low-byte register are TL0 (Timer 0 low byte). Like other registers, these registers can be accessed. For instance, the instruction MOV TL0, #4H inserts the value into Timer #0’s low-byte.
Timer 1 Register
Timer 1’s 16-bit register is accessed using low- and high-byte operations. The high-byte register is known as TH1, and the low-byte register is known as TL1 (Timer 1 low byte). Like other registers, these registers can be accessed. As an illustration, the instruction MOV TL1, #4H inserts the value into Timer 1’s low-byte.
TMOD (Timer Mode) Register
The same register is used to set the various timer operation modes for Timer 0 and Timer 1, respectively. It is an 8-bit register where Timer 0 occupies the lower 4 bits and Timers occupy the upper 4 bits. In each instance, the upper 2 bits are used to specify the location while the lower 2 bits are used to pre-set the timer mode.
Gate − When set, the timer only runs while INT(0,1) is high.
C/T − Counter/Timer select bit.
M1 − Mode bit 1.
M0 − Mode bit 0.
GATE
Each timer has a method for beginning and ending. Some timers control this through software, some through hardware, and some have both. Both software and hardware controls are available for 8051 timers. Software is used to start and stop timers using the commands SETB TR1 and CLR TR1 for timer 1 and SETB TR0 and CLR TR0 for timer 0. It is initiated by the SETB instruction, and it is terminated by the CLR instruction. As long as GATE = 0 is present in the TMOD register, these instructions will start and stop the timers. Making GATE = 1 in the TMOD register enables external sources to start and stop timing devices.
C/T (CLOCK / TIMER)
The TMOD register’s bit controls whether a timer is used as an event manager or a delay generator. It serves as a timer for generating timer delays if C/T = 0. The 8051’s crystal frequency serves as the clock source to produce the time delay. If C/T = 0, the crystal frequency that is connected to the 8051 controls how quickly the timer ticks at regular intervals. The frequency of the timer is always 1/12th that of the crystal that is connected to the 8051. We typically work with the XTAL frequency of 11.0592 MHz, even though different 8051-based systems have an XTAL frequency of 10 MHz to 40 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)
With a divide-by-32 prescaler, Timer 1 and Timer 0 in Mode 0 both function as 8-bit counters. The timer register is set up as a 13-bit register with all of TH1’s 8 bits and TL1’s lower 5 bits. The top 3 bits of TL1 are ambiguous and must be disregarded. The register is not cleared when the run flag (TR1) is set. When the count switches from being all 1s to being all 0s, the timer interrupt flag TF1 is set. Timer 0 and Timer 1 both operate in Mode 0 at the same time.
Mode 1 (16-Bit Timer Mode)
Timer mode “1” is a frequently used mode that uses a 16-bit timer. All 16 bits are utilized, as opposed to just 13 in 13-bit mode. Starting at 0, TLx is increased up to a maximum of 255. TLx resets to 0 once the value 255 is reached, and THx is then increased by 1 after that. The timer can hold up to 65536 different values because it is a full 16-bit timer, but it will overflow back to 0 after 65,536 machine cycles.
Mode 2 (8 Bit Auto Reload)
The TL1 and TL0 timer registers are both set up as 8-bit counters with automatic reload. Overflow from TL1 (TL0), which is preloaded by software, sets TF1 (TF0) and reloads TL1 (TL0) with its contents. The reload does not change TH1 (TH0).
With auto-reload mode, you can set the timer to always have a value between 200 and 255. If you choose to use mode 0 or 1, you must check the code to determine whether there is an overflow and, if so, reset the timer to 200. In this situation, valuable instructions are checked for value and/or reloaded. The microcontroller handles this in mode 2. When you set up a timer in mode 2, the microcontroller hardware takes care of checking to see if the timer has overflowed and resetting the value for you. As a result, you don’t have to worry about either of those things afterward. A standard baud rate is established using the auto-reload mode.
Mode 3 (Split Timer Mode)
Split-timer mode is also known as timer mode “3”. Timer 0 splits into two independent 8-bit timers when it is set to mode 3. Timer 0 has the value TL0, and Timer 1 has the value TH0. Both timers run from 0 to 255 and reset to 0 in the event of an overflow. Timer 1’s bits will all be connected to TH0 at this point. The real Timer 1 (TH1 and TL1) can be set to modes 0, 1, or 2 when Timer 0 is in split mode, but it cannot be started or stopped because the bits for doing so are now linked to TH0. Every machine cycle will result in an increase to the real timer 1.
Initializing a Timer
Choose a timer mode. Think about a 16-bit timer that functions without any external pins and runs continuously. Set up the TMOD SFR. Use Timer 0 and the lowest 4 bits of TMOD. We want the timer to be independent of the external pins, so leave the two bits, GATE 0 and C/T 0, at 0. Clear T0M1 and set T0M0 because timer mode 1 in 16-bit mode. Actually, bit 0 of TMOD is the only bit that needs to be turned on. Afterwards, carry out the command MOV TMOD,#01h.
Timer 0 is currently in 16-bit timer mode, but it is not currently running. Set the TR0 bit by running the instruction SETB TR0 to start the timer in running mode. Timer 0 will now start counting right away and will be increased once for each machine cycle.
Reading a Timer
There are two ways to read a 16-bit timer. Either you read the timer’s actual value as a 16-bit number or you can tell when it has overflowed.
Detecting Timer Overflow
The microcontroller automatically sets the TFx bit in the TCON register when a timer overflows from its highest value to 0. Therefore, the TFx bit can be checked rather than the exact value of the timer. Timer 0 has overflowed if TF0 is set, and Timer 1 has overflowed if TF1 is set.