Upcoming SlideShare
×

# Timers

1,761 views

Published on

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
1,761
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
30
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Timers

1. 1. TIMERS Prepared by: Islam Samir
2. 2. AGENDA      What is a timer? Why do we use it? Counters. Prescalars. Timers, how to make digital hardware that counts time? TMR0.
3. 3. 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. 
4. 4. 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.
5. 5. 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…..).
6. 6. COUNTERS Ex. For a 2-bit counter, it will count from 0 to 4 ( 2 n-1) and on the next clock edge it will overflow to 0 again. So, it counted 4 counts ( 2 2 ) to overflow. Rule#1: 2 ncounts to overflow.  An n-bit counter will count
7. 7. PRESCALARES  If you measured the frequency of the MSB in a n-bit counter operates with Fosc, you’ll find it (Fosc/2 n). Rule#2:  The MSB bit in an n-bit counter changes with a frequency equals the clock frequency divided by 2 n. - So, we can use synchronous counters to count, or to divide the input frequency (used as a clock to this counter).
8. 8. TIMERS Ex. - Suppose using a 4-bit counter, with a clock frequency =100 HZ 1  The time of each count= 100 sec. = one clock cycle.  The time for that counter to overflow= 1 1  24   16  0.16 sec. 100 100
9. 9. TIMERS If we changed the frequency to 500 HZ, the time to overflow will be: - 1 1 4 2   16  0.032 sec. 500 500 - If we used a 6-bit counter, the time to overflow will 1 1 be:  26   64  0.64 sec. 100 100
10. 10. 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
11. 11. TIMERS  1. - 2. - We determine the operating frequency by: 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. Dividing the frequency before operating the Timer. This is done by using a Prescaler. In this case, the operating frequency will be: Fosc/ 4 prescalar
12. 12. BASIC BLOCK DIAGRAM Fosc/4 8-bit Prescalar 3 (selection lines) Mux Timer
13. 13. HOW DO WE USE TIMERS? Suppose that we want an n-bit counter to overflows every seconds. and the operating frequency of the MCU is Fosc: 1. Determine the number of counts by the relation:  4 Time to overflow =No. of counts* Time of one count 4  prescalar Time  No.ofcounts ( ) Fosc change the number of counts, and the prescalar value until you get the required time.  Time  Fosc No.ofcounts  4  prescalar
14. 14. TIMERS - According to the required number of counts, and the timer’s number of bits, determine the initial value: 2 n - No. of counts Initial value=
15. 15. TIMERS - Or, we can use the compare mode of the CCP module.
16. 16. Determine time to overflow Set Fosc of the MCU Set prescalar value Get the number of counts F Counts< 2 n - 1 T Get initial value
17. 17. 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.
18. 18. EXAMPLE ON TMR0 Make TMR0 overflow and generate an interrupt every 4 sec. when using a 32.768 KHZ oscillator. - 1. 4 sec.  32 .768 KHZ No.ofcounts  4  prescalar Try first the no. of counts 2 n (256) , you’ll get: prescalar=128.
19. 19. 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.
20. 20. 7-SEGMENT DISPLAY
21. 21. APPLICATION: [0->99] COUNTER
22. 22. [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.
23. 23. 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.
24. 24. ASSIGNMENT  - Generate a 100KHZ square wave only using TMR0. Choose the suitable clock frequency and prescalar value.