Title: EGR 277
1Lecture 3 EGR 262 Fundamental Circuits Lab
EGR 262 Fundamental Circuits Lab Presentation
for Lab 3
Instructor Paul Gordy Office H-115 Phone
822-7175 Email PGordy_at_tcc.edu
2Lecture 3 EGR 262 Fundamental Circuits Lab
Background Logic Basics Lab 3 gets into more
detailed programming of the MicroStamp11 and
requires an understanding of logical operations
particularly bitwise logical operations. See pp.
113-114 in the lab manual for additional
information. Logic Levels The logical values 0
and 1 (or LOW and HIGH) correspond to 0V and 5V
with the MicroStamp11.
Logical 0 ? LOW ? 0V Logical 1 ? HIGH
? 5V
Additionally, the following terminology is
sometimes used
Setting a pin on the MicroStamp11 ? making the
pin HIGH Clearing a pin on the MicroStamp11 ?
making the pin LOW
3Lecture 3 EGR 262 Fundamental Circuits Lab
Logic Operations The logical operations NOT, AND,
OR, and Exclusive-OR (XOR) are defined below.
NOT
A A
0 1
1 0
AND
AB AB A AND B AB 1 only when both A 1
and B 1 Symbol
A B AB
0 0 0
0 1 0
1 0 0
1 1 1
4Lecture 3 EGR 262 Fundamental Circuits Lab
OR
A B AB
0 0 0
0 1 1
1 0 1
1 1 1
AB A OR B AB 1 when A 1 or B 1 (or
both equal 1) Symbol
XOR
A?B A XOR B A Exclusive-OR B A ? B 1
when A and B are different (i.e., A 1
and B 0 or B1 and A 0) Symbol
A B A?B
0 0 0
0 1 1
1 0 1
1 1 0
5Lecture 3 EGR 262 Fundamental Circuits Lab
Logical Operators in C The following symbols are
used for logical operators in C.
These logical operators might be used in an
expression involving logical variables and
logical values, as in the example below
if (xgt2 x lt 10) x 50
Logical Operator Meaning
Boolean AND
Boolean OR
Bitwise Binary Operators You may not be familiar
with bitwise binary operators as they are not
always covered in an introductory C course.
However, they are used extensively when working
with assembly language or microcontrollers (such
as the MicroStamp11). Whereas logical operators
work with logical variables or expressions,
bitwise operators act on the individual bits of
a variable.
6Lecture 3 EGR 262 Fundamental Circuits Lab
Bitwise Binary Operators The following symbols
are used for bitwise binary operators in C
(binary here means two or more arguments)
Bitwise Binary Operator Meaning
bitwise OR
bitwise AND
bitwise Exclusive-OR
ltlt shift left
gtgt shift right
Example Suppose that A and B are two 8-bit
variables If A 00001111 and B 10101010,
then find each of the following 1) AB
Answer 00001010 2) AB 3) AB
7Lecture 3 EGR 262 Fundamental Circuits Lab
Bitwise Unary Operators The following symbols are
used for bitwise unary operators in C (unary here
means one argument)
Bitwise Unary Operator Meaning
- negation
! logical NOT
1s complement
increment
-- decrement
Example Suppose that A is an 8-bit variable with
the value A 00001111. Find each of the
following 1) A-- Answer 00001110 2)
A 3) A
8Lecture 3 EGR 262 Fundamental Circuits Lab
Recall that in C or C the expression X X2
can be replaced by X 2 using the shortcut
operator . Shortcut bitwise operators are also
available. Bitwise Shortcut Operators The
following bitwise shortcut operators are
available
Shortcut Operator Meaning
Add value to
- Subtract value from
Multiply value to
/ Divide value to
OR value to
AND value to
Exclusive-OR value to
ltlt Shift value left
gtgt Shift value right
Modulo divide value to
9Lecture 3 EGR 262 Fundamental Circuits Lab
- Example (Masking) Find each of the following
- AB, where A A7A6A5A4A3A2A1A0 and B
00000100 -
- Answer A7A6A5A4A31A1A0
- Effect A2 is set and all other bits are
unaltered. - 2) AB, where A A7A6A5A4A3A2A1A0 and B
111111011 - Answer
- Effect
- 3) AB, where A A7A6A5A4A3A2A1A0 and B
00000100 - Answer
- Effect
Note These are three common operations used with
microcontrollers. This practice of extracting or
affecting certain bits is referred to as
masking. In example 1 above, B 00000100 is
sometimes called a bit mask.
10Lecture 3 EGR 262 Fundamental Circuits Lab
- Input/Output Ports on the MicroStamp11
- The majority of the pins on the MicroStamp11 (see
diagram below) are used for input and output so
that we can communicate with the microcontroller.
The input/output pins are arranged into two
ports with the names - PORTA (8 pins PA0 through PA7 corresponding
to pins 1-8) - PORTD (6 pins PD0 through PD5 corresponding
to pins 15-20)
11Lecture 3 EGR 262 Fundamental Circuits Lab
- Logical Value and Direction
- The pins on each port have two distinct states
- Logical State
- Logical 0 or LOW (0V)
- Logical 1 or HIGH (5V)
- Direction (the pin might be used as an input or
as an output) - IN (0)
- OUT (1)
- If a pin is configured as an output, then it acts
like a 0V or 5V source (depending on the logical
state) that can be applied to external circuits
or devices connected to the pin. - If a pin is configured as an input, then it acts
like a HIGH resistance load and the MicroStamp11
can read the logical state of the pin applied by
external circuits or devices connected to the
pin. - The names PORTA and PORTD are specified by
kernel.c. These ports correspond to hardware
registers so that inputs and outputs are tied
directly to memory.
12Lecture 3 EGR 262 Fundamental Circuits Lab
PORTD PORTD is a bi-directional register, so each
pin may be configured as an input or as an
output. The direction is controlled by the
direction register DDRD.
Hardware Pin PORTD bit Direction Direction controlled by
20 PD0 Bi-directional (input or output) Bit 0 of DDRD
19 PD1 Bi-directional (input or output) Bit 1 of DDRD
18 PD2 Bi-directional (input or output) Bit 2 of DDRD
17 PD3 Bi-directional (input or output) Bit 3 of DDRD
16 PD4 Bi-directional (input or output) Bit 4 of DDRD
15 PD5 Bi-directional (input or output) Bit 5 of DDRD
Example
DDRD (bit5 bit0)
PORTD (bit5 bit0)
Result PD0, PD1, PD3 PD4 are inputs. PD2 PD5
are outputs.
13Lecture 3 EGR 262 Fundamental Circuits Lab
PORTA PORTA has only two pins (PA3 and PA7) that
are bi-directional. It also has three pins
(PA0-PA2) that are always configured as inputs
and three pins (PA4-PA6) that are always
configured as outputs. Two bits in the hardware
control register PACTL are used to control the
direction of PA3 and PA7.
Hardware Pin PORTA bit Direction Direction controlled by
8 PA0 Input only - - -
7 PA1 Input only - - -
6 PA2 Input only - - -
5 PA3 Bi-directional (input or output) Bit 3 of PACTL
4 PA4 Output only - - -
3 PA5 Output only - - -
2 PA6 Output only - - -
1 PA7 Bi-directional (input or output) Bit 7 of PACTL
Example
PACTL (bit7 bit0)
PORTA (bit7 bit0)
Result PA7 set as an input. PA4-PA6 always
outputs. PA3 set as an output. PA0-PA2 always
inputs.
14Lecture 3 EGR 262 Fundamental Circuits Lab
PORTD Outputs Bitwise operations are used to set
port directions and to set output logical
values. kernel.c also defines a useful macro
bit(i) that creates a byte where only the ith bit
is set to 1. Examples
0
0
bit(0)
0
0
0
0
0
1
1
1
bit(2)
1
1
1
0
1
1
0
0
bit(5)
1
0
0
0
0
0
0
1
bit(7)
1
1
1
1
1
1
Example Set PD2 DDRD bit(2) // set the
direction of PD2 to 1 (output) PORTD
bit(2) // set the logical output of PD2 to 1
(logical HIGH)
15Lecture 3 EGR 262 Fundamental Circuits Lab
Example Clear PD2 DDRD bit(2) // set the
direction of PD2 to 1 (output) PORTD
bit(2) // clear the logical output of PD2 to 0
(logical LOW)
- PORTA Outputs
- Using PORTA for outputs is similar to using
PORTD, with a few exceptions - PA4-PA6 are dedicated outputs, so no direction
control is required - PA0-PA2 cannot be used for outputs (they are only
used for inputs) - When setting the direction control for PA3 and
PA7, use DDRA3 and DDRA7 rather than bit(3) and
bit(7). They work in a similar fashion.
16Lecture 3 EGR 262 Fundamental Circuits Lab
Example Set PA7 PACTL DDRA7 // set the
direction of PA7 to 1 (output) PORTA
bit(7) // set the logical output of PA7 to 1
(logical HIGH)
Example Clear PA6 PORTA bit(6) // clear
the logical output of PA6 to 0 (logical LOW)
// no
direction control is needed since PA6 is always
an output.
MicroStamp11
17Lecture 3 EGR 262 Fundamental Circuits Lab
Example Toggle PA3 PACTL DDRA3 // set the
direction of PA3 to 1 (output) PORTA
bit(3) // toggle the logical output of PA3 (0 to
1 or 1 to 0)
Inputs using PORTD and PORTA We have just seen
how to set or clear an output port, but we also
need to know how to read its value in a C
program. This is demonstrated in the following
examples.
Example Read the input value on PD5 DDRD
bit(5) // set the direction of PD5 to 0
(input) if((PORTD bit(5)) 0) // check to
see if bit 5 PORTD is 0 OutString(PD5 is
LOW) else OutString(PD5 is HIGH)
18Lecture 3 EGR 262 Fundamental Circuits Lab
Example Read the input value on PA2 // no
direction control is needed since PA2 is always
an input if((PORTA bit(2)) 0) // check to
see if bit 2 PORTA is 0 OutString(PA2 is
LOW) else OutString(PA2 is HIGH)
19Lecture 3 EGR 262 Fundamental Circuits Lab
Protecting the MicroStamp11 Several labs in this
course require that the instructor check your
circuits before power is applied to the
MicroStamp11. The reason for this safety
precaution is that the MicroStamp11 can be easily
destroyed by excessive voltages and currents.
Inputs and outputs are often protected using
current-limiting resistors and pull-down
resistors as illustrated below. Current-limiting
resistors Be sure to include current-limiting
resistors in series with LEDs that are connected
to outputs of the MicroStamp11. If the resistor
is omitted, excessive current through the LED
might destroy the MicroStamp11 (and the LED).
Figure 3 from page 40 of the lab guide
illustrates this point.
220 ?
20Lecture 3 EGR 262 Fundamental Circuits Lab
Pull-down resistors with button switches A button
switch will be used in several labs to apply
momentary inputs to the MicroStamp11. A button
switch is illustrated to the right (Figure 4 from
page 40 of the lab guide).
A pull-down resistor is often used with a
button switch. The purpose of the resistor is to
drain away any remaining charge on an input pin
after the applied 5V input from the button switch
is removed. Using a button switch with a
pull-down resistor is illustrated to the right
(Figure 5 from page 41 of the lab guide).
21Lecture 3 EGR 262 Fundamental Circuits Lab
Switch Bounce Standard switches, such as toggle
switches, slide switches, and button switches,
typically exhibit switch bounce. This means
that when the switch above 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.
22Lecture 3 EGR 262 Fundamental Circuits Lab
Additional kernel.c functions kernel.c includes
several functions to facilitate the
setting/clearing of individual input/output (I/O)
pins on the MicroStamp11. Using these functions
will be easier than the methods previously
covered for setting and clearing pins (although
the previous method will be required in some
later labs). In particular, kernel.c specifies 8
I/0 addresses using various pins on PORTA and
PORTD. The following important table (from page
42 of the lab guide) defines these addresses.
23Lecture 3 EGR 262 Fundamental Circuits Lab
kernel.c functions for communication with the
eight I/O ports
24Lecture 3 EGR 262 Fundamental Circuits Lab
kernel.c functions for communication with the
eight I/O ports (continued)
25Lecture 3 EGR 262 Fundamental Circuits Lab
- 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.
26Lecture 3 EGR 262 Fundamental Circuits Lab
7-segment displays A 7-segment display is made up
of seven LEDs 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 MicroStamp11 is HIGH for
one segment, 5V is connected to the anode. Since
all cathodes are grounded, the segment lights.
220 ?
27Lecture 3 EGR 262 Fundamental Circuits Lab
Common anode displays Common anode displays
require active-LOW outputs. When the output of
the decoder or MicroStamp11 is LOW for one
segment, 0V is connected to the cathode. Since
all anodes are connected to 5V, the segment
lights.
220 ?
28Lecture 3 EGR 262 Fundamental Circuits Lab
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
2
3
4
5
6
7
8
9
To display the digit 0, light all segments except
g
29Lecture 3 EGR 262 Fundamental Circuits Lab
Schematic for Lab 3
30Lecture 3 EGR 262 Fundamental Circuits Lab
- 4.1. Pre-lab Tasks
- (1) Draw a schematic diagram including the
button circuit and the 7-segment display circuit
(include pin numbers for the 7-segment display).
- (2) Explain how button circuit works.
- (3) Diagram identifying segments on a 7-segment
display and a truth table indicating which
segments are lit for each decimal digit. - (4) This lab will use common-anode 7-segment
displays. Discuss the difference between
common-anode displays and common-cathode
displays. Include diagrams. - (5) Pinout and part number for the 7-segment
display. - (6) Draw a diagram showing which segments are lit
for each of the ten decimal values. - Explain how the LED circuit works.
- (continued)
31Lecture 3 EGR 262 Fundamental Circuits Lab
(8) Write a C-language program such that when
the user presses a button an unsigned integer
will be incremented (mod-10) and the value of the
integer will be displayed both on PuTTY and on a
7-segment display. In particular A) when the
button connected to PA0 is pressed, the integer
should be incremented B) include a delay (pause)
to allow for switch debounce C) the value of the
integer should be displayed on PuTTY D) a
function named display_digit should be called to
diplay the integer value on a 7-segment display.
The function should accept an integer between 0
and 9 as input and then use the clear_pin and
set_pin kernel functions to display that digit on
your LED display. Outputs O1-O7 should be
connected to segments a-g on the 7-segment
display. A partial example of such a function is
provided below. This function will be used in
several later programs in this course. (see
start of display_digit function on the following
page) (9) Remember to begin all programs with a
section of comments including your name, course
number, lab number, filename of program (Lab3.c,
for example), and a brief description of the
program. All programs should include plenty of
comments.
32Lecture 3 EGR 262 Fundamental Circuits Lab
void display_digit(int data)int
ifor(i1ilt8i) set_pin(i) // Add a
comment explaining what this does switch (data)
case 0 // light segments a,b,c,d,e,f clear_p
in(1)clear_pin(2)clear_pin(3) clear_pin(4)cle
ar_pin(5)clear_pin(6) break case 1 //
light segments b,c clear_pin(2)clear_pin(3) br
eak // similar for case 2 through case 9
33Lecture 3 EGR 262 Fundamental Circuits Lab
4.2. In-lab Tasks (1) Detailed handwritten
comments (like a diary) of all activities during
lab. For example, if your program did not
successfully compile or download, discuss what
you did to correct the problem. (2) Breadboard of
button and 7-segment display circuit checked by
instructor. (3) Run the program which includes a
delay used for debouncing. The display should
operate perfectly. Print a sample of the results
from the terminal program. (4) Remove the delay
from the program above. The display should now
have problems with switch debounce. Press the
button (somewhat quickly) ten times and record
the sequence of numbers seen on the 7-segment
display. Calculate the average number of bounces
that occurred. Print out a sample of the results
the terminal program. Can you tell that there
was a switch debounce problem looking at the
results from the terminal program?
Explain. 4.3. Post-Lab Tasks (1) Provide a
listing of the ?nal project program along with an
explanation of how the program works. Be sure to
discuss the di?erences between your pre-lab
program listing and your ?nal program listing.
(2) Discuss the problem of switch debounce and
how it was handled in this lab. (3) Demonstrate
the functionality of your completed system to the
instructor.
34Lecture 3 EGR 262 Fundamental Circuits Lab
- Sample Program
- A sample program has been provided that is
somewhat similar the required program - for Lab 3. This sample program has the
following features - Connect a button circuit to the input I6
- Connect Green, Yellow, and Red LEDs to Outputs
O1, O2, and O3 (respectively). The LEDs should
be connected using active-HIGH outputs (i.e., a
HIGH output should turn on the LED). Be sure to
use current-limiting resistors with each LED. - Write a main program that counts button pushes
(1, 2, 3 then repeats) and calls a function to
light the appropriate LED (Green for 1, Yellow
for 2, and Red for 3). - Write a function that accepts an integer input
(1, 2, or 3) and uses the clear_pin and set_pin
kernel functions to turn on and off the
appropriate LEDs.
35Lecture 3 EGR 262 Fundamental Circuits Lab
36Lecture 3 EGR 262 Fundamental Circuits Lab
The circuit for this sample program will be
demonstrated by the instructor. Note that the
main program also displays the color of the LED
to be lit using the terminal program. The
results are shown below.