Generation of time delay is most important concept in embedded systems. Most of the times, we need to generate precise time delay between two actions in controller applications. We can generate the time delay using the techniques like LOOP or by using in built delay functions. But these are not precise methods for generating time delay so that we will go for timers to produce accurate time delay. This concept is similar to Time Delay Relay concept.
Most of the controllers have inbuilt timers. These timers are not only used for generating time delay but also used for counting purpose. The value of the counter is incremented by 1 when action or event occurs. Timers in a microcontroller are controlled by the SFRs (Special Function Registers). Timers in different mode of operations are configured by special function registers.
Get an idea about How to Interface DC Motor with 8051 Microcontroller?
- AT89C51 Microcontroller
- PCB board
- Programming Cable
- Resistor – 330 ohm
The major component in this circuit is AT89C51 controller. LED is connected to the P2.2 through the 330 ohm resistor to indicate the time delay.
8051 Timers Register Description:
The upper nibble (TMOD.7 to TMOD.4) is used to configure timer1 and the lower nibble (TMOD.3 to TMOD.0) is used to configure timer0.
GATE: If this pin is high, then corresponding timer is enabled when there is an interrupt at corresponding INT pin of the microcontroller.
C/T: This pin is used to select timer or counter. If this pin is high, then used as a counter to count the external event. If this pin is low, then used as a timer to produce time delay.
M1 and M0: These bits are used to select the different timer modes.
13 Bit Timer: This mode uses 8 bits from high byte and remaining 5 bits from low byte. The value of the timer in this mode is from 0000H to 1FFFH
16 Bit Timer: This mode is most commonly used for producing the time delay. In this mode all the 16 bits are used for timer and values vary from 0000H to FFFFH.
If the value XXXXH is loaded into the timer register, then the produced time delay is equal to the [(FFFFH – XXXXH+1)*(period of one clock pulse)].
The time period of one clock pulse is equal to the 1.085us 11.0592 MHz frequency.
8-bit Auto Reload: In this mode, initial value is loaded in to the high byte and the same value is loaded into the low byte. Timer value is from 00H to FFH. This mode is used to set the baud rates for serial communication.
Split Mode: In this mode timer is divided in to two 8 bit timers. These 8 bit timers can count from 00H to FFH. This mode is used in the applications where we require an additional 8 bit timer or counter.
TCON Register: It is a special function register used to control the timer operation. In this register only upper nibble is used to control the timer and remaining bits are used for interrupt control.
- TF1: This bit is set to 1 automatically on the timer 1 overflow.
- TR1: This bit is used to enable the timer 1. This pin must be high to enable the timer1.
- TF0: set to one automatically when timer0 overflows.
- TR0: place 1 in this bit to enable the timer 0.
In order to produce time delay accurately,
- Divide the time delay with timer clock period.
- Subtract the resultant value from 65536.
- Convert the difference value to the hexa decimal form.
MMMMd = XXYYh
- Load this value to the timer register.
Delay Function to Generate 1 ms Delay:
Void delay ()
TMOD = 0x01; // Timer1 mode0
TH0= 0xFC; //initial value for 1ms
TL0 = 0x66;
TR0 = 1; // timer start
while (TF0 == 0); // check overflow condition
TR0 = 0; // Stop Timer
TF0 = 0; // Clear flag
Circuit Simulation Video:
How to Operate?
- Initially burn the program to the AT89C51 controller
- Give the connections as per the circuit diagram
- Switch on the supply, now you can observe the toggling of LED with some time delay.
- If you connect the output to an oscilloscope you can observe the square wave.
- Used in embedded system applications where we want precise time Delay.
- This system used to generate square wave.
- Used in Ultrasonic module applications.