eng364 week4 parallel-io

Upload: valli-muthu

Post on 03-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 Eng364 Week4 Parallel-IO

    1/82

    Week #4Parallel IO Interfacing

    ENG3640Microcomputer Interfacing

  • 7/28/2019 Eng364 Week4 Parallel-IO

    2/82

    ENG3640 Fall 2012 2

    Topics

    I/O Addressing Techniques I/O Port Structure CPU12 I/O Ports

    Programming I/O Ports:

    Driving LEDs/7-Segment Displays Interfacing to Switches Switch Debouncing Keypad Interfacing Techniques/Issues Liquid Crystal Displays

  • 7/28/2019 Eng364 Week4 Parallel-IO

    3/82

    ENG3640 Fall 2012 3

    Resources

    Huang, Chapter 4 Sections 4.10 Intro to Parallel I/O Ports

    4.11 Simple I/O Devices

    Huang, Chapter 7 Section 7.2 I/O Related Issues

    7.3 I/O Addressing Issues

    7.5 The HCS12 Parallel Ports

    7.6 Electrical Characteristics

    7.7 Liquid Crystal Displays

    Interfacing Parallel Ports to a keypad

  • 7/28/2019 Eng364 Week4 Parallel-IO

    4/82

  • 7/28/2019 Eng364 Week4 Parallel-IO

    5/82

    ENG3640 Fall 2012 5

    I/O Addressing Techniques

    If the same address bus is used for bothmemory and I/O, how should the hardware

    be designed to differentiate betweenmemory and I/O reads and writes?

    CPU Memory I/OInterface

    Data

    Address

    Control

  • 7/28/2019 Eng364 Week4 Parallel-IO

    6/82

    ENG3640 Fall 2012 6

    Memory Mapped I/O vs. Isolated I/O

    Memory Mapped I/O (MOTOROLA):1. Any instruction that reads or writes memory can read/write

    I/O Port

    2. Address specifies which module (input, output, RAM, ROM),will communicate with the processor

    3. Ex: LDAA #56 STAA $0024 (copy value to port H)

    Isolated I/O (INTEL):

    1. The control bus signals that activate the I/O are separate from thosethat activate the memory device.

    2. These systems have a separate address space.

    3. Separate instructions are used to access I/O and Memory.

    4. Ex: IN AL, $10 (copy values of port $10 into register AL)

    Advantages/Disadvantages?

  • 7/28/2019 Eng364 Week4 Parallel-IO

    7/82

    ENG3640 Fall 2012 7

    I/O Port Structure

    1. Data Register: for data in transit2. Control Register: Hold commands from processor to port

    3. Status Register: Used to monitor I/O activity

    Principlefunctionality isserve as way

    station for datain transit

    between thecomputer andexternal world.

    Polling

    InterruptDriven

  • 7/28/2019 Eng364 Week4 Parallel-IO

    8/82

    ENG3640 Fall 2012 8

    68HC812A4

    Block

    Diagram

    CPU12

    1-KB SRAM

    4-KB EEPROM

  • 7/28/2019 Eng364 Week4 Parallel-IO

    9/82

    ENG3640 Fall 2012 9

    MemoryMap

  • 7/28/2019 Eng364 Week4 Parallel-IO

    10/82

    10

    Port Direction Function

    Port A I/OSingle-chip modes: general-purpose I/OExpanded modes: external address bus ADDR15ADDR8

    Port B I/OSingle-chip modes: general-purpose I/OExpanded modes: external address bus ADDR7ADDR0

    Port C I/O

    Single-chip modes: general-purpose I/OExpanded wide modes: external data bus DATA15DATA8Expanded narrow modes: external data bus DATA15DATA8/DATA7DATA0

    Port D I/OSingle-chip and expanded narrow modes: general-purpose I/OExternal data bus DATA7DATA0 in expanded wide mode(1)

    Port E I/O and I(2) External interrupt request inputs, mode select inputs, bus control signalsGeneral-purpose I/O

    Port F I/OChip selectGeneral-purpose I/O

    Port G I/OMemory expansionGeneral-purpose I/O

    Port H I/OKey wakeup(3)General-purpose I/O

    Port J I/OKey wakeup(4)General-purpose I/O

    Port S I/OSCI and SPI portsGeneral-purpose I/O

    Port T I/OTimer portGeneral-purpose I/O

    Port AD I ADC portGeneral-purpose input

    Port Details

  • 7/28/2019 Eng364 Week4 Parallel-IO

    11/82

    ENG3640 Fall 2012 11

    General Purpose I/O: Bidirectional

    Most GPIO pins on the 68HC12 MCU can be programmed for use ineither direction.

    Two registers: the data register PORT and data direction register DDR.

    The DDR determines the direction of the port pin.

    If the DDR = 1 then the port is an output and

    if the DDR = 0 the data register output is disabled and the port pin is placed inhigh impedance state.

  • 7/28/2019 Eng364 Week4 Parallel-IO

    12/82

    12

    I/O PORTS AddressesRegister Name Address Functionality

    PORTA $0000 Port A Data RegisterDDRA $0002 Port A Data Direction Register

    PORTB $0001 Port B Data Register

    DDRB $0003 Port B Data Direction Register

    PORTC $0004 Port C Data Register

    DDRC $0006 Port C Data Direction Register

    PORTD $0005 Port D Data Register

    DDRD $0007 Port D Data Direction Register

    PORTE $0008 Port E Data Register

    DDRE $0009 Port E Data Direction Register

    PORTF $0030 Port F Data Register

    DDRF $0032 Port F Data Direction Register

    PORTG $0031 Port G Data Register

    DDRG $0033 Port G Data Direction Register

    PORTH $0024 Port H Data Register

    DDRH $0025 Port H Data Direction Register

    PORTJ $0028 Port J Data Register

    DDRJ $0029 Port J Data Direction Register

  • 7/28/2019 Eng364 Week4 Parallel-IO

    13/82

    13

    I/O PORTS Usage on EVB

    1. Port B is connected to the Light Emitting Diodes (LEDS)a) Each Port B line is monitored by an LED.b) In order to turn on Port B LEDs, the PJ1 (Port J pin 1)

    must be programmed as output and set for logic zero.c) If you ignore the status of the LEDs, the Port B can drive

    any other I/O on the breadboard.2. Port P is connected to the Seven Segment Display

    a) There are 4 digits of 7-Segment Displays on the EVB.b) Port B is used to drive the 7-segment anodes and PP0-PP3

    (Port P) to drive common cathodes

    c) To use the 7-Segments you need to multiplex among them.3. Port K is connected to the Liquid Crystal Display (LCD)4. Port A is connected to the Hex Key Pad5. Port H is connected to the DIP Switches and Push buttons.

  • 7/28/2019 Eng364 Week4 Parallel-IO

    14/82

    ENG3640 Fall 2012 14

    General Purpose I/O Usage Parallel ports are often used for simple I/O such as

    turning on LEDs, 7-segment displays or readingswitches (unconditional transfer)

    Steps for using Ports:

    1. Identify the address of an I/O port and its DataDirection Register (DDR)

    2. Program the DDR by writing a value to it.

    3. The value written to the DDR reflects theappropriate setting for the port (i.e a `0 will makethe corresponding pin an input and a `1 will force

    the pin in the port to be an output).

    4. Load a register with a value and store this value tothe address of the I/O PORT.

  • 7/28/2019 Eng364 Week4 Parallel-IO

    15/82

    ENG3640 Fall 2012 15

    General Purpose I/O Usage

    To make bit 0 of PORTH (PH0) an output, wewould use the following instruction:

    DDRH EQU $25

    bset DDRH, $01 ; Set PORTH direction

    Once bit 0 of PORTH has been configured as anoutput, we can make the pin go to one by

    writing a one to bit0 of PORTH as follows:PORTH EQU $24

    bset PORTH,$01 ; Set PORTH bit-0 high

  • 7/28/2019 Eng364 Week4 Parallel-IO

    16/82

    ENG3640 Fall 2012 16

    Avoiding Transients and Glitches

    When the MCU is powered up or reset, all GPIO portsare con f igured as inputs.

    The port will remain an input until the software changes thedata direction register.

    Can th is cause a prob lemwhen a port is used as anoutput in normal operation?

    The external device connected to the port may do strangethings!

    For example, if an output is connected to a motor or solenoiddriver, the motor or solenoid may run intermittently duringthis time and have serious consequences!

  • 7/28/2019 Eng364 Week4 Parallel-IO

    17/82

    ENG3640 Fall 2012 17

    Avoiding Transients and Glitches

    1. A pull-up orpull-down resistorcan be addedto theport so the node will always be pulled to the inactivelevel instead of floating.

    2. The port can be preset to the inact ive levelbeforechanging its direction to an output (when the portdirection is changed a temporary glitch on theoutput will occur!)

    To avoid the glitch, we can write to the port data register

    before the direction is changed.bset PORTH, $00 ; preset PORTH bit-0 low

    bset DDRH, $01 ; PORTH bit-0 an output

  • 7/28/2019 Eng364 Week4 Parallel-IO

    18/82

    ENG3640 Fall 2012 18

    Interfacing: Voltage Parameters

    Like any digital device, before we can connect

    something to an input or output, we need to knowthe specification for the interface parameter.

    The first parameter to consider are the input andoutput voltage levels and corresponding noisemargins.

    VDD and VSS are supplyvoltages. The output voltageparameters are VOL and VOH.

    The input voltage parametersare V

    ILand V

    IH.For a digital

    system to work correctly, theoutput high voltage alwaysmust be between VIH,min and

    VDD.

    Noise Margin?

  • 7/28/2019 Eng364 Week4 Parallel-IO

    19/82

    ENG3640 Fall 2012 19

    Interfacing: Applications

    1. Like most digital logic devices, the output of MCUs

    can sink more current than they can source.

    2. Consequently, devices that require significant loadcurrent like an LED should be connected active-low.

  • 7/28/2019 Eng364 Week4 Parallel-IO

    20/82

    ENG3640 Fall 2012 20

    Light Emitting Diodes (LED)

    1. An LED emits light when current flows through it in

    the positive direction i.e. when the voltage on theanode side is made higher than the voltage on thecathode side.

    2. The forward voltage across the LED is typically about

    1.5 to 2 Volts.

    MCU prod uces

    low

  • 7/28/2019 Eng364 Week4 Parallel-IO

    21/82

    ENG3640 Fall 2012 21

    Connecting an LED to an Output Port

    Determine whether we can drive an active-low LED

    from an M68HC12 output? Assume that a high-efficiency LED with IF,min = 10mA

    and VF,max = 2.0 V is used

    The LED in the figureis connected activelowso that when theoutput goes low, the

    current IL flowsthrough the LED andturns it on.

  • 7/28/2019 Eng364 Week4 Parallel-IO

    22/82

    ENG3640 Fall 2012 22

    Connecting an LED to an Output Port

    Using a 5-volt supply and assuming that theLED has a 2.0 V drop across it, what resistorvalue will limit the current to 10mA?

    Answer:

    5V = 2.0V + IRx x Rx

    Setting IRx to 10mA the resistor Rx is solvedto be 300 Ohm.

  • 7/28/2019 Eng364 Week4 Parallel-IO

    23/82

    ENG3640 Fall 2012 23

    Connecting an LED to an Output Port

    74HC04

    VCC

    Figure 7.9 An LED connected to a CMOS inverter through a current-

    limiting resistor.

    An I/O port pin of a microcontroller generally

    does not have enough drive to supply thecurrent.

    So an inverter is often used as a switch to

    turn the LED on and off.

    Active High sinceMCU produced a 1 to

    turn the LED on

  • 7/28/2019 Eng364 Week4 Parallel-IO

    24/82

    ENG3640 Fall 2012 24

    Use the 68HC12 Port H to drive green, yellow, red, and blue LEDs.

    Light each of them for half of a second in turn and repeat. The 68HC12

    uses a 16-MHz crystal oscillator to generate internal clock signals.

    Solution:The upper four pins of the Port H can be used for this purpose.

    74HC04

    5V

    Figure 7.10 Circuit connection for example 7.3

    74HC04

    5V

    74HC04

    5V

    74HC04

    5V

    PP7

    PP6

    PP5

    PP4

    68HC12

    300W 300W 300W 300W

    green yellow red blue

  • 7/28/2019 Eng364 Week4 Parallel-IO

    25/82

    ENG3640 Fall 2012 25

    PORTH equ $24DDRH equ $25org $1000ldaa #$FF ; configure PORTH for outputstaa DDRH ;

    Forever ldaa #$80 ; turn on green LED and turn off other LEDsstaa PORTH ; jsr delay_hs ; wait for half of a second

    ldaa #$40 ; turn on yellow LED and turn off other LEDsstaa PORTH ; jsr delay_hs ; wait for half of a secondldaa #$20 ; turn on red LED and turn off other LEDsstaa PORTH ; jsr delay_hs ; wait for half of a second

    ldaa #$10 ; turn on blue LED and turn off other LEDsstaa PORTH ; jsr delay_hs ; wait for half of a secondjmp forever ; repeatswi

  • 7/28/2019 Eng364 Week4 Parallel-IO

    26/82

    ENG3640 Fall 2012 26

    Seven Segment Displays

    Consists of seven LED

    segments (a, b, c, d, e, f, g)

    Alphanumeric characterscan be displayed bycontrolling the segments.

    Two types of Seven SegmentDisplays:

    1. Common Cathode

    2. Common Anode

  • 7/28/2019 Eng364 Week4 Parallel-IO

    27/82

    ENG3640 Fall 2012 27

    Seven Segment Displays

    Common Anode:

    1. All anodes are tied incommon.

    2. Segment will be litwhenever a low voltage

    is applied. Common Cathode:

    1. all cathodes are tied incommon.

    2. Segment will be lit

    whenever a high voltageis applied.

    Current limiting resistorsmust be included or elseyou might damage

    display.

  • 7/28/2019 Eng364 Week4 Parallel-IO

    28/82

    ENG3640 Fall 2012 28

    Seven Segment Displays: Examples

    Depending on the type ofdisplay used a different hexcode is generated by theMCU.

    1. Common Anode: sending a``0 will illuminate the

    segment.

    2. Common Cathode:sending a ``1 will

    illuminate the segment.

  • 7/28/2019 Eng364 Week4 Parallel-IO

    29/82

    ENG3640 Fall 2012 29

    Seven Segment Displays: Decoders

    Some ICs are specially

    designed to drive 7-segment displays.

    They contain buffersto supply requireddrive currents.

    When usingMC144495 decoder, nocurrent limitingresistors have to beused since they are

    built in the MC14495

  • 7/28/2019 Eng364 Week4 Parallel-IO

    30/82

    ENG3640 Fall 2012 30

    Software 7-Segment Code Look-up

    If your interface circuit uses only buffers andresistors to connect an output port to a sevensegment display, you will have to use software togenerate the character codes.

    LAB #2 The program can do this by using a look-up table.

    The contents of the table depends on the application andtype of display.

    For example, to display hex digits for a common anode

    display, entry 4 would have the byte $19. What addressing mode to use?

  • 7/28/2019 Eng364 Week4 Parallel-IO

    31/82

    31

    Interfacing a Switch: Pull Up

    To convert the mechanical signal into an electrical signal, a

    resistor pull-up is used. The amount of current this output can source is determined by

    the resistor value.

    When the switch is open, Output?

    When the switch is closed, Output?

    switch Output

    Open +5V

    Closed 0V

    +5V

    1K

    I/O

    Port

    ENG3640 Fall 2012

  • 7/28/2019 Eng364 Week4 Parallel-IO

    32/82

    32

    Interfacing a Switch: Pull Down

    When the switch in pull-down circuit is open the output is

    pulled to the ground

    The amount of current this output can sink (IOL) is determinedby the resistor value.

    switch Output

    Open 0V

    Closed +5V

    +5V

    1K

    I/O

    Port

    ENG3640 Fall 2012

  • 7/28/2019 Eng364 Week4 Parallel-IO

    33/82

    33

    Interfacing a Switch: PORTJ MCU

    Port J on the MC68HC812A4 supports both internalpull-ups and pull-downs. Either of the two previous circuits could be

    implemented on the 6812 without the resistor

    +5V

    PJ1 with pull-down

    PJ0 with pull-up

    ENG3640 Fall 2012

  • 7/28/2019 Eng364 Week4 Parallel-IO

    34/82

    34

    PORTJ: Pull-up/Pull-down Registers

    Each bit in the PUPSJ Registercorresponds to a PORT J pin.

    Each bit selects a pull-up or pull-down device for theassociated PORT J pin.

    The pull-up or pull-down is active only if enabled by the PULEJRegister

    ENG3640 Fall 2012

  • 7/28/2019 Eng364 Week4 Parallel-IO

    35/82

    35

    PORTJ: Pull-up/Pull-down Registers

    DDRJ PUPSJ bit PULEJ bit Port J mode

    1 X X Regular output

    0 X 0 Regular input

    0 0 1 Input with passive pull-down

    0 1 1 Input with passive pull-up

    ENG3640 Fall 2012

  • 7/28/2019 Eng364 Week4 Parallel-IO

    36/82

    36

    PORTJ: Data Register/Data Direction

    RECALL: PORTJ can act as a general purpose I/O

    PORTJ (Data Register) and DDRJ (Data Direction Register) areused to setup the port for reading/writing information

    PORT J is also used in the key wakeup feature of the MC6812!

    ENG3640 Fall 2012

    PORTJ : Wakeup Flag/Interrupt Enable

  • 7/28/2019 Eng364 Week4 Parallel-IO

    37/82

    37

    PORTJ : Wakeup Flag/Interrupt Enable

    The key wakeup feature of the MC6812 issues an interrupt thatwakes up the CPU when it is in stop or wait mode.

    Wakeups are triggered with falling/rising signal edge An interrupt is generated when a bit in KWIFJ register and its

    corresponding KWIEJ bit are both set.

    ENG3640 Fall 2012

  • 7/28/2019 Eng364 Week4 Parallel-IO

    38/82

    38

    Switch Bouncing

    When mechanical switches are opened or closedthere are brief oscillations due to mechanicalbouncing (switch bounce).

    +5V

    A

    +5V

    0V

    What is the consequence ofsuch bouncing for

    interfacing?

    ENG3640 Fall 2012

  • 7/28/2019 Eng364 Week4 Parallel-IO

    39/82

    39

    Switch Debouncing

    When the mechanical switch is touched (pressed) or released it

    bounces microscopically for a period of milliseconds. The MCU will see many occurrences of ``make and ``break

    instead of one occurrence.

    The MCU should see only one``break and one ``make

    ENG3640 Fall 2012

  • 7/28/2019 Eng364 Week4 Parallel-IO

    40/82

    40

    Switch Debouncing: Techniques

    Several techniques exist to solve thedebouncing problem:

    Hardware Techniques:

    1. RS-flip flop

    2. Integrating debouncer (capacitor)

    3. Schmitt Trigger Circuit

    Software Techniques:

    1. Delay loops

    ENG3640 Fall 2012

  • 7/28/2019 Eng364 Week4 Parallel-IO

    41/82

    41

    Hardware Debouncing: Flip Flop

    I1 I2 F

    0 0 1

    0 1 1

    1 0 1

    1 1 0

    +5V

    +5V

    QS

    R

    S

    R

    Q

    NAND

    S R Q

    0 1 1

    1 1 1

    1 0 0

    1 1 0

    0 0 X

    An SR latch can be used to debounce a switch

    ENG3640 Fall 2012

  • 7/28/2019 Eng364 Week4 Parallel-IO

    42/82

    42

    Hardware Debouncing: Schmitt Trigger

    A Schmitt trigger is a special circuit that uses feedbackinternally to shift the switching threshold depending onwhether the input is changing from low to high or high to low.

    The difference between V T+ and V T- is called hysteresis.

    A 74LS14 Schmitt Trigger inverter can be used to debounce a

    switch.

    VIN

    VOUT

    2.1 2.9 5.0

    V T+

    V T-

    5.0

    Example: 74LS14

    42ENG3640 Fall 2012

  • 7/28/2019 Eng364 Week4 Parallel-IO

    43/82

    43

    A noisy slowlychanging input

    Output produced byordinary inverter

    Hardware Debouncing: Schmitt Trigger

    43ENG3640 Fall 2012

  • 7/28/2019 Eng364 Week4 Parallel-IO

    44/82

    44

    A noisy slowlychanging input

    Output produced byordinary inverter

    Output produced byinverter with 0.8 V

    of hysteresis

    Hardware Debouncing: Schmitt Trigger

    44ENG3640 Fall 2012

  • 7/28/2019 Eng364 Week4 Parallel-IO

    45/82

    45

    Integrating Debouncer:

    5V

    Vout

    The RC constant of the

    integrator determines therate at which the capacitorcharges up towards thesupply voltage.

    The capacitor value is

    chosen large enough so thatVout does not exceed thezero threshold value whilethe switch is bouncing!

    R

    CThreshold Level

    ENG3640 Fall 2012

    C l l ti th it l

  • 7/28/2019 Eng364 Week4 Parallel-IO

    46/82

    46

    Calculating the capacitor value

    Given R = 1K, bounce time = 5ms, what is thevalue of C such that the output voltage seenby the MCU = 0.7V?

    Vout = 5 5e-t/RC

    0.7 >= 5 5e -5ms/(1K.C)

    0.86 = e 5ms/(1K.C)

    1.16 > = e 5ms/(1K.C) ln(1.16) >= 5ms/(1K.C)

    C >= 5ms/(1K.ln(1.16)) = 33micro Farad.

    ENG3640 Fall 2012

  • 7/28/2019 Eng364 Week4 Parallel-IO

    47/82

    47

    Integrating Debouncer: The capacitor value is

    chosen such that thevoltage does not exceedthe 0.7-V threshold of theNOT gate while it isbouncing.

    5V

    Vout

    R

    C

    74LS14

    MCU

    Input Port

    Problem?

    ENG3640 Fall 2012

  • 7/28/2019 Eng364 Week4 Parallel-IO

    48/82

    48

    Integrating Debouncer:

    5V

    Vout1K

    C

    74LS14

    MCU

    Input Port22 ohm

    Current can be large (causes spark), these sparkswill produce carbon deposits on the switch that willbuild up until switch no longer works.

    To limit the current a small resistor is placed inseries with the switch.

    ENG3640 Fall 2012

  • 7/28/2019 Eng364 Week4 Parallel-IO

    49/82

    49

    Software Debouncing

    To debounce a switch we can use thefollowing simple approach:

    1. A software time delay is used that providesa time delay (usually 10-20 ms) longer than

    the duration of the switch bouncing action.

    2. So if switch goes low, wait for longer than10ms or 20ms and then test for the switch

    still being low.

    ENG3640 Fall 2012

  • 7/28/2019 Eng364 Week4 Parallel-IO

    50/82

    50

    Keyboard/Keypad Interfacing Keyboards are used to enter input into a computer.

    A common type of keyboard is the matrix type.

    It saves an amount of I/O wiring because the keys share wires.

    Each has its own combination of row and column.

    ENG3640 Fall 2012

    K d I t f i

  • 7/28/2019 Eng364 Week4 Parallel-IO

    51/82

    51

    Keypad Interfacing Each key has an identifying number (key code or scan code)

    as well as a character or function associated with it.

    For example key 11 has the character (0). How does the MCU identify that key 11 was pressed?

    By Sending a signal to terminal G and Checking terminal E, itwill find a short circuit thus it will identify the key to be 11.

    ENG3640 Fall 2012

    K d D di

  • 7/28/2019 Eng364 Week4 Parallel-IO

    52/82

    52

    Keypad Decoding

    PP7 PP6 PP5 PP4 Selected keys

    1

    1

    1

    0

    1

    1

    0

    1

    1

    0

    1

    1

    0

    1

    1

    1

    0,

    4,

    8,

    C,

    1,

    5,

    9,

    D,

    2,

    6,

    A,

    E,

    and 3

    and 7

    and B

    and F

    Table 7.6 Sixteen-key keypad row selections

    A

    B

    C

    D

    E

    F

    0

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10KW

    VCC

    PP7

    PP6

    PP5

    PP4

    PP3

    PP2

    PP1

    PP0

    68HC12

    Figure 7.23 Sixteen-key keypad connected to 68HC12

    1. To detect a short circuit, the MCU drives one of the output lines low

    2. Checks the corresponding input line, if it is low, key was pressed.3. If the key was not pressed, the open circuit allows the resistor to pull up

    the input line to logic high.

    4. The combination of both low logic column and row will identify thepressed key.

    1

    0

    K d D di

  • 7/28/2019 Eng364 Week4 Parallel-IO

    53/82

    53

    Keypad Decoding

    Write an assembly subroutine that reads a character from the keypad.

    The subroutine should perform keypad scanning, debouncing, andASCII code generation.

    Steps:

    1. Configure Port H such that 4 MSB are output and 4 LSB are input.

    2. Select the row containing keys 0,1,2,3

    3. If Key 0 pressed then

    4. debounce key 0 (i.e. jump to subroutine wait 10ms)

    5. get the ASCII code of 0

    6. else if key 1 pressed then

    7. debounce key 1 (i.e. jump to subroutine wait 10ms)

    See page 265, Example 7.9 in your text book

    ENG3640 Fall 2012

    Liquid Crystal Displays

  • 7/28/2019 Eng364 Week4 Parallel-IO

    54/82

    54

    Liquid Crystal Displays

    Liquid Crystal Displays (LCDs) are widely used inmicrocomputer systems (Embedded Systems).

    1. They are used in watches, calculators, instrument panels, consumerelectronic displays (VCRs)

    Advantages over LEDs and 7-Segment Displays?1. Low power consumption with respect to LEDs. This allows the

    display and even the computer system to be battery operated.

    2. LCDs are more flexible in their sizes and shapes. So this permits thecombination of numbers-letters, graphics to be driven withrelatively simple interface.

    Disadvantage?

    1. Low response.

    ENG3640 Fall 2012

    Liquid Crystal Displays: Structure

  • 7/28/2019 Eng364 Week4 Parallel-IO

    55/82

    55

    Liquid Crystal Displays: Structure

    1. An LCD display has two platesseparated by crystal material.

    2. The polarizer plates are usedto pass light (acts like acapacitor).

    3. The liquid crystal can be made

    to pass or stop light.

    4. Unlike LEDs that convertelectric power to optical power,an LCD uses AC voltage tochange the light.

    5. An LCD could be either ofreflective type or an absorptiontype.

    ENG3640 Fall 2012

    Liquid Crystal Displays: Operation

  • 7/28/2019 Eng364 Week4 Parallel-IO

    56/82

    56

    Liquid Crystal Displays: Operation

    1. The light energy is supplied by(a) room (b) separate back light.

    2. The LCD display requires analternating excitation waveapplied to selected electrodes tocharge selected areas.

    3. The excitation wave develops anelectrostatic field to align theliquid crystal molecules in theseselected areas.

    4. When the crystals are aligned

    they allow light to pass throughto the mirror.

    5. In the charged areas the mirrorreflects more light.

    ENG3640 Fall 2012

    Liquid Crystal Displays: Operation

  • 7/28/2019 Eng364 Week4 Parallel-IO

    57/82

    57

    Liquid Crystal Displays: Operation

    An LCD display requires an alternating excitation wave applied

    to selected electrodes to change selected areas.

    A constant (DC) excitation signal will polarize and destroythe crystal.

    60 HzOscillator

    Control

    BP

    FP

    XOR

    VLCD

    Front Plane

    Liquid Crystal Material

    Back Plane

    ENG3640 Fall 2012

    Liquid Crystal Displays: Operation

  • 7/28/2019 Eng364 Week4 Parallel-IO

    58/82

    58

    Liquid Crystal Displays: Operation The oscillator output BP is a square wave with frequency of 60Hz.

    When the Control signal is low, FP is in phase with BP.

    Therefore, VLCDwill be zero (display is blank).

    When control signal is high, VLCD will be an AC square wave andthe display reflects light (display is visible).

    ENG3640 Fall 2012

    Liquid Crystal Displays: HD44780

  • 7/28/2019 Eng364 Week4 Parallel-IO

    59/82

    59

    Liquid Crystal Displays: HD44780 The HD44780 is an industry standard LCD controller.

    An I/O port can easily be used to interface with the HD44780.

    By controlling the value of RS and R/W the MCU can easily eithersends instructions or data to the controller.

    Most operations require 40 micro seconds to complete.

    To be used in LAB #4.

    ENG3640 Fall 2012

  • 7/28/2019 Eng364 Week4 Parallel-IO

    60/82

    60

    HD44780 LCD Controller Instr. Set

    Instruction Command Code Description Time

    Clear Display 00000001 Clears Display 1.64 ms

    Cursor Home 0000001x Returns cursor to home position 1.64 ms

    Function Set 001 DL N F * * Sets interface data length, # ofdisplay lines, char font

    40 micro sec

    Display on/offcontrol 0000 1 D C B Set on/off of all display (D),cursor on/off (C), and blink (B) 40 micro sec

    D: display on/off. 0=off, 1=on

    F: font size 0=5x7 dots, 1=5x10 dots

    B: cursor blink on/off. 0=off, 1=on

    N: number of lines. 0=1 line, 1=2 lines

    ENG3640 Fall 2012

  • 7/28/2019 Eng364 Week4 Parallel-IO

    61/82

    61

    LCD Initialization Routine

    initlcd ldaa #$3C ; configure display format to 2x40jsr lcdcmd ; ldaa #$0f ; turn on display and cursor

    jsr lcdcmd ; ldaa #$14 ; shift cursor right

    jsr lcdcmd ; ldaa #$01 ; clear display and return cursor to home

    jsr lcdcmd ; rts

    ; Send a command in A to the LCD command registerlcdcmd staa cmd_reg ; write command

    jsr delay40 ; waitrts

    HD44780: Initialization

    ENG3640 Fall 2012

  • 7/28/2019 Eng364 Week4 Parallel-IO

    62/82

    ENG3640 Fall 2012 62

    Interfacing: Loads

  • 7/28/2019 Eng364 Week4 Parallel-IO

    63/82

    ENG3640 Fall 2012 63

    Interfacing: Loads

    The second case is to loads which willrequire more current than 10mA

    1. We might want to connect an LED or small relay toan output of the MCU.

    2. To do this we need to add a resistor to limit thecurrent or a driver IC to protect the MCU fromdangerous current levels or transients.

    The Maximum Ratingsin the electrical

    specifications for the 68HC12 is +/- 25mA. This Maximum ratings give the value that

    if exceeded may destroy the part.

    Interfacing: No Loads

  • 7/28/2019 Eng364 Week4 Parallel-IO

    64/82

    ENG3640 Fall 2012 64

    Interfacing: No-Loads

    The data sheet for a digital device lists two values

    for the voltage output levels.1. The first is for small loads, typically 10mA (use for

    the same family, i.e HCMOS devices)

    2. This represents about 10 HCMOS logic gates.

    Interfacing: Current Parameters

  • 7/28/2019 Eng364 Week4 Parallel-IO

    65/82

    ENG3640 Fall 2012 65

    Interfacing: Current Parameters

    The interface current parameters are the outputcurrents, IOH and IOL, and the input leakage currentIIN.

    1. IOH is the current f low ing ou tof a h igh output

    2. IOL is the current f low ing outof a low output

    3. IIN is the leakage current that flows into or out of an input pin.

    These currents are used to determine the stat icfanout o f a device, that is, the number of inputs thatcan be connected to one output while preserving the

    required voltage margins.1. Static fanout for a low output is: nL = | IOL,max |/| IIn |

    2. Static fanout for a high output is: nH = | IOH,max |/| IIn |

    3. n = min [ nH, nL ]

    Interfacing: Simple Load Model

  • 7/28/2019 Eng364 Week4 Parallel-IO

    66/82

    ENG3640 Fall 2012 66

    Interfacing: Simple Load Model

    To determine the output

    characteristics, we need to model theport output as shown.

    Rpis the equivalent resistance of theinternal PMOS, and Rn is the equivalent

    resistor of the internal NMOS. When the output is high, the PMOS is

    on and the NMOS is off.

    Assume that the resistance for a transistorthat is off is infinite.

    Rp(on)max = VDD-VOH,min / | IOH |

    Rn(on)max = VOL,max / | IOL |

  • 7/28/2019 Eng364 Week4 Parallel-IO

    67/82

  • 7/28/2019 Eng364 Week4 Parallel-IO

    68/82

    ENG3640 Fall 2012 68

    Operating Modes Normal Modes: Some registers and bits are protected against

    accidental changes. Special Modes: Protected control registers and bits are allowed

    greater access for special purposes such as testing and emulation.

    BKGD MODB MODA ModePort APort B

    Port C Port D

    0 0 0 Special single-chip G.P.(1) I/O G.P. I/O G.P. I/O

    0 0 1 Special expanded narrow ADDR DATA G.P. I/O

    0 1 0 Special peripheral ADDR DATA DATA

    0 1 1 Special expanded wide ADDR DATA DATA

    1 0 0 Normal single chip G.P. I/O G.P. I/O G.P. I/O

    1 0 1 Normal expanded narrow ADDR DATA G.P. I/O

    1 1 0Reserved

    (forced to peripheral)

    1 1 1 Normal expanded wide ADDR DATA DATA

  • 7/28/2019 Eng364 Week4 Parallel-IO

    69/82

    ENG3640 Fall 2012 69

    Operating Modes

    Normal Single Chip: No external buses. The MCU operates as astand-alone device and all program and data resources are onchip.

    Special Single-Chip Mode: This mode can be used to force theMCU to active Background Debug Mode (BDM).

    Normal Expanded Wide Mode: The 16-bit external address bususes port A for the high byte and port B for the low byte. The16-bit external data bus uses port C for the high byte and port Dfor the low byte (Factory Configured!)

    Special Peripheral Mode: The CPU is not active in this mode.

    An external master can control on-chip peripherals for testingpurposes.

    Normal Drive Strength Output Characteristics

  • 7/28/2019 Eng364 Week4 Parallel-IO

    70/82

    ENG3640 Fall 2012 70

    Normal Drive Strength Output Characteristics

    Parameter 68HC912B32 Value

    VOH,min (no load, IOH < 10m A) VDD-0.2V

    VOH,min

    (IOH

    = -0.8mA) VDD

    -0.8V

    VOL,max (no-load, IOL < 10m A) 0.2V

    VOL,max (IOL = 1.6mA) 0.4V

    R p(on), max 1000W

    R n(on), max 250W

    Connecting an LED to an Output Port

  • 7/28/2019 Eng364 Week4 Parallel-IO

    71/82

    ENG3640 Fall 2012 71

    Connecting an LED to an Output Port

    Step 1. Determine RL such that IL is at least 5mA.1. IL flows through RL the LED, and through R n(on) . R p(on) is

    infinite because PMOS is off.

    2. RL = (VDD - IF.R n(on) VF)/ IF3. = (5V 5mA.250W 2V) / 5mA = 150

    4. The closest 1% standard resistor value is 147

    Step 2. Determine if 147 is large enough to limit thecurrent to less than 25mA under all conditions andassume VDD,max = +5V (+-10%) = 5.5V

    1. Conservative approach: set R n(on), min =0 and V F,min = 02. Practical approach: use 50% of the maximum values for

    R n(on), min andV F,min

    Connecting an LED to an Output Port

  • 7/28/2019 Eng364 Week4 Parallel-IO

    72/82

    ENG3640 Fall 2012 72

    Connecting an LED to an Output Port

    Conservative Approach:IF,max = (VDD,max) / (RL,min)

    = 5.5V/145.5 = 37.8mA

    This exceed the max current specs (25mA)which means that the MCU could bedestroyed.

    Another IC such as a 74AC240 would have tobe used to drive the LED

    Connecting an LED to an Output Port

  • 7/28/2019 Eng364 Week4 Parallel-IO

    73/82

    ENG3640 Fall 2012 73

    Connecting an LED to an Output Port

    Practical Estimation (Cost of adding an extra IC canbe prohibitive for some cost-sensitive designs!)

    IF,max = (VDD,maxVF,min)) / (RL,min + R n(on).min)

    = (5.5V-1V)/(145.5 ohm 125 ohm) =16.64mA

    This is well within the 25mA limit of theoutput and the maximum current limit of theLED.

    PORTJ: Initialization

  • 7/28/2019 Eng364 Week4 Parallel-IO

    74/82

    74

    PORTJ: Initialization

    The software initialization sets bits in PUPEJ register to enablepull-up or pull-down.

    For each Port J pin that is enabled for pull-up or pull-down, thecorresponding bit in the PUPSJ registerdetermines ifit is pull-up(1) or pull-down (0).

    // MC68HC812A4

    // Port J bit 1 is connected to a switch to +5, using internal pull-down

    // port J bit 0 is connected to a switch to 0, using internal pull-up

    Void Initialization(void){

    DDRJ &= 0xFC; // PJ1 PJ0 inputs

    KPOLJ |= 0x03; // flags set on the rise of PJ1 and PJ0

    KWIEJ &= 0xFC; // disarm PJ1, PJ0

    KWIFJ = 0x03; // clear flags

    PUPSJ = (PUPSJ&0xFC)|0x01; // pull-down on PJ1, pull-up on PJ0

    PULEJ |= 0x03;} // enable pull-up and pull-down

    ENG3640 Fall 2012

  • 7/28/2019 Eng364 Week4 Parallel-IO

    75/82

    75

    Hardware Debouncing: Flip Flop

    An SR latch can be used to debounce a switch

    +5V

    +5V

    R

    S

    Q

    ENG3640 Fall 2012

  • 7/28/2019 Eng364 Week4 Parallel-IO

    76/82

    76

    Wait for press

    RTS

    Switch

    Wait 10 ms

    Software Flowcharts for debouncing

    Switch

    Wait for release

    Wait 10 ms

    RTS

    pressednot pressed not pressedpressed

    Polling vs Interrupt Driven?

    ENG3640 Fall 2012

    Using Hardware Decoding Chips

  • 7/28/2019 Eng364 Week4 Parallel-IO

    77/82

    77

    Using Hardware Decoding Chips 8x8 matrix keyboard with 64-keys, 74HC138 (3-to-8 Decoder), and74HC151 (multiplexer).

    PC0, PC1, PC2 are used to send values to rows

    The MCU will scan the columns one by one via PC3, PC4, PC5

    ENG3640 Fall 2012

  • 7/28/2019 Eng364 Week4 Parallel-IO

    78/82

    78

    Software Debouncing

    To debounce a switch we can use several approaches:

    Approach #1:

    1. A software time delay is used that provides a time delay (usually 10-20 ms) longer than the duration of the switch bouncing action.

    2. So if switch goes low, wait for longer than 10ms or 20ms and thentest for the switch still being low.

    Approach #2:

    1. Initialize a counter with a value of 10 and after the first logic lowlevel is detected, poll the switch every millisecond.

    2. If the switch output is low, decrement the counter. If the switchoutput is high increment the counter.

    3. When the counter reaches zero, we know the switch output hasbeen low for at least 10 ms. But if the counter reaches 20, we knowthat the switch has been open for at least 10 ms.

    ENG3640 Fall 2012

    Keyboard Decoding

  • 7/28/2019 Eng364 Week4 Parallel-IO

    79/82

    79

    Keyboard Decoding Some keyboards have an extra common terminal. So if a key is

    pressed, a short circuit occurs between the common and the

    keys row and column line. When PC0 is driven low and one key is pressed, one of the row

    inputs and one of the column inputs will be low.

    The keyboard software driver routine checks which inputs arelow and determine the key code.

    ENG3640 Fall 2012

    Keypad Decoding

  • 7/28/2019 Eng364 Week4 Parallel-IO

    80/82

    80

    Keypad Decoding

    1. To detect a short circuit, the MCUdrives one of the output lines low

    2. Checks the corresponding inputline.

    3. If it is low, the key was pressed.

    4. If the key was not pressed, theopen circuit allows the resistor topull up the input line to logic high.

    5. The combination of both low logiccolumn and row will identify the

    pressed key.6. For example, to check key code 5

    the MCU drives PC1 (terminal J)low and checks the input at PC5(terminal E) .

    ENG3640 Fall 2012

    Keypad Interfacing

  • 7/28/2019 Eng364 Week4 Parallel-IO

    81/82

    81

    Keypad Interfacing

    UnderstandKeypad Interface*(Note: 68HC12812A4 hasinternal pull-up on all pins;

    not all are shown here)

    To identify the key code,the MCU scans each

    contact in sequence.

    ENG3640 Fall 2012

    Keypad Decoding: 4x4 keypad

  • 7/28/2019 Eng364 Week4 Parallel-IO

    82/82

    MCUchecks

    these lines

    MCUchecks

    these lines

    MCUsends lowsignal to

    these lines