timers

24
TIMERS Prepared by: Islam Samir

Upload: engislam90

Post on 26-May-2015

1.448 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Timers

TIMERS

Prepared by:

Islam Samir

Page 2: Timers

AGENDA What is a timer? Why do we use it? Counters. Prescalars. Timers, how to make digital hardware that counts

time? TMR0.

Page 3: Timers

WHAT IS A TIMER? In many applications, we need to do certain action

every time interval T. So, we need some hardware which tells the MCU that

this time interval has elapsed to do this action.

Examples

I. Real time systems.

II. Data acquisition systems.

III. Time division multiplexing communication systems.

IV. Digital clocks.

V. Operating systems.

Page 4: Timers

WHAT IS A TIMER? To implement such applications, we need some digital

hardware that counts time.

We do this simply by using a digital counter and controlling the time it takes to overflow.

Page 5: Timers

COUNTERS

A counter is essentially a register that, for every clock cycle, goes through a predetermined sequence of binary states.(usually, the numbers sequence 0,1,2,3…..).

Page 6: Timers

COUNTERSEx. For a 2-bit counter, it will count from 0 to 4 ( -1)

and on the next clock edge it will overflow to 0 again.

So, it counted 4 counts ( ) to overflow.

Rule#1: An n-bit counter will count counts to overflow.

n2

22

n2

Page 7: Timers

PRESCALARES If you measured the frequency of the MSB in a n-bit

counter operates with Fosc, you’ll find it (Fosc/ ).

Rule#2:

The MSB bit in an n-bit counter changes with a frequency equals the clock frequency divided by .

- So, we can use synchronous counters to count, or to divide the input frequency (used as a clock to this counter).

n2

n2

Page 8: Timers

TIMERSEx.- Suppose using a 4-bit counter, with a clock frequency

=100 HZ

The time of each count=

The time for that counter to overflow=

100

1

16.016100

12

100

1 4

sec. = one clock cycle.

sec.

Page 9: Timers

TIMERS

- If we changed the frequency to 500 HZ,

the time to overflow will be:

- If we used a 6-bit counter, the time to overflow will be:

032.016500

12

500

1 4

64.064100

12

100

1 6

sec.

sec.

Page 10: Timers

TIMERS So, by determining:

1. The operating frequency of the clock

2. The number of counts.

We can determine the time for this counter to overflow, and do some action every time this happens

(by generating an interrupt).

- This is the idea of timers, they are counters which we configure to count time, according to the relation:

Time to overflow =No. of counts* Time of one count

Page 11: Timers

TIMERS We determine the operating frequency by:

1. Choosing the clock that operates the counter.- This clock can be the bus clock that operates the

MCU, it can be obtained also from another timer’s overflow.

2. Dividing the frequency before operating the Timer. - This is done by using a Prescaler. In this case, the

operating

frequency will be:

prescalar

4Fosc/

Page 12: Timers

BASIC BLOCK DIAGRAM

Fosc/4

8-bit Prescalar TimerMux

3 (selection lines)

Page 13: Timers

HOW DO WE USE TIMERS? Suppose that we want an n-bit counter to overflows every

4 seconds.

and the operating frequency of the MCU is Fosc:

1. Determine the number of counts by the relation:

change the number of counts, and the prescalar value until you get the required time.

Time to overflow =No. of counts* Time of one count

)4(.

Fosc

prescalarofcountsNoTime

prescalar

FoscTimeofcountsNo

4

.

Page 14: Timers

TIMERS- According to the required number of counts, and the

timer’s number of bits, determine the initial value:

Initial value= - No. of countsn2

Page 15: Timers

TIMERS

- Or, we can use the compare mode of the CCP module.

Page 16: Timers

Determine time to overflow

Set Fosc of the MCU

Set prescalar value

Get the number of counts

Counts< - 1n2

Get initial value

F

T

Page 17: Timers

TMR0 8-bit timer/counter. Readable and writable. 8-bit software programmable prescaler. Clock source selectable to be external or internal

To use TMR0, we can use the ins. clock (Fosc/4), or an external clock.

Edge select for external clock. Interrupt on overflow from FFh to 00h.

Page 18: Timers

EXAMPLE ON TMR0- Make TMR0 overflow and generate an interrupt

every 4 sec. when using a 32.768 KHZ oscillator.

1.

Try first the no. of counts (256) , you’ll get: prescalar=128.

prescalar

KHZofcountsNo

4

768.32.sec4.

n2

Page 19: Timers

EXAMPLE Generate a 1HZ square wave only using TMR0.

- Note that this example can be used to check if the timer operates as required or not when using it in any application.

Page 20: Timers

7-SEGMENT DISPLAY

Page 21: Timers

APPLICATION: [0->99] COUNTER

Page 22: Timers

[0->99] COUNTER

The program is divided into 2 parts:

1- Updating the counter every 1 second.

2- Switching the operating 7-segment every 1/60

second.

Page 23: Timers

REAL TIME SYSTEMS Very important:- The time of execution of the ISR associated with the

timer’s interrupt, must be less than the time that the timer takes to overflow.

- If it takes more time, optimize the code or use higher oscillator frequency.

Page 24: Timers

ASSIGNMENT Generate a 100KHZ square wave only using TMR0.- Choose the suitable clock frequency and prescalar

value.