0% found this document useful (0 votes)
33 views

Chapter Three

Uploaded by

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

Chapter Three

Uploaded by

Yoomif Tube
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

CHAPTER TREE

Basic Input/output Operations


All 8051 microcontrollers have 4 I/O ports each comprising 8 bits which can be configured as inputs
or outputs. Accordingly, in total 32 input/output pins enabling the microcontroller to be connected
peripheral devices are available for use
Pin configuration, i.e. whether it is to be configured as an input (1) or an output (0), depends on its
logic state. In order to configure a microcontroller pin as an input, it is necessary to apply a logic zero
(0) to appropriate I/O port bit. In this case, voltage level on appropriate pin will be 0. Similarly, in
order to configure a microcontroller pin as an input, it is necessary to apply a logic one (1) to
appropriate port. In this case, voltage level on appropriate pin will be 5V (as is the case with any TTL
input). This may seem confusing but don't lose your patience. It all becomes clear after studying
simple electronic circuits connected to an I/O pin.
Each pin of I/O ports Internal CPU bus communicate with CPU D latch store the value of this pin D
latch is controlled by “Write to latch”
Write to latch=1:write data into the D latch
2 Tristate buffer:
TB1: controlled by “Read pin” Read pin=1:really read the data present at the pin
TB2: controlled by “Read latch” Read latch=1:read value from internal latch A transistor M1 gate
Gate=0: open
Gate=1: close

Input/ Output (I/O) pin


Figure above illustrates a simplified schematic of all circuits within the microcontroller-connected one
of its pins. It refers to all the pins except those of the P0 port, which do not have pull-up resistors
built-in.
Output pin A logic zero (0) is applied to a bit of the P register. The output FE transistor is turned on,
thus connecting the appropriate pin to ground.

Input pin A logic one (1) is applied to a bit of the P register. The output FE transistor is turned off and
the appropriate pin remains connected to the power supply voltage over a pull-up resistor of high
resistance.
Logic state (voltage) of any pin can be changed or read at any moment. A logic zero (0) and logic
one (1) are not equal. A logic one (0) represents a short circuit to ground. Such a pin acts as an
output.
A logic one (1) is “loosely “connected to the power supply voltage over a resistor of high resistance.
Since this voltage can be easily “reduced” by an external signal, such a pin acts as an input.
Port 0
The P0 port is characterized by two functions. If external memory is used then the lower
address byte (addresses A0-A7) is applied on it. Otherwise, all bits of this port are
configured as inputs/outputs

The other function is expressed when it is configured as an output. Unlike other ports consisting of
pins with built-in pull-up resistor connected by its end to 5 V power supply, pins of this port have this
resistor left out. This apparently small difference has its consequences

If any pin of this port is configured as an input then it acts as if it “floats”. Such an input has unlimited
input resistance and in determined potential.
When the pin is configured as an output, it acts as an “open drain”. By applying logic 0 to a port bit,
the appropriate pin will be connected to ground (0V). By applying logic 1, the external output will
keep on “floating”. In order to apply logic 1 (5V) on this output pin, it is necessary to built in an
external pull-up resistor.
Port 1 P1 is a true I/O port, because it doesn't have any alternative functions as is the case with P0,
but can be cofigured as general I/O only. It has a pull-up resistor built-in and is completely
compatible with TTL circuits.
Port 2 P2 acts similarly to P0 when external memory is used. Pins of this port occupy addresses
intended for external memory chip. This time it is about the higher address byte with addresses A8-
A15. When no memory added, this port can used as a general input/output port showing features
similar to P1.
Port 3
All port pins can be used as general I/O, but they also have an alternative function. In order to use
these alternative functions, a logic one (1) must be applied to appropriate bit of the P3 register. In
terms of hardware, this port is similar to P0, with the difference that its pins have a pull-up resistor
built-in.

In the other way


Pins of I/O Port
The 8051 has four I/O ports
Port 0 (pins 32-39):P0(P0.0~P0.7)
Port 1(pins 1-8) :P1(P1.0~P1.7)
Port 2(pins 21-28):P2(P2.0~P2.7)
Port 3(pins 10-17):P3(P3.0~P3.7) Each port has 8 pins.
Named P0.X (X=0,1,...,7), P1.X, P2.X, P3.X
Ex:P0.0 is the bit 0(LSB)of P0
Ex:P0.7 is the bit 7(MSB)of P0
These 8 bits form a byte.
Each port can be used as input or output (bi-direction).
I/O Port Programming
Port 1(pins 1-8)
Port 1 is denoted by : P1. P1.0 ~ P1.7 use P1 as examples to show the operations on ports. P1 as
an output port (i.e., write CPU data to the external pin) P1 as an input port (i.e., read pin data into
CPU bus)
A Pin of Port 1

8051 IC
Hardware Structure of I/O Pin
 Each pin of I/O ports Internal CPU bus:communicate with CPU D latch store the value
of this pin
 D latch is controlled by “Write to latch” Write to latch=1:write data into the D latch 2
Tri-state buffer:
 TB1: controlled by “Read pin” Read pin=1:really read the data present at the pin
 TB2: controlled by “Read latch” Read latch=1:read value from internal latch A
transistor M1 gate
 Gate=0: open
 Gate=1: close
Tristate Buffer
Writing “0” to Output Pin P1.X
Reading “High” at Input Pin

Reading “Low” at Input Pin

Port 1 as Input Read from Port)


In order to make P1 an input, the port must be programmed by writing 1 to all the bit.
MOV A,#0FFH ;A=11111111B
MOV P1,A ;make P1 an input port BACK: MOV A,P1 ;get data from P0 MOV P2,A ;send data
to P2 SJMP BACK
To be an input port, P0, P1, P2 and P3 have similar methods.
Reading the Latch
Read-modify-write Feature
Read-modify-write Instructions
This features combines 3 actions in a single instruction:
1. CPU reads the latch of the port
2. CPU perform the operation
3. Modifying the latch
4. Writing to the pin Note that 8 pins of P1 work independently.

Port – 0
Port0

Port 0 with Pull-Up Resistors


Port – 1

Port – 2
Port 2

Port – 3
Port-3

The register used to access the stack is called SP (stack pointer) register.
The stack pointer in the 8051 is only 8 bits wide, which means that it can take value 00 to FFH.
When 8051 powered up, the SP register contains value 07.

Example:
MOV R6,#25H
MOV R1,#12H
MOV R4,#0F3H
PUSH 6
PUSH 1
PUSH 4
Difference between Microprocessor and Microcontroller
1. Key difference in both of them is presence of external peripheral, where microcontrollers have
RAM, ROM, EEPROM embedded in it while have to use external circuits in case of microprocessors.
2. As all the peripheral of microcontroller are on single chip it is compact while microprocessor or is
bulky.
3. Microcontrollers are mad by using complementary metal oxide semiconductor technology so they
are far cheaper than microprocessors. In addition, the applications made with microcontrollers are
cheaper because they need lesser external components, while the overall cost of systems made with
microprocessors are high because of the high number of external components required for such
systems.
4. Processing speed of microcontrollers is about 8 MHz to 50 MHz, but in contrary processing speed
of general microprocessors is above 1 GHz so it works much faster than microcontrollers do.
5. Generally microcontrollers have power saving system, like idle mode or power saving mode so
overall it uses less power and since external components are low overall consumption of power is
less. While in microprocessors generally, there is no power saving system and many external
components are use with it, so its power consumption is high in comparison with microcontrollers.
6. Microcontrollers are compact so it makes them favorable and efficient system for small products
and applications while microprocessors are bulky so they are preferred for larger applications.
7. Tasks performed by microcontrollers are limited and generally less complex. While task performed
by microprocessors are software development, Game development, website, documents making etc.
which are generally more complex so require more memory and speed so that is why external ROM,
RAM are used with it.
8. Microcontrollers are base on Harvard architecture where program memory and data memory are
separate while microprocessors are base on von Neumann model where program and data are
stored in same memory module.
Interfacing the input/output

The interfacing of a 20-character 2-line LCD module with the 8051 shown. The data lines are
connect to the port 1 of 8051 and control lines RS, R/W and E driven by 3.2, 3.3 and 3.4 lines of port
3, respectively .The voltage at V EE pin adjusted by a potentiometer to adjust the contrast of LCD.

LCD interface with 8051


To display a message on LCD module, it is necessary to initialize it by writing series of command
codes in the command in the register in the proper sequence .The initialization cursor automatically
after writing a character. After initialization, we can write data to either DD RAM or CG RAM. Both
RAMs read / write RAMS and have unique addresses to access each location. To write data in any
RAM we have to set the address for that RAM by issuing proper commands. Then we can write data
into it by activating write cycle .To activate write cycle we have to make R/W signal low, RS signal
high, send data on port A and apply high to low pulse of at least 450 ns of duration on E pin of the
module .The DD RAM stores the character in their ASCII code whereas CG RAM stores the
character in its internally generated character code .Let us see the 805 assembly language program
to display ‘WELCOME’ message on the LC module .Before sensing command or data it is necessary
to check busy flag , i.e. whether LCD us reading or not .

Main Routine
DAC and ADC for interfacing purpose
Most physical variables are analog in nature and can take on any value within a
continuous range of values. Examples include temperature, pressure, light intensity,
audio signals, position, rotational speed, and flow rate. Digital systems perform all of
their internal operations using digital circuitry and digital operations. Any information
that has to be inputted to a digital system must first be put into digital form. Similarly,
the outputs from a digital system are always in digital form
Example:-Transducer
The physical variable is normally a nonelectrical quantity. A transducer is a device
that converts the physical variable to an electrical variable. Some common
transducers include thermistors, photocells, photodiodes, flow meters, pressure
transducers, and tachometers. The electrical output of the transducer is an analog
current or voltage that is proportional to the physical variable it is monitoring. For
example, the physical variable could be the temperature of water. Let’s say that the
water temperature varies from 80 to 1500 F and that a thermistor and its associated
circuitry convert this water temperature to a voltage ranging from 800 to 1500mV.
Note that the transducer’s output is directly proportional to temperature; such that
each 10 F produces a 10mV output. Analog-to digital converter (ADC) and digital-to-
converter (DAC) used to interface a computer to the analog world so that the
computer can monitor and control a physical variable.
Analog-to-Digital Converter (ADC)
The transducer’s electrical analog output serves as the analog input to the ADC. The
ADC converts this analog input to a digital output. This digital output consists of a
number of bits that represent the value of the analog input. For example, the ADC
might convert the transducer’s 800to 1500-mV analog values to binary values ranging
from 01010000 (80) to 10010110 (150). Note that the binary output from the ADC is
proportional to the analog input voltages so that each unit of the digital output
represents 10mV.
The digital representation of the analog vales is transmitted from the ADC to the
digital computer, which stores the digital value and processes it according to a
program of instructions that it is executing.
Digital-to-Analog Converter (DAC)
This digital output from the computer is connected to a DAC, which converts it to a
proportional analog voltage or current. For example, the computer might produce a
digital output ranging from 0000000 to 11111111, which the DAC converts to a
voltage ranging from 0 to 10V.
An analog-to-digital converter takes an analog input voltage and after a certain
amount of time produces a digital output code which represents the analog input. The
A/D conversion process is generally more complex and time-consuming than the D/A
process. The techniques that are used provide and insight into what factors
determine an ADCs performance
Several important types of ADC utilize a DAC as part of their circuitry.

The basic operation of ADCs of this type consists of the following steps:
1.The START COMMAND pulse initiates the operation.
2. At a rate determined by the clock, the control unit continually modifies the binary
number that is stored in the register.
3. The binary number in the register is convert to an analog voltage, VAX, by the
DAC.

Fig. Basic diagram of ADC


Digital Systems and Computer Organization
4. The comparator compares VAX with the analog input VA. As long as VAX < VA1
the comparator output stays HIGH. When VAX exceeds VA by at least an amount =
VT (threshold voltage), the comparator output goes LOW and stops the process of
modifying the register number. At this point, VAX is a close approximation to VA. The
digital number in the register, which is the digital equivalent of VAX, is also the
approximate digital equivalent of VA1 within the resolution and accuracy of the
system.
5. The control logic activates the end-of-conversion signal, EOC, when the
conversion is complete.

You might also like