Timers and Counters in 8051 Microcontroller – Microcontroller and Its Applications


Do subscribe to Ekeeda channel and press bell icon to get updates about latest Engineering HSC and IIT-JEE Mains and Advanced videos. The 8051 microcontroller it has two timers which can act as counters also and these timers and counters they are of 16 bits means a 16-bit number can be loaded into these timers and the counters now these timers or counters they can act as time delay generators also Anas counters also so let us study about these timers and counters in detail. So 8051 on microcontroller it is having two counters and timers means these two can act as counter also and as timer also so when it is acting as timer it is used to generate our time delay and then it is acting as a counter it is used to come to the events which are happening outside the microcontroller so this time has a counter they perform two operations either to generate the time delay or to count the number of events now because it is having two counters and timers so these two timers are timer 0 and type of one and they can be used either as the timer or the counter now these timers they are 16 bits void means timer zero is also having sixteen bits and timer bun is also have a sixteen bits okay so the because each timer is having sixteen bits so it is also divided into eight eight bits you so 16 bits they are also divided into 8 8 bits 8 bits hi bite and 8 bits for the no bite so hi bite it is denoted as th and no bite it is denoted as T and okay so 16 bits if you want to have a 16 bit number then we can use the old timer and if you want only eight bits to be used then we can use the higher-order Noah bite timers you the 8:05 phone counters means when these timers and counters they are acting as counters counting the number of events so they always come up means we are going to initialize them with the value of zero we are going to load the count value that from zero to its maximum value it is going to increment the counter always count up means that the counter is going to increment okay you so these counters because they are 16-bits counters so each counter n load a value of 16 bits okay and each counter is having a special register account register in the special function register area in h0 – we have some space reserved for the special function registers these registers are eight 8-bit registers and they have special functions associated with it so for each counter that is come to zero and dr. bun or we can say timer zero in timer bun we have a count register in this SFR area and in that contra disto the count value can be loaded okay so here the time is out of 16-bit so 16-bit value can be loaded so if we talk about that the counters always counter so we will have the initialized value as 0 0 0 it can count maximum up to F F F F means when all the 16 bits are 0 with to the condition when all the 16 bits are 1 so when we load this count value into this count register this will be incremented like it will become 0 0 0 1 then 0 0 0 2 up till the value of F F F F so that is the maximum count value that can be counted by each of the counters of 8:05 bun okay now when this counter is incremented when it is initialized with 0 0 and it is incrementing and it reaches to the maximum value and again it comes again it is incremented so again it is going to come back to 0 0 0 0 ok and that condition is called the overflow condition means the counter has reached to its maximum value again it is incremented and that condition is called the overflow condition so in that condition for each count we have an overflow flag bit and that bill will be set in that condition for timer1 also we have a overflow flag and for timer0 also we have an overflow flag bit so for corresponding timers those flag bits will be set whenever the count value is overflowed okay you so this condition whenever the counter is overflowing that is called count rolls over so whenever there count rolls over from the maximum count that is F F F F again two zero zero zero zero means this condition rolls over condition the corresponding timer flag in the econ register it is said econ is the timer control register it is an 8-bit register we will study about it in later that this D count register it is having the flag bits for both the timers so that flag is going to be set whenever this overflow condition is achieved so this was the little introduction about the two timers of the 8:05 one microcontroller as I said that these timers they are 16-bit timers so four timer zero we will have 16 bits which are divided into two eight eight bits that is T H 0 and P L 0 that is for timer 0 the higher bits and for timer 0 the lower bits now if this is the 16-bit register you so out of these 16 bits we have the 8 bits for the higher and 8 bits for the floor so this is th 0 and this is still 0 and complete is the primer 0 so we can load an 8 bit chunk of value also and we can load a total 16 bits also so separately they can also be accessed as 280 bit registers and collectively also we can access it as a 16-bit register similarly for timer 0 in story timer 1 it is also 16 bit and it can act as 2 8 8 bit register that is th 1 and PL 1 timer 1 higher bytes and timer 1 lower bytes so this is from D 15 to d8 and this is from D naught to be say ok now let’s come to the 2 registers which are associated with the functions of these timers and counters we have the two registers T mode and pecan these are 2 special function registers means registers which are having a special function associated with it so this two registers are for the timer’s operation this is for timer mode timer mode register and this is for timer control register okay and these two are the special function registers both these registers are eight eight bit registers 8 bits for T mode and eight meds for pecan so this register it is going to decide that in which mode the timers is going to operate and this timer control registers a turd indicates the status of the timers that whether the overflow condition is achieved or not or the control bit is set or not that will be given by this econ root system let us study the format of these resistors in detail you so this tango mode register it is an 8-bit register 8 bits are specified here out of these 8 bits the lower four bits are for the timer 0 and the upper 4 bits are for timer 1 now each of these four four belts they have specific function like this d7 and d3 bits it is for the gate this gate bit means starting and stopping of the timer’s if we want that 8:05 on it counts if you want to use its counters so first we have to start the counters ok so this gate bit it is used to start or stop the counters if you want to start then we will set this bit as one and if you want to stop the counter then we will set this bit as 0 so this bit is used for starting and stopping of the these counters now this is starting and stopping of the counters it can be done by hardware means also and by software means also hardware means we can give an interrupt to it either we can give an interrupt or we can reset the microcontroller so if we are resetting the timer’s of counters they will be stopped and software means is that we can write the instructions we can set this bit 1 or 0 so to start and stop we have software names also software methods are also there and hardware methods are also them next bit we have in these two sections is the counter and the timer bit this bit is used to decide that whether the two timers they will operate as a counter or as a timer so this bit is going to decide that the timer will generate or delay or it will count the number of events so if this bit if it is one then it will act as a counter and if it is zero then it will act as a time word means it will generate the delay next bit in this t-mod register we have is M one and M naught these two bits in both the timers it is used to decide the modes of operation of timers these two bits they can take different combinations like 0 0 0 1 1 0 and 1 1 so depending upon the combination we have the four modes mode 0 mode 1 mode 2 and mode 3 okay so let us study in detail what are the four modes of operation of these timers so when these two birds they are having the value zero zero then mode zero is there which is also known as the 13 bit timer mode when it is 0-1 then mode 1 is there which is called the 16-bit timer mode when it is 1 0 then we have moved to which is called the 8-bit Auto reload and then we have move 3 it is called the split timer mode okay let us study that how the timer’s function in these moves first we will study the mode 1 which is the 16-bit timer mode so this mod is known as a 16-bit time and mode so 16-bit count value will be loaded into the come to register now this 16-bit count value it can take the values from 0 0 0 to F F F F okay so this count value can be loaded okay and we have the registers there that is timer one in time R 0 is there so we have TL and th that is lower and higher bytes so these bytes will be loaded into these two eight 8-bit registers okay now if you want at the timer starts counting then we have to start the counters okay and these counters can be started by using the instructions set B PR 0 and set B TR 1 this is for timer 0 and this is to start the timer 1 so faster step in whenever we want to start the counting operation is to start the timer’s comp value is loaded now counters are started by using these two instructions after that the counter starts pumping up suppose it is loaded with the value 0 0 0 0 it is incremented 0 0 0 1 again it is incremented 0 0 0 2 till it reaches its final count value which is F F F F okay now when this maximum count value is reached again it is incremented and it is come back stewards again its initial value that is 0 0 0 0 and that condition is called a rollover condition which we studied earlier now when this rollover condition is achieved I said that the overflow flag bit is said so for timer0 we have the flag bit which is called pf0 overflow flag bit and we have T f-14 timer one so whichever timer is overflowed that flag bit is said it means it is made equal to one okay and the programmer it can see this flag bit if it found that this flag bit is set so the it is going to stop the timer it is going to clear this set condition because we have said this bits as one so it is going to reset it it is going to stop the timers so that stopping can be done by the two instructions CLR TR 0 and CLR TR one this is to set the timers start the timers and this is to stop the timers this is for timer 0 and this is for timer what so if we talk about this mode 1 the first step will be to load the 16-bit come value into the to a titbit registers after that we have to start the timer’s the timer will start counting up okay when it reaches final value again it is incremented and it rolls back to its initial value so when this condition is achieved when overflow has occurred the overflow flag bits for the two timers it will be set and if we want to reset the timers if we want to stop the timers or if you want to reset them then we have to clear the two bits that is for timer 0 and for the timer 1 okay now if you want to repeat this process that if you want that again the counter it starts counting from 0 0 0 0 it means avoiding this overflow condition again we have to reload the 16-bit count into the Torah and again we have to start the okay so this was the more bun which is the 16-bit timer mode now let’s come to the mode zero the difference between mode 1 and mode 0 is that mode one was 16-bit timer mode and more 0s 13 bit timer mode otherwise all the operations are same first the timers are started count is loaded in 16-bit count the maximum value was F F F F but here we are having 13 bit so its range will be reduced 0 0 0 0 to 1 f F F so this is the maximum count value allowed in mode 0 this is loaded into the th and PL registers counter is started when overflow is achieved flag overflow flag bit is set then timers are stopped by clearing the bits makes this mode 2 more to is known as the 8 bit auto reload mode 8-bit mode means here we cannot load the 16-bit count we are going to use the timers as separate th and TL toys ok a tidbit timers are there this 8-bit count is loaded into the higher bytes of the timer’s and a copy of this is placed in the TL register also so suppose we have loaded it with the because it is an 8 bit number so 0 0 2 FF can be loaded okay maximum count value allow this FF so suppose we have initialized here means we have loaded the camp values 0 0 in th so that will be copied into the TL register also now this TL is going to be incremented it will be incremented till it reaches the maximum count value FF then it is FF again it is incremented and the rollover condition is their means of gain it is zero zero so when this rollover condition is achieved again the flag bits they are set for timer one and timer 0 that is TF 0 and TF 1 in this case also we have to first start the timer’s by setting the beds ok and when this overflow condition is set these two bits are set now if we want that if we clear it means so this rollover condition is there if you want to start that this process is again repeated the programmer need not to reload the come here okay because this th bit it is again automatically copied into the PL thump is first loaded in th and it remains there it will not be modified so again and again the programmer need not to reload the count value okay it is going to just every time that timer it is going to copy the some value from into the TL TL will be incremented okay that is why this mode 2 is called the auto reload mode but in mode 0 and mode 1 every time the comp review is overflowed the programmer has to reload the count value ok next mode is the mode 3 in more 0 1 & 2 the operations of the timer 0 and timer 1 they are independent of each other if suppose timer 0 is operating in both 0 the timer 1 is operating and more – so their functioning is completely different they are independent of each other but in mode 3 the two timers they are not independent of each other they will collectively work suppose that we have initialized the timer 1 in mode 3 so due to this the bits TR 1 and TF 1 that is the timer control bit and the timer overflow bits of the timer 1 they will be used by timer 0 so it means that the functioning of the two timers it does means it’s a combination of it they are cooperating with each other means they are not independent ok we can say so timer 1 bits they are used by timer 0 if it is initialized in mode 3 so that is my dispersed split timer mode the functioning of the timer’s is mixed up so these are the four modes of operation of the timer’s next this was the T mode register which is used to decide the modes of operation of further timers next special function registers associated with the timer’s is the T on register it is the timer control now this register is also an 8-bit register so it will have the 8 bits from d7 to D naught and each of the bit will have some function associated with it so let’s see the format of this decon transistor also you so these are the eight bits of the t-con register d7 bit it is for the TF one that is timer one overflow flag okay this condition is a means this bit is set set means its value is made one whenever the count value means counter is incremented from zero zero zero zero two one bang bang bang so when this condition is achieve the maximum count value is achieved then this bit is set okay otherwise it is preset so this is for timer 1 and this TM 0 it is for timer 0 then we have TR 1 and TR 0 this is for timer 1 and this is for timer 0 so these bits our timer run control bit okay so these bits are for the starting up of the these timers if you want to start the timer 1 then we are going to set this but if you want to start timer 0 then we are going to set this back using the instruction what we study set B TR 0 and set B ER 1 okay then we have ie1 and ie2 for the to enable the external interrupts these are further interrupts ok ie 1 and ie 0 so it is to enable the external interrupts these are the h-he is for the edge flag okay so this is to enable the external interrupt edge flag one and this is to enable the external interrupt edge flag zero so this is related with the interrupt structure also we have this I t1 and I t0 these are for the external interrupt signal type control bit okay these are to decide that at which the edge means either from low to high transition of the clock pulse of from high to low transition the interrupt is going to work so these bits are related for the interrupt operation for timers only we have this TF 1 TR 1 TF 0 in TR 0 for timer 0 and the timer 1 so this was the econ resistor which is the special function register having 8 bits so in this video we studied about the timers or we can say counters of da 0 5 fun my toe controller we studied that it has two timers and counters which can act as a delay generator also and as the counter also okay and we studied the two special function registers associated with it t con ante mod so I hope that this topic is clear to you thank you

46 thoughts on “Timers and Counters in 8051 Microcontroller – Microcontroller and Its Applications

  1. Madam if u posted this viedo yesterday I may got 10 marks. And I unsubscribed all my friends from ur channel.

  2. Thanks a lot to u mam… once again tqsm for this wonderful explanation… now I understood in best way because of this ur video 😇…God bless you mamm😇

  3. the beeest video, OMG thank you so muuuuch! I understooooood everything what i havent been understanding in whole my classes in just half hour! GOD BLESS YOU!

  4. Hello Friends,

    Watch Complete Video Series of Subject Microcontroller and Its Applications only on Ekeeda Application.

    Use Coupon Code "NEWUSER" and access any one Subject free for 3 days!

    Download Ekeeda Application and take advantage of this offer.

    Android:- https://play.google.com/store/apps/details?id=student.ekeeda.com.ekeeda_student&hl=en

    iOS:- https://itunes.apple.com/tt/app/ekeeda/id1442131224

  5. Hi Mam i have a question
    Can we load 34.5C(hex) for timer register ?? And How ?
    — I want to Load 34.5C in TH1 how do I do that?.

  6. I did not get anything in the class..This video is so simple I understood it clearly …Thank you so much …

Leave a Reply

Your email address will not be published. Required fields are marked *