0% found this document useful (0 votes)
19 views44 pages

8051 Microcontroller Timer and Serial Interface

The document provides an overview of interfacing the 8051 microcontroller with a focus on its timers and serial communication capabilities. It details the configuration and operation of Timer 0 and Timer 1, including their modes and control registers, as well as the functionality of the serial port and its modes of data transmission. Additionally, it explains how to initialize timers and manage serial communication, including baud rate generation and multiprocessor modes.

Uploaded by

sarvesvar29
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views44 pages

8051 Microcontroller Timer and Serial Interface

The document provides an overview of interfacing the 8051 microcontroller with a focus on its timers and serial communication capabilities. It details the configuration and operation of Timer 0 and Timer 1, including their modes and control registers, as well as the functionality of the serial port and its modes of data transmission. Additionally, it explains how to initialize timers and manage serial communication, including baud rate generation and multiprocessor modes.

Uploaded by

sarvesvar29
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

UNIT 5 – INTERFACING 8051 MICROCONTROLLER

COUNTERS / TIMERS

The 8051 microcontroller has two 16-bit timers Timer 0 (T0) and Timer 1(T1) which can be used
either to generate accurate time delays or as event counters. These timers are accessed as two
8-bit registers TLO, THO & TL1 ,TH1 because the 8051 microcontroller has 8-bit architecture.

TIMER 0 : The Timer 0 is a 16-bit register and can be treated as two 8-bit registers (TL0 &
TH0) and these registers can be accessed similar to any other registers like A,B or R1,R2,R3
etc…

Ex : The instruction Mov TL0,#07 moves the value 07 into lower byte of Timer0.

Similarly Mov R5,TH0 saves the contents of TH0 in the R5 register.

TIMER 1 : The Timer 1 is also a 16-bit register and can be treated as two 8-bit registers (TL1
& TH1) and these registers can be accessed similar to any other registers like A,B or R1,R2,R3
etc…

Ex : The instruction MOV TL1,#05 moves the value 05 into lower byte of Timer1.

Similarly MOV R0,TH1 saves the contents of TH1 in the R0 register


TMOD Register : The various operating modes of both the timers T0 and T1 are set by an 8-bit
register called TMOD register. In this TMOD register the lower 4-bits are meant for Timer 0 and
the higher 4-bits are meant for Timer1.

GATE: This bit is used to start or stop the timers by hardware .When GATE= 1 ,the timers can
be started / stopped by the external sources. When GATE= 0, the timers can be started or stopped
by software instructions like SETB TR0 or SETB TR1

C/T (clock/Timer) : This bit decides whether the timer is used as delay generator or event
counter. When C/T = 0 ,the Timer is used as delay generator and if C/T=1 the timer is used as
an event counter. The clock source for the time delay is the crystal frequency of 8051.

M1,M0 (Mode) : These two bits are the timer mode bits. The timers of the 8051 can be
configured in three modes.Mode0, Mode1 and [Link] selection and operation of the modes
is shown below.

[Link] M0 M1 Mode Operation


1 0 0 0 13-bit Timer mode
8-bit Timer/counter. THx with TLx as 5-bit
prescalar
2 0 1 1 16-bit Timer mode.16-bit timer /counter
without pre-scalar
3 1 0 2 8-bit auto reload. THx contains a value that
is to be loaded into TLx each time it
overflows
4 1 1 3 Split timer mode
Timer Control Register (TCON)

Operating Modes of Timer

Timer Mode-0:
In this mode, the timer is used as a 13-bit UP counter as follows.

The lower 5 bits of TLX and 8 bits of THX are used for the 13 bit [Link] 3 bits of TLX are
ignored. When the counter rolls over from all 0's to all 1's, TFX flag is set and an interrupt is
generated. The input pulse is obtained from the previous stage. If TR1/0 bit is 1 and Gate bit is 0,
the counter continues counting up. If TR1/0 bit is 1 and Gate bit is 1, then the operation of the
counter is controlled byINTX input. This mode is useful to measure the width of a given pulse
fed to INTX input.

Timer Mode-1:
This mode is similar to mode-0 except for the fact that the Timer operates in 16-bit mode.
Timer Mode-2: (Auto-Reload Mode)
This is a 8 bit counter/timer operation. Counting is performed in TLX while THX stores a
constant value. In this mode when the timer overflows i.e. TLX becomes FFH, it is fed with the
value stored in THX. For example if we load THX with 50H then the timer in mode 2 will count
from 50H to FFH. After that 50H is again reloaded. This mode is useful in applications like fixed
time sampling.

Timer Mode-3:
Timer 1 in mode-3 simply holds its count. The effect is same as setting TR1=0. Timer0 in mode-
3 establishes TL0 and TH0 as two separate counters.

Control bits TR1 and TF1 are used by Timer-0 (higher 8 bits) (TH0) in Mode-3 while TR0 and
TF0 are available to Timer-0 lower 8 bits(TL0).

Counting Circuit

If 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. This hardware way allows starting or stopping the timer externally
at any time via a simple switch.
Steps involved in initializing timer mode:

❑ Set the initial value of registers


❑ Start the timer(TRx) and then the 8051 counts up.
❑ Input from internal system clock (machine cycle)
❑ When the registers equal to 0 and the 8051 sets a bit (TFx)to denote time out

Example 1: To generate a square wave using timer 0 in P1.5 pin


Example 2: To generate a square wave of 50 Hz on pin P2.3 where the crystal frequency =
11.0592 MHz

Serial Interface
⮚ The serial port of 8051 is full duplex, i.e., it can transmit and receive simultaneously.
⮚ The register SBUF is used to hold the data. The special function register SBUF is physically
two registers. One is, write-only and is used to hold data to be transmitted out of the 8051 via
TXD. The other is, read-only and holds the received data from external sources via RXD.
Both mutually exclusive registers have the same address 099H.
Serial Port Control Register (SCON)
Register SCON controls serial data communication-Address: 098H (Bit addressable)

Mode select bits

SM2: multi processor communication bit


REN: Receive enable bit
TB8: Transmitted bit 8 (Normally we have 0-7 bits transmitted/received)
RB8: Received bit 8
TI: Transmit interrupt flag
RI: Receive interrupt flag

Power Mode control Register


Register PCON controls processor powerdown, sleep modes and serial data bandrate. Only one bit of
PCON is used with respect to serial communication. The seventh bit (b7)(SMOD) is used to generate
the baud rate of serial communication.

Address: 87H

SMOD: Serial baud rate modify bit


GF1: General purpose user flag bit 1
GF0: General purpose user flag bit 0
PD: Power down bit
IDL: Idle mode bit
Data Transmission
⮚ Transmission of serial data begins at any time when data is written to SBUF. Pin P3.1
(Alternate function bit TXD) is used to transmit data to the serial data network. TI is set to 1
when data has been transmitted. This signifies that SBUF is empty so that another byte can be
sent.
Data Reception
⮚ Reception of serial data begins if the receive enable bit is set to 1 for all modes. Pin P3.0
(Alternate function bit RXD) is used to receive data from the serial data network. Receive
interrupt flag, RI, is set after the data has been received in all modes. The data gets stored in
SBUF register from where it can be read.

Serial Data Transmission Modes:


Mode-0: SIMPLE SHIFT REGISTER MODE:
⮚ In this mode, the serial port works like a shift register and the data transmission works
synchronously with a clock frequency of fosc /12.
⮚ Serial data is received and transmitted through RXD. 8 bits are transmitted/ received aty a
time.
⮚ Pin TXD outputs the shift clock pulses of frequency fosc /12, which is connected to the
external circuitry for synchronization.

Data transmission/reception in Mode-0

Mode-1 (standard UART mode) :


⮚ In mode-1, the serial port functions as a standard Universal Asynchronous Receiver
Transmitter (UART) mode.
⮚ 10 bits are transmitted through TXD or received through RXD.
⮚ The 10 bits consist of one start bit (which is usually ‘0'), 8 data bits (LSB is sent first/received
first), and a stop bit (which is usually ‘1').
⮚ Once received, the stop bit goes into RB8 in the special function register SCON. The baud
rate is variable.

Data
transmission format in UART mode

⮚ Bit time= 1/fbaud


⮚ In receiving mode, data bits are shifted into the receiver at the programmed baud rate. The
data word (8-bits) will be loaded to SBUF if the following conditions are true.
1. RI must be zero. (i.e., the previously received byte has been cleared from SBUF)
2. Mode bit SM2 = 0 or stop bit = 1.
After the data is received and the data byte has been loaded into SBUF, RI becomes one.
Mode-1 baud rate generation:
⮚ Timer-1 is used to generate baud rate for mode-1 serial communication by using overflow
flag of the timer to determine the baud frequency.
⮚ Timer-1 is used in timer mode-2 as an auto-reload 8-bit timer. The data rate is generated by
timer-1 using the following formula.

Where,
SMOD is the 7th bit of PCON register
fosc is the crystal oscillator frequency of the microcontroller
It can be noted that fosc/ (12 X [256- (TH1)]) is the timer overflow frequency in timer mode-2, which
is the auto-reload mode.
If timer-1 is not run in mode-2, then the baud rate is,

Timer-1 can be run using the internal clock, fosc/12 (timer mode) or from any external source via pin
T1 (P3.5) (Counter mode).

Example: If standard baud rate is desired, then 11.0592 MHz crystal could be selected. To get a
standard 9600 baud rate, the setting of TH1 is calculated as follows.
Assuming SMOD to be ‘0'

Or,

Or,

In mode-1, if SM2 is set to 1, no receive interrupt (RI) is generated unless a valid stop bit is received

Serial Data Mode-2 - Multiprocessor Mode :( 9 bit UART )


⮚ In this mode 11 bits are transmitted through TXD or received through RXD.
⮚ The various bits are as follows: a start bit (usually ‘0'), 8 data bits (LSB first), a
programmable 9 th (TB8 or RB8)bit and a stop bit (usually ‘1').
⮚ While transmitting, the 9 th data bit (TB8 in SCON) can be assigned the value ‘0' or ‘1'. For
example, if the information of parity is to be transmitted, the parity bit (P) in PSW could be
moved into TB8.
⮚ On reception of the data, the 9 th bit goes into RB8 in ‘SCON', while the stop bit is ignored.
The baud rate is programmable to either 1/32 or 1/64 of the oscillator frequency.
f baud = (2 SMOD /64) fosc.
Mode-3 - Multi processor mode with variable baud rate :
⮚ In this mode 11 bits are transmitted through TXD or received through RXD.
⮚ The various bits are: a start bit (usually ‘0'), 8 data bits (LSB first), a programmable 9 th bit
and a stop bit (usually ‘1').
⮚ Mode-3 is same as mode-2, except the fact that the baud rate in mode-3 is variable (i.e., just
as in mode-1).
f baud = (2 SMOD /32) * ( fosc / 12 (256-TH1)) .
This baudrate holds when Timer-1 is programmed in Mode-2.

You might also like