pull-up resistor

Download Report

Transcript pull-up resistor

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.
General-Purpose Input/Output
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.
Input/Output Ports on the HCS12
•
Our HCS12 chip has ten general-purpose digital
I/O ports:
Port
Number and Names of Bits
Port A
8 bits: PA0 to PA7
Port B
8 bits: PB0 to PB7
Port E
8 bits: PE0 to PE7 (PE0 and PE1 are input only)
Port H
8 bits: PH0 to PH7
Port J
4 bits: PJ0, PJ1, PJ6, PJ7
Port K
7 bits: PK0 to PK5, PK7
Port M
8 bits: PM0 to PM7
Port P
8 bits: PP0 to PP7
Port S
8 bits: PS0 to PS7
Port T
8 bits: PT0 to PT7
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.
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.
Special-Function Registers
•
The HCS12 has hundreds of specialfunction 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.
How to Access the SpecialFunction Registers
•
In the HCS12’s memory map (page 26 of
Device User Guide), addresses from $0000
to $03FF are assigned to the specialfunction 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 specialfunction register.
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.
Special-Function Registers and the
Ports
•
Each I/O port has at least two specialfunction 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.
Data Direction Registers
•
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
Example: Using Port A for Output
•
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 Input
•
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
Using Labels for the Addresses
•
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 $0000
DDRA EQU $0002
LDAA #$00
STAA DDRA
LDAA PORTA
Using Labels for the Addresses
•
Including the other lines that we use in all our
programs, here’s a complete program to read in a
byte from Port A:
PORTA
DDRA
Entry:
ABSENTRY Entry
ORG $2000
EQU $0000
EQU $0002
LDAA #$00
STAA DDRA
LDAA PORTA
BRA *
END
Include Files
•
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.)
•
These are
called
include files,
which is why
the names
end in .inc.
What’s in These Include Files?
•
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.
How to Use These Include Files
•
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.
Earlier Example, But this Time
Using the Include Files
•
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
Names of the Port Registers and
Bits
•
CAUTION! Note variations:
Port
Name of Data
Register
Name of Data
Direction Register
Names of Bits
Port A
PORTA
DDRA
PA0 to PA7
Port B
PORTB
DDRB
PB0 to PB7
Port E
PORTE
DDRE
PE0 to PE7
Port H
PTH
DDRH
PH0 to PH7
Port J
PTJ
DDRJ
PJ0, PJ1, PJ6, PJ7
Port K
PORTK
DDRK
PK0 to PK5, PK7
Port M
PTM
DDRM
PM0 to PM7
Port P
PTP
DDRP
PP0 to PP7
Port S
PTS
DDRS
PS0 to PS7
Port T
PTT
DDRT
PT0 to PT7
Switches on the Dragon12 Trainer
•
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.)
LEDs on the Dragon12 Trainer
•
On our trainer board, Port B is wired to
eight LEDs.
•
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.)
Speaker on the Dragon12 Trainer
•
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.
•
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.
Review: BCLR and BSET
•
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.)
Using BSET with an I/O Port:
Example
•
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
Review: BRCLR and BRSET
•
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.
Using BRSET with an I/O Port:
Example
•
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
Some Electrical Considerations
•
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.
Connecting a Mechanical Switch
•
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.
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).
Pull-Up Resistor (Continued)

Good explanation at
http://www.seattlerobotics.org/encoder
/mar97/basics.html
Pull-Up Resistors on the Dragon12
Board’s DIP Switches

The Dragon12’s eight DIP switches are
connected to pull-up resistors (and pulldown resistors) on the board. See
Schematic Diagram 4.
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.)
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.
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.
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.
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.
Review: Connecting a Mechanical
Switch
•
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.
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.
Figure 11.37
Switch used as a clock input to a toggle flip-flop.
Digital Electronics: A Practical Approach with VHDL, 9th Edition
William Kleitz
Copyright ©2012 by Pearson Education, Inc.
All rights reserved.
Figure 11.38
Waveform at point Cp for Figure 11–37 assuming Q starts HIGH.
Digital Electronics: A Practical Approach with VHDL, 9th Edition
William Kleitz
Copyright ©2012 by Pearson Education, Inc.
All rights reserved.
Ways to Solve Contact Bounce



There are several standard ways to
debounce a switch.
In hardware, we can use a latch or flipflop, or an RC circuit with a Schmitttrigger gate. See Section 114 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 wellspaced time intervals.
No Debounce Circuit on the
Dragon12 Board’s DIP Switches

The Dragon12 does not include any
hardware to debounce the DIP
switches.
Debounce Circuit on Our Red
Trainer’s Pulse Switches

The red trainer
includes internal
hardware to
debounce the
pulse switches.
No Debounce Circuit on Red
Trainer’s Data Switches

The trainer
does not
include any
internal
hardware to
debounce the
data switches.
Some Timing Considerations
•
Un