Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Chapter 4 Parallel Input and Output Resources

This document discusses parallel input and output (I/O) resources for microprocessor systems. It describes different categories of digital data for input and output, including direct user interfaces, sensor measurements, output controls, and bulk data transfer. It also discusses serial vs parallel data transfer methods. The document then covers programmed and interrupt I/O techniques for servicing peripherals. It provides details on parallel ports for PIC microcontrollers and discusses power limitations of I/O pins. Finally, it describes connecting common peripherals like LEDs, switches, and dealing with bouncing effects in mechanical switches.

Uploaded by

ellyshacb-wp21
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 Parallel Input and Output Resources

This document discusses parallel input and output (I/O) resources for microprocessor systems. It describes different categories of digital data for input and output, including direct user interfaces, sensor measurements, output controls, and bulk data transfer. It also discusses serial vs parallel data transfer methods. The document then covers programmed and interrupt I/O techniques for servicing peripherals. It provides details on parallel ports for PIC microcontrollers and discusses power limitations of I/O pins. Finally, it describes connecting common peripherals like LEDs, switches, and dealing with bouncing effects in mechanical switches.

Uploaded by

ellyshacb-wp21
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

BTEH 2223 MICROPROCESSOR SYSTEMS

CHAPTER 4:
Parallel Input and Output Resources

1
Digital Data Categories
- Embedded system transfer digital data between its CPU and the outside world.

- Categories of digital data (input or output):


a) Direct user interface, including switches, keypads, light emitting diodes (leds)
and displays.
b) Input measurement information, from external sensors, possibly being acquired
through an analog to digital converter (ADC).
c) Output control information, for example to motors or other actuators.
d) Bulk data transfer to or from other systems or sub-systems, moving in serial or
parallel form, for example sending serial data to an external memory.
- Digital data transfer methods (serial versus parallel):
a) Serial transfer = Data is transferred 1 bit at a time on a single interconnection.
Other lines are used for synchronisation and control.
b) Parallel transfer = Data is transferred in a group of bits (bytes) on a set of
interconnection (for example 8-bits data bus) simultaneously.
Normally used for interfacing with peripheral such as switches, LEDs,
etc.
- A group of parallel I/O interconnections used to transfer a combination of data, appearing
on the pins of the microcontroller, is
called a ‘parallel port’. 2
INPUT/OUTPUT SERVICING TECHNIQUES
 The two most commonly used techniques to service a peripheral connected to a
microcontroller are:
 Programmed input/output
 Interrupt input/output
 Programmed I/O is basically a software technique that the program asks if the peripheral
needs attention by using the indication bits of the peripheral status (ready or not ready).
 There are two algorithms used by the two main variants of programmed I/O: (a) polling
I/O and (b) waiting I/O. The polling I/O manages the time for the microcontroller better.
 The main characteristic of the interrupt I/O technique is that the peripheral indicates the
need for attention by sending an interrupt request to the microcontroller. When the
microcontroller receives this signal, it interrupts the execution of the program and moves
toward executng the interrupt subroutine. When the microcontroller finishes with the
request from the peripheral, it continues with the program that had been interrupted.
Interrupt I/O uses a combination of hardware and software.

3
PARALLEL PORTS IN MEDIUM-END PIC MICROCONTROLLERS
 Medium-end PIC microcontrollers can have up to seven parallel ports
named PORTA, PORTB, PORTC, …, PORTG, with each of them having
up to 8 bits. (Refer data sheet for ports specification)
 Each parallel port has two special function registers used to manipulate
the port, PORT (store I/O data)and TRIS (determine the port as input
or output).
 Each bit in the TRIS register is programmed as:
clrf TRISA ; all pins of PORTA is set as output. If set, it will be input.

4
POWER LIMITATION OF PINS
 Each I/O pin can source or sink a maximum value of current. Furthermore, the total
current sourced or sunk by all the port pins cannot exceed a certain value. This
value is normally lower than the sum of all the allowed individual currents for each
pin in the port. Finally, to keep the voltage in the output pin within the limits for the
logic values 0 and 1, it is necessary to keep the output currents for high level (IOH)
and low level (IOL) within the limits specified by the manufacturer. All the devices
must comply with these specifications described in the manufacturer‟s data sheets.
 The following voltage and current values for the PIC16F873 pins are given by its manufacturer.
Each I/O design must take them into consideration.
1. Maximum sunk current by any pin: 25 mA.
2. Maximum sourced current by any pin: –25 mA.
3. Sourced or sunk current for port C or by ports A and B together cannot exceed 200 mA.
 Typical values for input and output voltages:
1. Logic value 0 in an input pin: Voltage in this pin is VIL < 0.75 V (TTL input) or VIL <
1.0 V (Schmitt trigger input) with VDD = 5.0 V.
2. Logic value 1 in an input pin: Voltage in this pins is VIH > 2.0 V (TTL input) or VIH >
4.0 V (Schmitt trigger input) with VDD = 5.0 V.
3. Output voltage for logic value 0: VOL < 0.6 V if output current sunk by pin is IOL < 8.5
mA with VDD = 4.5 V.
5
4. Output voltage for logic value 1: VOH > VDD – 0.7 V = 3.8 V if output current sourced
by pin is IOH < –3.0 mA with VDD = 4.5 V.
CONNECTION OF COMMONLY USED PERIPHERALS

(A) Light Emitting Diodes (LEDs)

-Voltage across the red LED


changes from 1.90 to 2.00 V if the
current increases from 5 to 20 mA.

-Typically, LED requires about


10mA.

- Different colors do not give equal


intensities for equal drive currents.

- Red is the most efficient.

6
High Efficiency Red Yellow
http://www.kingbright.com.tw/
Driving LEDs from Logic Gates (and hence Microcontroller Port Bit Outputs)

VS

R
ID
Current flows out of the gate
current flows into gate
and lights led when output R
and lights led when VD
is at logic 1 ID ≤ IOHmax output is at logic 0

ID
VD

ID ≤ IOLmax
a) Gate Output Sourcing b) Gate Output Sinking
Current to LED Current from LED

Datasheet provides the internal output resistances values at logic high and
logic low for PIC microcontroller. 7
In general, |IOLmax | > |IOHmax|, for this reason, when connecting LEDs to
the parallel ports it is recommended to use the configuration as current sink.
PRACTICE 4.1:
Q1) Using a PIC16F873, connect 2 LEDs and a switch as shown in
Figure. Calculate the values of resistances RL1, RL2 assuming the
voltage supply is VDD = 5 V. The LED1 is yellow LED that have
forward current IF = 10 mA while the LED2 is red LED that have
forward voltage about 2.0V. These values change slightly depending
on the LED color.

The value for the resistance in series with the LED 8


is not critical. Therefore it is possible to use standard
values with tolerances of 5% or even 10%.
(B) Switches
How to convert the switch position to a logic level that can
be read by a microcontroller port bit??
Active high signal !!!

Optional Pull-up
resistor for resistor
protection 10-100kΩ

Example (b) are push-button


and slide switches (This circuit Example (c) must be careful as
are popular as many switches current sourced from the gate input
Active low signal !!! may significantly affect the action
are SPST)
of the pull-down resistor.

Connecting switches to logic inputs. (a) SPDT connection. (b) SPST with pull-up resistor.
(c) SPST with pull-down resistor

Single-Pole, Double-
Throw (SPDT) 9

Single-Pole, Single-Throw
(SPST)
BOUNCING EFFECT IN MECHANICAL SWITCHES
 The bouncing effect is generated when the contacts in a mechanical switch bounce for a
certain period of time.
 This bouncing may cause a single switch action, either opening or closing, to be
understood by the microcontroller as a series of successive switch activations and
deactivations. When the switch opens or closes, the mechanical pieces vibrate generating
fast contacts before settling. This transient normally lasts less than 20 ms.
 The Bouncing effect can be solved by using hardware or software methods.
 A basic hardware solution is to use a non mechanical switch such as Hall-effect switches
or wet-contact (mercury) switches that are not affected by bouncing. However, the
number of available models for this type of switch is very limited compared to the
traditional mechanical models.
 A better hardware solution is to connect the mechanical switch to the input of a
monostable circuit that will increase the time for the first detected pulse long enough to
mask the pulses produced by bouncing. This solution, however, increases the size and cost
of the design. Another way is to put a single capacitor placed across a switch will almost
instantly discharge through the switch when the switch is closed. When the switch is
opened, the capacitor takes some time to recharge. Therefore contact bounce will have
negligible effect on the output.
 Software solutions are based on reading the state of the switch a certain amount of time
after the switch was first activated. A delay of about 20ms (typically 10 ms) is normally
long enough to ensure that the switch has reached a stable state.

10
SOFTWARE SOLUTION FOR BOUNCING EFFECT

11
(C) MATRIX KEYPADS
 Matrix keypads consist of keys interconnected in the shape of a matrix. Each key is a simple
mechanical switch located at the crossing between the matrix rows and columns. Figures shows a
16-key matrix keypad arranged in four rows and four columns. Only eight interconnections are
needed, rather than 16. The switches are connected in a matrix yet it would be extremely resource-
intensive if each of these switches were allocated to a port bit.
 When a key is pressed, its row and column form an electrical contact, otherwise, there is no
connections between rows and columns. Row and columns of the keypad are accessed by the
microcontroller through ports. Therefore, with two 8-bit ports, an 8 x 8 matrix of keys can be
connected to a microcontroller.
 The keypad allows numeric or alphanumeric information to be entered. It is widely used in
photocopiers, burglar alarms, central heating controllers and so on. In IBM PC keyboards, a single
microcontroller takes care of hardware and software interfacing of the keyboard. In such systems,
programs stored in the ROM of the microcontroller scan the keys continuously, identify which one
has been activated, and present it to the motherboard of the IBM personal computer.
 In programming for keypad interfacing, we must have two basic processes: (a) key press detection,
and (b) key identification. There are two ways for PIC 16 microcontrollers to perform key press
detection: (1) the scanning method, and (2) the interrupt method. In the PIC 16, the PORTB-
change interrupt can be used to implement the interrupt-based key press detection.

12
FLOWCHART FOR SCANNING METHOD OF KEY PRESS DETECTION AND KEY IDENTIFICATION
WITH PULL-DOWN RESISTOR

Sequential exploration of
rows

13
 A 16-key matrix keypad connected to port B in a PIC. Diodes are used to limit the
current in pins RB0 to RB3 in order to avoid short circuits between two exploration
lines if two or more keys in the same column are pressed simultaneously. These
diodes could be substituted by resistors between 1 kΩ and 2.2 kΩ.This configuration
uses the internal pull-up (not shown) available in port B to keep a logic 1 in the
return lines when no keys are pressed

14
4 X 3 MATRIX KEYPADS

VS Keypad
Pull-up
Resistors
1 2 3
Port bit
7
Row 6 4 5 6
Connections 5
4
7 8 9

* 0 #
3
Column
2
Connections
Simultaneous 1

exploration of
rows and
columns

15
PRACTICE 4.2: READING THE KEYPAD-SUBROUTINE
Q1) Write a subroutine code that uses the previous flow diagram to
read a keypad, which is connected to port B of PIC 16F84A.

16
KEY PRESS DETECTION BY INTERRUPT METHOD
 To detect keypad action, it uses the „interrupt on change‟ facility, available on the higher four bits of Port B.
Following initialization, all program activity is contained in the Interrupt Service Routine. The ISR does
essentially four things: reads a pattern from the keypad, converts this to ASCII code, sends the code to the
LCD (if the LCD is applied) and waits for the key to be released, before leaving the ISR.
 In the opening initialization of program, Port B is set up so that row bits are input and column bits are output.
We could start the other way round, but to make use of the „interrupt on change‟, the higher four bits must be
set as input. All output bits are subsequently set to zero and the „interrupt on change‟ interrupt is enabled.
 By referring to the program of next slide that is used to illustrate use of both keypad and LCD, an interrupt
occurs when a key is pressed and the Interrupt Service Routine, labeled kpad to lcd, is invoked. This
immediately calls the subroutine kpad rd, which continues the flow diagram below, started in the
initialization section. The value of Port B is read and stored in memory location kpad pat. Column and row
roles are then reversed, and the row bits are then read. These are ORed into kpad pat, ensuring that any
unwanted bits are removed by ANDing with 0. The routine then resets Port B to its initial value, ready for the
next keypad read.
 The kp code conv subroutine converts the patterns derived from reading the keypad, as seen in Table b at the
previous slide, into the ASCII code of the key pressed. It does this by deriving an address, held in memory
location kpad add, which is used to access the look-up table kp table. The address follows the format shown in
Figure below. The subroutine tests the bits of the pattern in turn, finding out which row and column has been
active. It sets up the address bits according to the outcome of its tests. The look-up table is then called.

17
18
(D) SEVEN-SEGMENT LEDS
 A single LED, or even groups of LEDs, are restricted in the information they can
give, and as their number increases they become increasingly complex to drive.
 Thus, seven-segment LEDs are mainly used to present numeric information.
 Like the keypad, there is a risk of excessive interconnections. Therefore the leds
are connected in either “common cathode”, or “common anode” format. In the
standard pattern each segment is balled with a letter, from a to g. Usually a
decimal point (DP) is included.

common
anode

19
common
cathode
MULTIPLEXING OF DIGITS
 Multiplexing purpose  to reduce the excessive connections (36 connections for 4 digits) and power requirement
(with all segment ON, each with 5 mA, total will be 160 mA).
 The value of the series resistors is selected experimentally to minimize the current while maintaining reasonable
visibility. Resistance Rb must guarantee that the transistors will saturate. The diodes placed in the transistor
bases ensure that these will be OFF when not directly driven.
 Each seven-segment display is activated sequentially using an appropriate refresh frequency(between 40 and 200
Hz), a person will see all the seven-segment displays lighted at the same time. Each device is selected one fourth
of the total time. For example, Digit 4 is therefore switched on, while all other digits are off. This is held for a time
(around 5–20 ms is generally appropriate) and then Digit 1 is illuminated in a similar way.

20
DISPLAY THE WORD HELP ON THE 7-SEGMENT DIGITS

21
(E) LIQUID CRYSTAL DISPLAYS (LCDS)
 LCDs are widely used to represent alphanumeric information. They are specifically
manufactured to be used with microcontrollers, which means that it cannot be
activated by standard IC circuits.
 A very commonly used microcontroller is HD44780 from Renesas (formerly Hitachi).
This microcontroller can directly handle an LCD screen of one or two lines, each
containing eight characters. To handle larger screens, it is necessary to use auxiliary
circuits such as the HD44100 driver that allows handling of an additional eight
characters for each line. Every character consists of most commonly used 5x8 (as
shown in figure) or 5x11 dot matrix.

22
 There are in total of 14 pins marked with numbers (16 if the backlight, blue or green
diodes, is built in). Thus, a current limiting resistor should be serially connected to one
of the pins for backlight (similar to LED diodes).

23
LCD Memories
 LCD display contains three memory blocks:
DDRAM - Display Data RAM
CGRAM - Character Generator RAM
CGROM - Character Generator ROM
 DDRAM memory is used for storing up to 80 alphanumeric characters (ASCII codes) that should be
displayed. If it is organized in two lines, it has two independent circular memories (the memory
addresses are not sequential), that is, once one line is full the next character is written in the initial
memory position of the same line.
 It works quite simply: it is enough to configure the display to increment addresses automatically
(shift right) and set the starting address for the message that should be displayed (for example 00
hex).
 If more than 16 characters are sent then all of them will be memorized, but only the first sixteen
characters will be visible. In order to display the rest of them, a shift command should be used.
Virtually, everything looks as if the LCD display is a window which shifts left-right over memory
locations containing different characters. In reality, this is how the effect of message shifting on the
screen has been created.
 If the cursor is on, it appears at the location which is currently addressed. In other words, when a
character appears at the cursor position, it will automatically move to the next addressed location.
This is a sort of RAM memory so data can be written to and read from it, but its contents is
irretrievably lost upon the power goes off.

24
 The ROM generator stores the matrix of dots needed to generate each character (160 characters
with 5 × 7 dots, 32 characters with 5 × 10 dots). CGROM memory contains the default character
map with all characters that can be displayed on the screen. Each ASCII character is assigned to
one memory location as shown in Table.
 If the program being currently executed encounters a command "send character P to port" then the
binary value 0101 0000 appears on the port. This value is the ASCII equivalent to the character P. It
is then written to LCD, which results in displaying the symbol from the 0101 0000 location of
CGROM. This applies to all letters of the alphabet (capitals and small), but not to the numbers!
 As seen on the character map, addresses of all digits are pushed forward by 48 (binary 0011 0000)
relative to their values (digit 0 address is 48, digit 1 address is 49, digit 2 address is 50 etc.).
Accordingly, in order to display digits correctly it is necessary to add a decimal number 48 to each of
them prior to sending them to LCD.

25
 The RAM generator (CGRAM) allows the user to define nonstandard characters that are not
stored in the ROM character generator.
 In the character generator RAM, the user can rewrite character patterns by program. For 5x8
dots, eight character patterns can be written, and for 5x10 dots, four character patterns can be
written.
 To show the character patterns stored in CGRAM, write into DDRAM the character codes at
the addresses shown as the left column of CGROM Table. Use the “Set CGRAM address”
command to send store and retrieve data from CGRAM.
 The HD44780 controller has an internal register called the address counter (AC). This register
contains the address of DDRAM or CGRAM that will be used to write or read data. Once one
type of memory has been selected, data will be read or written from that memory until the
selection is changed.

HD44780 LCD highlights


 Data is transferred on a 4- or 8-bit (DB0 - DB7) data bus, determined by the user. Using the 4-
bit mode allows the whole interface to be contained within just 7 bits, but is a little slower.
 Bit 7 of the data bus doubles as a “busy flag”, indicating whether the device is ready to accept
new data. When the voltage on this line is 0V (BF=0), the display is ready to receive new data.
 LCD Control is exercised by three control lines:
- Register Select (RS), which determines whether an instruction or character data is being
transferred through DB0-DB7.
- Read/Write# (R/W), which determines data direction.
- Enable (E), which provides a clock function to synchronize data transfer. The LCD module
captures data or control signals with the falling edge of E. 26
 There is a simple instruction set which allows control of operating characteristics – this includes
initializing and clearing the display, and controlling the position and characteristics of the
cursor.
HD44780 LCD Basic Commands

 RS = 1  Bits D0 - D7 are addresses of the characters to be displayed. LCD processor


addresses one character from the character map and displays it. The DDRAM address specifies
the location on which the character is to be displayed. This address is defined prior character
transfer or the address of the previously transferred character is automatically incremented.
 RS = 0 Bits D0 - D7 are commands which determine display mode.

27
HD44780 LCD Connecting

 Depending on how many lines are used for connecting the LCD to the microcontroller, there are 8-bit
and 4-bit LCD modes.
 The main purpose of 4-bit LCD mode is to save valuable I/O pins of the microcontroller. Only 4 higher
bits (D4-D7) are used for communication, while others may be unconnected. Each piece of data is sent
to the LCD in two steps- four higher bits are sent first (normally through the lines D4-D7) and four
lower bits are sent afterwards. Initialization enables the LCD to link and interpret received bits
correctly.
 As data is rarely read from the LCD, the R/W pin is normally connected to ground to save the pin
connection. However, this will disable the read of the busy flag to the microcontroller. Owing to the
fact that the execution of the slowest command lasts for approximately 1.64 ms, it will be sufficient to
wait approximately 2 ms for LCD execution after sending a character or a command to it.

28
 On power-up, the HD44780 must undergo a very specific initialization process.
It is important to get this absolutely right, or the display may just sit inactive.
 The LCD is automatically cleared when powered up. It lasts for approximately
15 ms. After this, display is ready for operation. The mode of operation is set by
default. It means that:
1. Display is cleared.
2. Mode
DL = 1 Communication through 8-bit interface
N = 0 Messages are displayed in one line
F = 0 Character font 5 x 8 dots
3. Display/Cursor on/off
D = 0 Display off
U = 0 Cursor off
B = 0 Cursor blink off
4. Character entry
ID = 1 Displayed addresses are automatically incremented by 1
S = 0 Display shift off

 Automatic reset is mostly done without any problems. Mostly, but not always! 29
Therefore, the process of initialization is applied to cause a new reset enabling
display to operate normally.
Procedure on 8-bit initialization

30
Procedure on 4-bit initialization

31
32
33
The HD44780 Timing Diagram
The bus operates in 8-bit mode in this example.
• Every data transfer is enabled by a pulse on the E line.
• With RS set low, the data on the data bus is interpreted as an instruction.
• To check for completion, R/W is taken high, - on the next cycle of E the lcd
controller outputs a word with the busy flag as msb. This is checked repeatedly until
the busy flag is cleared.
• When it goes low, RS in this example is taken high, R/|W is taken low, and the
next data transfer is therefore a character code.

RS
R/W

Not
DB7 Busy Busy Busy
Instruction Character
Code
Code
DB6-0
Address Address Address
Counter Counter Counter

Instruction Busy Flag Character


Write and Address Write
Counter read 34

HD44780 Timing Diagram, 8-bit Interface

You might also like