You are on page 1of 7

PRACTICAL 2 - A

CREATING TIME DELAY USING TIMERS IN 8051 MICROCONTROLLER


objective:
Configure timer control registers of 8051 and develop a program to genera
given time delay.

Software Requirement:
Editor like Kevil uvision ver 4 or less, Flash programmer.

Hardware Requirement:
Target boards with P89V51RD2/AT89S52 controller
Time delay generation was one of the important concepts dealing with the
8051 Microcontroller and also it holds significance in almost all Mc applications.
There are many ways to create a time delay using 8051 however no methods will
be precise to above method of creating delay using inbuilt timers in
Microcontrollers. So to do this 8051 has packed with timers which is capable o
generating required time delay and can serve as a counter. The above circuit
diagram was designed in a simple way to illustrate the generation of time delay
using timers in 8051 microcontroller. Here Port 2 was toggled from high to low
and from low to high for every five seconds delay. Before move into the
programming you need to understand the structure and functions of the time
registers in the 8051 Microcontroller.

8051 TIMERS REGISTERS:

8051 has two inbuilt 16-bit timer register known as Timer0 and Timer1. The
values of the timer register increases one by one with each machine cycle. This
timer registers can count values from 0000H to FFFFH which is a total of 65536
counts. The timer registers are accessed by means of two different (Upper and
lower registers) one for upper bytes and other one for the lower bytes. They are
indicated by THO and TLO for upper and lower byte of timer 0. TH1 and TL1 for
upper and lower byte of the timer 1 register.

TIMER TMOD REGIREGISTE:

TMOD is nothing but a 8 bit register used for configuring the timer for desired
Operation. This TMOD register splits into (TMOD,.0-TMOD.3) are for the timer o
and (TMOD.4 -TMOD.7) for timer. Gate Dit was used to, enable and disable the
timer 1 by means of a signal brought into the Interrupt pin. C/t was used assign
the timer register as timer or as a counter. When the C/T bit is low, timer is used
for time keeping whereas high signal in C/T will turn the timer into a counter.

TIMER TCON REGISTER :

TCON is also a 8 bit register used for the direct control of the timer operation.
This TCON register can be bit addressed for easy timer operation. TF is meant for
denoting the end of each count (0000H to FFFFH) in the timer operation and set
by the hardware on the timer overflow. This bit should be cleared by the software
for continuous counting of the timer. IE is interrupt edge flag, set by hardware
when external interrupt is detected and cleared after processing. IT is interrupt
type control bit, set/cleared by software to specify the falling edge/low level
triggered external interrupts.
FIVE SECONDS DELAY:

The value of the timer register increases by one after every machine cycle
One machine cycle is the 1/12th of the frequency of the crystal attached to the
controller. We are here using 6MHz crystal, then a single machine cycle will take
6/12 - 0.5 MHz
Each count takes 1/0.5 MHz resulting in 2us, so when timer1 counts fro
0000H to FFFFH (65535 counts) it takes
2us x 65535 0.131 seconds
So TF1 is set for every 0.131 seconds and we need to increase the count an
clear the TF1 flag for every increment we make For a delay of 5 Seconds
should give a count value of
5 sec/ 0.131 38
10sec/0.131 76 (For ten seconds delay)

Source Code

#include<stdio.h>
#include<reg51.h>
unsigned char count;
void main()
{
TMOD-0x10;. // Selecting timer mode MO (TIMO) refer above diagram "of TMOD reg
TH1-0x00; // Setting upper bits of timer register TRI to 0000
TLI-Ox00;. // Setting lower bits of timer register TR1 to 0000
TCON-Ox40; //Starting the timer1(TR1) refer the above diagram or
TCON reg
P2-0x00;
while(1)
{
if(TF1-=1). //Checking the timer overflow flag for completion of count
{
count++;
TF1-0; //Clearing the TF flag through software to continue
Counting
}
if(count==38) // Toggle the status of port 2 if the count reachces 38
{
P2-P2;
count-0;. //Clearing count value to restart counting
}
}
}

Conclusion:
produce a five seconds delay using the timer registers
References:
http://www.gadgetronicx.com/timers-in-8051-microcontroller-tutorial/
PRACTICAL 2-B
USE OF 1/0 PINS FOR DATA TRANSFER BETWEEN CONTROLLERS

Objective
To use general purpose port i.e. Input/ output port of two controllers for data
transfer between them.
Software Requirement:
Editor like Kevil uvision ver 4 or less, Flash programmer.
Hardware Requirement:
2 sets of target boards with P89V51 RD2/AT89S52 controller, Serial Cable
with DB9 Connector/ USB 89SXX Programmer with cable, some connectors.
Procedure:

1. Connect target boards as per given block diagram of figure 1.


2. Write programs, for both the controllers.
3. Build/Compile project.
4. Flash program both the controllers with respective source codes and observe
results.
:Program for microcontroller1, i.e. for Subroutine for delay
Sender; controller

ORG 0000OH
START MOV A, #OFEH
LOOP: RRA
MOV P3,A
MOV P2,A
LCALL DELAY
SJMP LOOP
Subroutine for delay
DELAY; MOV RO,#02
LOOP1; MOV R1,#255
LOOP2; MOV R2,#255
LOOP3; DUNZ R2, LOOP3
DJNZ R1, LOOP2
DUNZ RO, LOOP1
RET
END
Source code:

Program tor microcontroller2, i.e. for receiver controller. These controllers are
Connected through port3. No delay program is written here as delay will be take
care by controller1 ;routine.

ORG 000OH
START NOP
LOOP1:. MOV A,P3
MOV P2,A
SJMP LOOP1
END

Source code:
Result:
Output observed at LEDs, of both the target boards as per programs, i.e. LED
lighting in circular manner, is achieved at port2 of controller2 same as of
controller 1l at port2.
Conclusion:
General purpose Input/output pins can also be used to transfer data
between ports. However this process will be slow but benefit lies in its
easiness.
Remark:
Both controllers can send and receive data from other controller at different
as well as at same port but not at the same time. Different programs can be
tested for such functionalities.
Reference
1. Datasheet 89V5 1 RD2/AT89S52

You might also like