Microprocessor Based Systems Lab Manual Cairo University Faculty of Engineering, Computer Engineering Department, 2nd Year. Microprocessor Based Systems Lab Manual (1st Term) Contents: General Guidelines Lab 1: ALU design Lab 2: Traffic Light Controller Lab 3: Serial Communications General Guidelines 1/12 Microprocessor Based Systems Lab Manual General Guidelines Format of Lab Reports Although the assignments specify certain information to be included in the reports, all reports should include some standard information and follow a standard format. All reports should be typed, well-organized, grammatically correct, neat, and fastened using either a staple in the upper left corner or a report binder. All reports should be concise. All reports should have the following components, discussed below, in the given order. 1. Title Page 2. Validation Sheet(s) 3. Main Body of the Report 4. Program Listing(s) 5. Comment Sheet a. Title Page The title page should be the first page of the report and must include the following information. Cairo University Computer Engineering Department Experiment # ?? Experiment Title NAME: Section: ? Date: ? b. Validation Sheet A validation sheet is a page where you write the summary of your results or observations,... etc. and then you get a teaching assistant to sign it for you verifying your statements. Insert the validation sheet into the report immediately after the title page. c. Main Body of the Report In addition to specific information required for each assignment, the main body of the report should provide the following general information. The information should be brief and concise, yet complete. Brief problem statement. Description of the solution, using figures as appropriate. Description of the design including pseudo-code, schematics for new hardware, etc. (see Documentation Guidelines). Expected and observed results and performance. General Guidelines 2/12 Microprocessor Based Systems Lab Manual d. Program Listing(s) The report should include the listing file for the program(s). Organize the listing file using comments and assembler directives. Print the listing in a neat manner, especially avoid unnecessary line breaks. Separate the printer output and insert the pages in the report following the main body of the report. e. Comment Sheet Please take time to submit a comment sheet and attach it as the last page of the report. Your comments will be used to improve the project assignments, laboratory equipment, etc. Your input is valuable and greatly appreciated. Documentation Guidelines In addition to the text of your report, your design must be documented by schematic diagrams, pseudo-code, and listing file. Good documentation that can be easily understood by others, is neat and well organized, and conveys all necessary information in a complete, yet concise, manner. Some specific guidelines for schematic diagrams and pseudo-code are given below. 1. Schematic Diagrams Guidelines for preparing schematic diagrams are given below. 1. All diagrams must be neat and legible. You may draw diagrams neatly in pencil using a template or use a schematic capture or drawing program to generate diagrams. 2. Label the overall circuit with a title block. Give a drawing or page number for multipage diagrams. 3. Use standard logic symbols. 4. Label all components with unit numbers, e.g. U1, U2, etc., that appear in a parts list. 5. Indicate all power and ground connections either as pins on the first unit of a component, e.g. U1, or in the parts list. 6. Label all signals using unique names that are understandable and describe the function of the signal. 7. Use label cross references to avoid massive and confusing line drawing and to continue a signal to another page. 8. Signal lines that connect should be indicated with a heavy dot or “blob,” while lines that cross without connecting should be shown as crossing lines. Do not use curves or “bumps” in lines to indicate that there is no connection. 9. Organize inputs and outputs by function so as to keep related signals together and to minimize line crossovers and length. There is no need to make you drawing confusing in order to maintain pin ordering. 10. You may merge multiple signals that operate as a group, e.g. a data or address bus, into a single line. Labels should clearly indicate the signals in the bus. 11. As much as possible, logic and signal flow should go from top-to-bottom (inputs at top, outputs at bottom) or left-to-right (inputs at left, outputs at right). General Guidelines 3/12 Microprocessor Based Systems Lab Manual 2 Pseudo-code A few rules for pseudo-code are listed below. 1. Use high-level, meaningful terms for functions and variables. Do not use actual microprocessor instructions or register references. 2. Do not substitute a paragraph description for pseudo-code. 3. Use assignment statements, subroutine calls, macro names, and control structures such as “if-then-else" or “do-while.” 4. Use more than one level when necessary. For example, write pseudo-code for the main program that specifies subroutines by name, and then write pseudo-code for each subroutine. 5. Organize pseudo-code by using indentation and spacing. General Guidelines 4/12 Microprocessor Based Systems Lab Manual Lab 1 ALU Design Objectives -Introducing one of the basic computer components, ALU. -Reviewing combinational circuit design. -Practicing the use of design simulators (Altera-Max Plus 2 or Quartus II). ALU: a Brief Introduction An arithmetic logic unit (ALU) is a combinational circuit that performs a set of basic arithmetic and logic micro-operations. Because the ALU can perform many operations. There should be a number of selection lines to select a particular operation in the unit. Figure 1 shows the block diagram for a typical 4-bit ALU. Data input A A0 A1 A2 A3 Data input B B0 B1 B2 B3 Input carry { Function select Mode select Cin 4-bit ALU F0 F1 F2 F3 Cout Data output F Output carry S0 S1 S2 Figure 1: Block Diagram of a 4-bit ALU. The circuit accepts two 4-bit data inputs (A and B). Depending on the operation, it generates an output F. The mode select input S2 distinguishes between arithmetic and logic operations. The two function-select inputs S1 and S0 specify a particular arithmetic or logic operation to be generated at the output lines. With three selection lines, it is possible to specify four arithmetic operations and four logic operations. The input and output carries have meaning only during arithmetic operations. Lab 1 – ALU Design 5/12 Microprocessor Based Systems Lab Manual The following table shows some examples of ALU operations. Operation Select S2 S1 S0 Cin 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 0 0 Output F A A+1 A+B A+B+1 A+B` A+B`+1 A-1 A A AND B A OR B A XOR B A` Function Transfer A Increment A Addition Add with carry A plus B’s one’s complement Subtraction Decrement A Transfer A AND OR XOR Complement A Notice how the input carry Cin is used as a fourth selection variable for arithmetic operations. In this way the arithmetic operations are doubled from four to eight. Design steps of ALU The design of a typical ALU is carried out in three steps: 1- Designing the arithmetic section. 2- Designing the logic section. 3- Combining the two sections to form the desired ALU. Figure 2 shows one stage of the 4-bit ALU described in Figure 1. Ci Ai Bi Ci+1 One stage of arithmetic circuit 0 MUX Fi 1 S0 S1 Select One stage of logic circuit S2 Figure 2: One stage of the 4-bit ALU. Lab 1 – ALU Design 6/12 Microprocessor Based Systems Lab Manual As shown, one stage of arithmetic circuit is combined with that of logic circuit to produce one stage of ALU. Selection lines S1 and S0 can be common between both circuits provided that the third selection S2 is used to differentiate between the two. As shown in figure 2, the outputs of the arithmetic and logic circuits in each stage are applied to a 2x1 multiplexer with selection line S2. When S2 is 0, the arithmetic output is selected, and when S2 is 1, the logic output is selected. The figure shows just one stage of the ALU. This circuit must be repeated n times for an n-bit ALU. Points of Guidance for such Design Problems 12- Start by drawing a block diagram for the circuit identifying inputs and output. Simplify your design problem into a set of simpler design problems. This is done by dividing your design into its basic components. Draw each component showing its inputs and outputs. 3Repeat step 2 recursively until you reach a component that can be implemented directly giving the available material. 4For each component defined in step 3, Draw the circuit logic diagram. Find out the types and the number of ICs required. 5Implementation issue: A good design is the one with min. number of ICs (not min. number of gates). Try to minimize the number of ICs required for your design. Lab 1 – ALU Design 7/12 Microprocessor Based Systems Lab Manual Design Problem Requirements: Use a design simulator to design a 4-bit Arithmetic Logic Unit that accepts 2 4-bit inputs, A and B, 4 control signals, s3s2s1s0, and carry in, ci, and produce a 4-bit output, F, and carry out, co, (when appropriate), such that F could be one of the following 15 outputs depending on the control signals. Output F 0 A+1 A+B A+B+ci A+B`+ci A+B`+1 A-1 A AB AB AB A` Shr A Shl A AL*BL Operation Clear Increment Add Add with carry Sub with borrow Sub Decrement Transfer AND OR XOR Complement Shift right A Shift left A Multiply lower nibbles Available Material: Available ICs: 1-bit Binary Adder, 4-to-1 MUX, 2-to-1 MUX, 2-input (AND, OR, NAND and XOR) gates and Inverters. Note: Best grade for min. Number of ICs (not min. number of gates). By the way, a typical ALU chip is the 74181 which is capable of performing several arithmetic and logic functions. The 74181 has five function selection inputs M, S3, S2, S1, and S0 and a “carry” input to calculate the outputs. Lab 1 – ALU Design 8/12 Microprocessor Based Systems Lab Manual Lab 2 Traffic Light Controller Objective: - Design, Build, and Test a traffic light controller. Requirements: Design, build, and test a hardware circuit for a traffic light controller. N W E S System Description: There are four sensors embedded in the roadway (North (N) sensor, South (S) sensor, East (E) sensor, and West (W) sensor). Data switches may be used to implement the sensors. When a car has been detected waiting at a red light, the counter should count 12 pulses before it turns the green light of the other direction to yellow. The yellow light should remain on for 4 pulses and then turn red, simultaneously turning the red light on the other road to green. Since N/S Red is the complement of E/W Red, a flip-flop can be used to give the reds. The flip-flop toggles every 16 seconds. Once a mod sixteen counter has been connected to a flip-flop, the only additions are a few gates to indicate when the yellow and green lights are ON. For example, N/S Yellow is ON whenever E/W Red is ON and the count is between 12 and 15. It can be assumed that the green light is a particular direction is ON when the red and yellow light of the direction is not ON. If a vehicle stops at a red light makes a right turn, the counter should stop (provided no yellow light is ON). To cause the counting to continue, another vehicle must stop at a red light. Design aspects in detail: Some of the information given below may appear obvious but in the design process it is always useful to have all facts stated explicitly. 0) At any given instance both directions are not red. 1) Initially, assume some direction has the red light ON and the other the green light ON. 2) No cars in either direction ---- No change. ( If a car stops at a red light and makes a right turn then the count must start and as soon as the car leaves it should stop.) 3) Cars in only one direction ---- that direction remains green. 4) Cars in both directions ---- lights toggle every 16 seconds. (Yellow light of the direction that has the green light ON turns ON after 12 seconds. i.e. the yellow light is ON for the last 4 seconds. Lab 2 – Traffic Light Controller 9/12 Microprocessor Based Systems Lab Manual 5) If any yellow light is ON, the count continues even if there are no cars waiting. 6) Use six LEDs to represent N/S red, N/S yellow, N/S green, E/W red, E/W yellow, E/W green. 7) Since the counter chip (7493) has no “stop count” position the starting and stopping of the counter is achieved by turning on or off the clock as and when needed. Points of guidance 1- Start by drawing a block for the circuit showing its main components. 2- Use clock of frequency 1 Hz for the counter. 3- Use switches to simulate sensors. Only 2 switches are required; one for N/S direction, and one for E/W direction. 4- Read the design description then write down the logic equations for: a. The counter enable conditions. b. The flip-flop toggle conditions. c. The output LEDs of the circuit; N/S red, N/S yellow, N/S green, E/W red, E/W yellow, E/W green. 5- Given the available material, design a logic circuit for each of the above equations. Available Material - Digital-Analog Training System. - 4-bit Counter (7493) - JK flip flop (7476) - 2-input (AND, OR, NAND and NOR) gates and Inverters. Lab 2 – Traffic Light Controller 10/12 Microprocessor Based Systems Lab Manual Lab 3 Serial Communications Objective: - To be familiar with PC serial ports. Requirement: Connect two PCs directly through their serial port and write a program that splits the screen into two halves. The upper half displays what is being typed on the PC and the lower half displays what is being typed on the other PC. Guide Lines: 1. Take care when you are constructing your cable connection that both computers are DTEs so you need switch pin 2 and 3. 2. The program may contain two main procedures. One procedure for initializing the UART and the other procedure should detect any pressed keys and transmit it serially as well as detecting any received characters on the serial port. a. The first procedure is to be written for initializing the UART (baud rate, parity, data bits, stop bits,…) using IN/OUT instructions to the corresponding I/O ports without using the BIOS functions (interrupts). Use the same parameters on both terminals. Since you are connecting only two DTEs, they nee,,d not have I.Ds. (Remember that you could always use the BIOS function Int14h/AH=00h for initialization). b. You can use the BIOS function Int10h/AH=00h to clear the screen. (You could always go to the V-RAM directly to clear the screen). Setup your variables for the upper and lower halves of the screen. You could use different colors (attributer) for the upper and lower halves. You could also use windowing BIOS functions to support upper and lower half scrolling. For example; the following code uses Int 10h/AH=06 to initialize a window [(0,0) to (79,12)] or scroll window contents up mov ah,6 mov al,1 mov bh,7 mov ch,0 mov cl,0 mov dh,12 mov dl,79 int 10h Lab 3 – ; function 6 ; scroll by 1 line ; normal video attribute ; upper left Y ; upper left X ; lower right Y ; lower right X 11/12 Microprocessor Based Systems Lab Manual c. The second procedure should detect any pressed keys and transmit it serially as well as detecting any received characters on the serial port. i. If a key is pressed then a character is in the keyboard buffer (use the BIOS function Int 16h/AH=01h to check if a character is available in the keyboard buffer) then read it and display it in the upper half of the screen and send it to the UART using an Out instruction. If a character is present you could read it using Int16h/AH=00h, then display it at any place on the screen. Use the BIOS functions Int10h/AH=2 to set the cursor position to the upper half of the screen then use Int10h/AH=9 to display the character on the screen at the preset cursor position. ii. If a character is in the UART RDR (Receiver Data Register) then read it (using an In instruction) and display it in the lower half of the screen. You can check if a character is available in the UART RDR by reading the LSR (Line Status Register) using direct IN access from the UART RDR I/O Port, or by using the BIOS function Int14h/AH=3 which returns the LSR in AH. After reading the LSR check the Least Significant Bit. If it is high then a new byte is received in the RDR and is waiting to be read, otherwise the RDR is empty. You could read the RDR character using the BIOS function Int14h/AH=02h or by direct Input from the appropriate I/O port for the RDR. Finally use Int10/AH=2 to set the cursor position to the lower half of the screen, then use Int10/AH=9 to display the character at this position. iii. You scroll the upper and lower half of the screen independently. Int10h can be used for that also. iv. The above steps should be repeated until one of the users presses the ESC key. (The ASCII for ESC=27). Lab 3 – 12/12