Thursday, March 17, 2016

8051 Timers

  • 8051 has two 16- bit timers
  • Timers can be set, read, controlled and configured individually
  • Timers operate in Interval mode or Counter mode
  • Interval mode - used to measure the time interval between events
  • Counter mode - used as counter to count events
  • It can be used to trigger event periodically and to generate baud rate for serial communication
  • 8051 timer always counts up
  • In interval mode, if configured correctly, timer will increment by 1 for every machine cycle
  • A running timer will be incremented (11.059 MZ crystal/12-1 machine cycle )921,583 times per second and 46,079 times per half –a - second
  • 16-bit timer can count from 0 to 65,535 which is equal to (65535/921583)0.711 second
  • Timer value of 20,000 is (20000/921583)0.021 second and 46,079 is 0.05 second
  • Timer value along with overflow event is used to measure time interval, generating delays and executing periodic events.
Registers:
NameDescriptionAddress
TCONTimer Control88h
TMODTimer Mode89h
TL0Timer 0 Low Byte8Ah
TL1Timer 1 Low Byte8Bh
TH0Timer 0 High Byte8Ch
TH1Timer 1 High Byte8Dh

TMOD
BitNameFunction
7GATE1When this bit is set the timer1 will only run when INT1 (P3.3) is high. When this bit is clear the timer1 will run regardless of the state of INT1.
6C/T1When this bit is set the timer1 will count events on T1 (P3.5). When this bit is clear the timer1 will be incremented every machine cycle.
5T1M1Timer1 mode bit (see below)
4T1M0Timer1 mode bit (see below)
3GATE0When this bit is set the timer0 will only run when INT1 (P3.3) is high. When this bit is clear the timer0 will run regardless of the state of INT1.
2C/T0When this bit is set the timer0 will count events on T1 (P3.5). When this bit is clear the timer0 will be incremented every machine cycle.
1T0M1Timer0 mode bit (see below)
0T0M0Timer0 mode bit (see below)

TxM1TxM0ModeMode Description
00013-bit Timer
01116-Bit Timer
1028-bit auto-reload
113Split timer mode

13-bit Timer Mode 0:
  • bits 0-4 of TLx and bits 0-7 of THx are used
  • TLx will count from 0 to 31
  • When TLx is incremented from 31, it will "reset" to 0 and increment THx
  • Timer can count up to 8192 and then it will overflow back to zero
16-bit Timer Mode 1:
  • bits 0-7 of TLx and bits 0-7 of THx are used
  • TLx will count from 0 to 255
  • When TLx is incremented from 255, it will "reset" to 0 and increment THx
  • Timer can count up to 65535 and then it will overflow back to zero
8-bit Timer Mode 2:
  • Auto reload mode
  • bits 0-7 of TLx is used
  • TLx will count from 0 to 255
  • THx holds reload value
  • When TLx is incremented from 255, it will "reset" to the value stored in THx
  • Timer can count up to 255 and then it will overflow back to value stored in THx
Split Timer Mode 3:
  • Timer 0 can operate in mode 3
  • Timer 0 becomes two separate 8-bit timers
  • Timer 0 is TL0 and Timer 1 is TH0
  • Timers count from 0 to 255 and overflow back to 0
  • TCON bits that are related to real Timer 1 will now be tied to TH0
  • Real Timer 1 (i.e. TH1 and TL1) can be put into modes 0, 1 or 2. But real Timer 1 cannot be controlled. The real timer 1 will be incremented every machine cycle.
  • Real Timer 1 can be used as baud rate generator

TCON
BitNameFunction
7TF1Timer 1 Overflow. This bit is set by the microcontroller when Timer 1 overflows.
6TR1Timer 1 Run. When this bit is set Timer 1 is turned on. When this bit is clear Timer 1 is off.
5TF0Timer 0 Overflow. This bit is set by the microcontroller when Timer 0 overflows.
4TR0Timer 0 Run. When this bit is set Timer 0 is turned on. When this bit is clear Timer 0 is off.

Timer Initialization:
  • Set timer mode in TMOD register
  • Start timer using TCON register
  • Timer can be start/stop externally using P3.3 pin
Reading Timer value:
  • Stop timer using TCON register
  • Read TLx and THx register to get timer value
  • Start timer using TCON register
  • Reading timer value can be done without stopping timer. It has to be done carefully, since THx may get incremented while reading TLx. This will happen when TLx has its highest value (31 or 255). To avoid reading wrong value, read THx, read TLx and read THx again to make sure the value is same. If not, repeat the sequence until the THx values are same.
Timer Overflow:
  • Timer overflow can be detected using TFx bits in TCON register.
  • It is necessary to consider TFx bits while executing fixed delays, measure time intervals or triggering periodic events.
Counter:
  • Timer can be used as counter. To count external events or pulses.
  • Set C/Tx bit in TMOD register to count events/pulses on P3.5 pin.



Related topics:
8051 Register Bank and Stack   |   8051 Flags Bits   |   8051 Program Flow   |   8051 Serial Port   |   8051 Interrupts

List of topics: 8051

No comments:

Post a Comment