Module 4

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 97

MODULE 4

8051 TIMER
AND SERIAL
PORT
PROGRAMMING
outlin
e8051 Timers and Serial Port: 8051
Timers and Counters – Operation
and Assembly language programming to
generate a pulse using Mode-1 and a
square wave using Mode-2 on a port
pin. 8051 Serial Communication- Basics
of Serial Data Communication, RS-232
standard, 9 pin RS232 signals, Simple
Serial Port programming in Assembly
and C to transmit a message and to
receive data serially.
8051
Timers
The 8051 has two timers/counters,
they can be used as
◦ Timers to generate a time delay
◦ Event counters to count events
happening outside the microcontroller
Both Timer 0 and Timer 1 are
16bits wide
◦ Since 8051 has an 8-bit architecture, each
16- bits timer is accessed as two separate
registers of low byte and high byte
Timer 0 & 1 Registers

Accessed as low byte and high


byte
◦ The low byte register is called
TL0/TL1
◦ The high byte register is called
TH0/TH1
◦ Accessed like any other
register MOV TL0,#4FH
MOV R5,TH0
TMOD Register
Both timers 0 and 1 use the same
register, called TMOD (timer
mode), to set the various timer
operation modes
◦ TMOD is a 8-bit register
The lower 4 bits are for Timer 0
The upper 4 bits are for Timer 1
In each case, the lower 2 bits are used to set the
timer mode
The upper 2 bits to specify the operation
GATE
Timers of 8051 do starting and
stopping by either software or
hardware control
◦ In using software to start and stop the
timer where GATE=0
The start and stop of the timer are controlled
by way of software by the TR (timer start) bits
TR0 andTR1
The SETB instruction starts it, and it is stopped
by the CLR instruction
These instructions start and stop the timers as long
as GATE=0 in the TMOD register
GATE (cont.)
The hardware way of starting and
stopping the timer by an external
source is achieved by making GATE=1
in the
TMOD register
Mode 1
Programming
The following are the characteristics
and operations of mode1:
◦ It is a 16-bit timer
It allows value of 0000 to FFFFH to be loaded
into the timer’s register TL andTH
◦ After TH and TL are loaded with a16-
bit initial value, the timer must be
started
This is done by SETB TR0 for timer 0 and
SETBTR1
for timer 1
◦ After being started, it starts to count
Mode 1 Programming
(cont.)
When it rolls over from FFFFH to 0000, it sets
high a flag bit called TF (timer flag)
Each timer has its own timer flag:TF0 for timer 0, and
TF1
for timer 1
This timer flag can be monitored
When this timer flag is raised, one option would
be to stop the timer with the instructions CLR
TR0 or CLR TR1, for timer 0 and timer 1,
respectively
◦ In order to repeat the process
TH and TL must be reloaded with the original
value TF must be reloaded to 0
Steps to Mode 1 Program
Load the TMOD value register
◦ Indicating which timer (timer 0 or timer 1)
is to be used and which timer mode (1 or
2) is selected
Load registers TL and TH with
initial count value
Start the timer
Keep monitoring the timer flag
(TF)
◦ With the JNB TFx,target instruction to see
if it is raised
Steps to Mode 1 Program (cont.)
◦ Get out of the loop when TF becomes
high
Stop the timer
Clear the TF flag for the next
round Go back to Step 2 to
loadTH andTL again
If TH and TLare not reloaded. the program
generates a single pulse.
Finding the Loaded TimerValues
To calculate the values to be loaded
into theTL andTH registers:
◦ Assume XTAL = 11.0592MHz
Divide the desired time delay by 1.085 us
Perform 65536 – n, where n is the
decimal value we
got in Step1
Convert the result of Step2 to hex, where yyxx
is the initial hex value to be loaded into the
timer’s register
SetTL = xx andTH = yy
CPL P1.5 ; Comp. P1.5 to get Hi, Lo
CPL P2.3 ; Comp. P2.3 to get Hi, Lo
CLR TF1 ; Clear timer 1 flag
SJMP ;Reload timer
AGAIN
Mode 2 Programming
The following are the characteristics
and operations of mode 2:
◦ It is an 8-bit timer
It allows only values of 00 to FFH to be loaded
into the timer’s register TH
◦ After TH is loaded with the 8-bit
value,the 8051 gives a copy of it to TL
Then the timer must be started
This is done by the instruction SETB TR0 for timer 0
and
SETB TR1 for timer1
Mode 2 Programming (cont.)
◦ After the timer is started, it starts to
count up by incrementing the TL
register
It counts up until it reaches its limit of FFH
When it rolls over from FFH to 00, it sets high
the TF (timer flag)
◦ When TF is set to 1,TL is reloaded
automatically with the original value kept
by the TH register
◦ To repeat the process, we must simply
clear TF and let it go without any need
by the programmer to reload the original
Mode 2 Programming (cont.)
Mode 2 can auto-reload, in contrast
with mode 1 in which the programmer
has to reload TH andTL
Steps to Mode 2 Program
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
Load the TH registers with the
initial count value
Start timer
Keep monitoring the timer flag
(TF)
◦ With the JNB TFx,target instruction to
see whether it is raised
Steps to Mode 2 Program (cont.)
◦ Get out of the loop when TF goes
high
Clear the TF flag
Go back to Step
4
◦ Since mode 2 is
auto-reload
(253)
The number 200 is
the timer count till
the TF is set to 1
Counter Programming
Timers can also be used as counters
◦ Counting events happening outside the
8051
◦ A pulse outside of the 8051 increments
the TH,TL registers
◦ TMOD and TH,TL registers are the
same as for the timer
Programming the timer also applies to
programming it as a counter
Except the source of the frequency
◦ The C/T bit in the TMOD registers
decides the source of the clock for the
Counter Programming
(cont.)
When C/T = 1, the timer is used as a counter
and gets its pulses from outside the 8051
◦ The counter counts up as pulses are fed
from pins 14 and 15
These pins are called T0 (timer 0 input)
andT1 (timer 1 input)
TCON Register
TCON (timer control) register is an
8-bit register
TCON Register
(cont.)
TCON register is abit-
addressable register
Case of GATE =
1 The start and stop of the timer are
done externally through pins P3.2 and
P3.3 for timers 0 and 1, respectively
◦ Allows to start or stop the timer externally
at any time via a simple switch
Basics of Serial Communication
Computers transfer data in two ways:
◦ Parallel
Often 8 or more lines (wire conductors) are
used to transfer data to a device that is only a
few feet away
◦ Serial
To transfer to a device located many meters
away, the serial method is used
The data is sent one bit at a time
Basics of Serial Communication
(cont.)
◦ At the transmitting end, the byte of data
must be converted to serial bits
Using a parallel-in-serial-out shift register
◦ At the receiving end, the serial data
is received and packed into byte
Using a serial-in-parallel-out shift
register
Basics of Serial Communication
(cont.)
◦ When the distance is short, the digital
signal can be transferred as it is on a
simple wire and requires no modulation
◦ If data is to be transferred on the
telephone line, it must be converted from
0s and 1s to audio tones
This conversion is performed by a device called
a
modem,“Modulator/demodulator”
Basics of Serial Communication
(cont.)
Serial data communication uses
two methods:
◦ Synchronous method transfers a block of
data at a time
◦ Asynchronous method transfers a single
byte at a time
It is possible to write software to
use either of these methods
◦ The programs can be tedious and long
Basics of Serial Communication
(cont.)
There are special IC chips made by
many manufacturers for serial
communications:
◦ UART (universal asynchronous
receiver transmitter)
◦ USART (universal synchronous
asynchronous receiver transmitter)
Half- and Full-Duplex
Transmission
If data can be transmitted and
received, it is a duplex transmission
◦ If data transmitted one way a time,
it is referred to as half duplex
◦ If data can go both ways at a time, it
is full duplex
This is contrast to simplex
transmission
Start and Stop
Bits
A protocol is a set of rules agreed
by both the sender and receiver
on:
◦ How the data is packed
◦ How many bits constitute a character
◦ When the data begins and ends
Asynchronous serial data
communication is widely used for
character-oriented transmissions
◦ Each character is placed in between start
and stop bits, this is called framing
Start and Stop Bits (cont.)
The start bit is always one bit
The stop bit can be one or two
bits The start bit is always a 0
(low)
The stop bit(s) is 1 (high)
Block-oriented data transfers use
the synchronous method
Start and Stop Bits (cont.)
Due to the extended ASCII characters,
8- bit ASCII data is common
◦In older systems, ASCII characters were7-
bit In modern PCs the use of one stop
bit is standard
◦ In older systems, two stop bits were
used Due to the slowness of the receiving
mechanical device
To give the device sufficient time to organize
itself before transmission of the next byte
Start and Stop Bits (cont.)
Assuming that we are transferring a
text file of ASCII characters using 1
stop bit
◦ We have a total of 10 bits for each
character
This gives 20% overhead
In some systems, to maintain data
integrity, the parity bit of the
character byte is included in the
data frame
◦ UART chips allow programming of the
Data Transfer
Rate
The rate of data transfer in serial
data communication is stated in bps
(bits per second)
Another widely used terminology for
bps is baud rate
◦ It is modem terminology and is defined as
the number of signal changes per second
In modems, there are occasions when a single
change of signal transfers several bits of data
Data Transfer Rate
(cont.)
◦ As far as the conductor wire is
concerned, the baud rate and bps are
the same
The data transfer rate of given
computer system depends on
communication ports incorporated into
that system
◦ IBM PC/XT could transfer data at the rate
of 100 to 9600 bps
◦ Pentium-based PCs transfer data at rates
as high as 56K bps
RS232 Standards
An interfacing standard RS232 was set
by the Electronics Industries
Association (EIA) in 1960
◦ The standard was set long before the
advent of the TTL logic family, its input
and output voltage levels are not TTL
compatible
◦ A 1 is represented by -3 ~ -25 V
◦ A 0 bit is +3 ~ +25 V
Making -3 to +3 undefined
RS232 Standards (cont.)
Since not all pins are used in PC
cables, IBM introduced the DB-9
version of the serial I/O standard
Data Communication
Classification
Current terminology classifies
data communication equipment
as:
◦ DTE (data terminal equipment)
Terminal and computers that send and
receive data
◦ DCE (data communication
equipment)
Communication equipment, such as
modems
The simplest connection between a
PC and microcontroller at least
RS232
Pins
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
RS232 Pins
(cont.)
RTS (request to send)
◦ When the DTE device has byte to
transmit, it asserts 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 Pins
(cont.)
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
A line driver such as the MAX232 chip
is required to convert RS232 voltage
levels to TTL levels, and viceversa
8051 has two pins that are used
specifically for transferring and
receiving data serially
◦ These two pins are called TxD and RxD
Part of the port 3 group (P3.0 and P3.1)
These pins are TTL compatible
They require a line driver to make them RS232
compatible
MAX233
To save board space, some designers
use MAX233 chip from Maxim
◦ MAX233 performs the same job as
MAX232
But eliminates the need for capacitors
MAX233 and MAX232 are not pin
compatible
Serial
Communication
Programming
To allow data transfer between the
PC and an 8051 system without any
error
◦ The baud rate of 8051 system must match
the baud rate of the PC’s COM port
SBUF
Register
SBUF is an 8-bit register used solely
for serial communication
◦ For a byte data to be transferred via the
TxD line, it must be placed in the SBUF
register
The moment a byte is written into SBUF, it is
framed with the start and stop bits and
transferred serially via the TxD line
◦ SBUF holds the byte of data when
it is received by 8051 RxD line
When the bits are received serially via RxD,
the 8051 deframes it by eliminating the stop
and start bits, and then placing the byte in
SCON Register
SCON is an 8-bit register used to
program the start bit, stop bit, and
data bits of data framing, among
other things
SCON Register
(cont.)
SM0, SM1
◦ They determine the framing of data by
specifying the number of bits per
character, and the start and stop bits

SM2
◦ This enables the multiprocessing capability
of the 8051
SCON Register
(cont.)
REN (receive enable)
◦ 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
SCON Register
(cont.)
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
SCON is a bit-addressable register
Programming Serial
Data Transmitting
In programming the 8051 to
transfer character bytes serially
◦ TMOD register is loaded with the value
20H Indicating the use of timer 1 in mode 2 (8-
bit auto- reload) to set baud rate
◦ The TH1 is loaded with one of the values
to set baud rate for serial data transfer
◦ SCON register is loaded with the value
50H Indicating serial mode 1, where an 8-
bit data is framed with start and stop bits
Programming Serial
Data Transmitting
(cont.)
◦ TR1 is set to 1 to start timer 1
◦ TI is cleared by CLR TI instruction
◦ The character byte to be transferred
serially is written into SBUF register
◦ The TI flag bit is monitored with the use
of instruction JNB TI, xx to see if the
character has been transferred
completely
◦ To transfer the next byte, go to step 5
Importance of TI
Flag
The steps that 8051 goes through
in transmitting a character viaTxD
◦ The byte character to be transmitted
is written into the SBUF register
◦ The start bit is transferred
◦ The 8-bit character is transferred on bit
at a time
◦ The stop bit is transferred
It is during the transfer of the stop bit that
8051 raises the TI flag
Indicating that the last character was transmitted
Importance of TI Flag
(cont.)
◦ By monitoring the TI flag, we make sure
that we are not overloading the SBUF
If we write another byte into the SBUF before
TI is raised, the untransmitted portion of the
previous byte will be lost
◦ After SBUF is loaded with a new byte, the
TI flag bit must be forced to 0 by CLR TI
In order for this new byte to be transferred
By checking the TI flag bit, we
know whether or not the 8051 is
ready to transfer another byte
Importance of TI Flag
(cont.)
It must be noted that TI flag bit is
raised by 8051 itself when it finishes
data transfer
◦ It must be cleared by the programmer
with instruction CLR TI
If we write a byte into SBUF before the TI flag
bit is raised, we risk the loss of a portion of the
byte being transferred
◦ The TI bit can be checked by
The instruction JNB
TI,xx Using an interrupt
Programming Serial Data
Receiving
In programming the 8051 to
receive character bytes serially
◦ TMOD register is loaded with the value
20H Indicating the use of timer 1 in mode 2 (8-
bit auto- reload) to set baud rate
◦ TH1 is loaded to set baud rate
◦ SCON register is loaded with the value
50H Indicating serial mode 1, where an 8-
bit data is framed with start and stop bits
◦ TR1 is set to 1 to start timer 1
Programming Serial Data
Receiving (cont.)
◦ RI is cleared by CLR RI instruction
◦ The RI flag bit is monitored with the use
of instruction JNB RI,xx to see if an
entire character has been received yet
◦ When RI is raised, SBUF has the byte, and
its contents are moved into a safe place
◦ To receive the next character, go to step
5
MOVC
Importance of RI
Flag
In receiving bit via its RxD pin, 8051
goes through the following steps
◦ It receives the start bit
Indicating that the next bit is the first bit of
the character byte it is about to receive
◦ The 8-bit character is received one bit
at a time
◦ The stop bit is received
When receiving the stop bit 8051 makes RI =
1,
Indicating that an entire character byte has been
received and must be picked up before it gets
overwritten by an incoming character
Importance of RI Flag
(cont.)
◦ By checking the RI flag bit when it is
raised We know that a character has been
received and is sitting in the SBUF register
We copy the SBUF contents to a safe place in some
other
register or memory before it is lost
◦ After the SBUF contents are copied, the
RI flag bit must be forced to 0 by CLR
RI
In order to allow the next received character
byte to be placed in SBUF
Failure to do this causes loss of the received character

By checking the RI flag bit, we know


Importance of RI Flag
(cont.)
◦ If we failed to copy SBUF into a safe place,
we risk the loss of the received byte
◦ It must be noted that RI flag bit is raised
by 8051 when it finish receive data
◦ It must be cleared by the programmer
with instruction CLR RI
◦ If we copy SBUF into a safe place before
the RI flag bit is raised, we risk copying
garbage
The RI bit can be checked by
◦ The instruction JNB RI, xx
◦ Using an interrupt
Doubling Baud
Rate
There are two ways to increase the
baud rate of data transfer
◦ To use a higher frequency crystal
The system crystal is fixed
◦ To change a bit in the PCON register
PCON register is an 8-bit register
It is not a bit-addressable register
When 8051 is powered up, SMOD is zero
We can set it to high by software and
thereby double the baud rate
TI, H_1
A_1
References
1. “The 8051 Microcontroller and Embedded Systems – using assembly and C”,
Muhammad Ali Mazidi and Janice Gillespie Mazidi and Rollin D. McKinlay;
PHI, 2006 / Pearson, 2006.

2.“The 8051 Microcontroller”, Kenneth J. Ayala, 3rd Edition,


Thomson/Cengage Learning.

You might also like