Timers

18
TIMERS TIMERS A HARDWARE EXPLANATION A HARDWARE EXPLANATION

description

 

Transcript of Timers

Page 1: Timers

TIMERS TIMERS A HARDWARE EXPLANATIONA HARDWARE EXPLANATION

Page 2: Timers

WHAT IS TIMER?WHAT IS TIMER?In general, timer is nothing but a

process of time being or time based operation.

When coming to microcontroller, timer is a set of registers together doing the same time being operations.

The difference is, in microcontroller the time being operation was depends on crystal oscillator we used in microcontroller.

Page 3: Timers

Timers in 89C51Timers in 89C51

AT89c51 equipped with 2 timers, both of which may be controlled, set, read and configure individually.

They are called as timer 0 and timer 1.

Page 4: Timers

Functions of timersFunctions of timersThere are three general functions

the timers in 89C51 have◦Keeping time and calculating the

amount of the time between the events

◦Counting the event themselves◦Generating baud rate for serial

communication.

Page 5: Timers

Measuring TimeMeasuring TimeThis is one of the primary work of the timers

Another one is counting the eventsThe meaning of the measuring the time is “measuring the interval time between two events”.

Therefore it can also called as “interval timers”.

Page 6: Timers

Time required to do the Time required to do the process?process?Since we already explained that timer

was running under the frequency of microcontroller’s crystal oscillator and the atmel has 12 Tcycles for each machine cycle.

When timer is in interval timer mode and correctly configured,it will increment by 1 for every machine cycle.

Thus running timer will be incremented

11,059,200 / 12 = 921,583 times.

Page 7: Timers

Timer’s SFRTimer’s SFRAs we mentioned earlier, in 8051 we

have two timersOne is timer0 and another is timer1.These two timers shares 2 SFRs

◦TCON timer control◦TMOD timer mode select

Also these two timers have their own dedicated registers for sole purposeoTL0 and TH0 for timer0oTL1 and TH1 for timer1

Page 8: Timers

TMOD SFR operationTMOD SFR operationFirst we will discuss about TMOD..Each and every bit of the this mode

will give an information to the microcontroller, how to run the timers.

The MSB 4 bits(4 through 7) are allocated for timer 1

The LSB 4 bits(0 through 3) are allocated for timer 0

Each 4 bits holds the information about the operation of timer

Page 9: Timers

Mode 0 – 13 bit timerMode 0 – 13 bit timerThis is just a relic for microcontroller,

which never used in any operation.But for our knowledge, It is a13 bit

mode, that means the TLx will count 0 to 31 and reset to 0 and increment 1 to THx, THx will increment like this upto 255.

Totally 13 bits are used and its limitation of data is 8192 values only.

If you set a 13-bit mode, it will overflow after 8192 machine cycle.

Page 10: Timers

Mode 1 – 16 bit timerMode 1 – 16 bit timerIt is similar to mode 0 except it uses

all 16 bits.Therefore TLx increment from 0 to

255,when it reaches 255 it resets TLx and increment THx by 1.

Since 16 bits are used in this mode, 65,536 distinct values can be accessed using this mode.

When 65,536 machine cycles are over, the timer will be overflow in the microcontroller and reset to 0.

Page 11: Timers

Mode 2 – auto reload Mode 2 – auto reload timertimerIt is an 8 bit auto reload mode.When the timer is in 8 bit auto reload

mode, THx holds the reload value and TLx is the timer itself here.

TLx starts counting and reaches 255.When it reaches 255, instead of

resetting to 0, it loads the value stored in THx.

That’s why it is called as 8 – bit Auto reload mode.

Page 12: Timers

Mode 3 split timerMode 3 split timerWhen this mode is activated, the

timer will split itself into two timers.For example, if this mode is used in

timer0 then timer0 splits into two 8 bit timer, i.e., TL0 is timer0 and TH0 is timer1.

All bits tied up to timer1 will tied up with TH0 now.

TL0 and TH0 increment to 255 and resets to 0 individually.

Page 13: Timers

TCON SFRTCON SFRThis SFR is located in 0x88.In the 8 bits of this SFR only 4 bits

of MSB required to be set to run the timer

Those are TF1 TR1 TF0 TR0

This TCON is bit addressable, therefore if you want to set any particular bit, then it is easy to do it without disturbing other bits.

Page 14: Timers

Initializing the timer Initializing the timer The first thing to be decided while

initializing the timer is selecting the mode of operation.

For 16-bit you have to select mode 1

For 8-bit auto reload mode you have to select mode 2

This is depends on your application or use of timer in you system.

Page 15: Timers

Reading the values of Reading the values of timertimerThere are three methods using this timers◦Read the TL and TH values as 16- bit value

◦Read the TL and TH as separate 8 bit values

◦Calculate weather the timer is overflowed or not.

Page 16: Timers

Detection of timer Detection of timer overflowoverflowIt is very easy to check the timer

overflow rather than the exact value of timer.

Whenever the timer reset to 0 from a highest value the TFx in the TCON SFR will be set.

Therefore if timer 0 get overflowed then TF0 will be set

If timer 1 get overflowed then TF1 will be set

Page 17: Timers

Second purpose of timerSecond purpose of timerThe second purpose of the timer is

event countingThere are two methods in event

counting.◦Counting the length of the event(eg., how

many hours the tube light glown today)◦Counting the number of occurrence of

that event(eg., how many cars has passed this way)

Depends upon the purpose the method of usage varies.

Page 18: Timers

AssignmentsAssignmentsUse timer in a delay processUse timer for counting purposeGenerate a delay of 1 secondGenerate a square wave of 1 Khz.Generate a square wave using

timer 0 generate a saw tooth wave using timer 1 at the same time