- 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.
Name | Description | Address |
---|---|---|
TCON | Timer Control | 88h |
TMOD | Timer Mode | 89h |
TL0 | Timer 0 Low Byte | 8Ah |
TL1 | Timer 1 Low Byte | 8Bh |
TH0 | Timer 0 High Byte | 8Ch |
TH1 | Timer 1 High Byte | 8Dh |
TMOD | ||
---|---|---|
Bit | Name | Function |
7 | GATE1 | When 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. |
6 | C/T1 | When 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. |
5 | T1M1 | Timer1 mode bit (see below) |
4 | T1M0 | Timer1 mode bit (see below) |
3 | GATE0 | When 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. |
2 | C/T0 | When 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. |
1 | T0M1 | Timer0 mode bit (see below) |
0 | T0M0 | Timer0 mode bit (see below) |
TxM1 | TxM0 | Mode | Mode Description |
---|---|---|---|
0 | 0 | 0 | 13-bit Timer |
0 | 1 | 1 | 16-Bit Timer |
1 | 0 | 2 | 8-bit auto-reload |
1 | 1 | 3 | Split 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
- 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
- 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
- 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 | ||
---|---|---|
Bit | Name | Function |
7 | TF1 | Timer 1 Overflow. This bit is set by the microcontroller when Timer 1 overflows. |
6 | TR1 | Timer 1 Run. When this bit is set Timer 1 is turned on. When this bit is clear Timer 1 is off. |
5 | TF0 | Timer 0 Overflow. This bit is set by the microcontroller when Timer 0 overflows. |
4 | TR0 | Timer 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
- 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 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.
- 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