# EGR 277 � Digital Logic

Document Sample

```					Lecture #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@tcc.edu
1
Lecture #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

2
Lecture #3    EGR 262 – Fundamental Circuits Lab

Logic Operations
The logical operations NOT, AND, OR, and Exclusive-OR (XOR) are defined below.

NOT

A    A’        A’ = A = “A NOT” = “NOT A” = “A inverse” = “complement of A”
0    1        Symbol:
1    0                  A                 A’

AND

A    B     AB       AB = A·B = “A AND B”
0    0     0        AB = 1 only when both A = 1 and B = 1
0    1     0        Symbol:
1    0     0            A
1    1     1                                 AB
B

3
Lecture #3       EGR 262 – Fundamental Circuits Lab

OR

A     B        A+B      A+B = “A OR B”
0     0         0       A+B = 1 when A = 1 or B = 1 (or both equal 1)
0     1         1       Symbol:
1     0         1            A
1     1         1                                A+B
B

XOR

A        B     AB       AB = “A XOR B” = “A Exclusive-OR B”
0         0        0     A  B = 1 when A and B are different
0         1        1        (i.e., A = 1 and B = 0 or B=1 and A = 0)
1         0        1     Symbol:

1         1        0            A
AB
B

4
Lecture #3     EGR 262 – Fundamental Circuits Lab
Logical Operators in C
The following symbols are used for logical operators in C.

Logical         Meaning          These logical operators might be used in an
Operator                          expression involving logical variables and logical
values, as in the example below:
&&         Boolean AND
||         Boolean OR                   if (x>2 && x < 10) x = 50;

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.

5
Lecture #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
<<                       shift left
>>                      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:

2) A|B
3) A^B
6
Lecture #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
~                      1’s complement
++                         increment
--                        decrement

Example:
Suppose that A is an 8-bit variable with the value A = 00001111.
Find each of the following:

2) A++
3) ~A
7
Lecture #3        EGR 262 – Fundamental Circuits Lab
Recall that in C or C++ the expression X = X+2 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
-=             Subtract value from
*=               Multiply value to
/=                Divide value to
|=                  OR value to
&=                 AND value to
^=            Exclusive-OR value to
<<=                  Shift value left
>>=                 Shift value right
%=               Modulo divide value
to                                        8
Lecture #3       EGR 262 – Fundamental Circuits Lab
Example (Masking): Find each of the following:
1) A|=B, where A = A7A6A5A4A3A2A1A0 and B = 00000100

Effect: A2 is set and all other bits are unaltered.

2) A&=B, where A = A7A6A5A4A3A2A1A0 and B = 111111011

Effect:

3) A^=B, where A = A7A6A5A4A3A2A1A0 and B = 00000100

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”.
9
Lecture #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)

10
Lecture #3     EGR 262 – Fundamental Circuits Lab
Logical Value and Direction
The pins on each port have two distinct states:
1) Logical State
A.   Logical 0 or LOW (0V)
B.   Logical 1 or HIGH (5V)
2) Direction (the pin might be used as an input or as an output)
A.   IN (0)
B.   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. 11
Lecture #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   PORTD         Direction          Direction
Pin        bit                          controlled by
20        PD0        Bi-directional    Bit 0 of DDRD
(input or output)
Example:
19        PD1        Bi-directional    Bit 1 of DDRD
(input or output)                   DDRD (bit5 … bit0):
18        PD2        Bi-directional    Bit 2 of DDRD
(input or output)                     1 0 0 1 0 0
17        PD3        Bi-directional    Bit 3 of DDRD
(input or output)                   PORTD (bit5 … bit0):
16        PD4        Bi-directional    Bit 4 of DDRD
(input or output)
15        PD5        Bi-directional    Bit 5 of DDRD
(input or output)

Result:
PD0, PD1, PD3 & PD4 are inputs.
PD2 & PD5 are outputs.

12
Lecture #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   PORTA         Direction           Direction      Example:
Pin        bit                           controlled by
6         PA0         Input only            ---
PACTL (bit7 … bit0):
7         PA1         Input only            ---
8         PA2         Input only            ---
0 - - - 1 - - -
5         PA3        Bi-directional    Bit 3 of PACTL
(input or output)                    PORTA (bit7 … bit0):
4         PA4        Output only            ---
3         PA5        Output only            ---
2         PA6        Output only            ---
Result:
PA7: set as an input.
1         PA7        Bi-directional    Bit 7 of PACTL
(input or output)                    PA4-PA6: always outputs.
PA3: set as an output.
PA0-PA2: always inputs.
13
Lecture #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:

bit(0) =        0 0 0 0 0 0 0 1                   ~bit(2) =        1 1 1 1 1 0 1 1
bit(5) =        0 0 1 0 0 0 0 0                   ~bit(7) =        0 1 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)
PA7    1         20   PD0
PA6    2         19   PD1
PA5    3         18   PD2
PA4    4         17   PD3
PA3    5         16   PD4
PA2    6         15   PD5                                                 +
PA1    7         14                                                 5V    _
LED lights   Equivalent to                       LED lights
PA0    8         13
9         12
10         11

MicroStamp11

14
Lecture #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)
PA7    1         20   PD0
PA6    2         19   PD1
PA5    3         18   PD2
PA4    4         17   PD3
PA3    5         16   PD4
PA2    6         15   PD5                                                +
PA1    7         14                                                 0V   _
LED is off   Equivalent to                       LED is off
PA0    8         13
9         12
10         11

MicroStamp11

PORTA Outputs
Using PORTA for outputs is similar to using PORTD, with a few exceptions:
1) PA4-PA6 are dedicated outputs, so no direction control is required
2) PA0-PA2 cannot be used for outputs (they are only used for inputs)
3) 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.

15
Lecture #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)
PA7    1           20   PD0
PA6    2           19   PD1
PA5    3           18   PD2
PA4    4           17   PD3
PA3    5           16   PD4
PA2    6           15   PD5                                                          +
PA1    7           14                                                       5V       _
LED lights    Equivalent to                      LED lights
PA0    8           13
9           12
10           11

MicroStamp11

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.

PA7   1           20    PD0
PA6   2           19    PD1
PA5   3           18    PD2
PA4   4           17    PD3
PA3   5           16    PD4
PA2   6           15    PD5                                                +
PA1   7           14                                                  0V   _
LED is off    Equivalent to                      LED is off
PA0   8           13
9           12
10          11
16
MicroStamp11
Lecture #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)
PA7    1         20   PD0
PA6    2         19   PD1
PA5    3         18   PD2                     Equivalent to
PA4    4         17   PD3
PA3    5         16   PD4
PA2    6         15   PD5     LED toggles                          +      LED toggles
PA1    7         14           (ON to OFF or OFF to ON)        5V   _      (ON to OFF or OFF to ON)

PA0    8         13
9         12
10         11

MicroStamp11

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”);                                                       17
Lecture #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”);

18
Lecture #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 

19
Lecture #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).                           20
Lecture #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.

switch                      switch
thrown                      thrown

HIGH                       HIGH              HIGH       HIGH

LOW                    LOW         LOW    LOW
t                                         t

Debounced switch               Switch with contact bounce

21
Lecture #3     EGR 262 – Fundamental Circuits Lab
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

22
Lecture #3    EGR 262 – Fundamental Circuits Lab
kernel.c functions for communication with the eight I/O ports

23
Lecture #3      EGR 262 – Fundamental Circuits Lab
kernel.c functions for communication with the eight I/O ports (continued)

Recall the earlier example used to 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)
This could now more easily be accomplished using:
set_pin(2);            // configure O2 (PD2) as an output and set it to 1 (logical HIGH)
24
Lecture #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.
5V
220 

LED lights when
LED lights when
output is LOW
output is HIGH               220 

Active-HIGH LED connection                     Active-LOW LED connection

25
Lecture #3        EGR 262 – Fundamental Circuits Lab
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 MicroStamp11 is HIGH for one segment, 5V is connected to the anode.
Since all cathodes are grounded, the segment lights.
220 
a                 a     a
b                 b
7-segment        c                 c f
g    b
decoder/driver IC   d                 d
or           e                 e                  anode          cathode
f                 f e        c
MicroStamp11                                d              a
g                 g

Common-cathode             Typical segment
7-segment display

26
Lecture #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 
a                       a       a
7-segment        b                       b
decoder/driver IC   c                       c   f        b                          anode
d                       d       g
or           e                       e                    cathode                  +5V
MicroStamp11       f                       f   e       c
d               a
g                       g

Common- anode               Typical segment
A “bubble” is sometimes used to   7- segment display
indicate an active-LOW output

27
Lecture #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           To display the
digit 0, light
0       0      0       0       0       0       0       1           all segments
1                                                                  except g
2
3
a
4
f        b
5                                                                       g
e       c
6                                                                       d

7
Common-anode
8                                                                7-segment display

9
28
Lecture #3       EGR 262 – Fundamental Circuits Lab
Schematic for Lab #3
+5V                                                                             +5V

O1    1         20        220 
O7    2         19
O6    3         18   O2            a       a
4         17   O3            b
O4            c   f       b
5         16
d       g
6         15   O5
e
7         14
f   e       c
I6    8         13                         d
g
Button                 9         12        220 
switch                10         11
Common-anode
MicroStamp11
10 k
7-segment display
(see data sheet for pinout)

29
Lecture #3 EGR 262 – Fundamental Circuits Lab
(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.
(7) Explain how the LED circuit works.

(continued)

30
Lecture #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
Lecture #3 EGR 262 – Fundamental Circuits Lab
void display_digit(int data){
int i;
for(i=1;i<8;i++) set_pin(i); // Add a comment explaining what this does
switch (data){
case 0:        // light segments a,b,c,d,e,f
clear_pin(1);clear_pin(2);clear_pin(3);
clear_pin(4);clear_pin(5);clear_pin(6);
break;
case 1:        // light segments b,c
clear_pin(2);clear_pin(3);
break;
// similar for case 2 through case 9
}
}

32
Lecture #3      EGR 262 – Fundamental Circuits Lab
(1) Detailed handwritten comments (like a diary) of all activities during lab. For
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.

(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. 33
Lecture #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.
+5V

O1    1         20                        220 
2         19
3         18   O2           220 
4         17   O3
5         16
220 
6         15
7         14
Red           Yellow   Green
I6    8         13                  LED            LED     LED
Button                       9         12
switch                      10         11

MicroStamp11
10 k

34
Lecture #3   EGR 262 – Fundamental Circuits Lab

35
Lecture #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.

36

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 4 posted: 9/29/2012 language: Unknown pages: 36