Timers n Timers can be used for timing

Timers n Timers can be used for ¨ timing ¨ event counting ¨ pulse width measurement ¨ pulse generation ¨ frequency multiplication n There are 8 Timers (T 0 – T 7) ¨ T 0 and T 1 – CAPCOM 1 [CAPture and COMpare] ¨ T 7 and T 8 – CAPCOM 2 ¨ T 2, T 3, T 4, T 5 and T 6 – General Purpose Timers (GPT) 1

General Purpose Timers (GPTs) n n n The five GPT’s are 16 -bit timers that are grouped into the two timer blocks GPT 1 and GPT 2. Block GPT 1 contains 3 timers/counters with a maximum resolution of 16 TCL(TCL = 50 ns for f. CPU = 20 MHz) Block GPT 2 contains 2 timers/counters with a maximum resolution of 8 TCL and a 16 -bit Capture/Reload register (CAPREL). 2

GPT 1 Block From a programmer’s point of view, the GPT 1 block is composed of a set of SFR’s as summarized below. Those portions of port and direction registers which are used for alternate functions by the GPT 1 block are shaded. 3

Timers T 2, T 3 and T 4 n Four basic modes ¨ ¨ n n n Timer Gated timer Counter Incremental interface mode – for incremental encoders All timers can count up or down When a timer register overflows from FFFFH to 0000 H (when counting up), or when it underflows from 0000 H to FFFFH (when counting down), its interrupt request flag (T 2 IR, T 3 IR or. T 4 IR) in register Tx. IC will be set. Each timer has an alternate input function pin (Tx. IN) associated with it which serves as the count input in counter mode, or as the gate control in gated timer mode. x – refers to the timer number 4

Timer 2, 3 and 4 Physical microcontroller pin Tx. UD – External Timer Up/Down control Tx. IN – External clock input T 3 OUT – External output pin from Timer 3 fcpu – CPU clock input to prescaler (which divides by 8 , 16, 32, … 1024) 5

T 2, T 3 and T 4 Timers (contd. ) n n T 3 is the called core timer The count direction (Up/Down) may be programmed via software or may be dynamically altered by a signal at an external control input pin. Each overflow/underflow of core timer T 3 is latched in the toggle Flip-Flop T 3 OTL and may be indicated on an alternate output function pin. T 2 and T 4 do not have this feature. Some timer modes require the use of more than one timer. For example : The auxiliary timers T 2 and T 4 may additionally be concatenated with the core timer, or used as capture or reload registers for the core timer. 6

Timer Control Register T 3 CON – Timer 3 Control Register (bit addressable) 7

T 3 in Timer mode n = 3. . 10 The 3 Tx. I bits in Tx. CON determine the precaler factor as shown in the table below. Note the x in Tx. UD, Tx. UDE, Tx. I etc. refers to the timer number 8

T 3 in Timer mode UP(0)/DOWN(1) 9

Timer 3 generating a fixed delay // Initialise timer 3 for Timer mode, prescaler 101 (÷ 256), timer stopped // count down, disable external up/down, T 3 OTL output disabled T 3 CON = 0 x 0085; //00000 101 // timer period = prescaler / fcpu = 256/20 MHz = 12. 8 u. S // delay required = 400000 u. S. Timer value = 400000/12. 8 = 31250 // Clear T 3 interrupt request bit, load timer value and run timer T 3 IR = 0; T 3 = 31249; // 1 less than number of counts required T 3 R = 1; /* T 3 is now decremented by hardware every 12. 8 u. S, after 31250 decrements the interrupt request flag is set. The program should poll/test this flag. */ while (T 3 IR == 0); // wait until interrupt request is set 10

Example - Timer delay #include <stdio. h> /* standard I/O. h-file */ #include <reg 167. h> /* special function registers for 167’s */ /*************************/ /* Use Timer 3 to generate a delay of 0. 4 seconds*/ /*************************/ void main (void) { /* initialize the serial interface */ #ifndef MCB 167 /* do not initialize with Monitor-166 */ P 3 |= 0 x 0400; /* SET PORT 3. 10 OUTPUT LATCH (TXD) */ DP 3 |= 0 x 0400; /* SET PORT 3. 10 DIRECTION CONTROL(TXD O/P)*/ DP 3 &= 0 x. F 7 FF; /* RESET PORT 3. 11 DIRECTION CONTROL(RXD i/p)*/ S 0 TIC = 0 x 80; /* SET TRANSMIT INTERRUPT FLAG */ S 0 RIC = 0 x 00; /* DELETE RECEIVE INTERRUPT FLAG */ S 0 BG = 0 x 40; /* SET BAUDRATE TO 9600 BAUD */ S 0 CON = 0 x 8011; /* SET SERIAL MODE */ #endif. . . see next slide, which goes here. . . } 11

//Initialise timer 3 for timer mode, prescaler=256, timer stopped //count down, disable external up/down T 3 CON = 0 x 0085; //000010 000 101 while (1) //do this endlessly! { printf("n. Press a key to start timern"); _getkey(); // special Keil function to wait for a key // Clear T 3 interrupt request bit, load timer value and run timer T 3 IR = 0; T 3 = 31249; T 3 R = 1; printf("Timing startedn"); // Now wait for T 3 interrupt request bit to be set to 1 // This should take 31250 x 12. 8 u. S = 400000 u. S = 400 ms = 0. 4 S // based on a 20 MHz cpu clock frequency while( T 3 IR == 0); T 3 R = 0; //Stop timer //When loop terminates we know the required delay has elapsed printf("400 milliseconds has elapsedn"); } 12

Gated Timer Mode This mode is used for measuring the period of pulses n = 3. . 10 Timer mode bits Tx. M gate open Tx. IN – provides the gating of the clock 13

Counter mode The 3 bits Tx. I determine the transition for counter increment/decrement 14

Timer T 2 and T 4 15

Concatenation of Timers Concatenation of Core Timer T 3 and an Auxiliary Timer 16
- Slides: 16