eet 2261 unit 7 i/o pins and ports

45
EET 2261 Unit 7 I/O Pins and Ports Read Almy, Chapters 12 – 15. Homework #7 and Lab #7 due next week. Quiz next week.

Upload: yanka

Post on 24-Feb-2016

43 views

Category:

Documents


1 download

DESCRIPTION

EET 2261 Unit 7 I/O Pins and Ports. Read Almy , Sections 12 – 15. Homework #7 and Lab #7 due next week. Quiz next week. General-Purpose Input/Output Ports. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: EET 2261 Unit  7 I/O Pins and  Ports

EET 2261 Unit 7I/O Pins and Ports

Read Almy, Chapters 12 – 15.

Homework #7 and Lab #7 due next week.

Quiz next week.

Page 2: EET 2261 Unit  7 I/O Pins and  Ports

• A general-purpose I/O port is a group of pins (typically 8) that can be connected to generic digital input or output devices.• Example of a generic digital input device: a

switch that switches between 0 V and +5 V.

• Example of a generic digital output device: an LED.

• “Generic” means that these devices don’t have specialized synchronization or timing requirements, as some digital devices do.

General-Purpose Input/Output Ports

Page 3: EET 2261 Unit  7 I/O Pins and  Ports

• Our HCS12 chip has ten general-purpose digital I/O ports:

Input/Output Ports on the HCS12

Port Number and Names of BitsPort A 8 bits: PA0 to PA7Port B 8 bits: PB0 to PB7Port E 8 bits: PE0 to PE7 (PE0 and PE1 are input only)Port H 8 bits: PH0 to PH7Port J 4 bits: PJ0, PJ1, PJ6, PJ7Port K 7 bits: PK0 to PK5, PK7Port M 8 bits: PM0 to PM7Port P 8 bits: PP0 to PP7Port S 8 bits: PS0 to PS7Port T 8 bits: PT0 to PT7

Page 4: EET 2261 Unit  7 I/O Pins and  Ports

Multiplexed I/O Pins• To keep the pin count to a reasonable number,

most of the pins for the general-purpose I/O ports can also serve as inputs or outputs to specific modules on the HCS12. You must choose how you want to use the pins.

• Example: If you want to use the chip’s Enhanced Capture Timer module, then you can’t use Port T for general-purpose I/O.

Page 5: EET 2261 Unit  7 I/O Pins and  Ports

Multiplexed I/O Pins (Continued)• To see which

functions are shared by a single pin, see block diagram on page 6 of textbook or page 23 of Device User Guide.

• Also see pin diagram on page 52 of Device User Guide.

Page 6: EET 2261 Unit  7 I/O Pins and  Ports

Special-Function Registers• The HCS12 has hundreds of special-

function registers, some of which play a role in using the general-purpose I/O ports.

• Most of these registers are either:• Data registers, which transfer data from place

to place.

• Control registers, which control various aspects of the chip’s operation.

• Status registers, which hold status information about events that have occurred.

Page 7: EET 2261 Unit  7 I/O Pins and  Ports

How to Access the Special-Function Registers

• In the HCS12’s memory map (page 26 of Device User Guide), addresses from $0000 to $03FF are assigned to the special-function registers.

• When you execute an LDAA or STAA instruction to one of these addresses, you’re not reading or writing to memory; instead, you’re reading or writing to a special-function register.

Page 8: EET 2261 Unit  7 I/O Pins and  Ports

List of Special-Function Registers• Pages 27-49 in the Device User Guide list

all of the special-function registers and their addresses.• Example: the special-function register named

PORTA is assigned address $0000.

• So to copy data from Accumulator A to PORTA, you would use STAA $0000.

Page 9: EET 2261 Unit  7 I/O Pins and  Ports

• Each I/O port has at least two special-function registers:

1. A data register (such as PORTA or PORTB) that holds data traveling in or out through the port.

2. A control register called a data-direction register (such as DDRA or DDRB) that controls whether the port’s pins are configured as inputs or as outputs.

Special-Function Registers and the Ports

Page 10: EET 2261 Unit  7 I/O Pins and  Ports

• Each bit in a DDR configures the corresponding pin of the port as an input (if the bit = 0) or as an output (if the bit = 1).

• Example: Suppose that on Port A we want to use pins 0 to 3 as input pins, and we want to use pins 4 to 7 as output pins. Then we must write the value %11110000 (or $F0) to DDRA:

LDAA #$F0 ;0-3 in, 4-7 out STAA $0002 ;DDRA is at $0002

Data Direction Registers

Page 11: EET 2261 Unit  7 I/O Pins and  Ports

• Suppose we want to send %01011011 out on Port A. We must:

1. Use DDRA to configure all of Port A’s pins as output pins.

2. Send %01011011 to PORTA.

• This code will do the job: LDAA #$FF ;outputs STAA $0002 ;DDRA is at $0002 LDAA #$5B ;data to be sent STAA $0000 ;PORTA is at $0000

Example: Using Port A for Output

Page 12: EET 2261 Unit  7 I/O Pins and  Ports

• Suppose we want to read in a byte from Port A. We must:

1. Use DDRA to configure all of Port A’s pins as input pins. (May not be needed, since by default ports are configured as inputs.)

2. Load an accumulator from PORTA.

• This code will do the job: LDAA #$00 ;inputs STAA $0002 ;DDRA is at $0002 LDAA $0000 ;load from PORTA

Example: Using Port A for Input

Page 13: EET 2261 Unit  7 I/O Pins and  Ports

• Programs are easier to read and maintain if we use EQU directives to define labels for commonly used numbers.• Example: Instead of this:

LDAA #$00 STAA $0002 LDAA $0000

• We can do this:PORTA EQU $0000DDRA EQU $0002 LDAA #$00 STAA DDRA LDAA PORTA

Using Labels for the Addresses

Page 14: EET 2261 Unit  7 I/O Pins and  Ports

• Including the other lines that we use in all our programs, here’s a complete program to read in a byte from Port A:

ABSENTRY Entry ORG $2000PORTA EQU $0000DDRA EQU $0002

Entry: LDAA #$00 STAA DDRA LDAA PORTA BRA *

END

Using Labels for the Addresses

Page 15: EET 2261 Unit  7 I/O Pins and  Ports

• When you create a project, CodeWarrior automatically creates two files named derivative.inc and mc9s12dg256.inc. (The second file’s name depends on the device you select in the New Project Wizard.)

Include Files

• These are called include files, which is why the names end in .inc.

Page 16: EET 2261 Unit  7 I/O Pins and  Ports

• These files contain EQU directives that define thousands of commonly used labels such as PORTA, DDRA, and so on.

• You should not edit these files.

What’s in These Include Files?

Page 17: EET 2261 Unit  7 I/O Pins and  Ports

• The following assembler directive tells CodeWarrior to add everything from those two include files into your program.

INCLUDE 'derivative.inc'

• This will save you from having to type your own EQU directives for PORTA, DDRA, and all of the other special-function registers.

How to Use These Include Files

Page 18: EET 2261 Unit  7 I/O Pins and  Ports

• Now we can use PORTA and DDRA in our program without typing EQU statements that define those labels.

INCLUDE 'derivative.inc' ABSENTRY Entry ORG $2000

Entry: LDAA #$00 STAA DDRA LDAA PORTA BRA *

END

Earlier Example, But this Time Using the Include Files

Page 19: EET 2261 Unit  7 I/O Pins and  Ports

• CAUTION! Note variations:

Names of the Port Registers and Bits

Port Name of Data Register

Name of Data Direction Register

Names of Bits

Port A PORTA DDRA PA0 to PA7Port B PORTB DDRB PB0 to PB7Port E PORTE DDRE PE0 to PE7Port H PTH DDRH PH0 to PH7Port J PTJ DDRJ PJ0, PJ1, PJ6, PJ7Port K PORTK DDRK PK0 to PK5, PK7Port M PTM DDRM PM0 to PM7Port P PTP DDRP PP0 to PP7Port S PTS DDRS PS0 to PS7Port T PTT DDRT PT0 to PT7

Page 20: EET 2261 Unit  7 I/O Pins and  Ports

• On our trainer board, Port H is wired to eight DIP switches.

• If we want to use these switches, we must configure Port H for input.

• (See Schematic Diagram 4.)

Switches on the Dragon12 Trainer

Page 21: EET 2261 Unit  7 I/O Pins and  Ports

• On our trainer board, Port B is wired to eight LEDs.

LEDs on the Dragon12 Trainer

• If we want to use these LEDs, we must configure Port B for output. We must also configure PJ1 as an output and set it equal to 0. (See p. 24 of Dragon12 manual and Schematic Diagram 4.)

Page 22: EET 2261 Unit  7 I/O Pins and  Ports

• On our trainer board, bit 5 of Port T is wired to a speaker (buzzer).

• See p. 28 of Dragon12 manual and Schematic Diagram 1.

Speaker on the Dragon12 Trainer

• To make a noise on the speaker, you must configure bit PT5 for output, and then cause this output pin to alternate rapidly between HIGH and LOW. The pitch of the sound depends on how quickly you alternate it.

Page 23: EET 2261 Unit  7 I/O Pins and  Ports

• We’ve used BCLR and BSET to manipulate single bits in memory.

• We can also use BCLR and BSET to manipulate single bits of an output port. (Recall that I/O ports “look” just like memory locations to the HCS12’s CPU.)

Review: BCLR and BSET

Page 24: EET 2261 Unit  7 I/O Pins and  Ports

• Example: Suppose we want to set bit 3 of Port E.

• Here’s how to do it:

BSET $0008, %00001000

• Better yet, use a label instead of a number for the address:

BSET PORTE, %00001000

Using BSET with an I/O Port: Example

Page 25: EET 2261 Unit  7 I/O Pins and  Ports

• We’ve used BRCLR and BRSET to branch based on bits in memory.

• We can also use BRCLR and BRSET to branch based on bits of an input port.

Review: BRCLR and BRSET

Page 26: EET 2261 Unit  7 I/O Pins and  Ports

• Example: Suppose we want to branch if bit 3 of Port E is a 1 (in other words, if bit 3 of Port E is set).

• Here’s how to do it:

BRSET $0008, %00001000, GoHere

• Better yet, use a label instead of a number for the address:

BRSET PORTE, %00001000, GoHere

Using BRSET with an I/O Port: Example

Page 27: EET 2261 Unit  7 I/O Pins and  Ports

• Unused input pins must not be allowed to float.

• An output pin’s current consumption is roughly proportional to the rate (frequency) at which the pin’s values changes.

• Example: If an output pin is being used to make an LED blink on and off, a blink rate of 10 times per second consumes roughly twice as much current as a rate of 5 times per second.

Some Electrical Considerations

Page 28: EET 2261 Unit  7 I/O Pins and  Ports

• Two important considerations when connecting a mechanical switch to an input pin:

1. The pin must never be allowed to float. A common way to achieve this is to use the HCS12’s internal pull-up resistors.

2. The switch may need to be debounced, either in hardware or in your program.

Connecting a Mechanical Switch

Page 29: EET 2261 Unit  7 I/O Pins and  Ports

Pull-Up Resistors

A pull-up resistor is a resistor with one end connected to a HIGH voltage level and the other end connected to a point in a digital circuit.

The resistor’s purpose is to pull up the voltage at that point to a HIGH level when it would otherwise be in a float condition (not HIGH or LOW).

Page 30: EET 2261 Unit  7 I/O Pins and  Ports

Pull-Up Resistor (Continued)

Good explanation at http://www.seattlerobotics.org/encoder/mar97/basics.html

Page 31: EET 2261 Unit  7 I/O Pins and  Ports

Pull-Up Resistors on the Dragon12 Board’s DIP Switches

The Dragon12’s eight DIP switches are connected to pull-up resistors (and pull-down resistors) on the board. See Schematic Diagram 4.

Page 32: EET 2261 Unit  7 I/O Pins and  Ports

Pull-Up Resistors on the Red Trainer’s Data Switches

The red trainer’s eight data switches are also connected to pull-up resistors. (Schematic diagram from trainer’s user manual.)

Page 33: EET 2261 Unit  7 I/O Pins and  Ports

Internal Pull-Up Resistors on the HCS12

The HCS12, like many microcontrollers, has an internal pull-up resistor on each I/O pin.

These pull-up resistors are disabled by default, but the programmer can enable them. Very handy for when you’re connecting an

HCS12 input pin to a switch that’s not already connected to a pull-up resistor.

Next slide shows the hardware associated with a single pin on Port A.

Page 34: EET 2261 Unit  7 I/O Pins and  Ports

Hardware Associated with a Single I/O Pin on Port A

See page 143 of text.

This figure applies to Ports A, B, E, and K.

The other ports are more complicated. See figure on page 141 of text.

Page 35: EET 2261 Unit  7 I/O Pins and  Ports

Activating the Internal Pull-Up Resistors: Ports A, B, E, K

How do we activate the HCS12’s internal pull-up resistors? By using bits in special function registers.

For Ports A, B, E, and K, use the PUCR register. See page 24 of the MEBI Block User Guide.

Page 36: EET 2261 Unit  7 I/O Pins and  Ports

Activating the Internal Pull-Up Resistors: Ports H, J, M, P, S, T

For these ports, use the PERx and PPSx registers. See page 37 of the Port Integration Module Block User Guide.

Page 37: EET 2261 Unit  7 I/O Pins and  Ports

• Two important considerations when connecting a mechanical switch to an input pin:

1. The pin must never be allowed to float. A common way to achieve this is to use the HCS12’s internal pull-up resistors.

2. The switch may need to be debounced, either in hardware or in your program.

Review: Connecting a Mechanical Switch

Page 38: EET 2261 Unit  7 I/O Pins and  Ports

Contact Bounce

Although it’s not obvious to us in everyday life, most mechanical switches suffer from contact bounce.

When you flip the switch lever, the contacts inside the switch repeatedly bounce open and closed for a short time ( 50 ms).

This can lead to problems in digital circuits, as shown in the following figures from your Digital Electronics textbook.

Page 39: EET 2261 Unit  7 I/O Pins and  Ports

Copyright ©2012 by Pearson Education, Inc.All rights reserved.

Digital Electronics: A Practical Approach with VHDL, 9th EditionWilliam Kleitz

Figure 11.37 Switch used as a clock input to a toggle flip-flop.

Page 40: EET 2261 Unit  7 I/O Pins and  Ports

Copyright ©2012 by Pearson Education, Inc.All rights reserved.

Digital Electronics: A Practical Approach with VHDL, 9th EditionWilliam Kleitz

Figure 11.38 Waveform at point Cp for Figure 11–37 assuming Q starts HIGH.

Page 41: EET 2261 Unit  7 I/O Pins and  Ports

Ways to Solve Contact Bounce

There are several standard ways to debounce a switch.

In hardware, we can use a latch or flip-flop, or an RC circuit with a Schmitt-trigger gate. See Section 114 of your Digital Electronics textbook (by Kleitz) for details.

As noted on page 136 of our textbook, debouncing can also be done in your program by checking the input at well-spaced time intervals.

Page 42: EET 2261 Unit  7 I/O Pins and  Ports

No Debounce Circuit on the Dragon12 Board’s DIP Switches

The Dragon12 does not include any hardware to debounce the DIP switches.

Page 43: EET 2261 Unit  7 I/O Pins and  Ports

Debounce Circuit on Our Red Trainer’s Pulse Switches

The red trainer includes internal hardware to debounce the pulse switches.

Page 44: EET 2261 Unit  7 I/O Pins and  Ports

No Debounce Circuit on Red Trainer’s Data Switches

The trainer does not include any internal hardware to debounce the data switches.

Page 45: EET 2261 Unit  7 I/O Pins and  Ports

• Un

Some Timing Considerations