Search This Blog

Monday, November 14, 2011

TMOD register

As we know there are 2 timer registers in 8051. Timer 0 and timer 1. Both of these registers use the same register called TMOD to set various timer operation modes.

TMOD is an 8 bit register, in which lower 4 bits are for Timer 0 and upper 4 bits are for Timer 1.

See table below. MSB ---- LSB
GATE
C/T
M1
M0
GATE
C/T
M1
M0
TIMER 1
TIMER 0

GATE
Gating control when set. The timer/counter is enabled only while the INTx pin is high and TRx control pin is set. When cleared, the timer is enabled whenever TRx control pin is set.
C/T
Timer or counter; 0 or  clear for timer operation.(connected to input from internal system clock). 1 or set for counter operation(connected to input from Tx input pin).
M1
Mode bit 1
M0
Mode bit 0

What the heck is M1 and M0??

M0 and M1 select the timer mode in TMOD. 4 modes as mode 0, mode 1, mode 2, mode 3. See table.
M1
M0
MODE
OPERATING MODE
0
0
MODE 0
13 bit timer mode
8-bit timer/counter THx with TLx as 5 bit prescaler
0
1
MODE 1
16 bit timer mode.
16 bit timer/counter THx and TLx are cascaded with no prescaler.
1
0
MODE 2
8 bit auto reload.
8 bit auto reload timer/counter  THx holds a value that’s to be reloaded in TLx each time it overflows.
1
1
MODE 3
Split timer mode

C/T (clock/timer) in TMOD
  •  This bit in TMOD register is used to know whether it is used as a delay generator or as an event counter. 
  •  C/T = 0, then it is used as a timer for time delay generation. The clock source for the time delay is the crystal frequency of 8051. Timer gets pulses from the crystal.
  • C/T = 1, then it is used as a counter and gets its pulses from outside the 8051. That is from pins T0 and T1 belonging to port 3. In case of timer 0, when C/T = 1 pin 3.4 provides the clock pulse and counter counts up for each clock pulse coming from that pin. Similarly for timer 1, when C/T = 1 pin 3.5 provides the clock pulse and counter counts up for each clock pulse coming from that pin.

GATE – IN-- TMOD
  • One of the bit of TMOD is GATE bit. What does this GATE thingy do or whats its purpose on this heavenly earth and in our pretty little microcontroller??
  • Every timer has means of starting and stopping. This can be achieved either by software or by hardware or by both. 8051 gives us facility of both.
  • 8051 the start and stop of timer is controlled by way of software by TR(timer start) bits TR0 and TR1, using instructions :
  • SETB TR1
  • CLR TR1
  • For timer 1 and for timer 0:
  • SETB TR0
  • CLR TR0.
  • The software start and stop is achieved when GATE = 0 in TMOD register.
  • When GATE = 1 in TMOD register it means hardware control.  Its preferable to use GATE = 0 for most of the time.

Values of TMOD to operate as timers in following mode.
Mode 1 timer 1  0001 0000 = 10h
Mode 2 timer 0, mode 2 timer 1 0010 0010 = 22h
 
Note whatever the frequency of crystal is timer will always function at 1/12 th of the crystal frequency.

No comments:

Post a Comment