Lab #3

advertisement
Lab 3
EGR 262 – Fundamental Circuits Lab
EGR 262
Fundamental Circuits Lab
Presentation for
Lab #3: Switches and 7-Segment Displays
1
Lab 3
EGR 262 – Fundamental Circuits Lab
2
Protecting Microprocessor Inputs and Outputs
Microprocessors can be easily destroyed by excessive voltages and currents. Inputs and
outputs are often protected using current-limiting resistors and pull-down resistors or
pull-up resistors as discussed below.
Microprocessor Outputs
Most microprocessor outputs can only produce currents in the mA range.
Arduino UNO specifications:
DC current per I/O pin (at 5V): 40 mA max (20 mA recommended)
Total DC current: 200 mA
Be sure to include current-limiting resistors in series with LEDs that are connected to
outputs of the Arduino. If the resistor is omitted, excessive current through the LED
might destroy the Arduino.
Typical values for current-limiting resistors are 220 , 330 , and 470 .
If devices other than LEDs (such as motors, bulbs, etc.) are connected to Arduino
outputs, be sure that their current does not exceed the limits listed above. Other
methods for controlling high current outputs (such as using transistors, motor
controllers, relays, etc) may be covered in later labs.
Lab 3
EGR 262 – Fundamental Circuits Lab
3
Illustration: Protecting Microprocessor Outputs
Arduino
UNO
Arduino
UNO
X
Incorrect way to connect an
LED to the output of the
Arduino
LED
220 

Correct way to connect an
LED to the output of the
Arduino
Note: If a 7-segment display is controlled using 7 Arduino outputs,
7 current-limiting resistors will be needed.
LED
Lab 3
EGR 262 – Fundamental Circuits Lab
4
Microprocessor Inputs
Digital inputs on the Arduino are high-impedance inputs that are not connected
to HIGH or LOW, but are “floating.” The inputs might randomly be read as
HIGH or LOW values, which is why a pull-up resistor or pull-down resistor is
needed. Inputs without pull-up or pull-down resistors are also subject to
damage due to static discharge. See the illustration below.
+5V
+5V
Arduino
UNO
Arduino
UNO
10 k
10 k
“Pull-up
resistor”
“Pull-down
resistor”
Pushbutton switch with a pull-down
resistor. Pushing the switch connects
5V (HIGH) to the input. When the
switch is not pressed, the resistor pulls
the input to ground (LOW).
Pushbutton switch with a pull-up
resistor. Pushing the switch connects
ground (LOW) to the input. When the
switch is not pressed, the resistor pulls
the input to 5V (HIGH).
Lab 3
EGR 262 – Fundamental Circuits Lab
5
Illustration: Protecting Microprocessor Inputs
+5V
Arduino
UNO
+5V
Arduino
UNO
X
Incorrect way to connect 5V
to an Arduino input using a
pushbutton switch
10 k

Correct way to connect 5V
to an Arduino input using a
pushbutton switch
Notes:
• We will typically use pull-down resistors with input switches in lab.
• 10 k is a typical value for a pull-up or pull-down resistor
• The Arduino also has an optional built-in pull-up resistor (see next slide).
Lab 3
EGR 262 – Fundamental Circuits Lab
6
Configuring Arduino Pins as Inputs and Outputs
Arduino Digital I/O pins may be configured in three ways:
1. Inputs
• Example: pinMode(3, INPUT);
// Make pin 3 an input
• This is the default, so you can often omit this statement
2. Outputs
• Example: pinMode(3, OUTPUT);
// Make pin 3 an output
3. Inputs with Internal Pull-up Resistors
• Example: pinMode(3, INPUT_PULLUP) // Make pin 3 an input
// with an internal pull-up R
• No pull-up resistor is connected by default
• The internal pull-up resistor has a value of 20 k - 50 k
Lab 3
EGR 262 – Fundamental Circuits Lab
7
Reading Inputs and Outputs
Inputs: Arduino digital inputs can be read using digitalRead( ).
It is good practice to configure the pin as an input first (although this is the
default).
Example:
Value = digitalRead(6); // Value = LOW or HIGH based on pin 6 state
Example:
if (digitalRead(7) == HIGH)
// typically 3V or higher
Serial.println(“Pin 7 is HIGH”);
else
Serial.println(“Pin 7 is HIGH”);
Outputs: Arduino digital outputs can be read using digitalWrite( ).
Be sure to configure the pin as an output first.
Example:
pinMode(6, OUTPUT); // Make pin 6 an output
digitalWrite(6, LOW); // Make pin 6 LOW (0V)
digitalWrite(6, HIGH); // Make pin 6 HIGH (5V)
Lab 3
EGR 262 – Fundamental Circuits Lab
8
Switch Bounce
Standard switches, such as toggle switches, slide switches, and button switches,
typically exhibit “switch bounce.” When the switch is thrown, the contacts will
bounce for several milliseconds before settling down. This could cause several
transitions which can cause problems in many circuits, including
microcontroller inputs. This problem can often be handled by either changes to
hardware or to software. A hardware solution involves purchasing or
constructing “debounced” switches which insure only one transition from LOW
to HIGH or from HIGH to LOW. Software solutions often involve adding
small delays to give the switch contacts time to settle. The figures below
illustrate the difference in debounced switches and switches that experience
contact bounce.
switch
thrown
switch
thrown
HIGH
HIGH
HIGH
LOW
Debounced switch
LOW
t
HIGH
LOW
You pressed the
button once, but the
microcontroller
thinks you pressed it
3 times!
LOW
Switch with contact bounce
t
Lab 3
EGR 262 – Fundamental Circuits Lab
Poor Example
(switch bounce ignored)
Suppose that you wanted to
toggle an LED on D13 every
time you pressed a button
connected to D9. The
problem is that each time you
press the button, the
microprocessor might think
that you pressed it 4 or 5
times, so the result is
unpredictable.
Poor program! Do
not use. A better
solution is on the
following slide.
9
Arduino
UNO
+5V
D9
Button
switch
220 
D13
LED
10 k
Lab 3
EGR 262 – Fundamental Circuits Lab
Good Example
(switch bounce
properly handled)
This example is similar
to the previous one
except that it handles
switch bounce
properly. It ignores
switch transitions for
up to 50ns.
You can copy the
highlighted
sections into your
program when
you need to work
with pushbutton
switches. Use the
function
readButton( ) in
your program.
10
Lab 3
EGR 262 – Fundamental Circuits Lab
11
Active-HIGH and active-LOW outputs
Outputs of many logic devices are configured as active-HIGH or active-LOW.
Active-HIGH output:
• Output = HIGH (1) to activate (turn on) the output
• Output = LOW (0) to de-activate (turn off) the output
Active-LOW output:
• Output = LOW (0) to activate (turn on) the output
• Output = HIGH (1) to de-activate (turn off) the output
Example:
In the figure on the left, the LED lights when the output of the logic gate is
HIGH. In the figure on the right, the LED lights when the output of the logic
gate is LOW.
220 
5V
LED lights when
output is HIGH
Active-HIGH LED connection
220 
LED lights when
output is LOW
Active-LOW LED connection
Lab 3
EGR 262 – Fundamental Circuits Lab
12
7-segment displays
A 7-segment display is made up of seven LED’s configured to display the
decimal digits 0 through 9. There are two types of 7-segment displays:
1) common anode (all anodes at +5V)
2) common cathode (all cathodes at ground)
Common cathode displays
Common cathode displays require active-HIGH outputs. When the output of
the decoder or microcontroller is HIGH for one segment, 5V is connected to the
anode. Since all cathodes are grounded, the segment lights.
220 
a
b
7-segment
c
decoder/driver IC d
e
or
microcontroller f
g
a
a
b
c f
b
g
d
e
f e d c
g
Common cathode
7 - segment display
anode
cathode
a
Typical segment
Lab 3
EGR 262 – Fundamental Circuits Lab
13
Common anode displays
Common anode displays require active-LOW outputs. When the output of the
decoder or microcontroller is LOW for one segment, 0V is connected to the
cathode. Since all anodes are connected to 5V, the segment lights.
220 
a
b
7-segment
c
decoder/driver IC d
e
or
f
microcontroller g
A “bubble” is sometimes
used to indicate an
active-LOW output
a
a
b
c f
g b
d
e
f e d c
g
Common anode
7-segment display
cathode
a
anode
+5V
Typical segment
We will use
common anode
displays in lab.
EGR 262 – Fundamental Circuits Lab
Lab 3
14
Truth table for common anode displays
For a common anode display:
0 – used to turn a segment ON
1 – used to turn a segment OFF
So we can easily develop a truth table showing which segments should be ON
and which segments should be OFF for each digit.
Digit
a
b
c
d
e
f
g
0
0
0
0
0
0
0
1
1
To display the
digit 0, light all
segments except g
2
3
4
a
f
g
b
5
6
e
d
c
7
8
9
Common-anode
7-segment display
Lab 3
EGR 262 – Fundamental Circuits Lab
15
Schematic for Lab #3
+5V
Button
switch
10 k
Arduino
UNO
D2
D3
D9
D4
D5
D6
D7
D8
220 
+5V
a
b
c
d
e
f
g
a
f
g
b
e d c
Common anode
7-segment display
(see data sheet for pinout)
Notes:
• A pull-down resistor is used with the input switch.
• Current-limiting resistors are used with each of the 7 outputs.
• Active-LOW outputs are used (LOW outputs used to light segments).
Lab 3
EGR 262 – Fundamental Circuits Lab
Writing a function to control
a 7-segment display
Using a function is an
efficient way to communicate
with a 7-segment display.
Write a function such that:
• Segments a-g are tied to
digital outputs 2-8 as
shown on the previous
schematic.
• A common anode 7segment display will be
used.
• Refer to the table generated
on a previous slide for
which segments to light for
each digit.
• Cases 1-9 are not shown,
but will be completed as
part of the Pre-Lab work.
16
Lab 3
EGR 262 – Fundamental Circuits Lab
+5V
Button
switch
10 k
Arduino
UNO
D2
D3
D9
D4
D5
D6
D7
D8
17
220 
+5V
a
b
c
d
e
f
g
a
f
g b
e d c
Common anode
7-segment display
(see data sheet for pinout)
Writing a program for Lab 3
The program should operate as follows:
• Display brief instructions on the computer screen.
• The count should be initialized to 0 and shown on the 7-segment display and the
computer screen.
• When the user presses the button the count should advance and the new value should
be displayed on the 7-segment display and the computer screen.
• The count should be mod-10 (0 to 9 and repeat).
• Use the display_digit( ) function described on the previous slide.
• Use the readButton( ) function provided on an earlier slide to allow for switch
debounce.
• See the following slide for more details on the code.
Lab 3
EGR 262 – Fundamental Circuits Lab
Writing a program for Lab 3
// Global variables for readButton( ) function (see Lab #3 Lecture)
// Other global variables
void setup( )
{ // Define input and output pins
// Set up serial communication
// Display instructions on the computer screen
// Display initial value (0) on 7-segment display
// Display initial value (0) on computer screen
}
void loop( )
{ // If button is pressed:
// Increment counter
// Adjust counter for mod-10 operation
// Display count on computer screen
// Display count on 7-segment display
}
void readButton(int buttonPin)
{
// Use code provided in Lab #3 Lecture
}
void display_digit(N)
{
// See Lab #3 Lecture for details
}
18
Download