National University of Computer and Emerging Sciences School of Engineering Islamabad Campus Serial No: EE303-Microprocessor Interfacing & Programming Final Exam Total Time: 03 Hours Total Marks: 120 Wednesday 18 December, 2019 Course Instructors ________________ Dr. Waseem Ikram Engr. Azhar Rauf Signature of Invigilator ____________________________ _______ __________ _____________________ Student Name Roll No Section Signature DO NOT OPEN THE QUESTION BOOK OR START UNTIL INSTRUCTED . Instructions: 1. Verify at the start of the exam that you have a total of six (6) questions printed on fourteen (14) pages including this title page. 2. Attempt all questions on the question-book and in the given order. 3. The exam is open books, closed notes. Please see that the area in your threshold is free of any material classified as ‘useful in the paper’ or else there may a charge of cheating. 4. Read the questions carefully for clarity of context and understanding of meaning and make assumptions wherever required, for neither the invigilator will address your queries, nor the teacher/examiner will come to the examination hall for any assistance. 5. Fit in all your answers in the provided space. You may use extra space on the last page if required. If you do so, clearly mark question/part number on that page to avoid confusion. 6. Use only your own stationery and calculator. If you do not have your own calculator, use manual calculations. 7. Use only permanent ink-pens. Only the questions attempted with permanent inkpens will be considered. Any part of paper done in lead pencil cannot be claimed for rechecking. Total Marks Q-1 Q-2 Q-3 Q-4 Q-5 Q-6 Total 20 20 20 20 20 20 120 Marks Obtained Assessment of CLO CLO 2 CLO 4 CLO 4 CLO 4 CLO 5 CLO 5 Vetted By: ____________________________Vetter Signature: ____________________ National University of Computer and Emerging Sciences School of Engineering Islamabad Campus CLO-02: Describe the AVR based microcontroller architecture, its internal registers and instruction set. Q1. Briefly answer the following short Q/A on instructions and addressing modes. [20] a. Name four types of memory found in AVR ATMega32 systems and their typical sizes. [1] GPR (32), IO (64), SRAM (2048), External SRAM, EEPROM b. What would be the exact instructions to use if we want to substitute: a. LDS R17, $29 with the corresponding IN instruction? [2] IN R17, UBRRL or IN R17, $09 b. STS R29, $67 with the OUT instruction? Not possible, there is no corresponding IO Register for address $67 c. Write STS 55, 0x15 in pneumonic assembly form (i.e. refer to the registers by name). [1] STS DDRB, R21 d. Where does RISC stand with respect to different addressing modes? Elaborate. What benefit of indirect and indexed addressing modes? is the [2] RISC wants reuse and simple instructions. So, RISC microprocessors opt for few, most essential addressing modes. They avoid duplication and lot of complex indexing and indirect addressing. Benefit is the ability to use dynamic counting and looping as well as pointers in code where the count value is changed on the fly. e. Will assembly language programs written for ATMega32 work on ATMega128 version? Why or why not? [1] Since the register address boundaries will be different for both, it may work if only pneumonics are used to address registers. However, if physical or IO hex addresses are used, the program will most likely not run. f. Even though there are three register sets used for indirection, they differ in the way they can be used. Mention the major differences between these three registers. [1] All three are 16-bit registers. Register X cannot be used in the displacement mode while Y and Z can be. Register Z is the only one that can be used to load from program memory – others cannot. Final Exam Fall 2019 Page 2 of 14 National University of Computer and Emerging Sciences School of Engineering Islamabad Campus g. What is the difference between Little Endian and Big Endian architecture? Which one is ATMega 32? [1] h. How is the overflow flag affected by the Indirect addressing mode post-increment and predecrement operations? [1] i. What are the benefits of using IN/OUT vs. LDS/STS instructions? j. Given the below memory contents and code, indicate the final contents of the registers used and memory. Indicate one register for the pointer registers (e.g. use Z for ZL and ZH). [4] LDI DEC LDS LD MOV LDI LDI ST INC ST 26, 0x01 26 $1B, 26 R21, X R23, R21 28, 0x01 $1D, 0x03 Y, R23 R23 -Y, R23 [1] Address Bus (hex) 0100 0101 0102 0103 0104 Data Bus (dec) 67 99 -56 0 82 Register Contents k. Suppose seismic data from two sensors in local magnitude (ML) scale, commonly referred to as "Richter scale", are stored in two areas of contiguous AVR SRAM memory. Write a program to compare the two sets of data and display the higher value on Port C continuously. First set is at location 0x500 and the other at 0x600. Each set has 10 data values [5] Final Exam Fall 2019 Page 3 of 14 National University of Computer and Emerging Sciences School of Engineering Islamabad Campus CLO-04: Design hardware interface using datasheets and write software routines to fully exploit the capabilities of the devices. Q2. Two AVRs A and B are connected to each other through serial link as shown in figure below. The AVR ‘A’ reads character data from its RAM and transmits it serially to AVR ‘B’. The assembly code shows the functionality of AVR B controller. Answer the questions for the code and explanation given above. [20] Here: AVR AVR AVR A AVR B LDI R20, 0 ; counter which keeps track of the specific character LDI R16, 0xFF OUT DDRB, R16 LDI R16, 0x90 OUT UCSRB, R16 LDI R16, 0x86 Rx_ISR: IN R16, UDR OUT UCSRC, R16 CPI R16, ‘f’ LDI R16, 0x33 BREQ Equal OUT UBRRL, R16 RJMP Exit SEI Equal: INC R20 OUT PORTB, R20 Exit: RETI RJMP Here a. What are necessary condition(s) to enable communication between the two AVRs? [3] b. What function is performed by AVR ‘B’ controller as per the assembly code given? [2] Final Exam Fall 2019 Page 4 of 14 National University of Computer and Emerging Sciences School of Engineering Islamabad Campus c. Assume the interrupt for AVR ‘B’ develops a fault so interrupts no longer occur. Convert the interrupt based AVR ‘B’ assembly code shown above into a polling based code. [10] Final Exam Fall 2019 Page 5 of 14 National University of Computer and Emerging Sciences School of Engineering Islamabad Campus d. Connect the two processors for full-duplex communication. Is it possible to generate the serial communication clock baud rate using an external clock instead of the internal processor clock? Final Exam [5] Fall 2019 Page 6 of 14 National University of Computer and Emerging Sciences School of Engineering Islamabad Campus CLO-04: Design hardware interface using datasheets and write software routines to fully exploit the capabilities of the devices. Q3. A temperature monitoring system is to be implemented. The block diagram of the system is shown below. The temperature sensor is interfaced with analog input channel ADC0 of AVR. The temperature is measured after every 1 minute interval. Eight LEDs are connected to Port D of AVR to show the measured temperature. The buzzer is connected with PB1. It is turned on if the temperature is above the threshold otherwise it remains off. A 555 chip is configured to generate 1 Hz pulses and connected to T0 pin of AVR controller and AVR uses Timer 0 to measure 1 minute interval. [20] Temp. Sensor Buzzer AVR LEDS a. Write the main program which should perform following tasks: • Declare pin T0 and ADC0 as input and PORT D and PB1 as output. [2] • Configure Timer 0 to measure the interval of 1 minute and enable its interrupt. [4] • Display read temperature on Port D continuously. Assume that the sensed temperature can be accommodated in single byte. [2] Final Exam Fall 2019 Page 7 of 14 National University of Computer and Emerging Sciences School of Engineering Islamabad Campus b. Write the ISR which will be called every time the counter counts an interval of 1 minute. The ISR should perform following tasks: • • • Write the code to turn on the ADC module of the AVR, select the conversion speed of AVR (clk/4) and select ADC channel and reference voltage (Vref= 2.56 V internal). The ADC data should be right justified. [6] Write the code to sample and digitize the temperature and reads its value. [4] Compare the read value of temperature with threshold value of 350. If it is above the threshold, the buzzer must be turned on otherwise it must remain off. [2] Final Exam Fall 2019 Page 8 of 14 National University of Computer and Emerging Sciences School of Engineering Islamabad Campus CLO-04: Design hardware interface using datasheets and write software routines to fully exploit the capabilities of the devices. Q4. An AVR is setup to test its serial interface (transmitter and receiver) and also the wire link which connects the transmitter to the receiver. This done by connecting the TXD pin of the AVR to the RXD pin. The transmitter then transmits a test message to the receiver. The transmitted test message is compared with the received test message. The serial communication interface is verified if the two messages match. The testing can be repeated at different baud rates. [20] a. Write assembly code which initializes the USART for asynchronous communication, no parity, 1 stop bit and 8-bit character. [4] LDI R16, 0x86 OUT UCSRC, R16 ; asyn, parity disabled, 1 stop, 8-bit char LDI R16, 0x33 OUT UBRRL, R16 ; 9600 baud LDI R16, 0x18 OUT UCSRB, R16 ; enable TX and RX b. Write assembly code which transmits a message ‘TEST MESSAGE’ and receives the same and stores the received message in memory. [10] LDI XH, 0x05 LDI XL, 0x00 LDI YH, 0x06 LDI YL, 0x00 AGAIN: [1] LPM R17, X+ CPI R17, 0 BREQ EXIT RCALL TMT RCALL RCV RJMP AGAIN [2] MSG .DB ‘TEST MESSAGE’,0 [1] TMT: SBIS USCRA, UDRE EXIT: .ORG $500 RJMP TMT OUT UDR, R17 RET RCV: [2] SBIS UCSRA, RXC RJMP RCV IN R17, UDR ST Y+, R17 RET Final Exam [4] Fall 2019 Page 9 of 14 National University of Computer and Emerging Sciences School of Engineering Islamabad Campus c. Write assembly code which compares the ‘TEST MESSAGE’ with the received message. If the sent and received messages match Port A displays 00 otherwise it displays FF [6] COMPARE: LDI XH, 0x05 LDI XL, 0x00 LDI YH, 0x06 LDI YL, 0x00 NEXT: [1] LM R16, X+ LM R17, Y+ CPI R17, 0 BREQ MATCH CP R16, R17 BRNE NOMATCH RJMP NEXT NOMATCH: [3] LDI R17, 0xFF OUT PORTA, R17 RJMP EXIT MATCH: LDI R17, 0 OUT PORTA, R17 [2] EXIT: Final Exam Fall 2019 Page 10 of 14 National University of Computer and Emerging Sciences School of Engineering Islamabad Campus CLO-05: Design and implement stand-alone microcontroller based systems. Q5. A stepper motor based robot can move precise steps in the forward and backward direction. The user specifies the direction and steps and the robot moves precisely in the specified direction. [20] a. Draw the connection of the stepper motor to the AVR through a driver. The stepper motor is a unipolar motor. The stepper motor rotates 1o with each step. Robot wheel diameter is 10 cm. How much distance will the robot cover if 0b0010101 specifies the number of steps. [8] [6] Distance covered = (21/360) x 10 = 1.83 cm [2] b. Write assembly code which reads an 8-bit value from Port A. The distance to be moved is calculated and the robot moves in the forward and backward direction for the specified distance. The MSB specifies the forward (MSB = 1) or backward (MSB = 0) direction of movement. The remaining 7 bits specify the steps. [12] LDI R20, 0xFF OUT DDRB, R20 LDI R20, 0 OUT DDRA, R20 LDI R20, 0x66 L1: OUT PORTB, R20 IN R16, PINA SBRS R16, 7 BRGE forward ANDI R16, 0x7F BKAgain: LSR R20 BRCC OV1 ORI R20,0x80 OV1: OUT PORTB, R20 RCALL DELAY DEC R16 BNEQ BKAgain RJMP L1 Final Exam ; Port B o/p ; Port A i/p [2] ; ; ; ; ; ; [1] step pattern read input check for direction forward get number of steps next step [3] ; step pattern ; last step backward? [3] Fall 2019 Page 11 of 14 National University of Computer and Emerging Sciences School of Engineering forward: ANDI R16, 0x7F FDAgain: LSL R20 BRCC OV2 ORI R20,0x01 OV2: OUT PORTB, R20 RCALL DELAY DEC R16 BNEQ FDAgain RJMP Ll Final Exam Islamabad Campus ; get number of steps ; next step ; step pattern ; last step forward? [3] Fall 2019 Page 12 of 14 National University of Computer and Emerging Sciences School of Engineering Islamabad Campus CLO-05: Design and implement stand-alone microcontroller based systems. Q6. The speed of a variable speed DC motor is proportional to the pulse width of an input signal. Thus wider the input pulse higher is the motor speed and vice versa. The following specification are used to implement the DC motor speed controller: [20] XTAL = 8 MHz Pulse width of the input signal is in the range of 1 μs to 255 μs The frequency of the Phase correct PWM mode used to control the DC motor speed is 1.961 kHz. Since the motor speed is proportional to pulse width therefore: Input signal pulse width 255 s 191 s 128 s 64 s Duty cycle for DC motor 100% 75% 50% 25% a. To which pin of the AVR is the input signal connected? [2] b. Draw the connection of the DC motor with the AVR through a driver circuit. Timer 2 is used to generate the Phase correct PWM to control the DC Motor speed. [6] c. Write assembly code for the speed of the DC motor based on the input pulse width. [12] A. a. ICP1 (PD6) b. Connected to OC2 (PD7) pin Final Exam Fall 2019 Page 13 of 14 National University of Computer and Emerging Sciences School of Engineering c. Fgenerated wave = Foscillator/510N Islamabad Campus N = Foscillator/510 Fgenerated wave = 8000/510 x 1.961 = 8 BEGIN: [1] LDI R20, 0x00 OUT TCCR1A, R20 L3: L1: LDI R20,0x42 ; input capture on rising edge OUT TCCR1B, R20 ; prescalar divide/8 SBI DDRD, 7 ; set OC2 output [2] IN R21, TIFR SBRS R21, ICF1 ; check flag for edge RJMP L1 ; no edge goto check again IN R16, ICR1L ; save rising edge arrival time OUT TIFR, R21 ; reset flag LDI R20, 0x02 ; reconfigure for falling edge [3] OUT TCCR1B, R20 L2: IN R21, TIFR SBRS R21, ICF1 ; check flag for edge RJMP L2 ; no edge goto check again IN R22, ICR1L ; save falling edge arrival time OUT TIFR, R21 ; reset flag SUB R22, R16 ; calculate pulse width OUT OCR2, R22 ; duty cycle [3] LDI R20, 0x62 Final Exam OUT TCCR2, R20 ; non-inverted mode, prescalar div/8 RJMP L3 ; measure next pulse Fall 2019 [3] Page 14 of 14