Thursday 2 May 2013

TIMERS IN INTEL 8051 MICRO CONTROLLER




TIMERS IN INTEL 8051 MICRO CONTROLLER
Timers are used to generate time delays and to count events in real time applications executing on customized micro controller or micro processor based hardware. By Interpreting the count value of a timer properly, many timer applications can be realized in embedded and telecom domain. A basic timer consists of a register that can be read from or write to by the processor/controller and is driven by some frequency source.
Timer Operations of 8051
The 8051 has two timers/counters, the can be used as either as
  • Timer to generate a time delay or as
  • Event count to count events happening outside micro controller.
  • 8052 has a third timer: T2
  • Used as interval timer (fosc/12) or event counter (T0-P3.4, T1-P3.5 pins)
  • 4 operation modes for T0, T1
  • Up-counting timer/counter
  • Use TMOD (89H), and TCON (88H) to set/control timer modes and operation
  • TL0 (8AH), TH0 (8CH), TL1 (8BH), TH1 (8DH) contain timer current count
Both Timer 0 and Timer 1 are 16 bit wide as shown in the figure 1.
Since 8051 has an 8-bit architecture each 16 bits timer is accessed as two separate registers of lower byte and higher byte.
 The lower byte register is called TL0/TL1 and higher byte register is called TH0/TH1 and accessed like any other register.
Figure 1
Both timer 0 and 1 use the same register, called TMOD (timer mode), to set the various timer operation modes. TMOD is a 8-bit register as shown in the figure 2. The lower 4 bits are for Timer 0 and upper 4 bits are for Timer 1. In each case the lower 2 bits are used to set the timer mode and upper 2 bits to specify the operation as shown in the figure 2.

Figure 2




Programming Intel 8051 timers
  • For example the 8051 based instruction MOV TMOD, #20h indicates that mode 2 of timer 2 is selected.
  • Timers of 8051 do starting and stopping by either software or hardware control.
  • In using software to start and stop the timer when GATE = 0.
  •  The start and stop of timer are controlled by way of software by the TR (timer start) bits TR0 and TR1.
  • The SETB instruction starts it and it is stopped by CLR instruction.
  • These instructions start and stop the timers as long as GATE = 0 in the TMOD register.
  •  The hardware way of starting and stopping the timer by an external source is achieved by making GATE =1 in the TMOD register.
EXAMPLE: 1 Write a sub routine to create a time delay of 20 ms by assuming an oscillator running at 12 MHZ controls an Intel 8051 micro controller.
Solution:
STEP1: If C/T =0, then clock source to the timer 1 = osc freq/12 = 12 MHZ/12 = 1 MHZ
STEP2: For 20 ms delay the Timer register (TH1, TL1) value = 1/1us * 20 ms = 20,000
STEP3:  Timer register (TH1 &TL1) value =65,536-20,000=45,536(d) =B1E0 (H)
ALGORITHM:
STEP 4: Configure timer 1 to operate in mode 1 and choose the oscillator /12 as clock i/p.
C/T=0, GATE =0, TF1=0
STEP 5: Place value B1E0h into the timer 1 register and wait until the overflow flag is set to 1
Intel 8051 Subroutine:
DELAY:                  MOV TMOD, #10H                           //MODE 1, 16BIT TIMER
                                CLR TF1                                                 // CLEAR TIMER 1 OVERFLOW FLAG
CLR ET1                                                 //CLEAR TIMER 1 INTERRUPT FLAG
LCALL DELAY:     MOV TH1, #B1H                                // STORE UPPER BYTE OF COUNT
MOV TL1, #E0H                                 // STORE LOWER BYTE OF COUNT
SETB TR1                                              // ENABLE TIMER 1
WAIT:                    JNB TF1, WAIT                                   //WAIT UNTIL TF1 IS SET TO 1
RET



No comments:

Post a Comment