Pre-Lab 3: Subroutines

Table of Contents

Before you begin I recommend reviewing the Introduction to Lab 02

In this Lab you will begin by writing four (4) subroutines. TurnLeft, TurnRight, TurnAround, and HitWall. These four subroutines are the subject of this pre-lab. After you have completed these four subroutines you will write two new subroutines named RightPaw and LeftPaw. These two subroutines will illustrate the power of subroutines by simply using the previously written subroutines to answer the questions “Did the bear’s right paw touch a wall?” and “Did the bear’s left paw touch a wall?”

Teaching the Bear to Turn Left, Turn Right, and Turn Around

In the first part of this Lab you are going to teach the bear how to change directions – turn left, turn right, and to turn around. Let’s first consider the TurnLeft subroutine (i.e, pseudo-instruction).

Table 1: Truth Table for TurnLeft Pseudo-instruction
Facing Direction Direction after turning Left
 dir.1  dir.0  dir.1  dir.0
 x  y
 South  0  0  East  0  1
 East  0  1  North  1  1
 West  1  0  South  0  0
 North  1  1  West  1  0

Note: The syntax dir.1 is a shorthand way of saying variable dir bit 1.

As an example, if the bear is facing north (dir = 11) and you tell him to turn left, then after calling the TurnLeft subroutine, he should be facing west (dir = 10).

As you did in the previous lab, the above table can be viewed as a truth table with inputs dir and outputs dir.

Having variable dir for both input and output can be a little confusing so I have added x as a pseudonym for inputs dir.1 and y for dir.0 and broken the problem into two blocks as shown above and represented as two truth tables. Again inputs dir bit 1 (dir.1) and dir bit 0 are now renamed x and y respectively.

Table 2: Truth Table for dir.0
Input Output
x y dir bit 0
0 0 1
0 1 1
1 0 0
1 1 0
Table 3: Truth Table for dir.1
Input Output
x y dir bit 1
0 0 0
0 1 1
1 0 0
1 1 1

Boolean Logic

Applying what you learned in your Digital Logic Design class construct a sum-of-product (SOP) expression for each table entry (min term). To review, begin by writing a product expression for each one (1) in the output . These are the input conditions where you want to produce an output of 1. In other words, if the first condition OR the second input condition are true (1) you want to output a 1. This reasoning leads directly to our first SOP expression for Table 1.

Now write the SOP expression for dir.1 shown in Table 2.
dir.1 = ?

Simplify your two expressions (dir.1 and dir.0) by observation or using Boolean algebra. You may find it helpful to refer to this Basic Laws and Theorems of Boolean Algebra document. Please, do not draw a 2 variable Karnaugh map. Include all your work in your lab notebook.

     Distributive Law
      Law of Complements
              Dual of Identity Law

After all that work you can, by simple observation, see that our solution is correct. To draw the Boolean equations in Microsoft Word select Insert – Equation …

Flowchart

Applying what you learned in your programming class construct a flowchart for subroutines TurnLeft, TurnRight, and TurnAround. Each flowchart should show how to determine a new value for variable dir based on the current value of dir. For example, if I am writing the flowchart for TurnLeft I will start by testing if dir is equal to 0x00. If it is the bear is facing south and because I want the bear to turn left I will set variable dir equal to 0x01.

Did the Bear Hit a Wall?

Reading: Before you begin this part of the pre-lab, you may want to review Chapter 1 of your textbook on logical operators. For this pre-lab you will need to understand AVR Logical Instructions COM, AND, OR, and EOR by reading about each in AVR Studio’s Assembler Help document.

  1. Complete the following truth table.
    Table 4: Switch to LED0 Truth Table
    Input Output
    Logic Gate Selected SW1 SW0 LED0
    AND 0 0
    0 1
    1 0
    1 1
    OR 0 0
    0 1
    1 0
    1 1
    EOR (Exclusive OR) 0 0
    0 1
    1 0
    1 1
    COM(Inverter) 0
    1

    To give you a little practice using the Assembler Help window in AVR Studio (Help – Assembler Help), look-up the CBR and SBR instructions. Use the information from the help provided for these two instructions to answer the following two questions.

  2. The Clear Bit(s) in Register (CBR) instruction performs what logical operation to clear individual bits in a register?
  3. The Set Bit(s) in Register (SBR) instruction performs what logical operation to set individual bits in a register?
  4. What byte-wide logical instruction can be used to test if a bit or bits are set? You only need to indicate the appropriate operator. Do not worry about the entire instruction. (IE the focus is on MOV from MOV R7, R6) To help you answer this question, consider the following drawing depicting 8 2-input gates.

    The type is unknown at this time so I have simply drawn 8 boxes. I then named one of the inputs to each of the gates “test.” To test if a bit or bits are set (bn = 1), you set the corresponding “test” bit to 1. If any of the bit or bits being tested is 1 the corresponding output will be a non-zero value.
    In the following example I want to see if bit 1 in register r24 is set. Consequently, I would set bit 1 in my “test” byte (register r16) to 1. The answer is no so the contents of output register r24 is zero.
  5. Write the single logical instruction that would be used to test the bits, using inputs R19 and R24. R19 and R24 are two of the AVR’s 32 8-bit general purpose registers. Assume r24 initially contains the byte you want to test, and register r19 contains the test sequence (in our example 000000102). The output of the test should be into R24.
  6. What would the contents of R26 be after the instruction COM R26 is executed if it originally has the value 0b11001010?

What Should I Turn In?

Turn in the following material. Make sure all your original work is in your Lab Notebook. All written material must be typed or neatly done in ink. Once again please do not copy.
Page

  1. Title page with the pre-lab number, your name and picture, today’s date, and the day your lab meets.
  2. Truth tables and simplified Boolean expressions for TurnLeft, TurnRight, and TurnAround
  3. Flowcharts for TurnLeft, TurnRight, and TurnAround You may use more than one page to show your flowcharts
  4. The problem statements (remove explanatory text) and solutions to the five (5) pre-lab questions in Part B of the pre-lab. Circle and/or highlight your answers. Make sure all your original work is located in your lab notebook. This part of your pre-lab must be typed. You may use more than one page to write the question and answer these five questions.