Introduction to Micro Controllers Embedded System Design Timer
Introduction to Micro Controllers & Embedded System Design Timer Operation Department of Electrical & Computer Engineering Missouri University of Science & Technology hurson@mst. edu A. R. Hurson 1
Introduction to Micro Controllers & Embedded System Design • We begin with a general view of timers as they are common in microprocessor/microcontroller. Then we will focus on 8051 on-chip timers. A. R. Hurson 2
Introduction to Micro Controllers & Embedded System Design • A timer is a series of divide-by-two flip flops that receive an input signal as a clock source. The clock is applied to the first flip flop which divides the clock frequency by two. • The output of the first flip flop clocks the second one, which also divides the input by two, and so on. A. R. Hurson 3
Introduction to Micro Controllers & Embedded System Design + 5 V D D Q 0 Clock LSB Clock D Q 1 Clock D Q 2 Q Clock MSB Clock Q 0 Q 1 Q 2 Flag A. R. Hurson Flag is set on 7 -to-0 timer overflow 4
Introduction to Micro Controllers & Embedded System Design • It is clear that the 1 st stage (Q 0) toggles at ½ the clock frequency, the 2 nd stage at ¼ the clock frequency, and so on. • So a timer with n stages divides the input clock frequency by 2 n. • The output of the last stage clocks a timer overflow flip flop, or flag, which is tested by software or generates an interrupt. A. R. Hurson 5
Introduction to Micro Controllers & Embedded System Design • The binary value in the timer flip flops can be thought of as a “counter” of the number of clock pulses since the timer was started. • A 16 -bit timer would count from 0000 H to FFFFH. The overflow flag is set on the FFFFH-to-0000 H of the count. A. R. Hurson 6
Introduction to Micro Controllers & Embedded System Design • In 8051, there are 2 16 -bit timers with four modes of operations. • A third 16 -bit timer with three modes of operations is added in 8052. • Timers are used for: • • • Internal timing, Event counting, or Baud rate generation for the built-in serial port. A. R. Hurson 7
Introduction to Micro Controllers & Embedded System Design • In internal timing applications, a timer is programmed to overflow at a regular interval and set the timing overflow flag. • The flag is then used to synchronize the program to perform an action such as checking the state of inputs or setting data to outputs. • Other applications can use the regular clocking of timer to measure the elapse time between two conditions. A. R. Hurson 8
Introduction to Micro Controllers & Embedded System Design • Event counting is used to determine the number of occurrences of an event rather than to measure the elapse time between events. • An event is any external stimulus that provide a 1 -to-0 transition on a pin on the 8051 IC. • The timer can also provide the baud rate clock for 8051’s internal serial port. A. R. Hurson 9
Introduction to Micro Controllers & Embedded System Design • The 8051 timers are accessed using six special function registers: Timer SFR Purpose Address Bit addressable TCON Control 88 H Yes TMOD Mode 89 H No TL 0 Timer 0 low-byte 8 AH No TL 1 Timer 1 low-byte 8 BH No TH 0 Timer 0 high-byte 8 CH No TH 1 Timer 1 high-byte 8 DH No A. R. Hurson 10
Introduction to Micro Controllers & Embedded System Design Byte address 90 P 1 F 0 B E 0 ACC D 0 PSW B 8 IP B 0 P 3 A 8 IE A 0 P 2 DPH DDL SP 99 11 SBUF P 0 98 SCON 8 D TH 1 8 C TH 0 8 B TL 1 8 A TL 0 89 TMOD TCON PCON 88 87 83 82 81 80 A. R. Hurson FF
Introduction to Micro Controllers & Embedded System Design • Timer MODe register: • It contains two groups of four bits that set the operation mode for Timer 0 and Timer 1. Bit Name Timer 7 Gate 1 6 Semantic 1 Counter/timer select bit: 1 = event counter 0 = interval timer 5 M 1 1 Mode bit 1 4 M 0 1 Mode bit 0 3 Gate 0 Timer 0 gate bit 0 Timer 0 Counter/timer select bit M 1 0 Timer 0 mode bit 1 0 A. R. Hurson M 0 0 Timer 0 mode bit 0 2 1 12
Introduction to Micro Controllers & Embedded System Design • Timer MODe register: • Timer modes M 1 M 0 Mode 0 0 0 13 -bit timer mode (8048 model) 0 1 1 16 -bit timer mode 1 0 2 8 -bit auto-reload mode 1 1 3 Split timer mode: Timer 0: TL 0 is an 8 -bit timer controlled by timer 0 mode bits; TH 0, the same except controlled by timer 1 mode bits Timer 1: stopped A. R. Hurson Semantic 13
Introduction to Micro Controllers & Embedded System Design • Timer CONtrol register: • It contains the status and control bits for timer 0 and timer 1. The upper four bits in TCON (i. e. , TCON. 4 TCON. 7) are used to turn the timers on and off (TR 0, TR 1), or to signal a timer overflow (TF 0, TF 1). The lower 4 bits (i. e. , TCON. 0 -TCON. 3) have nothing to do with the timers. They are used to detect and initiate external interrupts. A. R. Hurson 14
Introduction to Micro Controllers & Embedded System Design • Timer CONtrol register: Bit Symbol Address Semantic TCON. 7 TF 1 8 FH Timer 1 overflow flag. Set by hardware upon overflow; cleared by software or hardware when processor vectors to interrupt service routine TCON. 6 TR 1 8 EH Timer 1 run-control bit. Set/cleared by software to turn timer on/off TCON. 5 TF 0 8 DH Timer 0 overflow flag TCON. 4 TF 1 8 CH Timer 0 run-control bit A. R. Hurson 15
Introduction to Micro Controllers & Embedded System Design • Timer CONtrol register: Bit Symbol Address TCON. 3 IE 1 8 BH TCON. 2 IT 1 8 AH External interrupt 1 type flag. Set/cleared by software TCON. 1 IE 0 89 H External interrupt 0 flag. TCON. 0 IT 0 88 H External interrupt 0 type flag A. R. Hurson Semantic 16
Introduction to Micro Controllers & Embedded System Design • Timer Modes and overflow flag • 13 -bit Timer mode (Mode 0): This is a 13 -bit timer mode that provides compatibility with 8051 predecessor (i. e. , 8048). The upper three bits of TLx are not used. TLx (5 bits) Timer clock A. R. Hurson THx (8 bits) TFx Overflow flag 17
Introduction to Micro Controllers & Embedded System Design • Timer Modes and overflow flag • 16 -bit Timer mode (Mode 1): This is a 16 -bit timer mode and similar to mode 0. As the clock pulses are received, the timer counts up: i. e. , 0000 H, 0001 H, 0002 H, …. An overflow occurs on the transition from FFFFH-0000 H counts that sets the timer overflow flag. • • The timer continues to count. The overflow flag is the TFx bit in TCON that is read or written by software. A. R. Hurson 18
Introduction to Micro Controllers & Embedded System Design • Timer Modes and overflow flag: Mode 1 • The MSB of the value in the timer register is THx bit 7 and LSB is TLx bit 0. The LSB toggles at the input clock frequency divided by 2 and the MSB toggles at the input clock frequency divided by 216 = 65, 536. • The timer registers (TLx/THx) may be read or written at any time by the software. TLx (8 bits) Timer clock A. R. Hurson THx (8 bits) TFx Overflow flag 19
Introduction to Micro Controllers & Embedded System Design • Timer Modes and overflow flag • 8 -bit Auto-reload mode (Mode 2): In this mode, the timer lowbyte (TLx) operates as an 8 -bit timer while the timer high-byte (THx) holds a reload value. • When the counts overflows from FFH, not only the timer flag is set, but also the value in THx is loaded into TLx. If THx contains 4 FH, the timer counts continuously from 4 FH to FFH. A. R. Hurson 20
Introduction to Micro Controllers & Embedded System Design • Timer Modes and overflow flag: Mode 2 TLx (8 bits) TFx Timer clock Reload Overflow flag THx (8 bits) A. R. Hurson 21
Introduction to Micro Controllers & Embedded System Design • Timer Modes and overflow flag • Split timer mode (Mode 3): This mode is different for each timer: • Timer 0 in mode 3 is split into two 8 -bit timers. TL 0 and TH 0 acting as separate timers with overflows setting TF 0 and TF 1, respectively. • Timer 1 stopped in mode 3, but can be started by switching it into one of the other modes. A. R. Hurson 22
Introduction to Micro Controllers & Embedded System Design • Timer Modes and overflow flag: Mode 3 • Mode 3 essentially provides an extra 8 -bit timer: The 8 o 51 appears to have a third timer. When timer 0 is in mode 3, Timer 1 can be turned on and off by switching it out of and into its own mode 3. It can be used by the serial port as a baud rate generator, or it can be used in any way not requiring interrupts (since it is no longer connected to TF 1). A. R. Hurson 23
Introduction to Micro Controllers & Embedded System Design • Timer Modes and overflow flag: Mode 3 TLx (8 bits) THx (8 bits) Timer clock TL 0 (8 bits) TF 0 Timer clock Overflow flag TH 0 (8 bits) Fosc/12 A. R. Hurson TF 1 Overflow flag 24
Introduction to Micro Controllers & Embedded System Design • A. R. Hurson 25
Introduction to Micro Controllers & Embedded System Design • A. R. Hurson 26
Introduction to Micro Controllers & Embedded System Design • A. R. Hurson 27
Introduction to Micro Controllers & Embedded System Design Bit number Function Pin Number Remarks P 3. 0 Rx. D 10 Serial communication P 3. 1 Tx. D 11 Serial communication P 3. 2 12 External Interrupt P 3. 3 13 External interrupt P 3. 4 T 0 14 Timer 0 P 3. 5 T 1 15 Timer 1 P 3. 6 16 External Memory P 3. 7 17 External Memory A. R. Hurson 28
Introduction to Micro Controllers & Embedded System Design • Clocking Sources Oscillator T 0 or. T 1 12 0 = interval Timing 1 = Event Counting A. R. Hurson 29
Introduction to Micro Controllers & Embedded System Design • Starting/Stopping and Controlling the Timers • The simplest way to staring and stopping the timers is with the run control bit (i. e. , TRx) in TCON register. • TRx is cleared after a system reset, therefore the timers are disabled (stopped) by default. TRx is set by the program to start the timers. A. R. Hurson 30
Introduction to Micro Controllers & Embedded System Design • Starting/Stopping and Controlling the Timers Timer Clock 0 = Timer stopped 1 = Timer started SETB TR 0 ; Starts Timer 0 CLR TR 0 ; Stops Timer 0 A. R. Hurson 31
Introduction to Micro Controllers & Embedded System Design • A. R. Hurson 32
Introduction to Micro Controllers & Embedded System Design • A. R. Hurson 33
Introduction to Micro Controllers & Embedded System Design • Starting/Stopping and Controlling the Timers • Use the following diagram, tabulate the bit and byte addresses for timer registers and control bits. 12 Oscillator T 1 (P 3. 5) TL 1 TH 1 TF 1 ) GATE TR 1 A. R. Hurson 34
Introduction to Micro Controllers & Embedded System Design A. R. Hurson 35
Introduction to Micro Controllers & Embedded System Design • Initializing and Accessing Timer Registers • Timers are usually initialized once at the beginning of a program to set the correct operating mode. During the course of the program, the timers are started, stopped, flag bits tested, cleared, and so on, as required by the application. A. R. Hurson 36
Introduction to Micro Controllers & Embedded System Design • Initializing and Accessing Timer Registers • TMOD is the first register to be initialized, since it sets the mode of operation. • • Of course Timer 1 is not activated until its run control bit (TR 1) is set. • Note: The counters count up and set the carry flag on an FFFFH-to 0000 H transition. If an initial count is necessary, the timer register TL 1/TH 1 must also be initialized. A. R. Hurson 37
Introduction to Micro Controllers & Embedded System Design MOV TL 1, #9 CH TH 1, #0 FFH SETB TR 1 WAIT: JNB ; These two instructions initializes ; the timer by _100 H ; Starts the timer and as the result ; the overflow flag (TF 1) is set 100 ; sec later. During this time the ; program remain in a “wait loop” TF 1, WAIT ; when the timer overflows, we need ; to stop the timer and clear the ; overflow flag CLR A. R. Hurson TR 1 TF 1 38
Introduction to Micro Controllers & Embedded System Design • Example: Write a program that creates a periodic waveform on P 1. 0 8100 8102 8104 D 290 C 290 80 FA ORG LOOP: SETB CLR SJMP END 8100 P 1. 0 LOOP ; one machine cycle ; two machine cycle Duration of this loop is 4 machine cycle. A 12 MHz crystal results in a 1 sec clock. So the duration of this loop is 4 sec. Adding NOPs in the loop allows us to extend this. A. R. Hurson 39
Introduction to Micro Controllers & Embedded System Design • Short intervals and Long intervals • A 12 MHz crystal results in an on-chip clock of 1 MHz. The short interval is limited by the software not the timer clock and this is the duration of instructions. The shortest instruction in 8051 requires one machine cycle or 1 micro second. A. R. Hurson 40
Introduction to Micro Controllers & Embedded System Design • Short intervals and Long intervals • The following table summarizes the techniques for creating intervals of various length. Maximum interval ( sec) Technique 10 software 256 8 -bit timer with auto-reload 65536 No limit A. R. Hurson 16 -bit timer plus software loops 41
Introduction to Micro Controllers & Embedded System Design • Example: Write a program using Timer 0 to create a 10 KHz square wave on P 1. 0 8100 8103 8106 8108 810 B 810 D 810 F 758902 758 CCE D 28 C 308 DFD LOOP: C 28 D B 290 80 F 7 A. R. Hurson ORG MOV SETB JNB CLR CPL SJMP END 8100 TMOD, #02 H TH 0, #-50 TR 0 TF 0, LOOP TF 0 P 1. 0 LOOP ; 8 -bit auto-reload mode ; reload value to TH 0 ; Start timer ; Wait for overflow 42
- Slides: 42