Programming 8051 Timers

Registers used for normal operation (No interrupts)

  • TMOD, TH1,TL1, TH0, TL0, and TCON
  • TMOD to set the Mode
  • Timer Register to set the count
  • TCON to start and stop the timer

Registers used for Interrupt operation

  • IE, TMOD, TH1,TL1, TH0, TL0, and TCON
  • TMOD to set the Mode
  • Timer-0 and Timer-1 Register to set the count
  • TCON to start and stop the timer

Register Formats:

Interrupt Enable (IE) Register

IE.7 IE.6 IE.5 IE.4 IE.3 IE.2 IE.1 IE.0
EA ET2 ES ET1 EX1 ET0 EX0
=0, disable

=1 enable

Enable/disable serial port interrupt Enable/disable timer-1 Ov interrupt Enable/disable external interrupt-1 Enable/disable timer-0 Ov interrupt Enable/disable external interrupt-0

 

TMOD Register:

Timer-1 Timer-0
GATE C/T’ M1 M0 GATE C/T’ M1 M0
=1; The timer/counter enabled while INTx and TRx bit is set

=0, timer enabled whenever TRx bit is set

1 = Counter

0=Timer

00=13 bit timer mode (8-bit timer/counter with TLx as 5-bit prescalar

01=16 bit timer mode, THx and TLx cascaded, no prescalar

10= 8-bit auto reload. THx holds the value that is to be reloaded

11= Split Timer Mode

=1; The timer/counter enabled while INTx and TRx bit is set

=0, timer enabled whenever TRx bit is set

1 = Counter

0=Timer

 

TCON register (Addr 88h) –

This register controls the timer/counter operations. Lower four bits cater

TCON.7 TCON.6 TCON.5 TCON.4 TCON.3 TCON.2 TCON.1 TCON.0
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Timer-1 overflow flag Timer-1 run control bit Timer-0 overflow flag Timer-0 run control bit Set by controller if there is an interrupt at INT1,

=0(cleared) when there is a jump to ISR of INT1

=0 (clear this bit) for interrupt generated by a low level signal at INT1,

=1 (set this bit) for interrupt generated by a falling edge signal at INT1

Set by controller if there is an interrupt at INT0,

=0(cleared) when there is a jump to ISR of INT0

=0 clear this bit for interrupt generated by a low level signal at INT0,

=1 (set this bit), for interrupt generated by a falling edge signal at INT0

Timer-0 and Timer-1 Register (TH1,TL1, TH0, TL0)

THn   (n=0 or 1) TLn   (n=0 or 1)
D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0

 

Timer frequency is XTAL/12:
To generate the proper timer/counter, it is required to calculate the timer operating frequency and the period of the pulse.

 

Examples-1:

Calculate the timer frequency and the period

a) XTAL=12MHz; Tf = 12/12MHz =1MHz; T= 1/1MHz = 1micro seconds
b) XTAL=11.0592MHz; Tf = 11.0592MHz/12 =921.6KHz; T= 1/921.6 KHz = 1.085 micro seconds

Count value for the timers

Example-2 find the count to be loaded in counter for generating 20ms

Time for one cycle – 1.085 microsecond
For 20 ms
Count = 20ms /1.084micro = 21,198
Value to be loaded in counter = 65536- 21,198 = 44338+1 = 44339

Example-3: Suppose XTAL is of frequency 22MHz connected to the system generate. WAP to generate a pulse of frequency 100KHz.

Sol:
XTAL = 22MHz
Timer frequency = 22M/12 =1.833 MHz
Timer pulse period = 1/1.833MHz = 0.546 microseconds
Frequency required for generation = 100KHz
Period T= 1/100K = 10micro sec
Half this frequency for ON / OFF = 10micro sec/2 = 5 microseconds
Count = 5 microseconds / 0.546micro sec = 9
Count to be loaded in Timer register = 65536 – 9 = 65527 = FFF7 TH = FF , TL = F7
STEPS in Mode-1 Programming
i. Configure TMOD register indicating which timer (0 or 1) to be used and in which mode (0 or ) to be selected
ii. Load TL and TH with initial count value
iii. Start the timer (bit TR0/TR1)
iv. Keep monitoring the flag(TF) with the “JNB TFx, Label” to see if set. Get out of the loop when TF becomes high
v. Stop the timer
vi. Clear TF for next count
vii. Go to step ii and repeat

Example-4: Ex. Find the delay generated by Timer-1 in the following program

MOV TMOD, #01
Here: MOV TL0, #0F2
MOV TH0, #0FF
CPL P1.2
ACALL Delay
SJMP Here
Delay:
SETB TR0
Again: JNB TF0, Again
CLR TR0
CLR TF0
RET
Analysis
TH0, TL0 = FFF2
Number of counts for rollover FFFFh – FFF2h = 0D =13 +1 (for roll over) =14
For 11.0592MHz, each clock has a period of 1.085micro seconds
So delay = 1.085 micro seconds x 14 = 15.19 micro seconds for half the period of the cycle
Total time delay for ON and OFF period = 2×15.19 = 30.38 microseconds

Example-5: Create 50% duty cycle on pin P1.5. Use timer0 for delay

Sol:
MOV TMOD, #01 ; TIMER 0 MODE 1, 16 BIT
HERE: MOV TH0, #0F2h
MOV TL0, #0FFh
CPL P1.5
ACALL DELAY
SJMP HERE
;
DELAY:
SETB TR0
AGAIN : JNB TF0, AGAIN
CLR TR0
CLR TF0
RET

Example-6: Find the count value to be loaded in timer registers to achieve a time delay of 5ms

HINT : 5ms/1.085micro seconds = 4608
Value to be uploaded = 65535 – 4608 = 60928 = EE00h

Mode-2 Programming:

• Mode-2 is useful in applications such setting baud rate for serial communication
• Mode-2 is 8-bit timer with auto reload. Therefore the timer can count from 00h to FFh.
• Initially we need to load the TH register with a count value, automatically TL get a copy of it.
• Also when the counter rolls over i.e when “TF==1”, TL will get the copy of TH register.

Programming Steps:

• Set the TMOD for mode-2 register
• Calculate the count value to be loaded in TH register.
• Load TH register
• Set TR bit to start the timer
• Keep checking TF bit, use SJMP again to this line as we don’t need to reload the TL register, it is in auto-reload mode

Example-1: What is the smallest and largest frequency that can be achieved in timer mode-2

Ans: Time operate as 8-bit in Mode-2.
i. Smallest frequency: The minimum/smallest frequency we have to count maximum count. That is when we load TH=00, we have to count from 00h to FFh (255 counts). So frequency will be minimum. How?
2xCount x 1.085 = 2x255x1.085 = 555.52 micro seconds
Frequency = 1/t = 1/555.52 micro = 1.8KHz.
ii. Maximum frequency : will be when we count a small value. That is when we load the counter with high value. If we load TH=FF, then it counts only one for rollover. So time taken = 2x1x1.085 = 2.17 microseconds. Frequency = 1/2.17micro = 0.46 MHz

Example-2: assume 11.0592MHz crystal connected to 8051 microcontroller. WAP to generate 1KZ square wave pulse at P1.2.

Solution:
• Timer pulse time = 1.085micro seconds
• Time pulse for 1KHz= 1 ms
• ON/OFF pulse time required= 0.5 ms
• Count value = 0.5ms/1.085 micro sec = 460
• But the timer in mode-2 is an 8-bit register and can be loaded with value less than 256. We can solve this by using a register to store the divisor. 460 / 2 = 230.
• Two options for loading TH:
• Convert 230 into Hex
• Simply load -230 into TH, then assembler will automatically calculate the Hex value internally, this is quite simple for programmer.
Program:
MOV TMOD, #02
repeat : CPL P1.2
MOV R0, #02H
again :MOV TH0, #-230 ; this is same as MOV TH0, #0E6h
SETB TR0
here: JNB TF0, here
CLR TR0
CLR TF0
DJNZ R0, again
SJMP repeat

The effective delay in the above program is:
2x230x1.085 = 499 or approx. 500 micro seconds or 0.5 milli seconds
T= 2×0.5 milli sec = 1 mill seconds or F = 1/1ms = 1KHz

Timer connected to external frequency signal

In this case the timer (T1/T0) is an event counter that counts the events/pulses at the input pin. This may represent no. of people entering the hall, or the products going out of an assembly line etc.

Ex-1: calculate the frequency of an unknown signal applied at pin P3.4 of 8051

ORG 0000h
Repeat: MOV TMOD, #15h
SETB P3.4 //make port P3.4 as input
MOV TL0, #00
MOV TH0, #00
SETB TR0
MOV R0, #28
Again: MOV TL0, #00
MOV TH0, #00
SETB TR1
Back : JNB TF1, back
CLR TF1
CLR TR1
DJNZ R0, again
MOV A, TL0
MOV P2, A
MOV A, TH0
MOV P1, A
SJMP repeat
END

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!