Time delay in 8051 using timers |
Time delay generation was one of the important concepts dealing with the 8051 Microcontroller and also it holds significance in almost all Mc applications.There are many ways to create a time delay using 8051 however no methods will be precise to above method of creating delay using inbuilt timers in Microcontrollers.So to do this 8051 has packed with timers which is capable of generating required time delay and can serve as a counter.The above circuit diagram was designed in a simple way to illustrate the generation of time delay using timers in 8051 microcontroller.Here Port 2 was toggled from high to low and from low to high for every five seconds delay.Before move into the programming you need to understand the structure and functions of the timerregisters in the 8051 Microcontroller.
8051 TIMERS REGISTERS:
8051 has two inbuilt 16-bit timer register known as Timer0 and Timer1.The values of the timer register increases one by one with each machine cycle.This timer registers can count values from 0000H to FFFFH which is a total of 65536 counts.The timer registers are accessed by means of two different (Upper and lower registers) one for upper bytes and other one for the lower bytes.They are indicated by TH0 and TL0 for upper and lower byte of timer 0.TH1 and TL1 for upper and lower byte of the timer 1 register.
TIMER TMOD REGISTER:
TMOD is nothing but a 8 bit register used for configuring the timer for desired operation.This TMOD register splits into (TMOD.0-TMOD.3) are for the timer 0 and (TMOD.4 -TMOD.7) for timer 1.Gate bit was used to enable and disable the timer 1 by means of a signal brought into the Interrupt pin.C/t was used assign the timer register as timer or as a counter.When the C/T bit is low, timer is used for time keeping whereas high signal in C/T will turn the timer into a counter.
TIMER TCON REGISTER:
TCON is also a 8 bit register used for the direct control of the timer operation.This TCON register can be bit addressed for easy timer operation.TF is meant for denoting the end of each count (0000H to FFFFH) in the timer operation and set by the hardware on the timer overflow.This bit should be cleared by the software for continuous counting of the timer. IE is interrupt edge flag , set by hardware when external interrupt is detected and cleared after processing.IT is interrupt type control bit, set/cleared by software to specify the falling edge/low level triggered external interrupts.
I hope above description would have given a clear understanding on the registers in a 8051 Microcontroller.We are aimed to produce a five seconds delay using the timer registers and this needs some simple calculations which are explained below.
FIVE SECONDS DELAY:
The value of the timer register increases by one after every machine cycle.One machine cycle is the 1/12th of the frequency of the crystal attached to the controller.We are here using 6MHz crystal, then a single machine cycle will take
6/12 = 0.5 MHz
Each count takes 1/0.5 MHz resulting in 2us , so when timer1 counts from 0000H to FFFFH (65535 counts) it takes
2us x 65535 = 0.131 seconds
So TF1 is set for every 0.131 seconds and we need to increase the count and clear the TF1 flag for every increment we make.
For a delay of 5 Seconds we should give a count value of
5 sec/ 0.131 = 38
10sec/0.131 = 76 (For ten seconds delay)
PROGRAM:
#include#include unsigned char count!void main() { TMOD=0x10; //Selecting timer mode M0 (T1M0) refer above diagram of TMOD reg TH1=0x00; //Setting upper bits of timer register TR1 to 0000 TL1=0x00; //Setting lower bits of timer register TR1 to 0000 TCON=0x40; //Starting the timer1 (TR1) refer the above diagram of TCON reg P2=0x00!while(1) { if(TF1==1) //Checking the timer overflow flag for completion of count { count++!TF1=0; //Clearing the TF flag through software to continue counting.} if(count==38) //Toggle the status of port 2 if the count reachces 38 { P2=~P2!count=0; //Clearing count value to restart counting } } }
Hope you all have understood the operation of timers in a 8051 Microcontroller and how to generate specific timing delays using it.I have also added the Proteus simulation file along with the hex code of the program, check it out for better understanding.Get it hereDownload
2us x 65535 = 0.131 seconds how 0.131 came sir .pls explain it sir
I want to write micro-controller program for Traffic controlling based on density.I am not from electronic background.Actually i am from IT background.
金宝搏188beat网登录appPlease guide me for this.
Awesome….easy to understand and clear..thanks for the article
You are welcome