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

Chapter 4

The document discusses digital I/O pins on microcontrollers. It explains that microcontrollers have digital I/O pins that can be directly connected to hardware to monitor and control it. These pins are grouped into ports that can be configured as inputs, outputs, or bidirectional. The behavior of pins is controlled by data direction, port, and pin registers. Pull resistors are important for digital inputs to define the pin state when not driven. Interrupts allow the microcontroller to react to external events without constantly polling input pins.

Uploaded by

Aman Ethio Lij
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Chapter 4

The document discusses digital I/O pins on microcontrollers. It explains that microcontrollers have digital I/O pins that can be directly connected to hardware to monitor and control it. These pins are grouped into ports that can be configured as inputs, outputs, or bidirectional. The behavior of pins is controlled by data direction, port, and pin registers. Pull resistors are important for digital inputs to define the pin state when not driven. Interrupts allow the microcontroller to react to external events without constantly polling input pins.

Uploaded by

Aman Ethio Lij
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Micro-link college

Computer Engineering Department

I/O Ports
Digital I/O
 the ability to directly monitor and control hardware
 practically all microcontrollers have at least 1-2 digital
I/O pins that can be directly connected to hardware
(within the electrical limits of the controller).
 you can find 8-32 pins on most controllers, and some
even have a lot more
 I/O pins are generally grouped into ports of 8 pins,
which can be accessed with a single byte access. Pins
can either be input only, output only, or —most
commonly,— bidirectional, that is, capable of both
input and output.
 Apart from their digital I/O capabilities, most pins
have one or more alternate functions to save pins and
keep the chip small.
Digital I/O …
All other modules of the controller which require
I/O pins, like the analog module or the timer, use
in fact alternate functions of the digital I/O pins.
The application programmer can select which
function should be used for the pin by enabling
the functionality within the appropriate module.
Of course, if a pin is used for the analog module,
then it is lost for digital I/O and vice versa, so the
hardware designer must choose carefully which
pins to use for which functions.
Digital I/O …
 When we read the voltage level of a pin with a
voltmeter (with respect to GND), we will see an
analog voltage. However, the microcontroller digitizes
this voltage by mapping it to one of two states, logical
0 or logical 1.
 So when we talk about digital I/O, we mean that the
value of the pin, from the controller’s perspective, is
either 1 or 0.
 Note that in positive-logic, 1 corresponds to the
“high” state (the more positive resp. less negative
state) of the line, whereas 0 corresponds to the “low”
state (the less positive resp. more negative state).
 In negative-logic, 1 corresponds to “low” and 0 to
“high”. Microcontrollers generally use positive-logic.
Digital I/O …
Three registers control the behavior of the pins:
1. Data Direction Register (DDR): Each bidirectional
port has its own DDR, which contains one bit for
each pin of the port. The functionality of a pin
(input or output) is determined by clearing or
setting its bit in the DDR. Different pins of a port
may be configured differently, so it is perfectly
okay to have three pins configured to output and
use the other five as inputs. After a reset, the
DDR bits are generally initialized to input.
Reading the register returns its value.
Digital I/O …
2. Port Register (PORT): This register is used to control
the voltage level of output pins. Assuming a pin has
been configured to output, then if its bit in the PORT
register is set, the pin will be high; if the bit is
cleared, the pin will be low. To avoid overwriting the
other bits in the port when setting a particular bit, it
is generally best to use the controller’s bit
operations.
 For output pins, reading the register returns the value
you have written. For input pins, the functionality
depends on the controller. Some controllers allow you
to read the state of input pins through the port
register. Other controllers, e.g. the ATmega16, use the
port bits for other purposes if the corresponding pins
are set to input, so here you will read back the value
you have written to the register.
Digital I/O …
3. Port Input Register (PIN): The PIN register is
generally read-only and contains the current state
(high or low) of all pins, whether they are configured
as output or as input. It is used to read the state of
input pins, but it can also be used to read the state
of output pins to verify that the output was taken
over correctly. A write to this register generally has
no effect.
Digital Input (never float it!!!)
 Is used whenever the monitored signal should be
interpreted digitally, that is, when it only changes
between the two states “high” (corresponding to logic 1)
and “low” (corresponding to 0).
 Whether a given signal should be interpreted as high or
low depends on its voltage level, which must conform to
the controller’s specifications, which in turn depend on
the operating voltage of the controller.
 For example, the operating voltage VCC of the ATmega16
must be within the interval [4.5, 5.5] V, its input low
voltage must be within [-0.5, 0.2VCC] V, and its input high
voltage must be within [0.6VCC, VCC+0.5] V. This leaves
the interval (0.2VCC, 0.6VCC) within which the signal is
said to be undefined.
Digital Input …
Pull Resistors: Many controllers integrate pull
resistors into their input circuitry. Most of the
time, they provide pull-up resistors, some
controllers also offer pull down resistors .
The task of the pull resistor is to connect the input
pin to a defined voltage if it is not driven by the
external hardware. Pull resistors are controlled via
a register, where they can be enabled or disabled
for each pin independently.
The ATmega16, for example, uses the PORT
register bits of input pins for controlling their pull
resistors. Other controllers provide dedicated
registers for this purpose.
Digital Input …
When using simple mechanical switches, like DIP
switches or buttons. Here, the input pin is
connected to a defined value as long as the switch
is closed, but left floating (that is, unconnected
and at an undefined voltage level) whenever the
switch is open.
Since floating pins are A Bad Thing (they are very
prone to noise!), a pull resistor must be used to
set the pin to a defined level while the switch is
open,
Digital Input …
If current flows from the controller to the external
circuit (pull up), the input is called a source input
because it provides current. If current flows from
the hardware into the controller(pull down), this is
called a sink input.
 Controllers are very particular about the amount
of current they can source and sink, and exceeding
the bounds stated in the datasheet may destroy
the pin and even the controller itself.
Digital Output
The digital output functionality is used to set
output pins to given voltage levels. The levels
corresponding to high and low are again specified
by the controller and depend on the controller’s
operating voltage. For the ATmega16 at VCC = 5V,
the maximum output low voltage is 0.7 V, and the
minimum output high voltage is 4.2 V.
Digital Output …
 Whenever the DDR of a pin is set to output, the
controller drives the pin according to the value given
in the PORT register. An output pin generally has to
sink or source current, so we can again distinguish
between a sink output and a source output.
 Output pins are more critical than input pins in the
sense that they heavily depend on external current
protection. After all, you could connect an output pin
directly to GND and then set it to 1, thus creating a
short-circuit.
 Although controllers tend to tolerate such short-
circuits for a brief amount of time (generally less than
a second), a short will eventually destroy the
controller. So the hardware designer must ensure that
the external hardware cannot produce a short-circuit.
Analog I/O
Useful for situations in which the actual voltage of
the line transports information, for example when
using a photo transistor as light sensor: The
voltage drop it produces at its output is directly
proportional to the amount of light falling on the
transistor, and to adequately evaluate the sensor
output, the microcontroller must deal with the
analog value.
The microcontroller is inherently digital, so we
need appropriate ways of converting analog
signals into the digital world and back again. This
problem is addressed by the analog module of the
microcontroller. ADC!!!
Pin diagram of 8085
Pin description of 8085
Pin description of 8085 …
Pin description of 8085 …
Pin description of 8085 …
Pin description of 8085 …
Pin description of 8085 …
Interrupts
Microcontrollers tend to be deployed in systems
that have to react to events. Events signify state
changes in the controlled system and generally
require some sort of reaction by the
microcontroller. Reactions range from simple
responses like incrementing a counter whenever a
workpiece crosses a photoelectric barrier on the
conveyor belt to time-critical measures like
shutting down the system if someone reaches into
the working area of a machine.
Interrupts …
Assuming that the controller can observe the
event, that is, there is an input line that changes
its state to indicate the event, there is still the
question of how the controller should monitor the
input line to ensure a proper and timely reaction.
It is of course possible to simply poll the input
signal, that is, to periodically check for state
changes. However, this polling has its drawbacks:
Not only does it unnecessarily waste processor
time if the event only occurs infrequently, it is also
hard to modify or extend.
Interrupts …
a microcontroller generally has a lot more to do
than just wait for a single event, so the event gets
polled periodically in such a way that the rest of
the program can be executed as well. On the other
hand, the signal may have to be polled with a
certain maximum period to avoid missing events,
so the polling code may have to be called from
several places in the main program. It is already
time-consuming to establish from which positions
in the code the signal should be polled in the first
place, and these positions must be reconsidered
whenever the main code changes.
Interrupts …
The microcontroller polls the signal and interrupts
the main program only if a state change is
detected. As long as there is no state change, the
main program simply executes without any
concerns about the event. As soon as the event
occurs, the microcontroller calls an interrupt
service routine (ISR) which handles the event. The
ISR must be provided by the application
programmer.
Interrupt Control
The interrupt enable (IE): bit is set by the
application programmer to indicate that the
controller should call an ISR in reaction to the
event. The interrupt flag (IF) bit is set by the
microcontroller whenever the event occurs, and it
is cleared either automatically upon entering the
ISR or manually by the programmer. Basically, the
IF bit shows that the interrupt condition has
occured, whereas the IE bit allows the interrupt
itself to occur.
Interrupt Control …
Apart from the IE and IF bits, the controller will
most likely offer additional control bits (interrupt
mode) for some of its interrupt sources. They are
used to select which particular signal changes
should cause an interrupt (e.g., only a falling edge,
any edge, . . . ). It is sometimes even possible to
react to the fact that an input signal has not
changed. This is called a level interrupt.
Interrupt Control …
Since it would be inconvenient and time-
consuming to disable all currently enabled
interrupts whenever the program code should not
be interrupted by an ISR (atomic action), a
microcontroller also offers one global interrupt
enable bit which enables/disables all currently
enabled interrupts.
Hence, an ISR is only called if both the IE bit for
the interrupt source and the global IE bit are
enabled. Note that in the case of the global IE bit,
“enabled” does not necessarily mean “set”, so
always check whether the bit should be set or
cleared to enable interrupts.
Interrupt Vector Table
 Apart from enabling a given interrupt, the
programmer must also have the means to tell the
controller which particular interrupt service routine
should be called. The mapping of interrupts to ISRs is
achieved with the interrupt vector table, which
contains an entry for each distinct interrupt vector.
 An interrupt vector is simply a number associated
with a certain interrupt. Each vector has its fixed
address in the vector table, which in turn has a fixed
base address in (program) memory.
 At the vector address, the application programmer
has to enter either the starting address of the ISR
Interrupt Vector Table …
When an interrupt condition occurs and the
corresponding ISR should be called, the controller
either jumps to the location given in the table or it
directly jumps to the appropriate vector,
depending on the nature of the entry in the vector
table. In any case, the final result is a jump to the
appropriate ISR
Interrupt Priorities
Since a controller has more than one interrupt
source and can in fact feature quite a lot of
different interrupts, the question arises how to
treat situations where two or more interrupt
events occur simultaneously.
Most controllers with many interrupts and a
vector table use the interrupt vector as an
indication to the priority.
Some controllers allow the user to dynamically
assign priorities to at least some interrupts. Others
enable the user to select within the ISR which
interrupts should be allowed to interrupt the ISR.
Calling the ISR
Calling the ISR entails more than just a jump to the
appropriate address. First, the controller has to
save the return address on the stack, as with any
other subroutine.
ISR execution steps
 Set interrupt flag: The controller stores the occurrence of the
interrupt condition in the IF. Finish current instruction: Aborting
half-completed instructions complicates the hardware, so it is
generally easier to just finish the current instruction before
reacting to the event. If the controller was in a sleep mode when
the event occured, it will not have to finish an instruction, but
nevertheless it will take the controller some time to wake up.
 Identify ISR: The occurence of an event does not necessarily
imply that an ISR should be called. If the corresponding IE bit is
not set, then the user does not desire an interrupt. Furthermore,
since the controller has several interrupt sources which may
produce events simultaneously, more than one IF flag can be set.
So the controller must find the interrupt source with the highest
priority out of all sources with set IF and IE bits.
 Call ISR: After the starting address has been determined, the
controller saves the PC etc. and finally executes the ISR.
END!!!

You might also like