TUTORIAL QUESTIONS ON TIMERS ANDCOUNTERS 8051 microcontrollers
Example 1
create a square wave of 50% duty cycle (with equal portions high and low) on the P1.5 bit. Timer 0
is used to generate the time delay. Analyze the program. Also calculate the delay generated. Assume
XTAL=11.0592MHz.
Example 1
Find the delay generated by timer 0 in the following code, using hex as wellas decimal method. Do not
include the overhead due to instruction.
Program:
CLR P2.3 ;Clear P2.3
MOV TMOD,#01 ;Timer 0, 16-bitmode
HERE: MOV TL0,#3EH ;TL0=3Eh, the low byte
MOV TH0,#0B8H ;TH0=B8H, the high byte
SETB P2.3 ;SET high timer 0
SETB TR0 ;Start the timer 0
AGAIN: JNB TF0,AGAIN ;Monitor timer flag 0
CLR TR0 ;Stop the timer 0
CLR TF0 ;Clear TF0 for next round
CLR P2.3
Example 3: Assume that XTAL = 11.0592 MHz. What value do we need to load the
timer’s register if we want to have a time delay of 5 ms ? Show the program for timer 0
to create a pulse width of 5 ms on P2.3.
Example 4: Assume that XTAL = 11.0592 MHz, write a program to generate a square
wave of 2 kHz frequency on pin P1.5. Assume XTAL = 11.0592 MHz, find the frequency of the
square wave
generated on pin P1.0 in the following program
Program:
MOV TMOD, #20H ; T1/8-bit/auto reload
MOV TH1, #5 ; TH1 = 5
SETB TR1 ; start the timer 1
BACK: JNB TF1, BACK ; till timer rolls over
CPL P1.0 ; P1.0 to high, low
CLR TF1 ; clear Timer 1 flag
SJMP BACK ; mode 2 is auto-reload