Sie sind auf Seite 1von 24

MPMC UNIT 5 SIETK

Unit 5 syllabus : Interfacing of 8051 – keyboard ,displays, ADC converters,


multiple inettrupts-8051 data communication modes and applications
INTERFACING 7 SEGMENT DISPLAY segment LED display is a very popular and it can display
digits from 0 to 9 and quite a few characters like A, b, C, , H, E, e, F,, etc. Knowledge about how to
interface a seven-segment display to a micro controller is very essential in designing embedded
systems. A seven-segment display consists of seven LEDs arranged in the form of a squarish ‘8’
slightly inclined to the right and a single LED as the dot character. Different characters can be
displayed by selectively glowing the required LED segments.
Seven segment displays are of two types, common cathode and common anode. In common
cathode type , the cathode of all LEDs are tied together to a single terminal which is usually labeled
as ‘com‘ Â and the anode of all LEDs are left alone as individual pins labeled as a, b, c, d, e, f, g & h
(or dot) . In common anode type, the anode of all LEDs are tied together as a single terminal and
cathodes are left alone as individual pins. The pin out scheme and picture of a typical 7 segment LED
display is shown in the image below.

In common cathode, all the cathodes of LEDs are tied together and labeled as com. and the anode
are left alone. In common anode, seven segment display all the anodes are tied together and
cathodes are left freely. Below figure shows the internal connections of seven segment Display.
MPMC UNIT 5 SIETK

To display the digits on 7 segment, we need to glow different logic combinations of segments. For
example if you want to display the digit 3 on seven segment then you need to glow the segments a,
b, c, d and g. The below table show you the Hex decimal values what we need to send from PORT2
to Display the digits from 0 to 9.

DIGIT DP G F E D C B A HEX VALUE

0 0 0 1 1 1 1 1 1 0x3f
MPMC UNIT 5 SIETK

1 0 0 0 0 0 1 1 0 0x06

2 0 1 0 1 1 0 1 1 0x5b

3 0 1 0 0 1 1 1 1 0x4f

4 0 1 1 0 0 1 1 0 0x66

5 0 1 1 0 1 1 0 1 0x6d

6 0 1 1 1 1 1 0 1 0x7d

7 0 0 0 0 0 1 1 1 0x07

8 0 1 1 1 1 1 1 1 0x7f

9 0 1 1 0 0 1 1 1 0x67
MPMC UNIT 5 SIETK
MPMC UNIT 5 SIETK

INTERFACING 16X2 LCD


Display units are the most important output devices in embedded projects and electronics products.
16x2 LCD is one of the most used display unit. 16x2 LCD means that there are two rows in which 16
characters can be displayed per line, and each character takes 5X7 matrix space on LCD.

Pin description

PIN
NAME FUNCTION
NO

1 VSS Ground pin

2 VCC Power supply pin of 5V

3 VEE Used for adjusting the contrast commonly attached to the potentiometer.

4 RS RS is the register select pin used to write display data to the LCD (characters),
this pin has to be high when writing the data to the LCD.
During the initializing sequence and other commands this pin should low.

5 R/W Reading and writing data to the LCD for reading the
data R/W pin should be high (R/W=1) to write the data to LCD R/W pin should be low
(R/W=0)

6 E Enable pin is for starting or enabling the module.


A high to low pulse of about 450ns pulse is given to this pin.

7 DB0

8 DB1

9 DB2
MPMC UNIT 5 SIETK

10 DB3

11 DB4 DB0-DB7 Data pins for giving data (normal data like numbers characters or command
data)
which is meant to be displayed

12 DB5

13 DB6

14 DB7

15 LED+ Back light of the LCD which should be connected to Vcc

16 LED- Back light of LCD which should be connected to ground.


MPMC UNIT 5 SIETK

INTERFACING 4X4 MATRIX KEYPAD


Keypads are widely used input devices being used in various electronics and embedded projects.
They are used to take inputs in the form of numbers and alphabets, and feed the same into system
for further processing.
Matrix keypad consists of set of Push buttons, which are interconnected. Like in our case we are
using 4X4 matrix keypad, in which there are 4 push buttons in each of four rows. And the terminals
of the push buttons are connected according to diagram. In first row, one terminal of all the 4 push
buttons are connected together and another terminal of 4 push buttons are representing each of 4
columns, same goes for each row. So we are getting 8 terminals to connect with a microcontroller.
MPMC UNIT 5 SIETK

As shown in above circuit diagram, to interface Keypad, we need to connect 8 terminals of


the keypad to any port (8 pins) of the microcontroller. Like we have connected keypad
terminals to Port 1 of 8051. Whenever any button is pressed we need to get the location of
the button, means the corresponding ROW an COLUMN no.
The hex keypad has 8 communication lines namely R1, R2, R3, R4, C1, C2, C3 and C4. Â R1 to
R4 represents the four rows and C1 to C4 represents the four columns. When a particular
key is pressed the corresponding row and column to which the terminals of the key are
connected gets shorted. For example if key 1 is pressed row R1 and column C1 gets shorted
and so on. The program identifies which key is pressed by a method known as column
scanning. In this method a particular row is kept low (other rows are kept high) and the
columns are checked for low. If a particular column is found low then that means that the
MPMC UNIT 5 SIETK

key connected between that column and the corresponding row (the row that is kept low) is
been pressed. For example if  row R1 is initially kept low and column C1 is found low
during scanning, that means key 1 is pressed.
Optocoupler

Transformers that they can not only provide a step-down (or step-up) voltage, but they also provide
“electrical isolation” between the higher voltage on the primary side and the lower voltage on the
secondary side.In other words, transformers isolate the primary input voltage from the secondary
output voltage using electromagnetic coupling and this is achieved using the magnetic flux
circulating within their laminated iron core.

But we can also provide electrical isolation between an input source and an output load using just
light by using a very common and valuable electronic component called an Optocoupler. The basic
design of an optocoupler, also known as an Opto-isolator, consists of an LED that produces infra-red
light and a semiconductor photo-sensitive device that is used to detect the emitted infra-red beam.
Both the LED and photo-sensitive device are enclosed in a light-tight body or package with metal legs
for the electrical connections as shown.

An optocoupler or opto-isolator consists of a light emitter, the LED and a light sensitive receiver
which can be a single photo-diode, photo-transistor, photo-resistor, photo-SCR, or a photo-TRIAC
with the basic operation of an optocoupler being very simple to understand.

Phototransistor Optocoupler

Assume a photo-transistor device as shown. Current from the source signal passes through the input
LED which emits an infra-red light whose intensity is proportional to the electrical signal. This
emitted light falls upon the base of the photo-transistor, causing it to switch-ON and conduct in a
similar way to a normal bipolar transistor. The base connection of the photo-transistor can be left
MPMC UNIT 5 SIETK

open (unconnected) for maximum sensitivity to the LEDs infra-red light energy or connected to
ground via a suitable external high value resistor to control the switching sensitivity making it more
stable and resistant to false triggering by external electrical noise or voltage transients.

When the current flowing through the LED is interrupted, the infra-red emitted light is cut-off,
causing the photo-transistor to cease conducting. The photo-transistor can be used to switch current
in the output circuit. The spectral response of the LED and the photo-sensitive device are closely
matched being separated by a transparent medium such as glass, plastic or air. Since there is no
direct electrical connection between the input and output of an optocoupler, electrical isolation up
to 10kV is achieved.

Optocouplers are available in four general types, each one having an infra-red LED source but with
different photo-sensitive devices. The four optocouplers are called the: Photo-transistor, Photo-
darlington, Photo-SCR and Photo-triac as shown below.

Applications

Optocouplers and opto-isolators can be used on their own, or to switch a range of other larger
electronic devices such as transistors and triacs providing the required electrical isolation between a
lower voltage control signal, for example one from an Arduino or micro-controller, and a much
higher voltage or mains current output signal.

Common applications for opto-couplers include microprocessor input/output switching, DC and AC


power control, PC communications, signal isolation and power supply regulation which suffer from
current ground loops, etc. The electrical signal being transmitted can be either analogue (linear) or
digital (pulses).

Relay
In some electronic applications we need to switch or control high voltages or high currents. In these
cases we may use electromagnetic or solid state relays. For example, it can be used to control home
appliances using low power electronic circuits.
MPMC UNIT 5 SIETK

An electromagnetic relay is a switch which is used to switch High Voltage or Current using Low
power circuits. It magnetically isolates low power circuits from high power circuits. It is activated by
energizing a electromagnet, coil wounded on a soft iron core.

Generally, the relay consists a inductor coil, a spring (not shown in the figure), Swing terminal, and
two high power contacts named as normally closed (NC) and normally opened (NO). Relay uses an
Electromagnet to move swing terminal between two contacts (NO and NC). When there is no power
applied to the inductor coil (Relay is OFF), the spring holds the swing terminal is attached to NC
contact.

Whenever required power is applied to the inductor coil, the current flowing through the coil
generates a magnetic field which is helpful to move the swing terminal and attached it to the
normally open (NO) contact. Again when power is OFF, the spring restores the swing terminal
position to NC.

Advantage of relay:

A relay takes small power to turn ON, but it can control high power devices to switch ON and OFF.
Consider an example; a relay is used t control the ceiling FAN at our home. The ceiling FAN may runs
at 230V AC and draws a current maximum of 4A. Therefore the power required is 4X230 = 920 watts.
Off course we can control AC, lights, etc., depend up on the relay ratings. Relays can be used to
control DC motors in ROBOTICs.

Interfacing relay with 8051 microcontroller:

There are many ways to interface a relay to 8051 microcontroller. But simple and easy way for
beginners is by using ULN2003/ULN2803.

What Happens When A Relay Is Directly Interfaced To 8051 Micro Controller?

Generally, a relay takes 70mA (some relays may works with 50mA) current to excite the inductor
coil. But the current sinking capability (IoL) of each port pin of 89S52/89C52/89C51 has 20mA. So
whenever 70mA current flow into the port pin may cause damage to that particular port pin. So to
avoid this problem we need a large current sinker.
MPMC UNIT 5 SIETK

Transistor is wired as a switch. which drives the relay. The transistor will be in OFF state when the
when the pin P2.0 is in LOW state. When 1 is written to P2.0 current will flow to the base of the
transistor and the relay energises.
MPMC UNIT 5 SIETK

INTERFACING ADC WITH 8051 :

ADC is the Analog to Digital converter, which converts analog data into digital format; usually it is
used to convert analog voltage into digital format. Analog signal has infinite no of values like a sine
wave or our speech, ADC converts them into particular levels or states, which can be measured in
numbers as a physical quantity. Instead of continuous conversion, ADC converts data periodically,
which is usually known as sampling rate. Telephone modem is one of the examples of ADC, which is
used for internet, it converts analog data into digital data, so that computer can understand,
because computer can only understand Digital data. The major advantage, of using ADC is that, we
noise can be efficiently eliminated from the original signal and digital signal can travel more
efficiently than analog one. That’s the reason that digital audio is very clear, while listening.

In present time there are lots of microcontrollers in market which has inbuilt ADC with one or more
channels. And by using their ADC register we can interface. When we select 8051 microcontroller
family for making any project, in which we need of an ADC conversion, then we use external ADC.
Some external ADC chips are 0803,0804,0808,0809 and there are many more.

ADC0808

One of the most commonly used ADC is ADC0808. ADC 0808 is a Successive approximation type with
8 channels i.e. it can directly access 8 single ended analog signals. The difference between ADC0804
and ADC0808 is number of analog input signals. I mean, In ADC0804 we can give only one analog
input. But in ADC0808 we can give 8 analog inputs. So if you want to interface more number of
analog sensors, please select this ADC0808.
MPMC UNIT 5 SIETK

ADC0808 is an 8 bit analog to digital converter with eight input analog channels, i.e., it can take eight
different analog inputs. The input which is to be converted to digital form can be selected by using
three address lines. The voltage reference can be set using the Vref+ and Vref- pins. The step size is
decided based on set reference value. Step size is the change in analog input to cause a unit change
in the output of ADC. The default step size is 19.53mV corresponding to 5V reference voltage.
ADC0808 needs an external clock to operate unlike ADC0804 which has an internal clock. The ADC
needs some specific control signals for its operations like start conversion and bring data to output
pins. When the conversion is complete the EOC pins goes low to indicate the end of conversion and
data ready to be picked up.

Features

• Easy interface to all microprocessors


• Operates ratio metrically or with 5 V DC or analog span adjusted voltage reference
• No zero or full-scale adjust required
• 8-channel multiplexer with address logic
• 0V to 5V input range with single 5V power supply
• Outputs meet TTL voltage level specifications
• Standard hermetic or molded 28-pin DIP package
• 28-pin molded chip carrier package

Pin Description
PIN
FUNCTION NAME
NO
1 Analog Input Pin 3 IN3

2 Analog Input Pin 4 IN4

3 Analog Input Pin 5 IN5

4 Analog Input Pin 6 IN6

5 Analog Input Pin 7 IN7

6 Start conversion; input pin; a low to high pulse is given START

7 End of conversion; output pin; goes low when the conversion is over EOC

8 Digital output bit D3

Input pin; a low to high pulse brings data to output pins from the internal registers Output
9
at end of conversion Enable

10 Clock input; to provide external clock Clock Input

11 Supply voltage; 5V Vcc

12 Positive reference voltage Vref+


MPMC UNIT 5 SIETK

PIN
FUNCTION NAME
NO
13 Ground (0v) GND

14 Digital output bit D1

15 Digital output bit D2

16 Negative reference voltage Vref-

17 Digital output bit D0

18 Digital output bit D4

19 Digital output bit D5

20 Digital output bit D6

21 Digital output bit D7

22 Address latch enable; Input pin; low to high pulse is required to latch in the address ALE

23 Address line C Address C

24 Address line B Address B

25 Address line A Address A

26 Analog Input Pin 0 IN0

27 Analog Input Pin 1 IN1

28 Analog Input Pin 2 IN2

Selection of Channel

We can select the any input channel by using the Address lines ADD A, ADD B and ADD C). We can
select the input line IN0 by keeping all three address lines (ADD A, ADD B and ADD C) Low. If we
want to select input channel IN4 then we need to keep ADD A, ADD B low and ADD C high. For
selecting all the other input channels, have a look on the given table:

ADC CHANNEL NAME ADD C ADD B ADD A


IN0 LOW LOW LOW

IN1 LOW LOW HIGH

IN2 LOW HIGH LOW


MPMC UNIT 5 SIETK

ADC CHANNEL NAME ADD C ADD B ADD A


IN3 LOW HIGH HIGH

IN4 HIGH LOW LOW

IN5 HIGH LOW HIGH

IN6 HIGH HIGH LOW

IN7 HIGH HIGH HIGH

Calculating Step Size

ADC 0808 is an 8 bit ADC i.e. it divides the voltage applied at Vref+ & Vref- into 28 i.e. 256 steps.

Step Size = (Vref+ - Vref-)/256

Suppose Vref+ is connected to Vcc i.e. 5V & Vref- is connected to the Gnd then the step size will be

Step size= (5 - 0)/256= 19.53 mv


MPMC UNIT 5 SIETK

Key or Switch bouncing & Debouncing:


Haven’t you all noticed about the fact that sometimes while pressing a remote of a TV once, the
channel shifts by 2 levels or more. Some may have even seen when a tuning button of an old FM
radio has pressed the channel advances more than the desired. What is the reason behind it? Have
anyone thought about it? The answer lies within the switches. The problem is ‘Switch Bounce‘.

Switch Bounce

Switch bounce or contact bounce or even called as chatter is a common problem associated with
mechanical switches and relays. Switch, relay contacts are made up of spring metals which are
forced to contact each other by an actuator. While they collide each other there is a possibility of
rebounding for some time before they make a stable contact. It’s similar to case of dropping a
bouncing ball or basketball. The ball keeps on bouncing till it comes to rest. This case can be taken
analogous to on/off of a switch. When the ball touches the ground analogous to on and when it rises
to certain level analogous to off of a switch. As a result of this effect there will be on/off transitions
generated as the contacts rapidly open and close.

• In short Switch bounce is a non ideal behavior which generates multiple transitions for a
single user input.

Switch Bouncing in Pull Down Connection

Switch Bouncing in Pull Up Connection


MPMC UNIT 5 SIETK

This effect is not taken as important while doing power circuits or electrical circuits but creates a
major problem while we deal with logic circuits. For example if you made a counter and it
increments not by one but by any other value when you press the input switch and what if the units
of increment is not uniform? So here we have to remove bounces. And the method to get rid of such
bounces is called Switch Debouncing.

Switch Debouncing

It can be implemented in 4 ways,

1. Hardware Debouncing
2. R-C Debouncing
3. Software Debouncing
4. Using Debouncing IC’s
5.
1. Hardware Debouncing : Hardware debouncing technique uses an S-R latch to avoid bounces in
the circuit along with the pull up resistors. S-R circuit is most effective of all debouncing approaches.
The figure below is a simple debouncing circuit which is often used.

Hardware Switch Debouncing

The circuit uses two cross coupled NAND gates which form an S-R latch, A SPDT (Single Pole Double
Throw) switch, two pull up resistors. The resistor generates a logic ‘one’ for the gates, Switch pulls
one of the inputs to ground.

If the switch is in position as shown in figure the output of the upper gate is ‘1’ irrespective of the
input of the other gate and the one created by the bottom pull up resistor which drives the lower
NAND gate to zero which in return races back to the other gate. If the switch moves back and forth
between the contacts and is suspended for a while in neither region between the terminals,the latch
maintains its state because ‘0’ from the bottom NAND gate is fed back. The switch may move
between the contacts but the latch’s output ensures it never bangs back and thus switch is bounce
free.

2. R-C Debouncing : The S-R circuit is common but the bulkiness of the circuit causes it to be used
rarely also SPDT switches are costlier than SPST (Single Pole Single Throw) switch. Another method
of debouncing is to use a R-C circuit. The basic idea behind such circuit is to use a capacitor to filter
out quick changes in the switch signal.
MPMC UNIT 5 SIETK

RC Switch Debouncing

The basic R-C circuit used for debouncing is shown above. The circuit uses two Resistors,
Capacitor, Schmidt trigger hex inverter (eg : 7414) , SPST switch.

• If the switch is open, the voltage across capacitor which is initially zero now charges to Vcc
through the R1 & R2. The voltage at Vin is high hence the output of the inverting Schmitt
trigger is low ( logic 0 )

• If the switch is closed the capacitor discharges to zero hence the voltage at Vin is ‘0’ and
output of the inverting Schmidt trigger is high ( logic 1 )

During the bouncing condition the capacitor will stop the voltage at Vin when it reaches either Vcc or
Gnd.

3. Software Debouncing : Software debouncing is another method to get rid of bounces in the
circuit. The basic principle is to sample the switch signals and filter out glitches if any. There are two
methods for software debouncing.

• Using counters
• Using shift registers
The algorithm for software debouncing is shown.
Counter Method
1. Initially set up a count value to zero
2. Using a timer set up a sampling event with a period (say 1 ms)
3. On the sample event:
4. If switch signal high then
5. Set count=0
6. Set internal switch state released
7. else
8. Increment count to a max of 10
9. end if
10. if count =10 then
11. Set internal switch state to pressed
12. end if
MPMC UNIT 5 SIETK

Shift Register Method

Similar to that of counter method.Only difference is that it uses shift register. The algorithm
assumes unsigned 8 bit reg value usually found in microcontrollers

1. Initially set up a shift register variable to xFF


2. Using a timer set up a sampling event with a period (say 1 ms)
3. On the sample event:
4. Shift the variable towards MSB
5. Set LSB to current switch value
6. if shift register value =0 then
7. Set internal switch state to pressed
8. else
9. Set internal switch state to released
10. end if
4. Switch Debouncing IC

Switch debouncing can also be done by some specialized IC. They are only a few of them and rarely
used.

• MAX6816, MAX 6817, MAX 6818


• MC 14490
• LogiSwitch LS118

Data Communication Modes


Serial communication maybe simplex, half-duplex or full duplex. simplex communication means that
data will be transmitted only in one direction while half duplex means data will be transmitted in
both directions but at one time, only one device can transmit, whereas full duplex means data may
be transmitted in both directions at one time, while one device is transmitting, it can also receive
MPMC UNIT 5 SIETK

data transmitted from other device at same time. As we have seen in unit 3 serial communication,
transmitter and receiver are configured to communicate at some data transfer rate before
communication starts. This data transfer rate or number of bits transmitted per second is called
baud rate for handling serial communication.

Parallel communication is fast but it is not applicable for long distances (for printers). Moreover it is
also expensive. Serial is not much fast as parallel communication but it can deal with transmission of
data over longer distances (for telephone line, ADC, DAC). It is also cheaper and requires less
physical wires, that’s why we use serial communication.

8051 microcontroller has a built-in serial port called UART. We can easily read and write values to
the serial port. For using serial port we simply need to configure the serial port:

• Operation mode (how many data bits we want)


• Baud rate

There are 21 Special function registers (SFR) in 8051 microcontroller and 21 unique locations are for
these 21 SFR. Each of these register is of 1 byte size. The “Serial Control” (SCON) is the SFR which is
used to configure serial port.

SM0: Serial port mode


Bit 0 of serial port mode.

SM1: Serial port mode


Bit 1 of serial port mode.

SM2: Enable multiprocessor


Enable multiprocessor communication in modes 2 and 3 (for 9 bit UART).

REN: Receiver Enable


Set/clear by software to enable/disable receive operation

TB8: Transmit bit 8


Set or clear by software. The 9 bits will be transmitted in mode 2 and 3.
TB8 = 1, a value is written to the serial port, 9th bit of data = 1.
TB8 = 0, 9th bit of data = 0, RI will not set.

RB8: Receive bit 8


Set or clear by software. The 9 bits will be received in mode 2 and 3. First eight bits are the data
received and 9th bit received will be placed in RB8.
MPMC UNIT 5 SIETK

TI: Transmit Interrupt flag


Set by hardware when a byte is transmitted completely. Now the port is free and ready to send the
next byte. This bit must be cleared by software.

RI: Receive Interrupt flag


Set by hardware when a byte has been completely received. This lets the program to know that it
needs to read the value quickly before another byte is read. This bit must be cleared by software.

CONFIGURATION SETTINGS

• Setting Operation Mode of Serial Port:

The Upper four bits are configuration bits. SM0 and SM1 set the serial mode between 0 and 3. The
four modes along with the baud rates are in the table given below. In modes 0 and 2 the baud rate is
fixed which is based on the oscillator frequency. In modes 1 and 3 the baud rate is variable which
depends on how often Timer 1 overflows.

Baud rate
SM0 SM1 MODE Description

0 0 0 shift register (Fosc./12)

0 1 1 8 bit UART Variable (set by Timer 1)

1 0 2 9 bit UART (Fosc./64)

1 1 3 9 bit UART Variable (set by Timer 1)

SM2 is a flag bit for Multiprocessor communication. When SM2 is set, the 8051 microcontroller will
set the RI (Receive Interrupt) flag whenever a byte is received (if the 9th bit received is “1”). By this
way, program knows that a byte has been received and it needs to be processed. If 9th bit is clear,
the RI flag will never be set. We will clear this RI bit so that the flag is set upon reception
of any character.

If SM2=1, RI=1 (only if 9th bit of received byte is 1)

REN is Receiver Enable bit. Set this bit if we want to receive data through serial port.

REN=1 (for serial communication)

• Setting the Baud Rate of Serial Port:


MPMC UNIT 5 SIETK

Baud Rate:

It is defined as number of bits transmitted or received per second and is usually expressed in Bits per
second bps. After setting the operation mode of serial port, the program must configure the serial
ports baud rate which is only applicable to Serial Port modes 1 and 3. For mode 0 and 2 oscillator
frequency determines the Baud Rate.

For mode 0:

Baud rate is always the oscillator frequency divided by 12. For crystal of frequency 11.059MHz, Baud
rate will be 921,583 baud.

Baud rate = (11.0592MHz/12) = 921,583 baud

For mode 2:

Baud rate is always the oscillator frequency divided by 64. For crystal of frequency 11.059MHz, Baud
rate will be 172,797 baud.

Baud rate == (11.0592MHz/64) = 172,797 baud

For mode 1 and 3:

For them, the baud rate is determined by how frequently timer 1 overflows. The more frequent
timer 1 overflows, the higher the baud rate. There are many ways due to which timer 1 overflows at
a rate that determines a baud rate. The most common method is:

• We put timer 1 in 8-bit auto-reload mode (timer mode 2).

• This 8 bit timer that allows only values of 00 to FFH loaded in timer register TH1.

• Set a reload value in TH1 that causes Timer 1 to overflow at a frequency appropriate to
generate a baud rate.

• If PCON.7 is clear (SMOD=0), then to generate a given baud rate, the value to be put in TH1
can be determined from an equation:

TH1 = 256 – ((Crystal / 384) / Baud)

• If the value to be load in TH1 by above equation is not in whole number then we can’t get
accurate baud rate. For that case, set PCON.7 (SMOD=1). The baud rate gets double and the
equation becomes:

TH1 = 256 – ((Crystal / 192) / Baud)

How to calculate baud rate for serial communication in 8051 microcontroller

Let us take an example. If we have crystal of frequency 11.059MHz and we want to configure the
serial port to 19,200 baud rate, firstly we try equation 1. By using equation 1:
MPMC UNIT 5 SIETK

TH1 = 256 – ((Crystal / 384) / Baud)


TH1 = 256 – ((11.059MHz/ 384) / 19200 )
TH1 = 256 – 1.5 = 254.5

By setting 254 and 255, we don’t achieve accurate baud rate so we need to set PCON.7 (SMOD=1).
This doubles the baud rate and we will use equation 2 for determining TH1 value. By using equation
2:

TH1 = 256 – ((Crystal / 192) / Baud)


TH1 = 256 – ((11.059MHz/ 192) / 19200)
TH1 = 256 – 3 = 253

Thus by setting TH1 to 253 we get correct frequency for 19,200 baud rate.

Das könnte Ihnen auch gefallen