timers-of-8051
timers-of-8051
PROGRAMMING
• The 8051 has two 16-bit Programmable timers / counters (Timer 0 – Timer 1).
• Which can be used either as timer to generate a time delay or
as counter to count events happening outside the microcontroller.
• Timer Mode
• Increments every machine cycle (12 clock cycles-internal)
• Counter mode
• Increments when T0/T1 go from 1 - 0 (External signal)
• Timer can cause an interrupt
• Timer 1 can be used to provide programmable
baud rate for serial communications
• Timer/Counter operation
• Mode control register (TMOD)
• Control register (TCON)
Basic registers of the 8051 Timers
• Timer 0 and Timer 1 are 16 bits wide
• Each 16-bit timer is accessed as two separate registers of low byte and high byte.
Timer 0 registers
Low byte register is called TL0 and
the High byte register is referred to as TH0
Timer 1 registers
Low byte register is called TL1 and
the High byte register is referred to as TH1
GATE0 enables and disables Timer 0 using a signal brought to the INT0 pin (P3.2): ( INT1 P3.3 for Timer 1)
1 - Timer 0 operates only if the INT0 bit is set.
0 - Timer 0 operates regardless of the logic state of the INT0 bit.
C/T0 selects pulses to be counted up by the timer/counter 0:
1 - Timer counts pulses brought to the T0 pin (P3.4). (P3.5 For Timer 1)
0 - Timer counts pulses from internal oscillator.
• C/T’ = 0 ; Timer operation [input from internal system clock –
increments for every mc (12 clock cycles) ]
• C/T = 1 ; Counter operation – Input from Tx input Pin (External)
Programming the Timers
• Clock source for timer
• Timer needs a clock pulse to tick
• if C/T’ = 0, the crystal frequency attached to the 8051 is the source of the clock for the timer
• Frequency for the timer is always 1/12th the frequency of the crystal attached to the 8051
(XTAL = 11.0592 MHz allows the 8051 system to communicate with the PC with no errors)
• In our case, the timer frequency is 1MHz since our crystal frequency is 12MHz
Mode 1 programming
T = 1/50 Hz = 20 ms
1/2 of it for the high and low portions of the pulse = 10 ms
10 ms / 1.085 us = 9216
65536 - 9216 = 56320 in decimal = DC00H
TL = 00 and TH = DCH
The calculation for 12MHz crystal uses the same steps
Example
Examine the following program and find the time delay in seconds.
Exclude the time delay due to the instructions in the loop.
This is one of the rarities being kept only for the purpose of compatibility with the previuos versions
of microcontrollers. This mode configures timer 0 as a 13-bit timer which consists of all 8 bits of TH0
and the lower 5 bits of TL0. As a result, the Timer 0 uses only 13 of 16 bits. How does it operate?
Each coming pulse causes the lower register bits to change their states. After receiving 32 pulses,
this register is loaded and automatically cleared, while the higher byte (TH0) is incremented by 1.
This process is repeated until registers count up 8192 pulses. After that, both registers are cleared
and counting starts from 0.