SlideShare a Scribd company logo
MODULE 4
TIMERS AND SERIAL PORT
COMMUNICATION
Prof BINDU H M
DSCE, Bengaluru
INTRODUCTION TO TIMERS
• 8051 microcontroller have two timers timer 0 and timer 1.These are
16 bit timers, but 8051 is a 8 bit controller so they are referred are
lower byte and higher byte shown below TL0, TH0, TL1,TH1.
• They can be used as either timers to generate time delay or as
counters to count events.
• The required delay is obtained by loading the number machine cycles
into those registers. Normally delay count value will be decremented
in software delay program, but count value will be incremented in
timers. i.e.( means delay count need to be subtracted from max count
value and then timer has to be activated.)
BIT FORMAT OF TIMER 0/1
TMOD REGISTER
• Both the timers,timer0 and timer 1 use TMOD register to set various
timer operation modes.
• TMOD is an 8 bit register with lower 4 bits set aside for timer 0 and
higher 4 bits for timer 1.
• Lower two bits are used are to set the timer mode and higher 2 bits
specify the operation.
TMOD REGISTER
Gate Bit:
• Timer control will be decided through this bit value
• Gate-=0 : indicates that timer can be controlled through program by
TR and TF bit’s in TCON register.
• Bit TR is used to start the timer by making TR=1 and stop the timer by
making TR=0.This is called software control.
• Gate=1: indicates that timer control is done by external source. This is
called hardware control.
C/T (Counter/ Timer) Bit:
This bit in the TMOD register is used to decide whether the timer is
used As a delay generator or an event counter.
• If C/T =0, Then timer is working as delay generator
• If C/T=1, Then that timer is working as event counter
TMOD REGISTER
M1, M0:
• These two bits are used to select different timer modes. The below
table 1 shows the different modes.
TMOD REGISTER
Indicate which mode and which timer/counter are
selected for each of the following
a) MOV TMOD,#01h TMOD=00000001H,mode 1 timer 0 is selected.
b) MOV TMOD,#20h TMOD=00100000H,mode 2 ,timer 1 is selected.
c) MOV TMOD,#69h TMOD=01101001,counter1 is selected with mode2
and software gating control. Timer 0 is selected with mode1 and gating
control is through external
TIMER MODE-1 PROGRAMMING
• Load the TMOD register value indicating which timer (timer 0 or timer 1) is
to be used and which timer mode (0 or 1) is selected.
• Load registers TL and TH with initial count value. Since it’s a 16 bit timer
values from 0000h to FFFFh can be loaded into timer register.
• Start the timer using “SETB TR0” and “SETB TR1” for timer0 and timer1
respectively.
• Keep monitoring the timer overflow flag (TF) with the “JNB TFx, target
“instruction to check if it is raised.
• Stop the timer using “CLR TR0” and “CLR TR1” instructions for timer0 and
timer1 respectively.
• Clear the TF flag for the next round.
• Go back to Step 2 to load TH and TL again
TIMER MODE-1 PROGRAMMING
Example 1: In the following program, we 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.
MOV TMOD,#01 Timer 0, mode 1(16-bit mode)
HERE: MOV TL0,#0F2H TL0=F2H, the low byte
MOV TH0,#0FFH TH0=FFH, the high byte
CPL P1.5 toggle P1.5 to create a square wave.
ACALL DELAY call a delay subroutine
SJMP HERE
DELAY: SETB TR0 start the timer 0
AGAIN: JNB TF0,AGAIN monitor timer flag 0 until it rolls over
CLR TR0 stop timer 0
CLR TF0 clear timer 0 flag
RET Return from subroutine.
DELAY CALCULATION
• The timer works with a clock frequency of 1/12 of the XTAL frequency; therefore,
we get 11.0592 MHz / 12 = 921.6 kHz as the timer frequency.
• So each clock has a period of T = 1/921.6kHz = 1.085us. Delay = number of counts
× 1.085us.
• The number of counts for the roll over is FFFFH – FFF2H = 0DH (13 decimal).
• However, we add one to 13 because of the extra clock needed when it rolls over
from FFFF to 0 and raise the TF flag.
• This gives 14 × 1.085us = 15.19us for half the pulse.
• For the entire period it is T = 2 × 15.19us = 30.38us as the time delay generated
by the timer.
• The count to be loaded can be calculated using the formula Initial count value=
[Maximum value – Required delay *crystal freq/12]
Find the delay generated in the following
program.
CLR P2.3
MOV TMOD,#01
HERE: MOV TL0,#3EH
MOV TH0, #0B8H
SETB P2.3
SETB TR0
AGAIN: JNB TF0, AGAIN
CLR TR0
CLR TF0
CLR P2.3
DELAY CALCULATION
• (FFFF-B83E+1 )= 47C2H
• 47C2H = 18370(D)
• 18370*1.085us = 19.93145ms
Find the delay generated in the following
program. (Use XTAL frequency = 22Mhz)
CLR P1.3
MOV TMOD, #10H
MOV TH0, #0FFH
MOV TL0, #00H
SETB P1.3
SETB TCON.6
AGAIN : JNB TCON.7, AGAIN
CLR TCON.6
CLR TCON.7
CLR P1.3
DELAY GENERATED
• FFFFH-FF00H= FFH = 255+1 = 256
• DELAY = 256*0.546 = 139.2us
f = 22Mhz,
Instruction cycle = 1/12*22Mhz = 1.833Mhz
T= 1/f= 0.546us
Finding values to be loaded into timer
• Divide the desired time delay by 1.0856us
• Perform 65536-n, where n is the decimal value from first step.
• Convert the result from step 2 to hexadecimal , where yyxx is the
initial value to be loaded.
• Set TL = xx, TH = yy.
Assume crystal frequency = 11.059Mhz.What value is to be loaded to timer register
if we want delay of 5ms? Show the program for timer 0 to create a pulse width of
5ms on P2.3.
• Calculation :
XTAL = 11.0592Mhz , Delay =5ms
-- 5ms/1.085us = 4608 clocks
-- 65536-4608= EE00H.
-- therefore, TH = EEh, TL = 00h
Program
CLR P2.3
MOV TMOD, #01
HERE: MOV TL0,#0
MOV TH0, #0EEH
SETB P2.3
SETB TR0
AGAIN: JNB TF0, AGAIN
CLR P2.3
CLR TR0
CLR TF0
Generate a square wave with ON time of 3ms and
OFF time of 10ms on port0. Assume XTAL=22Mhz
MOV TMOD, #01H
BACK: MOV TL0, #075H
MOV TH0, #0B8H
MOV P0, #00H
ACALL DELAY
MOV TL0,#8AH
MOV TH0, #0EAH
MOV P0, #0FFH
ACALL DELAY
SJMP BACK
ORG 300H
DELAY: SETB TR0
AGAIN: JNB TF0, AGAIN
CLR TR0
CLR TF0
RET
END
OFF time = 10ms/0.546 = 18315 cycles.
65536-18315=47221=B875h
TIMER-MODE 2 PROGRAMMING
• It is an 8-bit timer, therefore allows only values of 00 to FFh to be
loaded to timer register TH.
• After TH is loaded with 8-bit value, 8051 copies it to TL. Then timer is
started . Done by SETB TR0/TR1.
• After timer is started it starts to count up by incrementing TL register.
• Once it reaches FFh and rolls to 00, TF flag is set high.
• When TL rolls over from FF to 0, TF is set high and TL is automatically
reloaded with original value kept by TH register. To repeat we just
have to clear TF. This auto-reload.
TIMER-MODE 2 PROGRAMMING
STEPS FOR MODE-2 PROGRAMMING
1. Load the TMOD value register indicating which timer (timer 0 or
timer 1) is to be used, and the timer mode (mode 2) is selected.
2. Load the TH registers with the initial count value.
3. Start timer.
4. Keep monitoring the timer flag (TF) with the JNB TFx, target
instruction to see whether it is raised .Get out of the loop when TF
goes high
5. Clear the TF flag.
6. Go back to Step4; since mode 2 is auto reload.
Assume XTAL = 11.0592 MHz, find the frequency of the square wave
generated on pin P1.0 in the following program. Also find the smallest
achievable frequency in this program and TH value to do that.
ORG 00H
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 Create square wave
CLR TF1 clear Timer 1 flag
SJMP BACK Since mode 2 is auto-reload jump to label back to monitor the overflow flag .
END
SOLUTION
Now (256 - 05) × 1.085 us = 251 × 1.085 us = 272.33 us is the high
portion of the pulse. Since it is a 50% duty cycle square wave, the
period T is twice that; as a result T = 2 × 272.33 us = 544.67 us
Frequency (F) = 1.83597 kHz.
Write an 8051 ALP to generate a square wave of
frequency 10Khz on pin P1.4.Use timer 0 mode2
with XTAL Frequency=22MHz.
• Time period of square wave =1/f=1/10K=0.1ms Ton=Toff=0.05ms Count
value=256-(0.05m/0.54µ) =256-94 =A4h
ORG 00H
MOV TMOD,#02H
MOV TH0,#0A4H
SETB TR0
BACK: JNB TF0,BACK
CPL P1.4
CLR TF0
SJMP BACK
END
Write an 8051 ALP to generate a square wave of
frequency 10Khz on pin P1.4.Use timer 0 mode2
with XTAL Frequency=22MHz.
MOV TMOD, #02H
MOV TH0, #0A4H
SETB TR0
HERE : SET P1.4
BACK: JNB TF0, BACK
CPL P1.4
CLR TF0
SJMP HERE
END
Write an 8051 ALP to toggle all bits of port 2
continuously every 50ms.Use timer1 mode2 with
XTAL Freq=11.0592MHz.
Calculation:
• Maximum delay possible in mode 2 is 0.277ms but given is 50ms .So
we have to go for multiple delay count. Take 0.25ms,now to get 50ms
,50ms/0.25ms=200. Count value to be loaded to get 0.25ms delay i.e.,
=256-0.25m/1.085µ =20h or 26 in decimal
Program
ORG 00H
MOV TMOD,#20H
MOV TH1,#20H
SETB TR1
AGAIN: MOV P2,#00H
MOV R0,#200
ACALL DELAY
MOV P2,#0FFH
MOV R0,#200
ACALL DELAY
SJMP AGAIN
DELAY: JNB TF1,DELAY
CLR TF1
DJNZ R0,DELAY
CLR TR1
RET
C PROGRAMMING IN 8051
Write a C program to toggle P1.5 every 50ms. Use Timer 0, mode 1to
create a delay of 50ms.
Write a C program to toggle P1.5 every 50ms. Use
Timer 0, mode 1 to create a delay of 50ms.
# include<reg51.h>
void T0M1delay(void);
Sbit mybit = P1^5;
void main(void)
{ while(1)
{ mybit = ~mybit;
T0M1delay();
}
}
Write a C program to toggle P1.5 every 50ms. Use
Timer 0, mode 1 to create a delay of 50ms.
void T0M1 delay(void)
{
TMOD = 0X01;
TL0 = 0XFD;
TH0 = 0X4B;
TR0 = 1;
while(TF0==0)
TR0 = 0;
TF0 = 0;
}
A switch is connected to pin P1.2.Write an 8051 C program to monitor
SW and create the following frequencies on pin P1.7:
SW=0:500Hz SW=1:750Hz.Use timer 0, mode1.
For 500Hz-count value to be loaded-FC67h
For 750 Hz-count value to be loaded-FD9Ah
#include<reg51.h>
sbit mybit = P1^7;
sbit SW = P1^2;
Void T0M1delay(unsigned char);
Void main(void)
{
SW= 1;
While(1)
{
mybit = ~mybit;
If(SW==0)
T0M1delay(0);
else
T0M1delay(1);
}
}
void T0M1delay(unsigned char c)
{
TMOD = 0X01;
If(c==0)
{
TL0 = 0X67;
TH0 = 0XFC;
}
else
{
TL0 = 0X9A;
TH0 = 0XFD;
}
TR0=1;
while(TF0==0)
TR0=0;
TF0=0;
}
Write an 8051 C program to create a frequency of
2500hz on p2.7. Use timer 1, mode 2 to create the
delay.
SERIAL PORT COMMUNICATION
BASICS OF SERIAL COMMUNICATION
• Computer transfers data in two ways parallel and serial.
• In parallel data transfer often 8 or more lines are used to transfer data
to a device.
• Examples of parallel data transfer are printer and hard disk using
cable wire strips. A lot of data can be transferred within short time
but distance cannot be great.
• To transfer to a device located many meters away serial
communication is used.
• In serial data is sent one bit at a time , in parallel data is sent in bytes.
INTRODUCTION
• When a processor communicates with outside world provides data in
byte sized chunks.
• In case of printers information can be grabbed and presented. But if
cable is not too long, it can diminish and distort the signals.
• For transferring data between two systems located far away serial
communication is used.
• In serial communication there are two methods synchronous and
asynchronous. Synchronous method transfer block of data at a time
while asynchronous transfer single byte at a time.
• For serial communication parallel-in-serial-out and serial-in-parallel-
out registers must be used.
HALF AND FULL-DUPLEX
• In data transmission if data can be transmitted and received, it is
duplex transmission.
• In printers we use simplex transmission only computer sends data.
• Duplex transmission can be full duplex or half duplex depending on
whether data is passed simultaneously.
• If data is transmitted one way it is half or else full duplex.
ASYNCHRONOUS SERIAL COMMUNICATION AND DATA FRAMING
• Data coming in at the receiving end is all 0’s and 1’s both sender and
receiver should agree on a set of protocols.
• Start and stop bits:
Asynchronous serial data communication is widely used for character
oriented method while block oriented is used for synchronous method.
In asynchronous each character is placed between start and stop. This
is called framing.
The start bit is always low and stop bit is high. LSB is sent out first.
ASYNCHRONOUS SERIAL COMMUNICATION AND DATA FRAMING
• In modern PC, use of stop bit is standard ASCII character use stop bit,
total 10 bits gives 20% overhead.
• Some of the system include parity bit. Parity is calculated including
parity bit.
DATA TRANSFER RATE
• The rate of data transfer in serial data communication is bits per
second (bps) another widely used term is baud rate.
• In modem technology it is defined as number of signal
changes/second.
• If a serial port has 9600 baud , then serial port is capable of
transferring max 9600 bits per second.
RS232
• To allow compatibility among data communication equipment made
by various manufactures, an interfacing standard called RS232 was set
by the Electronics Industries Association (EIA) in 1960.
• The standard was set long before the advent of logic family, so its
input and output voltage levels are not TTL compatible.
• In RS232, a logic one (1) is represented by -3 to -25V and referred as
MARK while logic zero (0) is represented by +3 to +25V and referred
as SPACE.
• To connect any RS232 to a microcontroller system voltage converters
such as MAX232 to convert the TTL logic level to RS232 voltage levels
and vice-versa are used. MAX232 IC chips are commonly referred as
line drivers.
DB9 MALE || DB25 FEMALE CONNECTOR
PIN DESCRIPTION
DATA COMMUNICATION CLASSIFICATION
• DTE (data terminal equipment) refers to terminal and computers that
send and receive data.
• DCE (data communication equipment) refers to communication
equipment, such as modems.
• The simplest connection between a PC and microcontroller requires a
minimum of three pins, TxD, RxD, and ground.
RS232 HANDSHAKING SIGNALS
To ensure fast and reliable communication data transfer must be
coordinated.
• DTR (data terminal ready): When terminal is turned on, it sends out
signal DTR to indicate that it is ready for communication.
• DSR (data set ready): When DCE is turned on and has gone through
the self-test, it assert DSR to indicate that it is ready to communicate.
• RTS (request to send) When the DTE device has byte to transmit, it
assert RTS to signal the modem that it has a byte of data to transmit.
• CTS (clear to send): When the modem has room for storing the data
it is to receive, it sends out signal CTS to DTE to indicate that it can
receive the data now .
RS232 HANDSHAKING SIGNALS
• DCD (data carrier detect): The modem asserts signal DCD to inform
the DTE that a valid carrier has been detected and that contact
between it and the other modem is established.
• RI (ring indicator): An output from the modem and an input to a PC
indicates that the telephone is ringing.It goes on and off in
synchronous with the ringing sound
8051 CONNECTION TO RS232
• The 8051 has two pins that are used specifically for transferring and
receiving data serially. These two pins are called TXD, RXD. Pin 11 of
the 8051 (P3.1) assigned to TXD and pin 10 (P3.0) is designated as
RXD.
• These pins TTL compatible; therefore they require line driver (MAX
232) to make them RS232 compatible.
• MAX 232 converts RS232 voltage levels to TTL voltage levels and vice
versa. One advantage of the MAX232 is that it uses a +5V power
source which is the same as the source voltage for the 8051.
8051 CONNECTION TO RS232
8051 CONNECTION TO RS232
• MAX232 has two sets of drivers for transferring and receiving data.
• Line drivers used as T1 and T2(Txd) and for receiving Rxd(R1 and R2).
• In many applications T1 and R1 are used together , second one is left
unused.
• T1 in pin is TTL side and converted to TXD of MC and T1 out is RS232
converted to RXD pin.
• MAX232 requires 4 capacitors ranging from 1 to 22uf. Most widely
used is 22uf.
8051 SERIAL PORT PROGRAMMING IN ASSEMBLY
• To allow data transfer and between PC and 8051 system without any error
we must make sure baud rate of 8051 and PC port matches.
• Baud rate in 8051: 8051 receives and transfers data serially at different
baud rates.
• 8051 divides the crystal frequency by 12.
• In case of Xtal = 11.0592Mhz then machine cycle= 921.6Khz
• 8051 serial communication UART circuit divides machine cycle of 921.6Khz
by 32 before it is used by timer 1 to set baud rate.
• 921.6khz/21 = 28,800Hz
• This the number used to find TH1 to set baud rate.
• When timer 1 is used to set baud rate it must be programmed in mode 2,8-
bit auto reload.
TH1 VALUES
BAUD RATE TH1 (DECIMAL) TH1(HEXADECIMAL)
9600 -3 FD
4800 -6 FA
2400 -12 F4
1200 -24 E8
BAUD RATE CALCULATION
With XTAL = 11.0592 MHz, find the TH1 value needed to have the
following baud rates. (a) 9600 (b) 2400 (c) 1200 .
SOLUTION:
The machine cycle frequency of 8051 = 11.0592 / 12 = 921.6 kHz, and
921.6 kHz / 32 = 28,800 Hz is frequency by UART to timer 1 to set baud
rate.
(a) 28,800 / 3 = 9600 where -3 = FD (hex) is loaded into TH1.
(b) 28,800 / 12 = 2400 where -12 = F4 (hex) is loaded into TH1.
(c) 28,800 / 24 = 1200 where -24 = E8 (hex) is loaded into TH1
SERIAL CONTROL REGISTER(SCON)
SERIAL CONTROL REGISTER(SCON)
• SM0, SM1 They determine the framing of data by specifying the
number of bits per character and the start and stop bits.
SERIAL CONTROL REGISTER(SCON)
• SM2: This enables the multiprocessing capability of the 8051
• REN (receive enable): It is a bit-addressable register When it is high, it
allows 8051 to receive data on RxD pin .If low, the receiver is disable.
• TI (transmit interrupt): When 8051 finishes the transfer of 8-bit character.
It raises TI flag to indicate that it is ready to transfer another byte. TI bit is
raised at the beginning of the stop bit.
• RI (receive interrupt): When 8051 receives data serially via RxD, it gets rid
of the start and stop bits and places the byte in SBUF register. It raises the
RI flag bit to indicate that a byte has been received and should be picked up
before it is lost. RI is raised halfway through the stop bit.
SERIAL CONTROL REGISTER(SCON)
• TB8 : Used for serial mode 2&3.
• Rb8 : In serial mode 1, this bit gets a copy of stop bit when 8 bit data
Is received . It is also used in 2 & 3 mode.
SERIAL BUFFER REGISTER
• SBUF is a 8-bit register for serial communication in 8051.
• For a byte of data to be transferred via TXD line, it must be placed in
SBUF register.
• Similarly SBUF holds the byte of data when it is received by 8051 via
RXD line.
• Eg: MOV SBUF, #’D’
• MOV SBUF, A
SERIAL PROGRAMMING IN ASSEMBLY
STEPS:
• The TMOD register is loaded with the value 20H, indicating the use of the
Timer 1 mode 2 (8-bit auto reload) to set the baud rate.
• The TH1 is loaded with one of the values in table 5.1 to set the baud rate
for serial data transfer.
• The SCON register is loaded with the value 50H, indicating serial mode 1,
where an8-bit data is framed with start and stop bits.
• TR1 is set to 1 start timer 1.
• TI is cleared b the “CLR TI” instruction.
• The character byte to be transferred serially is written into the SBUF
register.
• The TI flag bit is monitored with the use of the instruction JNB TI, target to
see if the Character has been transferred completely.
• To transfer the next character, go to step 5
IMPORTANCE OF TI FLAG
• The byte character to be transferred is written into SBUF.
• Start bit is transferred.
• 8-bit is transferred one at a time.
• Stop bit is transferred. It is during this transfer of stop bit 8051 raises
TI flag indicating last character, ready to transfer next character.
• By monitoring TI Flag, we make sure we are not overloading SBUF.
• If we write another byte into SBUF before TI is raised , untransmitted
portion of previous byte will be lost.
• After SBUF is loaded with a new byte TI must be forced ‘0’ by CLR TI in
order for new byte to be transferred.
PROGRAMMING 8051 TO RECEIVE DATA SERIALLY
• The TMOD register is loaded with the value 20H, indicating the use of
the Timer 1 imode 2 (8-bit auto reload) to set the baud rate.
• The TH1 is loaded with one of the values in table 5.1 to set the baud
rate for serial data transfer.
• The SCON register is loaded with the value 50H, indicating serial
mode 1, where an8-bit data is framed with start and stop bits.
• TR1 is set to 1 start timer 1.
• RI is cleared by the “CLR RI” instruction.
• RI is monitored with “JNB RI, XX”
• When RI is raised , SBUF has byte contents are moved to safe place.
• To receive next character , go to step 5.
IMPORTANCE OF RI FLAG
• It receives the start bit indicating that next bit is the first bit of
character.
• 8-bit is received one at a time.
• Stop bit is received . When receiving RI=1, indicating that entire
character byte has been received and must be picked before
overwriting.
• By checking RI, a character is received copy SBUF to safe place.
• After copying RI is forced to ‘0’ by CLR RI, to allow next instruction.
Write an 8051 ALP to receive data serially and put
them in P1. Set the baud rate as 4800, 8bit data,
1stop and start bit.
Write an 8051 ALP to receive data serially and put
them in P1. Set the baud rate as 4800, 8bit data,
1stop and start bit.
MOV TMOD,#20H
MOV TH1,#-6
MOV SCON,#50H
SETB TR1
HERE: JNB RI,HERE
MOV A, SBUF
MOV P1,A
CLR RI
SJMP HERE
WRITE A PROGRAM TO TRANSFER ‘Y’ SERIALLY AT
9600 baud , also ‘N’ through PORT 0 Connected to
Device.
• Here one byte of data is transmitted serially through pin 11(p3.1) and
another parallel form.
8051
DISPLAY
‘Y’
P3.1
‘N’
P0
PROGRAM
MOV TMOD, #20H
MOV TH1, #-3
MOV SCON, #50H
SETB TR1
Again: MOV SBUF, #’Y’
HERE: JNB TI, HERE
CLR TI
MOV P0, #’N’
SJMP Again
Take data in ports 0,1,2 one after the other
and transfer data serially
MOV TMOD, #20H
MOV TH1, -6
MOV SCON, #50H
MOV P0, #0FFH
MOV P1, #0FFH
MOV P2, #0FFH
SETB TR1
Continued
rpt : MOV A, P0
ACALL SEND
MOV A, P1
ACALL SEND
MOV A , P2
ACALL SEND
SJMP rpt
SEND: MOV SBUF, A
HERE: JNB T1, HERE
CLR TI
RET
Write an ALP to implement following operation do a once, and b & c continuously
4800 baud rate. a. Send to the PC message “we are ready ’ b. Receive an data sent b
PC and put it on LED’s connected to P1 c. Get data on switches connected to P2 and
send to PC

More Related Content

What's hot

Communicationlabmanual
CommunicationlabmanualCommunicationlabmanual
Communicationlabmanualjkanth26
 
Sensor interfacing in 8051
Sensor interfacing in 8051Sensor interfacing in 8051
Sensor interfacing in 8051
Irfan Ahmad
 
Pn sequence
Pn sequencePn sequence
Pn sequence
Darshil Shah
 
Line follower robot
Line follower robotLine follower robot
Line follower robotPriya Hada
 
Interfacing Stepper motor with 8051
Interfacing Stepper motor with 8051Interfacing Stepper motor with 8051
Interfacing Stepper motor with 8051
Pantech ProLabs India Pvt Ltd
 
Timer programming for 8051 using embedded c
Timer programming for 8051 using embedded cTimer programming for 8051 using embedded c
Timer programming for 8051 using embedded c
Vikas Dongre
 
NYQUIST CRITERION FOR ZERO ISI
NYQUIST CRITERION FOR ZERO ISINYQUIST CRITERION FOR ZERO ISI
NYQUIST CRITERION FOR ZERO ISI
FAIZAN SHAFI
 
Lecture5 teletraffic
Lecture5 teletrafficLecture5 teletraffic
Lecture5 teletraffic
mazlina1202
 
Microwave Engineering Lecture Notes
Microwave Engineering Lecture NotesMicrowave Engineering Lecture Notes
Microwave Engineering Lecture Notes
FellowBuddy.com
 
Introduction to 8085 microprocessor
Introduction to 8085 microprocessorIntroduction to 8085 microprocessor
Introduction to 8085 microprocessor
kunj desai
 
Digital signal Processing all matlab code with Lab report
Digital signal Processing all matlab code with Lab report Digital signal Processing all matlab code with Lab report
Digital signal Processing all matlab code with Lab report
Alamgir Hossain
 
Phase Shift Keying & π/4 -Quadrature Phase Shift Keying
Phase Shift Keying & π/4 -Quadrature Phase Shift KeyingPhase Shift Keying & π/4 -Quadrature Phase Shift Keying
Phase Shift Keying & π/4 -Quadrature Phase Shift Keying
Naveen Jakhar, I.T.S
 
Density Based Traffic signal system using microcontroller
Density Based Traffic signal system using microcontrollerDensity Based Traffic signal system using microcontroller
Density Based Traffic signal system using microcontroller
krity kumari
 
I/O port programming in 8051
I/O port programming in 8051I/O port programming in 8051
I/O port programming in 8051
ssuser3a47cb
 
T-states in microprocessor 8085
T-states in microprocessor 8085T-states in microprocessor 8085
T-states in microprocessor 8085yedles
 
a159143892914.pdf
a159143892914.pdfa159143892914.pdf
a159143892914.pdf
tusar18
 
FM demodulation using PLL
FM demodulation using PLLFM demodulation using PLL
FM demodulation using PLL
mpsrekha83
 
Convolution codes - Coding/Decoding Tree codes and Trellis codes for multiple...
Convolution codes - Coding/Decoding Tree codes and Trellis codes for multiple...Convolution codes - Coding/Decoding Tree codes and Trellis codes for multiple...
Convolution codes - Coding/Decoding Tree codes and Trellis codes for multiple...
Madhumita Tamhane
 
Companding & Pulse Code Modulation
Companding & Pulse Code ModulationCompanding & Pulse Code Modulation
Companding & Pulse Code Modulation
Yeshudas Muttu
 

What's hot (20)

Communicationlabmanual
CommunicationlabmanualCommunicationlabmanual
Communicationlabmanual
 
Sensor interfacing in 8051
Sensor interfacing in 8051Sensor interfacing in 8051
Sensor interfacing in 8051
 
Pn sequence
Pn sequencePn sequence
Pn sequence
 
Line follower robot
Line follower robotLine follower robot
Line follower robot
 
Interfacing Stepper motor with 8051
Interfacing Stepper motor with 8051Interfacing Stepper motor with 8051
Interfacing Stepper motor with 8051
 
Timer programming for 8051 using embedded c
Timer programming for 8051 using embedded cTimer programming for 8051 using embedded c
Timer programming for 8051 using embedded c
 
NYQUIST CRITERION FOR ZERO ISI
NYQUIST CRITERION FOR ZERO ISINYQUIST CRITERION FOR ZERO ISI
NYQUIST CRITERION FOR ZERO ISI
 
Lecture5 teletraffic
Lecture5 teletrafficLecture5 teletraffic
Lecture5 teletraffic
 
Microwave Engineering Lecture Notes
Microwave Engineering Lecture NotesMicrowave Engineering Lecture Notes
Microwave Engineering Lecture Notes
 
Introduction to 8085 microprocessor
Introduction to 8085 microprocessorIntroduction to 8085 microprocessor
Introduction to 8085 microprocessor
 
Digital signal Processing all matlab code with Lab report
Digital signal Processing all matlab code with Lab report Digital signal Processing all matlab code with Lab report
Digital signal Processing all matlab code with Lab report
 
Phase Shift Keying & π/4 -Quadrature Phase Shift Keying
Phase Shift Keying & π/4 -Quadrature Phase Shift KeyingPhase Shift Keying & π/4 -Quadrature Phase Shift Keying
Phase Shift Keying & π/4 -Quadrature Phase Shift Keying
 
Density Based Traffic signal system using microcontroller
Density Based Traffic signal system using microcontrollerDensity Based Traffic signal system using microcontroller
Density Based Traffic signal system using microcontroller
 
I/O port programming in 8051
I/O port programming in 8051I/O port programming in 8051
I/O port programming in 8051
 
T-states in microprocessor 8085
T-states in microprocessor 8085T-states in microprocessor 8085
T-states in microprocessor 8085
 
Chap 5
Chap 5Chap 5
Chap 5
 
a159143892914.pdf
a159143892914.pdfa159143892914.pdf
a159143892914.pdf
 
FM demodulation using PLL
FM demodulation using PLLFM demodulation using PLL
FM demodulation using PLL
 
Convolution codes - Coding/Decoding Tree codes and Trellis codes for multiple...
Convolution codes - Coding/Decoding Tree codes and Trellis codes for multiple...Convolution codes - Coding/Decoding Tree codes and Trellis codes for multiple...
Convolution codes - Coding/Decoding Tree codes and Trellis codes for multiple...
 
Companding & Pulse Code Modulation
Companding & Pulse Code ModulationCompanding & Pulse Code Modulation
Companding & Pulse Code Modulation
 

Similar to Microcontrollers-MODULE4.pptx

Timer programming
Timer programming Timer programming
Timer programming vijaydeepakg
 
5-Timer Mode 2 Programming-18-03-2024.pptx
5-Timer Mode 2 Programming-18-03-2024.pptx5-Timer Mode 2 Programming-18-03-2024.pptx
5-Timer Mode 2 Programming-18-03-2024.pptx
Rahultater4
 
8051 Timer
8051 Timer8051 Timer
8051 Timer
Ramasubbu .P
 
4.Timer_1.ppt
4.Timer_1.ppt4.Timer_1.ppt
4.Timer_1.ppt
YashwanthChandra7
 
Micro c lab7(timers)
Micro c lab7(timers)Micro c lab7(timers)
Micro c lab7(timers)
Mashood
 
9 timer programming
9 timer programming9 timer programming
9 timer programming
Channabasappa Kudarihal
 
Microprocessor Week 9: Timer and Counter
Microprocessor Week 9: Timer and CounterMicroprocessor Week 9: Timer and Counter
Microprocessor Week 9: Timer and Counter
Arkhom Jodtang
 
lecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.pptlecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.ppt
HebaEng
 
8051 timers--2
   8051 timers--2   8051 timers--2
8051 timers--2
Syed Basharat Hussain
 
MICROCONTROLLER TIMERS.ppt
MICROCONTROLLER TIMERS.pptMICROCONTROLLER TIMERS.ppt
MICROCONTROLLER TIMERS.ppt
reemasajin1
 
Programming 8051 Timers
Programming 8051 Timers Programming 8051 Timers
Programming 8051 Timers ViVek Patel
 
6-Interrupts Programming-27-03-2024.pptx
6-Interrupts Programming-27-03-2024.pptx6-Interrupts Programming-27-03-2024.pptx
6-Interrupts Programming-27-03-2024.pptx
Rahultater4
 
8051 ch9-950217
8051 ch9-9502178051 ch9-950217
8051 ch9-950217
Gopal Krishna Murthy C R
 
Timers
TimersTimers
Timers
Vima Mali
 
8051 Timers and Counters
8051 Timers and Counters8051 Timers and Counters
8051 Timers and Counters
Shreyans Pathak
 
8051 timer counter
8051 timer counter8051 timer counter
8051 timer counterankit3991
 

Similar to Microcontrollers-MODULE4.pptx (20)

Timer programming
Timer programming Timer programming
Timer programming
 
8051 ch9
8051 ch98051 ch9
8051 ch9
 
5-Timer Mode 2 Programming-18-03-2024.pptx
5-Timer Mode 2 Programming-18-03-2024.pptx5-Timer Mode 2 Programming-18-03-2024.pptx
5-Timer Mode 2 Programming-18-03-2024.pptx
 
UNIT-5.ppt
UNIT-5.pptUNIT-5.ppt
UNIT-5.ppt
 
12 mt06ped007
12 mt06ped007 12 mt06ped007
12 mt06ped007
 
8051 Timer
8051 Timer8051 Timer
8051 Timer
 
4.Timer_1.ppt
4.Timer_1.ppt4.Timer_1.ppt
4.Timer_1.ppt
 
Micro c lab7(timers)
Micro c lab7(timers)Micro c lab7(timers)
Micro c lab7(timers)
 
9 timer programming
9 timer programming9 timer programming
9 timer programming
 
Microprocessor Week 9: Timer and Counter
Microprocessor Week 9: Timer and CounterMicroprocessor Week 9: Timer and Counter
Microprocessor Week 9: Timer and Counter
 
lecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.pptlecture 12 counter_microcontroller2.ppt
lecture 12 counter_microcontroller2.ppt
 
8051 timers--2
   8051 timers--2   8051 timers--2
8051 timers--2
 
MICROCONTROLLER TIMERS.ppt
MICROCONTROLLER TIMERS.pptMICROCONTROLLER TIMERS.ppt
MICROCONTROLLER TIMERS.ppt
 
Programming 8051 Timers
Programming 8051 Timers Programming 8051 Timers
Programming 8051 Timers
 
6-Interrupts Programming-27-03-2024.pptx
6-Interrupts Programming-27-03-2024.pptx6-Interrupts Programming-27-03-2024.pptx
6-Interrupts Programming-27-03-2024.pptx
 
8051 ch9-950217
8051 ch9-9502178051 ch9-950217
8051 ch9-950217
 
8051e
8051e8051e
8051e
 
Timers
TimersTimers
Timers
 
8051 Timers and Counters
8051 Timers and Counters8051 Timers and Counters
8051 Timers and Counters
 
8051 timer counter
8051 timer counter8051 timer counter
8051 timer counter
 

Recently uploaded

Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
SupreethSP4
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 

Recently uploaded (20)

Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 

Microcontrollers-MODULE4.pptx

  • 1. MODULE 4 TIMERS AND SERIAL PORT COMMUNICATION Prof BINDU H M DSCE, Bengaluru
  • 2. INTRODUCTION TO TIMERS • 8051 microcontroller have two timers timer 0 and timer 1.These are 16 bit timers, but 8051 is a 8 bit controller so they are referred are lower byte and higher byte shown below TL0, TH0, TL1,TH1. • They can be used as either timers to generate time delay or as counters to count events. • The required delay is obtained by loading the number machine cycles into those registers. Normally delay count value will be decremented in software delay program, but count value will be incremented in timers. i.e.( means delay count need to be subtracted from max count value and then timer has to be activated.)
  • 3. BIT FORMAT OF TIMER 0/1
  • 4. TMOD REGISTER • Both the timers,timer0 and timer 1 use TMOD register to set various timer operation modes. • TMOD is an 8 bit register with lower 4 bits set aside for timer 0 and higher 4 bits for timer 1. • Lower two bits are used are to set the timer mode and higher 2 bits specify the operation.
  • 5. TMOD REGISTER Gate Bit: • Timer control will be decided through this bit value • Gate-=0 : indicates that timer can be controlled through program by TR and TF bit’s in TCON register. • Bit TR is used to start the timer by making TR=1 and stop the timer by making TR=0.This is called software control. • Gate=1: indicates that timer control is done by external source. This is called hardware control.
  • 6. C/T (Counter/ Timer) Bit: This bit in the TMOD register is used to decide whether the timer is used As a delay generator or an event counter. • If C/T =0, Then timer is working as delay generator • If C/T=1, Then that timer is working as event counter TMOD REGISTER
  • 7. M1, M0: • These two bits are used to select different timer modes. The below table 1 shows the different modes. TMOD REGISTER
  • 8. Indicate which mode and which timer/counter are selected for each of the following a) MOV TMOD,#01h TMOD=00000001H,mode 1 timer 0 is selected. b) MOV TMOD,#20h TMOD=00100000H,mode 2 ,timer 1 is selected. c) MOV TMOD,#69h TMOD=01101001,counter1 is selected with mode2 and software gating control. Timer 0 is selected with mode1 and gating control is through external
  • 9. TIMER MODE-1 PROGRAMMING • Load the TMOD register value indicating which timer (timer 0 or timer 1) is to be used and which timer mode (0 or 1) is selected. • Load registers TL and TH with initial count value. Since it’s a 16 bit timer values from 0000h to FFFFh can be loaded into timer register. • Start the timer using “SETB TR0” and “SETB TR1” for timer0 and timer1 respectively. • Keep monitoring the timer overflow flag (TF) with the “JNB TFx, target “instruction to check if it is raised. • Stop the timer using “CLR TR0” and “CLR TR1” instructions for timer0 and timer1 respectively. • Clear the TF flag for the next round. • Go back to Step 2 to load TH and TL again
  • 11. Example 1: In the following program, we 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. MOV TMOD,#01 Timer 0, mode 1(16-bit mode) HERE: MOV TL0,#0F2H TL0=F2H, the low byte MOV TH0,#0FFH TH0=FFH, the high byte CPL P1.5 toggle P1.5 to create a square wave. ACALL DELAY call a delay subroutine SJMP HERE DELAY: SETB TR0 start the timer 0 AGAIN: JNB TF0,AGAIN monitor timer flag 0 until it rolls over CLR TR0 stop timer 0 CLR TF0 clear timer 0 flag RET Return from subroutine.
  • 12. DELAY CALCULATION • The timer works with a clock frequency of 1/12 of the XTAL frequency; therefore, we get 11.0592 MHz / 12 = 921.6 kHz as the timer frequency. • So each clock has a period of T = 1/921.6kHz = 1.085us. Delay = number of counts × 1.085us. • The number of counts for the roll over is FFFFH – FFF2H = 0DH (13 decimal). • However, we add one to 13 because of the extra clock needed when it rolls over from FFFF to 0 and raise the TF flag. • This gives 14 × 1.085us = 15.19us for half the pulse. • For the entire period it is T = 2 × 15.19us = 30.38us as the time delay generated by the timer. • The count to be loaded can be calculated using the formula Initial count value= [Maximum value – Required delay *crystal freq/12]
  • 13. Find the delay generated in the following program. CLR P2.3 MOV TMOD,#01 HERE: MOV TL0,#3EH MOV TH0, #0B8H SETB P2.3 SETB TR0 AGAIN: JNB TF0, AGAIN CLR TR0 CLR TF0 CLR P2.3
  • 14. DELAY CALCULATION • (FFFF-B83E+1 )= 47C2H • 47C2H = 18370(D) • 18370*1.085us = 19.93145ms
  • 15. Find the delay generated in the following program. (Use XTAL frequency = 22Mhz) CLR P1.3 MOV TMOD, #10H MOV TH0, #0FFH MOV TL0, #00H SETB P1.3 SETB TCON.6 AGAIN : JNB TCON.7, AGAIN CLR TCON.6 CLR TCON.7 CLR P1.3
  • 16. DELAY GENERATED • FFFFH-FF00H= FFH = 255+1 = 256 • DELAY = 256*0.546 = 139.2us f = 22Mhz, Instruction cycle = 1/12*22Mhz = 1.833Mhz T= 1/f= 0.546us
  • 17. Finding values to be loaded into timer • Divide the desired time delay by 1.0856us • Perform 65536-n, where n is the decimal value from first step. • Convert the result from step 2 to hexadecimal , where yyxx is the initial value to be loaded. • Set TL = xx, TH = yy.
  • 18. Assume crystal frequency = 11.059Mhz.What value is to be loaded to timer register if we want delay of 5ms? Show the program for timer 0 to create a pulse width of 5ms on P2.3. • Calculation : XTAL = 11.0592Mhz , Delay =5ms -- 5ms/1.085us = 4608 clocks -- 65536-4608= EE00H. -- therefore, TH = EEh, TL = 00h
  • 19. Program CLR P2.3 MOV TMOD, #01 HERE: MOV TL0,#0 MOV TH0, #0EEH SETB P2.3 SETB TR0 AGAIN: JNB TF0, AGAIN CLR P2.3 CLR TR0 CLR TF0
  • 20. Generate a square wave with ON time of 3ms and OFF time of 10ms on port0. Assume XTAL=22Mhz MOV TMOD, #01H BACK: MOV TL0, #075H MOV TH0, #0B8H MOV P0, #00H ACALL DELAY MOV TL0,#8AH MOV TH0, #0EAH MOV P0, #0FFH ACALL DELAY SJMP BACK ORG 300H DELAY: SETB TR0 AGAIN: JNB TF0, AGAIN CLR TR0 CLR TF0 RET END OFF time = 10ms/0.546 = 18315 cycles. 65536-18315=47221=B875h
  • 21. TIMER-MODE 2 PROGRAMMING • It is an 8-bit timer, therefore allows only values of 00 to FFh to be loaded to timer register TH. • After TH is loaded with 8-bit value, 8051 copies it to TL. Then timer is started . Done by SETB TR0/TR1. • After timer is started it starts to count up by incrementing TL register. • Once it reaches FFh and rolls to 00, TF flag is set high. • When TL rolls over from FF to 0, TF is set high and TL is automatically reloaded with original value kept by TH register. To repeat we just have to clear TF. This auto-reload.
  • 23. STEPS FOR MODE-2 PROGRAMMING 1. Load the TMOD value register indicating which timer (timer 0 or timer 1) is to be used, and the timer mode (mode 2) is selected. 2. Load the TH registers with the initial count value. 3. Start timer. 4. Keep monitoring the timer flag (TF) with the JNB TFx, target instruction to see whether it is raised .Get out of the loop when TF goes high 5. Clear the TF flag. 6. Go back to Step4; since mode 2 is auto reload.
  • 24. Assume XTAL = 11.0592 MHz, find the frequency of the square wave generated on pin P1.0 in the following program. Also find the smallest achievable frequency in this program and TH value to do that. ORG 00H 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 Create square wave CLR TF1 clear Timer 1 flag SJMP BACK Since mode 2 is auto-reload jump to label back to monitor the overflow flag . END
  • 25. SOLUTION Now (256 - 05) × 1.085 us = 251 × 1.085 us = 272.33 us is the high portion of the pulse. Since it is a 50% duty cycle square wave, the period T is twice that; as a result T = 2 × 272.33 us = 544.67 us Frequency (F) = 1.83597 kHz.
  • 26. Write an 8051 ALP to generate a square wave of frequency 10Khz on pin P1.4.Use timer 0 mode2 with XTAL Frequency=22MHz. • Time period of square wave =1/f=1/10K=0.1ms Ton=Toff=0.05ms Count value=256-(0.05m/0.54µ) =256-94 =A4h ORG 00H MOV TMOD,#02H MOV TH0,#0A4H SETB TR0 BACK: JNB TF0,BACK CPL P1.4 CLR TF0 SJMP BACK END
  • 27. Write an 8051 ALP to generate a square wave of frequency 10Khz on pin P1.4.Use timer 0 mode2 with XTAL Frequency=22MHz. MOV TMOD, #02H MOV TH0, #0A4H SETB TR0 HERE : SET P1.4 BACK: JNB TF0, BACK CPL P1.4 CLR TF0 SJMP HERE END
  • 28. Write an 8051 ALP to toggle all bits of port 2 continuously every 50ms.Use timer1 mode2 with XTAL Freq=11.0592MHz. Calculation: • Maximum delay possible in mode 2 is 0.277ms but given is 50ms .So we have to go for multiple delay count. Take 0.25ms,now to get 50ms ,50ms/0.25ms=200. Count value to be loaded to get 0.25ms delay i.e., =256-0.25m/1.085µ =20h or 26 in decimal
  • 29. Program ORG 00H MOV TMOD,#20H MOV TH1,#20H SETB TR1 AGAIN: MOV P2,#00H MOV R0,#200 ACALL DELAY MOV P2,#0FFH MOV R0,#200 ACALL DELAY SJMP AGAIN DELAY: JNB TF1,DELAY CLR TF1 DJNZ R0,DELAY CLR TR1 RET
  • 30. C PROGRAMMING IN 8051 Write a C program to toggle P1.5 every 50ms. Use Timer 0, mode 1to create a delay of 50ms.
  • 31. Write a C program to toggle P1.5 every 50ms. Use Timer 0, mode 1 to create a delay of 50ms. # include<reg51.h> void T0M1delay(void); Sbit mybit = P1^5; void main(void) { while(1) { mybit = ~mybit; T0M1delay(); } }
  • 32. Write a C program to toggle P1.5 every 50ms. Use Timer 0, mode 1 to create a delay of 50ms. void T0M1 delay(void) { TMOD = 0X01; TL0 = 0XFD; TH0 = 0X4B; TR0 = 1; while(TF0==0) TR0 = 0; TF0 = 0; }
  • 33. A switch is connected to pin P1.2.Write an 8051 C program to monitor SW and create the following frequencies on pin P1.7: SW=0:500Hz SW=1:750Hz.Use timer 0, mode1. For 500Hz-count value to be loaded-FC67h For 750 Hz-count value to be loaded-FD9Ah
  • 34. #include<reg51.h> sbit mybit = P1^7; sbit SW = P1^2; Void T0M1delay(unsigned char); Void main(void) { SW= 1; While(1) { mybit = ~mybit; If(SW==0) T0M1delay(0); else T0M1delay(1); } }
  • 35. void T0M1delay(unsigned char c) { TMOD = 0X01; If(c==0) { TL0 = 0X67; TH0 = 0XFC; } else { TL0 = 0X9A; TH0 = 0XFD; } TR0=1; while(TF0==0) TR0=0; TF0=0; }
  • 36. Write an 8051 C program to create a frequency of 2500hz on p2.7. Use timer 1, mode 2 to create the delay.
  • 38. BASICS OF SERIAL COMMUNICATION • Computer transfers data in two ways parallel and serial. • In parallel data transfer often 8 or more lines are used to transfer data to a device. • Examples of parallel data transfer are printer and hard disk using cable wire strips. A lot of data can be transferred within short time but distance cannot be great. • To transfer to a device located many meters away serial communication is used. • In serial data is sent one bit at a time , in parallel data is sent in bytes.
  • 39.
  • 40. INTRODUCTION • When a processor communicates with outside world provides data in byte sized chunks. • In case of printers information can be grabbed and presented. But if cable is not too long, it can diminish and distort the signals. • For transferring data between two systems located far away serial communication is used. • In serial communication there are two methods synchronous and asynchronous. Synchronous method transfer block of data at a time while asynchronous transfer single byte at a time. • For serial communication parallel-in-serial-out and serial-in-parallel- out registers must be used.
  • 41. HALF AND FULL-DUPLEX • In data transmission if data can be transmitted and received, it is duplex transmission. • In printers we use simplex transmission only computer sends data. • Duplex transmission can be full duplex or half duplex depending on whether data is passed simultaneously. • If data is transmitted one way it is half or else full duplex.
  • 42.
  • 43. ASYNCHRONOUS SERIAL COMMUNICATION AND DATA FRAMING • Data coming in at the receiving end is all 0’s and 1’s both sender and receiver should agree on a set of protocols. • Start and stop bits: Asynchronous serial data communication is widely used for character oriented method while block oriented is used for synchronous method. In asynchronous each character is placed between start and stop. This is called framing. The start bit is always low and stop bit is high. LSB is sent out first.
  • 44.
  • 45. ASYNCHRONOUS SERIAL COMMUNICATION AND DATA FRAMING • In modern PC, use of stop bit is standard ASCII character use stop bit, total 10 bits gives 20% overhead. • Some of the system include parity bit. Parity is calculated including parity bit.
  • 46. DATA TRANSFER RATE • The rate of data transfer in serial data communication is bits per second (bps) another widely used term is baud rate. • In modem technology it is defined as number of signal changes/second. • If a serial port has 9600 baud , then serial port is capable of transferring max 9600 bits per second.
  • 47. RS232 • To allow compatibility among data communication equipment made by various manufactures, an interfacing standard called RS232 was set by the Electronics Industries Association (EIA) in 1960. • The standard was set long before the advent of logic family, so its input and output voltage levels are not TTL compatible. • In RS232, a logic one (1) is represented by -3 to -25V and referred as MARK while logic zero (0) is represented by +3 to +25V and referred as SPACE. • To connect any RS232 to a microcontroller system voltage converters such as MAX232 to convert the TTL logic level to RS232 voltage levels and vice-versa are used. MAX232 IC chips are commonly referred as line drivers.
  • 48. DB9 MALE || DB25 FEMALE CONNECTOR
  • 50. DATA COMMUNICATION CLASSIFICATION • DTE (data terminal equipment) refers to terminal and computers that send and receive data. • DCE (data communication equipment) refers to communication equipment, such as modems. • The simplest connection between a PC and microcontroller requires a minimum of three pins, TxD, RxD, and ground.
  • 51. RS232 HANDSHAKING SIGNALS To ensure fast and reliable communication data transfer must be coordinated. • DTR (data terminal ready): When terminal is turned on, it sends out signal DTR to indicate that it is ready for communication. • DSR (data set ready): When DCE is turned on and has gone through the self-test, it assert DSR to indicate that it is ready to communicate. • RTS (request to send) When the DTE device has byte to transmit, it assert RTS to signal the modem that it has a byte of data to transmit. • CTS (clear to send): When the modem has room for storing the data it is to receive, it sends out signal CTS to DTE to indicate that it can receive the data now .
  • 52. RS232 HANDSHAKING SIGNALS • DCD (data carrier detect): The modem asserts signal DCD to inform the DTE that a valid carrier has been detected and that contact between it and the other modem is established. • RI (ring indicator): An output from the modem and an input to a PC indicates that the telephone is ringing.It goes on and off in synchronous with the ringing sound
  • 53. 8051 CONNECTION TO RS232 • The 8051 has two pins that are used specifically for transferring and receiving data serially. These two pins are called TXD, RXD. Pin 11 of the 8051 (P3.1) assigned to TXD and pin 10 (P3.0) is designated as RXD. • These pins TTL compatible; therefore they require line driver (MAX 232) to make them RS232 compatible. • MAX 232 converts RS232 voltage levels to TTL voltage levels and vice versa. One advantage of the MAX232 is that it uses a +5V power source which is the same as the source voltage for the 8051.
  • 55. 8051 CONNECTION TO RS232 • MAX232 has two sets of drivers for transferring and receiving data. • Line drivers used as T1 and T2(Txd) and for receiving Rxd(R1 and R2). • In many applications T1 and R1 are used together , second one is left unused. • T1 in pin is TTL side and converted to TXD of MC and T1 out is RS232 converted to RXD pin. • MAX232 requires 4 capacitors ranging from 1 to 22uf. Most widely used is 22uf.
  • 56. 8051 SERIAL PORT PROGRAMMING IN ASSEMBLY • To allow data transfer and between PC and 8051 system without any error we must make sure baud rate of 8051 and PC port matches. • Baud rate in 8051: 8051 receives and transfers data serially at different baud rates. • 8051 divides the crystal frequency by 12. • In case of Xtal = 11.0592Mhz then machine cycle= 921.6Khz • 8051 serial communication UART circuit divides machine cycle of 921.6Khz by 32 before it is used by timer 1 to set baud rate. • 921.6khz/21 = 28,800Hz • This the number used to find TH1 to set baud rate. • When timer 1 is used to set baud rate it must be programmed in mode 2,8- bit auto reload.
  • 57. TH1 VALUES BAUD RATE TH1 (DECIMAL) TH1(HEXADECIMAL) 9600 -3 FD 4800 -6 FA 2400 -12 F4 1200 -24 E8
  • 58. BAUD RATE CALCULATION With XTAL = 11.0592 MHz, find the TH1 value needed to have the following baud rates. (a) 9600 (b) 2400 (c) 1200 . SOLUTION: The machine cycle frequency of 8051 = 11.0592 / 12 = 921.6 kHz, and 921.6 kHz / 32 = 28,800 Hz is frequency by UART to timer 1 to set baud rate. (a) 28,800 / 3 = 9600 where -3 = FD (hex) is loaded into TH1. (b) 28,800 / 12 = 2400 where -12 = F4 (hex) is loaded into TH1. (c) 28,800 / 24 = 1200 where -24 = E8 (hex) is loaded into TH1
  • 60. SERIAL CONTROL REGISTER(SCON) • SM0, SM1 They determine the framing of data by specifying the number of bits per character and the start and stop bits.
  • 61. SERIAL CONTROL REGISTER(SCON) • SM2: This enables the multiprocessing capability of the 8051 • REN (receive enable): It is a bit-addressable register When it is high, it allows 8051 to receive data on RxD pin .If low, the receiver is disable. • TI (transmit interrupt): When 8051 finishes the transfer of 8-bit character. It raises TI flag to indicate that it is ready to transfer another byte. TI bit is raised at the beginning of the stop bit. • RI (receive interrupt): When 8051 receives data serially via RxD, it gets rid of the start and stop bits and places the byte in SBUF register. It raises the RI flag bit to indicate that a byte has been received and should be picked up before it is lost. RI is raised halfway through the stop bit.
  • 62. SERIAL CONTROL REGISTER(SCON) • TB8 : Used for serial mode 2&3. • Rb8 : In serial mode 1, this bit gets a copy of stop bit when 8 bit data Is received . It is also used in 2 & 3 mode.
  • 63. SERIAL BUFFER REGISTER • SBUF is a 8-bit register for serial communication in 8051. • For a byte of data to be transferred via TXD line, it must be placed in SBUF register. • Similarly SBUF holds the byte of data when it is received by 8051 via RXD line. • Eg: MOV SBUF, #’D’ • MOV SBUF, A
  • 64. SERIAL PROGRAMMING IN ASSEMBLY STEPS: • The TMOD register is loaded with the value 20H, indicating the use of the Timer 1 mode 2 (8-bit auto reload) to set the baud rate. • The TH1 is loaded with one of the values in table 5.1 to set the baud rate for serial data transfer. • The SCON register is loaded with the value 50H, indicating serial mode 1, where an8-bit data is framed with start and stop bits. • TR1 is set to 1 start timer 1. • TI is cleared b the “CLR TI” instruction. • The character byte to be transferred serially is written into the SBUF register. • The TI flag bit is monitored with the use of the instruction JNB TI, target to see if the Character has been transferred completely. • To transfer the next character, go to step 5
  • 65. IMPORTANCE OF TI FLAG • The byte character to be transferred is written into SBUF. • Start bit is transferred. • 8-bit is transferred one at a time. • Stop bit is transferred. It is during this transfer of stop bit 8051 raises TI flag indicating last character, ready to transfer next character. • By monitoring TI Flag, we make sure we are not overloading SBUF. • If we write another byte into SBUF before TI is raised , untransmitted portion of previous byte will be lost. • After SBUF is loaded with a new byte TI must be forced ‘0’ by CLR TI in order for new byte to be transferred.
  • 66. PROGRAMMING 8051 TO RECEIVE DATA SERIALLY • The TMOD register is loaded with the value 20H, indicating the use of the Timer 1 imode 2 (8-bit auto reload) to set the baud rate. • The TH1 is loaded with one of the values in table 5.1 to set the baud rate for serial data transfer. • The SCON register is loaded with the value 50H, indicating serial mode 1, where an8-bit data is framed with start and stop bits. • TR1 is set to 1 start timer 1. • RI is cleared by the “CLR RI” instruction. • RI is monitored with “JNB RI, XX” • When RI is raised , SBUF has byte contents are moved to safe place. • To receive next character , go to step 5.
  • 67. IMPORTANCE OF RI FLAG • It receives the start bit indicating that next bit is the first bit of character. • 8-bit is received one at a time. • Stop bit is received . When receiving RI=1, indicating that entire character byte has been received and must be picked before overwriting. • By checking RI, a character is received copy SBUF to safe place. • After copying RI is forced to ‘0’ by CLR RI, to allow next instruction.
  • 68. Write an 8051 ALP to receive data serially and put them in P1. Set the baud rate as 4800, 8bit data, 1stop and start bit.
  • 69. Write an 8051 ALP to receive data serially and put them in P1. Set the baud rate as 4800, 8bit data, 1stop and start bit. MOV TMOD,#20H MOV TH1,#-6 MOV SCON,#50H SETB TR1 HERE: JNB RI,HERE MOV A, SBUF MOV P1,A CLR RI SJMP HERE
  • 70. WRITE A PROGRAM TO TRANSFER ‘Y’ SERIALLY AT 9600 baud , also ‘N’ through PORT 0 Connected to Device. • Here one byte of data is transmitted serially through pin 11(p3.1) and another parallel form. 8051 DISPLAY ‘Y’ P3.1 ‘N’ P0
  • 71. PROGRAM MOV TMOD, #20H MOV TH1, #-3 MOV SCON, #50H SETB TR1 Again: MOV SBUF, #’Y’ HERE: JNB TI, HERE CLR TI MOV P0, #’N’ SJMP Again
  • 72. Take data in ports 0,1,2 one after the other and transfer data serially MOV TMOD, #20H MOV TH1, -6 MOV SCON, #50H MOV P0, #0FFH MOV P1, #0FFH MOV P2, #0FFH SETB TR1
  • 73. Continued rpt : MOV A, P0 ACALL SEND MOV A, P1 ACALL SEND MOV A , P2 ACALL SEND SJMP rpt SEND: MOV SBUF, A HERE: JNB T1, HERE CLR TI RET
  • 74. Write an ALP to implement following operation do a once, and b & c continuously 4800 baud rate. a. Send to the PC message “we are ready ’ b. Receive an data sent b PC and put it on LED’s connected to P1 c. Get data on switches connected to P2 and send to PC