lab 3 egr 262 – fundamental circuits lab

18
1 Lab 3 EGR 262 – Fundamental Circuits Lab EGR 262 Fundamental Circuits Lab Presentation for Lab #3: Switches and 7-Segment Displays

Upload: tokala

Post on 25-Feb-2016

70 views

Category:

Documents


1 download

DESCRIPTION

Lab 3 EGR 262 – Fundamental Circuits Lab. EGR 262 Fundamental Circuits Lab Presentation for Lab #3: Switches and 7-Segment Displays. Lab 3 EGR 262 – Fundamental Circuits Lab. Protecting Microprocessor Inputs and Outputs - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lab 3        EGR 262 – Fundamental Circuits Lab

1Lab 3 EGR 262 – Fundamental Circuits LabEGR 262

Fundamental Circuits Lab

Presentation forLab #3: Switches and 7-Segment Displays

Page 2: Lab 3        EGR 262 – Fundamental Circuits Lab

Protecting Microprocessor Inputs and OutputsMicroprocessors 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 OutputsMost microprocessor outputs can only produce currents in the mA range.Arduino UNO specifications:

2Lab 3 EGR 262 – Fundamental Circuits Lab

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.

Page 3: Lab 3        EGR 262 – Fundamental Circuits Lab

Illustration: Protecting Microprocessor Outputs3Lab 3 EGR 262 – Fundamental Circuits Lab

ArduinoUNO

LED

Incorrect way to connect an LED to the output of the

Arduino

XArduino

UNO

220

LED

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.

Page 4: Lab 3        EGR 262 – Fundamental Circuits Lab

Microprocessor InputsDigital 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.

4Lab 3 EGR 262 – Fundamental Circuits Lab

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).

ArduinoUNO

“Pull-down resistor”

10 k

+5V

ArduinoUNO

“Pull-up resistor”10 k

+5V

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).

Page 5: Lab 3        EGR 262 – Fundamental Circuits Lab

Illustration: Protecting Microprocessor Inputs5Lab 3 EGR 262 – Fundamental Circuits Lab

XArduino

UNO10 k

+5V

Correct way to connect 5V to an Arduino input using a

pushbutton switch

ArduinoUNO

+5V

Incorrect 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).

Page 6: Lab 3        EGR 262 – Fundamental Circuits Lab

Configuring Arduino Pins as Inputs and OutputsArduino 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

6Lab 3 EGR 262 – Fundamental Circuits Lab

Page 7: Lab 3        EGR 262 – Fundamental Circuits Lab

Reading Inputs and OutputsInputs: 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 stateExample:

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)

7Lab 3 EGR 262 – Fundamental Circuits Lab

Page 8: Lab 3        EGR 262 – Fundamental Circuits Lab

Switch BounceStandard 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.

HIGH

LOW

switchthrown

LOW

switchthrown

t tLOW LOW

HIGH HIGH HIGH

Debounced switch Switch with contact bounce

8Lab 3 EGR 262 – Fundamental Circuits Lab

You pressed the button once, but the microcontroller thinks you pressed it 3 times!

Page 9: Lab 3        EGR 262 – Fundamental Circuits Lab

9Lab 3 EGR 262 – Fundamental Circuits Lab

10 k

Buttonswitch

+5VArduino

UNO

D13220

D9

LED

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.

Page 10: Lab 3        EGR 262 – Fundamental Circuits Lab

10Lab 3 EGR 262 – Fundamental Circuits LabGood 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.

Page 11: Lab 3        EGR 262 – Fundamental Circuits Lab

Active-HIGH and active-LOW outputsOutputs 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.

LED lights whenoutput is HIGH

LED lights whenoutput is LOW

5V

Active-HIGH LED connection Active-LOW LED connection

220

220

11Lab 3 EGR 262 – Fundamental Circuits Lab

Page 12: Lab 3        EGR 262 – Fundamental Circuits Lab

7-segment displaysA 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 displaysCommon 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.

a

b

c d e

f g

a

b

c

d

e

f g

a

b

c

d

e

f g

Common cathode 7 - segment display

7-segmentdecoder/driver IC

ormicrocontroller

a

anode cathode

Typical segment

220

12Lab 3 EGR 262 – Fundamental Circuits Lab

Page 13: Lab 3        EGR 262 – Fundamental Circuits Lab

Common anode displaysCommon 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.

a

b

c

d

e

f g

a

b

cde

f g

a

bcdefg

Common anode 7-segment display

a

cathode anode

Typical segment

+5V

7-segmentdecoder/driver IC

ormicrocontroller

A “bubble” is sometimes used to indicate an active-LOW output

220

13Lab 3 EGR 262 – Fundamental Circuits Lab

We will use common anode displays in lab.

Page 14: Lab 3        EGR 262 – Fundamental Circuits Lab

Truth table for common anode displaysFor a common anode display: 0 – used to turn a segment ON 1 – used to turn a segment OFFSo 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 g0 0 0 0 0 0 0 1123456789

a

b

c d e

f g

Common-anode7-segment display

To display the digit 0, light all segments except g

14Lab 3 EGR 262 – Fundamental Circuits Lab

Page 15: Lab 3        EGR 262 – Fundamental Circuits Lab

Schematic for Lab #315Lab 3 EGR 262 – Fundamental Circuits Lab

10 k

Buttonswitch

+5VArduino

UNOD2D3D4D5D6D7D8

220

a

b

cde

f g

a bcdefg

Common anode 7-segment display(see data sheet for pinout)

+5V

D9

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).

Page 16: Lab 3        EGR 262 – Fundamental Circuits Lab

Writing a function to control a 7-segment display

16Lab 3 EGR 262 – Fundamental Circuits Lab

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 7-segment 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.

Page 17: Lab 3        EGR 262 – Fundamental Circuits Lab

17Lab 3 EGR 262 – Fundamental Circuits Lab

Writing a program for Lab 3The 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.

10 k

Buttonswitch

+5VArduino

UNOD2D3D4D5D6D7D8

220

a

b

cde

f g

a bcdefg

Common anode 7-segment display(see data sheet for pinout)

+5V

D9

Page 18: Lab 3        EGR 262 – Fundamental Circuits Lab

18Lab 3 EGR 262 – Fundamental Circuits LabWriting a program for Lab 3

// Global variables for readButton( ) function (see Lab #3 Lecture)// Other global variablesvoid 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}