Module 4
Module 4
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
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