You are on page 1of 21

MICROCONTROLLERS &

EMBEDDED SYSTEMS
Module-III

Contents
2

Timer Programming
Mode 0

Mode 1
Mode 2

Counter Programming
Mode 1

Mode 2

Ragam R_DEEE_RSET

Timer - Mode 1
3

1. Load the TMOD value register indicating which timer


(timer 0 or timer 1) is to be used and which timer mode (0
or 1) is selected
2. Load registers TL and TH with initial count value
3. Start the timer
4.Keep monitoring the timer flag (TF) with JNB TFx,target
instruction to see if it is raised
Get out of the loop when TF becomes high
5. Stop the timer
6. Clear the TF flag for the next round
7. Go back to Step 2 to load TH and TL again
Ragam R_DEEE_RSET

Mode 1 Timer Programming examples


4

1.WAP to create a square wave of 50% duty cycle on


the P1.5 bit. Use Timer 0 to generate the time delay.
MOV P1,#00H
MOV TMOD,#01H;Timer 0 in mode 1
HERE: MOV TL0,#0F2H
MOV TH0,#0FFH
CPL P1.5
ACALL DELAY
SJMP HERE
Ragam R_DEEE_RSET

DELAY:
SETB TR0
AGAIN: JNB TF0,AGAIN
CLR TR0
CLR TF0
RET

(a)In hex

(FFFF-YYXX+1)NNNNN (decimal)
Then NNNNN x 1.085s=time delay
(half period)
Where YYXX is the hex value which was
loaded into the timer registers initially.

T=30.38s
Ragam R_DEEE_RSET

2. Assume XTAL = 11.0592 MHz, WAP to generate a square


wave of 100 Hz frequency with 50% duty cycle on pin P2.3
6

MOV P2,#00H
MOV TMOD,#10H;Timer 1 Mode 1
AGAIN: MOV TL1,#00H
MOV TH1,#0EEH
CPL P2.3
SETB TR1
BACK: JNB TF1,BACK
CLR TR1
CLR TF1
SJMP AGAIN
Ragam R_DEEE_RSET

3. Assume XTAL = 11.0592 MHz. When the following


values are loaded to the timer registers, a square
wave is generated on P2.3.
TL134H
TH176H

Find the frequency of the square wave generated.

Ragam R_DEEE_RSET

4. Generate a square wave with on time of 3ms and off


time of 10ms on P1.0. Assume an XTAL of 22MHz.
8

MOV TMOD,#10H;Timer 1 Mode 1


MOV P1,#00H
AGAIN: MOV TL1,#8AH
MOV TH1,#0EAH
SETB P1.0
ACALL DELAY
MOV TL1,#75H
MOV TH1,#0B8H
CLR P1.0
ACALL DELAY
SJMP AGAIN

Ragam R_DEEE_RSET

DELAY: SETB TR1


BACK: JNB TF1,BACK
CLR TR1
CLR TF1
RET

Ragam R_DEEE_RSET

10

How to generate maximum delay in mode


1??????
MOV TLx,#00H
MOV THx,#00H
Pulse width=71.1ms (for XTAL=11.0592 MHz)
Pulse width=35.75ms (for XTAL=22 MHz)
Ragam R_DEEE_RSET

For creating large time delays


11

Assume XTAL=11.0592MHz
MOV P2,#00H
MOV TMOD,#10H ;Timer 1 mode 1
LOOP:MOV R3,#28 ;counter for multiple delay
CPL P2.4
AGAIN: MOV TL1,#00H
Time period=4sec
MOV TH1,#00H
SETB TR1
BACK: JNB TF1,BACK
CLR TR1
CLR TF1
DJNZ R3,AGAIN
SJMP LOOP
Ragam R_DEEE_RSET

Write an 8051 C program to generate a square wave with 50%


duty cycle on P1.5. Use Timer 0, 16-bit mode to generate the
delay.
12

#include <reg51.h>
sbit mybit=P1^5;
void T0Delay(void);
void main(void)
{P1=0x00;
while (1)
{
mybit=1;
T0Delay();
mybit=0;
T0Delay();
}
}

void T0Delay()
{
TMOD=0x01;
TL0=0x00;
TH0=0xEE;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}
Ragam R_DEEE_RSET

Mode 2 Timer Programming examples


13

Assume XTAL = 11.0592 MHz, find the frequency of the square


wave generated on pin P1.0 in the following program

MOV TMOD,#20H ;T1/8-bit/auto reload


MOV TH1,#5H
f=1835 Hz
SETB TR1
BACK: JNB TF1,BACK
CPL P1.0
CLR TF1
SJMP BACK
Ragam R_DEEE_RSET

Assume XTAL = 22 MHz, WAP to generate a square


wave of frequency 1kHz on pin P1.6
14

MOV TMOD,#02H
RPT:CPL P1.6
MOV R0,#05
AGAIN:MOV TH0,#49H
SETB TR0
BACK:JNB TF0,BACK
CLR TR0
CLR TF0
DJNZ R0,AGAIN
SJMP RPT
END

OR
MOV TH0,#-183
T=1/(1k)=1ms
1ms/2=0.5ms
0.5ms/0.546=915 cycles
So, 915/5=183
So repeat the delay loop
corresponding to 183 , 5
times.
Value to be loaded to
TH0=256-183=
73d or 49H

Ragam R_DEEE_RSET

Counters
15

Design a counter for counting no. of pulses of an


input signal for 1sec and display the count at Port
2(LSB) & Port 1(MSB). The pulses are to be fed to
pin P3.4.Use XTAL=22MHz.

Timing Timer 1 in mode 1C/T*=0


Counting Timer 0 in mode 1C/T*=1
TMOD=15H
Ragam R_DEEE_RSET

16

RPT:MOV TMOD,#15H
SETB P3.4
To make P3.4 an I/P Port bit
MOV TL0,#00
To start counting from 0 onwards
MOV TH0,#00
SETB TR0
Start Counter
MOV R0,#28H
AGAIN:MOV TL1,#00
28x35.75ms=1sec
MOV TH1,#00
Ragam R_DEEE_RSET

SETB TR1 Start Timer


BACK:JNB TF1,BACK
CLR TF1
CLR TR1
DJNZ R0,AGAIN
MOV A,TL0
MOV P2,A
MOV A,TH0
MOV P1,A
SJMP RPT

Move LSB to Port 2


Move MSB to Port 1

END
17

Ragam R_DEEE_RSET

Assume that a 1-Hz external clock is being fed into pin T1 (P3.5).Write a C
program for counter 1 in mode 2 (8-bit auto reload) to count up and display
the state of the TL1 count on P1. Start the count at 0H.
18

#include <reg51.h>
sbit T1=P3^5;
void main(void)
{
T1=1;
TMOD=0x60;
TH1=0;

while (1)
{
do
{
TR1=1;
P1=TL1;
}
while (TF1==0);
TR1=0;
TF1=0;
}
}
Ragam R_DEEE_RSET

Mode 0 Timer Programming


19

Find the time period of the square wave generated by


the program. Assume XTAL=22MHz.
MOV TMOD,#00H;Timer 1 in mode 0
AGAIN: MOV TL1,#00H
MOV TH1,#0FH
SETB TR1
BACK: JNB TF1,BACK
CLR TR1
CPL P1.5
CLR TF1
Ragam R_DEEE_RSET
SJMP AGAIN

20

0
F
0
0
0000 1111 0000 0000
0000111100000 01E0
1FFF-01E0+1=1E20
1E20(H)7712(d)
7712X0.546s=4.2ms
T=8.4ms
f=1/T=119 Hz
Ragam R_DEEE_RSET

References
21

Muhammad Ali Mazidi and Janice Gillispie Mazidi,


The 8051 Microcontroller and Embedded Systems,
Pearson Education Asia.
Kenneth J. Ayala, The 8051 Microcontroller
Architecture, Programming and Applications, Penram
International Publishing (India),3rd Ed.

Ragam R_DEEE_RSET

You might also like