You are on page 1of 56

Unit 2

input / output port interfacing- I

The 8051 has four important ports. Port 0, Port 1, Port 2


and Port 3. These ports allow the microcontroller to
connect with the outside world. The four ports of 8051
microcontrollers have certain specific functions and
corresponding features.
features of the four ports of 8051
• Each port has 8 pins. Thus the four ports jointly
comprise 32 pins.
• All ports are bidirectional.
• They are constructed with a D type output latch. They
have output drivers and input buffers.
• We can modify their functions using software and
hardware that they connect to.
• All the ports are configured as input ports on Reset.
• To configure ports as an input port 1 must be written
to that port
• To configure it as an output port 0 must be written to
it.
Port 0 structure in 8051
features of Port 0 in 8051
• Address is 80H
• Construction: Port 0 has a D-type latch,
unidirectional buffer, and 2 FETs at each pin. It does
not have an internal pull-up resistor. An external pull-
up resistor is needed when Port 0 is defined as an
output port.
• Port 0 of the 8051 has two main functions: To be
used as a simple input-output port and to access
external memory in conjunction with Port 2.
Port 1 structure in 8051
Features of Port 1:
• Address is 90H
• Construction: Port 1 has one D latch, two unidirectional
buffers, 1 FET, and one internal pull-up resistor at each pin.
• It has only one function – to act as an Input-Output port.
The function of Port 1 – I/O port:
• When Port 1 is functioning in the capacity of an input port,
a digital „1‟ (FFH) is written to the latch. At 90H. This turns
off the transistor, and the pin floats in a high impedance
state. Consequently, it connects to the input buffer.
• When Port 1 is functioning in the capacity of an output
port, the latch is given a „LOW‟ signal (00H). This turns the
FER (Field Effect Transistor) o. The pull-up resistor is
OFF, and the port is used as an output port.
Port 2 in 8051
Features of Port 2
• Address is 10H
• Construction: Port 2 has a D type latch, 1 FET, an internal
pull-up resistor, two unidirectional buffers, and a Control
Logic block.
• Its main functions are kind of similar to those of Port 0. It
can be used as an input-output port. And can access
external memory in conjunction with Port 0.
Functions of Port 2
• Quite similar to Port 0. The only difference here is that in
Port 2, we use one FET with an internal pull-up resistor
instead of the two FETs we saw in Port 0.
Port 3 in 8051
Features of Port 3
• Address is B0H
• Construction: The third Port of 8051 has a D-type latch. In
addition to that, it has three unidirectional buffers. A FET
with an internal pull-up resistor. Additionally, it also has a
NAND gate connected to the FET.
• Port 3 performs two main functions, as we will see below.
Functions of Port 3
• Just like Port 2, Port 3 can function as an input-output port.
Embedded C Program To Generate Square
Wave In 8051
#include <reg51.h> void Msdelay(unsigned int times)
sbit pin = P1^0; {
Void main ( ) unsigned int i,j;
{ for (i=0; i<itimes; i++);
While (1) for (j=0; j<itimes; j++);
{
Pin=1; }
Msdelay(200);
pin=0;
Msdelay(200);
}
}
Write Embedded C program for 8051 to send values 0-F
to port 0.
#include <reg51.h> void T1delay(void)
Void T1delay(void); {
Void main(void) TMOD=0x10;
{ TL1=0x00;
unsigned char i; TH1=0x00;
for(i=0; i<=15; i++) TR1=1;
P0=i; while(TF1==0);
T1delay(); TR1=0;
} TF1=0;
}
LED interfacing with 8051
Write an embedded C program to display hex counter on LEDs
connected to port 2.
#include<reg51.h> unsigned int i,j;
void delay(); for(i=0;i< 500;i++)
void main() for(j=0;j< 1000;j++);
{ }
unsigned int count;
while(1){
for(count=0;count<=15;count++)
P1=count;
delay();
}
}
void delay()
{
Switches and key interface
Types of Switches
1. DIP switch : A DIP switch is a manual electric switch that is packaged
with others in a group in a standard dual in-line package (DIP). The term
may refer to each individual switch, or to the unit as a whole. This type of
switch is designed to be used on a printed circuit board along with other
electronic components and is commonly used to customize the behavior of
an electronic device for specific situations.
2. Push-button Key: A push button switch is a mechanical device used to
control an electrical circuit in which the operator manually presses a
button to actuate an internal switching mechanism. They come in a variety
of shapes, sizes, and configurations, depending on the design requirements
What is Key debouncing?
• Debouncing is removing unwanted input noise from buttons,
switches or other user input. Debouncing prevents extra
activations or slow functions from triggering too often.
Debouncing is used in hardware switches, programs and
websites.
Interfacing of switches and keypad with 8051
microcontroller
• Fig. shows how to interface the 4 X 4 matrix keypad to two ports in
microcontroller. The rows are connected to an output port and the columns
are connected to an input port.

• To detect a pressed key, the microcontroller grounds all rows by providing


0 to the output latch, and then it reads the columns. If the data read from the
columns is D3-D0=1111, no key has been pressed and the process
continues until a key press is detected. However, if one of the column bits
has a zero, this means that a key press has occurred. For example, if D3-
D0=1101, this means that a key in the D1 column has been pressed.

• After a key press is detected, the microcontroller will go through the


process of identifying the key. Starting with the top row, the
microcontroller grounds it by providing a low to row D0 only; then it reads
the columns.

• If the data read is all 1s, no key in that row is activated and the process is
moved to the next row. It grounds the next row, reads the columns, and
checks for any zero. This process continues until the row is identified. After
identification of the row in which the key has been pressed, the next task is
to find out which column the pressed key belongs to.
Interfacing of 4x4 matrix keyboard
7 segment display
• A seven segment display is the most basic electronic display
device that can display digits from 0-9. The most common
configuration has an array of eight LEDs arranged in a special
pattern to display these digits. They are laid out as a squared-
off figure „8‟. Every LED is assigned a name from „a‟ to „h‟
and is identified by its name. Seven LEDs „a‟ to „g‟ are used to
display the numerals while eighth LED „h‟ is used to display
the dot/decimal.
7 segment display
• LEDs are often grouped to form seven-segment display. The below Fig.
shows the front of a seven segment display. It contains seven LEDs (A, B,
C, D, E, F and G) shaped in a figure of 8. Each LED is called a segment. If
a particular LED is forward biased, that LED or segment will light and
produces a bar of light. By forward biasing various combinations of seven
LEDs, it is possible to display any number from 0 to 9. For example, if
LEDs A, B, C, D and G are lit (by forward biasing them), the display will
show the number 3. Similarly, if LEDs C, D, E, F, A and G are lit, the
display will show the number 6. To get the number 0, all segments except
G are lit.
• The Below Fig. shows the schematic diagram of seven-segment display.
External series resistors are included to limit currents to safe levels. Note
that the anodes of all seven LEDs are connected to a common positive
voltage source of +5 V. This arrangement is known as common-anode
type. In order to light a particular LED, say A, we ground the point A in
Fig. It forward biases the LED A which will be lit.
Common Anode Display
The Common Anode (CA) – In the common anode display, all the
anode connections of the LED segments are joined together to logic
“1”. The individual segments are illuminated by applying a ground,
logic “0” or “LOW” signal via a suitable current limiting resistor to the
Cathode of the particular segment (a-g)
The Common Cathode (CC) – In the common cathode display,
all the cathode connections of the LED segments are joined
together to logic “0” or ground. The individual segments are
illuminated by application of a “HIGH”, or logic “1” signal via a
current limiting resistor to forward bias the individual Anode
terminals (a-g).
ADC interfacing
• Analogue to Digital Converter, or ADC, is a data converter
which allows digital circuits to interface with the real world by
encoding an analogue signal into a binary code.
• Main Types of ADC Converters
There are really five major types of ADCs in use today:
• Successive Approximation (SAR) ADC
• Delta-sigma (ΔΣ) ADC
• Dual Slope ADC
• Pipelined ADC
• Flash ADC
Features : ADC0808/ADC0809 :
• Easy Interface to All Microprocessors
• Operates with 5 VDC
• No Zero or Full-Scale Adjust Required
• 8-Channel Multiplexer with Address Logic
• 0V to VCC Input Range
• Outputs meet TTL Voltage Level Specifications
• ADC0808 Equivalent to MM74C949
• ADC0809 Equivalent to MM74C949-1
• 28-pin molded chip carrier package
Types of ADC
1. Flash Type ADC
• Also called the parallel A/D converter, this circuit is the simplest to understand.
It is formed of a series of comparators, each one comparing the input signal to
a unique reference voltage. The comparator outputs connect to the inputs of a
priority encoder circuit, which then produces a binary output.
• A flash ADC (also known as a direct-conversion ADC) is a
type of analog-to-digital converter that uses a linear voltage
ladder with a comparator at each "rung" of the ladder to
compare the input voltage to successive reference voltages.
• Often these reference ladders are constructed of many
resistors; however, modern implementations show that
capacitive voltage division is also possible.
• The output of these comparators is generally fed into a digital
encoder, which converts the inputs into a binary value.
2. Successive Approximation Type ADC
It consists of a successive approximation register (SAR), DAC and comparator.
The output of SAR is given to n-bit DAC. The equivalent analog output voltage
of DAC, VD is applied to the non-inverting input of the comparator. The second
input to the comparator is the unknown analog input voltage VA. The output of
the comparator is used to activate the successive approximation logic of SAR.
When the start command is applied, the SAR sets the MSB to logic 1 and other
bits are made logic 0, so that the trial code becomes 1000.
Advantages:
• 1 Conversion time is very small.
• 2 Conversion time is constant and independent of the
amplitude of the analog input signal VA.

Disadvantages:
• 1 Circuit is complex.
• 2 The conversion time is more compared to flash type ADC.
Interfacing of ADC to 8051
DAC
• Microcontroller are used in wide variety of applications like
for measuring and control of physical quantity like
temperature, pressure, speed, distance, etc.
• In these systems microcontroller generates output which is in
digital form but the controlling system requires analog signal
as they don't accept digital data thus making it necessary to
use DAC which converts digital data into equivalent
analog voltage.
• In the figure shown, we use 8-bit DAC 0808. This IC converts
digital data into equivalent analog Current. Hence we require
an I to V converter to convert this current into equivalent
voltage.
• According to theory of DAC Equivalent analog output is
given as:
Interfacing Diagram
• Types of Digital to Analog Converter (DAC)
• There are basically four types of Digital to Analog
Converter circuits namely:
• Binary Weighted Resistor D/A Converter Circuit
• Binary ladder or R–2R ladder D/A Converter
Circuit
• Segmented DAC
• Delta-Sigma DAC
• Advantages of Digital to Analog Converter
(DAC)
• The advantages of Digital to Analog Converter
are:
• Weighted Resistor Circuit is the fastest
converting circuit compared to other
methods.
• High resolution and accuracy can be achieved.
• DAC’s are simple to implement.
Disadvantages of Digital to Analog Converter (DAC)
The disadvantages of DAC are:
• Voltage levels must be exactly the same for all
inputs in Weighted Resistors DAC. E.g. 4-bit
Converter requires 4 resistors.
• Binary weighted Resistor circuit that require Op-
Amps are expensive.
• Power dissipation of Binary weighted Resistors
Circuit is very high.
• In R-2R Ladder converters, Delay is caused as the
circuit needs switching based on the inputs.
• Gain Error, Offset Error, Non-linearity is commonly
caused by Resistors used in the circuit.
Draw an interacting diagram of DAC with 8051 and write an embedded C
program for generation of Triangular having frequency of 50Hz.

#include<reg51.h> }
void delay(unsigned int); }
void main(void) }
{
unsigned int x;
For(;;) void delay(unsigned int time)
{ {
for(x=0;x<250;x++) unsigned int i,j;
{ for(i=0;i<time;i++)
P1=x; for(j=0;j<1275;j++);
delay(100); }
}

for(x=250;x>0;x–)
{
P1=x;
delay(100);
Stepper motor
• The Stepper motor is a brushless DC motor that divides the full rotation
angle of 360° into a number of equal steps.
• The motor is rotated by applying a certain sequence of control signals. The
speed of rotation can be changed by changing the rate at which the control
signals are applied.
• Various stepper motors with different step angles and torque ratings are
available in the market.
• A microcontroller can be used to apply different control signals to the
motor to make it rotate according to the need of the application.
• For more information about Stepper Motor and how to use it, refer to the
topic Stepper Motor in the sensors and modules section.
Interfacing of 8051 with stepper motor
Write an embedded C program to rotate the stepper motor in
clockwise direction continuously with highest delay generated
using timer 0 mode1 .
#include< reg51.h>
void delay(void); P2 = 0x03;
Void main() delay();
{ P2 = 0x01;
while( 1) delay();
{ }
for(i=0; i<12; i++) }
{ void delay(void)
P2 = 0x09; {
delay(); TMOD=0x01;
P2 = 0x08; TL0=0x00;
delay(); TH0=0x00;
P2 = 0x0C; TR0=1;
delay(); While(TF0==0);
P2 = 0x04; TR0=0;
delay(); TF0=0;
P2 = 0x06; }
delay();
P2 = 0x02;
delay();
Relay
• A relay is an electronically operated switch that is remotely
activated by an electromagnet which pulls a set of contacts to
either make or break a circuit.
• Electrical relays are commonly used for switching signals,
radio frequencies, high current circuits when using a lower
current circuit, and loads such as resistive, motor, lamp,
inductive, and capacitive applications.
• This is helpful when an in-line switch or existing circuit does
not have the capacity to handle the required current.
Interfacing Relay with 8051 microcontroller
#include<reg51.h> }
sbit relay_pin = P2^0; while(1);
void Delay_ms(int); }
void main() void Delay_ms(int k)
{ {
do int j;
{ int i;
relay_pin = 0; //Relay ON for(i=0;i<k;i++)
Delay_ms(1000); for(j=0;j<100;j++)
relay_pin = 1; //Relay OFF }
Delay_ms(1000);
Buzzer
• An audio signaling device like a beeper or buzzer may be
electromechanical or piezoelectric or mechanical type.
• The main function of this is to convert the signal from audio to
sound. Generally, it is powered through DC voltage and used
in timers, alarm devices, printers, alarms, computers, etc.
• Based on the various designs, it can generate different sounds
like alarm, music, bell & siren.
Interfacing Buzzer with 8051 microcontroller
#include "REG51.h" for(i=0;i<4500;i++); // Delay
#define buz P1 buz=0x00; // OFF Buzzer
sbit SW=P3^0; for(i=0;i<4500;i++); // Delay
long int i; }
void main() }
{ }
while(1) }
{
if (SW==0)
{ for(i=0;i<=9000;i++);
if(SW==0)
{
while(SW==0);
buz=0x01; // ON Buzzer
Optoisolators
• optocoupler is also called optoisolator, photocoupler and
optical isolator. It is used to provide isolation between two
electrical circuits.
• It is a electrical component which is used to transmit input
signal by using light energy signals. It provides electrical
coupling between input and output through light waves.
• Its main purpose is to avoid changing in voltages at output
side to appearing at input side. Higher voltage fluctuations
may damage input side electrical components.
Interfacing Optoisolator with 8051 microcontroller
#include<reg51.h> void Delay_ms(int k)
sbit opto= P2^0; {
void Delay_ms(int); int j;
void main() int i;
{ for(i=0;i<k;i++)
While(1) for(j=0;j<100;j++)
{ }
opto = 0; //ON Delay_ms(1000);
opto = 1; //OFF
Delay_ms(1000);
}
while(1);
}
Data Acquisition System
• A data acquisition system is a system that comprises sensors,
measurement devices, and a computer. A data acquisition system
is used for processing acquired data, which involves collecting the
information required to understand electrical or physical
phenomena.
Types of Data Acquisition Systems
Data acquisition systems can be classified into the following two types.
• Analog Data Acquisition Systems
• Digital Data Acquisition Systems

Analog Data Acquisition Systems


• The data acquisition systems, which can be operated with analog signals
are known as analog data acquisition systems. Following are the
blocks of analog data acquisition systems.
• Transducer − It converts physical quantities into electrical signals.
• Signal conditioner − It performs the functions like amplification and
selection of desired portion of the signal.
• Display device − It displays the input signals for monitoring purpose.
• Graphic recording instruments − These can be used to make the
record of input data permanently.
• Magnetic tape instrumentation − It is used for acquiring, storing &
reproducing of input data.
Digital Data Acquisition Systems
The data acquisition systems, which can be operated with digital signals are
known as digital data acquisition systems. So, they use digital
components for storing or displaying the information.
• Mainly, the following operations take place in digital data acquisition.
• Acquisition of analog signals
• Conversion of analog signals into digital signals or digital data
• Processing of digital signals or digital data
Following are the blocks of Digital data acquisition systems.
• Transducer − It converts physical quantities into electrical signals.
• Signal conditioner − It performs the functions like amplification and
selection of desired portion of the signal.
• Multiplexer − connects one of the multiple inputs to output. So, it acts
as parallel to serial converter.
• Analog to Digital Converter − It converts the analog input into its
equivalent digital output.
• Display device − It displays the data in digital format.
• Digital Recorder − It is used to record the data in digital format.
Interfacing of DAS with 8051 microcontroller
Flow chart

You might also like