In this tutorial, we will learn how to use timers on the 8051 microcontroller to generate a delay. The 8051 microcontroller has two 16-bit built-in timers. We can program each timer individually and configure them as either timers (to generate time delays) or counters (to count events occurring outside the microcontroller). The 8051 timer has three general functions, which are as follows:
Before learning about 8051 timer programming, you should know how to use KEIL for 8051 programming and how to use input and output ports of 8051 microcontroller
Types of 8051 timersThe 8051 microcontroller has two timers: Timer0 and Timer1. Both of these timers are 16-bit each, and we can also use them as counters. In the case of the 8051 microcontroller, its architecture is 8-bit, so the 16-bit timer is accessible as two separate 8-bit registers, low byte, and high byte.
8051 Timer0 RegistersIt is a 16-bit register that is accessed as a low byte and a high byte. The low byte is referred to as TL0, and the high byte is referred to as TH0. These registers are accessible like any other register.
8051 Timer1 RegistersTimer1 is also a 16-bit register, which is split into two bytes: a low byte referred to as TL1 and a high byte referred to as TH1.
Note: Both the timers (Timer0 and Timer1) share two common SFRs (special function registers): TMOD and TCON. We will discuss these two SFRs below and see how they control Timer 0 and Timer 1.
8051 Timer Mode (TMOD) RegisterThis timer can set various timer modes for timer 0 and timer 1. In this 8-bit TMOD register, the 8051 microcontroller assigns the lower 4 bits to Timer1 and the upper 4 bits to Timer0. For each timer, the lower 2 bits set the timer mode, while the upper 2 bits specify the operation.
Symbol and pins Functions GATE This bit controls whether to use an internal or external source for the timer or counter. GATE = 1 We can start and stop the timer or counter from an external source. GATE = 0 In this case, we don’t need any external hardware to start and stop the timer or counter. C/T This bit decides whether the timer is used as a time delay generator or as an event counter. C/T = 0 The timer (Timer0 or Timer1) is used as a timer. C/T = 1 The timer (Timer0 or Timer1) is used as a counter. Table 1: Symbol, pins and functions of TMOD register MODE Bits M1, M0:The last two bits for each timer, i.e., M1 and M0, select the timer mode. 8051 has four timer modes, which are as follows:
Mode 0:For time delay, the timer uses the clock source frequency of the crystal attached to 8051. The frequency of the timer will be 1/12th of the frequency of the external crystal oscillator.
Mode 2:The second special function register is the timer control register. It is an 8-bit register, and each bit has a special function. The bits, symbols, and functions of every bit of the TCON register are as follows:
Symbol and Bits Functions TF1 Overflow flag for Timer1. TF1 = 1 Set when timer rolls from all 1s to 0. TF1 = 0 Cleared to execute the interrupt service routine. TR1 Run the control bit for Timer1. TR1 = 1 Turns on Timer1. TR1 = 0 Turns off Timer1. TF0 Overflow flag for timer0, same as TF1. TR0 Run the control bit for Timer0, same as TR1. IE1 External interrupt 1 Edge flag It is not related to timer operations. IT1 External interrupt 1 signal type control bit. IT1 = 1 Enables external interrupt 1 to be triggered by a falling edge signal. IT1 = 0 Enables a low-level signal on external interrupt 1 to generate an interrupt. IE0 External interrupts 0 Edge flag. It is also not related to timer operations. IT0 External interrupt 0 signal type control bit, same as IT1. Table 2: Symbol, bits and function TCON register Delay Calculation of 8051 Timer Timer Clock SourceThe timer needs a clock source. If C/T = 0, the crystal frequency attached to the 8051 is the source of the clock for the timer. The value of the crystal frequency attached to the microcontroller determines the speed at which the timer ticks. Now suppose that the crystal frequency is 11.059 MHz.
Timer Clock FrequencyThe frequency for the timer is always 1/12th of the frequency of the crystal attached to the 8051.
The time delay of one machine cycle is given below. We use this to generate the delay.
Here we have explained in 3 steps how we can add a 10 ms Delay using timers:
In this Proteus schematic, we have simulated this circuit by adding a delay of 10 ms between the on and off states of the LED. The code is provided in the section below.
We have set the crystal oscillator’s frequency at 11.059 MHz.
8051 Microcontroller Timer Delay CodeThis sketch adds a delay between toggling LED states between on and off. This sketch initializes Port 2’s pin 0 as an LED pin. This sketch also uses mode 1 of timer 0 to add a 10 ms delay between the toggling of LED states.
#include<reg51.h>
sbit led = P2 ^ 0; // led at PORT 2 pin 0
void Delay(void); // Delay function declaration
void main() // main function
{
led = 0; //output PORT
while (1) // infinite loop
{
led = 1; // LED ON
Delay();
led = 0; // LED OFF
Delay();
}
}
void Delay()
{
TMOD = 0x01; // Timer0 mode1
TH0 = 0xDC; //initial value for 10ms
TL0 = 0x00;
TR0 = 1; // timer0 start
while (TF0 == 0); // check overflow condition
TR0 = 0; // Stop Timer
TF0 = 0; // Clear flag
}
Code Explanation
In this section, we will explain each part of the sketch provided above.
#include<reg51.h>
sbit led = P2 ^ 0; // led at PORT 2 pin 0
void Delay(void); // Delay function declaration
In the first part, we include the header file <reg51.h>. This will help us manage the registers of the 8051 microcontroller. In the next line, we have set the LED pin on PORT 2 of the 8051 microcontroller. On the last line, we have declared a function for the delay.
void main() // main function
{
led = 0; //output PORT
while (1) // infinite loop
{
led = 1; // LED ON
Delay();
led = 0; // LED OFF
Delay();
}
}
In the void main function, we have first set the LED state to off. Then, in the while loop, we will toggle between the on and off states of the LED. Between changing LED states, we are also calling the Delay function. Now let’s look at the Delay function.
void Delay()
{
TMOD = 0x01; // Timer0 mode1
TH0 = 0xDC; // High byte initial value for 10ms
TL0 = 0x00; // Low byte initial value for 10ms
TR0 = 1; // timer0 start
while (TF0 == 0); // check overflow condition
TR0 = 0; // Stop Timer
TF0 = 0; // Clear flag
}
The void delay function is quite simple. First, we set the timer mode, which we have set to 1 for timer 0 in our case. Then we set the initial values for the low byte and the high byte to achieve a 10 ms delay. After this, we start this timer using TR0 = 1. Now in the while loop, we check for the overflow condition; once the timer reaches overflow, it goes back to the void main function.
Video Demonstration Using a timer of 8051 microcontroller ConclusionIn this tutorial, we have covered the following topics:
Related Articles:
If you liked reading this article, please check these similar links:
This concludes our tutorial, if you face any issues while following this tutorial, let us know in the comment section below.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4