THE UNIVERSITY OF THE WEST INDIES EXAMINATIONS OF APRIL MID-TERM 2005 Code and Name of Course: EE25M Introduction to Microprocessors Paper: MidTerm Date and Time: Thursday April 14th 2005 8AM Duration: One Hour INSTRUCTIONS TO CANDIDATES: This paper has 9 pages and 19 questions Answer ALL questions in Sections A and B. Marks for each question are given in the margin. Approved calculators may be used. SOLUTIONS!! DO NOT DISTRIBUTE PRIOR TO EXAM!! Section A (15 marks) contains 15 multiple-choice questions. Questions in Section A should be answered on this exam script. Choose the most appropriate answer for each question, and circle the letter for your choice. You should spend about 20 minutes on Section A. Section B (30 marks) contains 4 questions. Questions in Section B should be answered on this exam script. Any code written for Section B should be appropriately commented for clarity. You should spend about 40 minutes on Section B. The following datasheets/information are attached to/distributed with this exam paper. PIC16F87x instruction set summary Data sheet for Timer0 Please write your ID# on the front cover and on each page in the spaces provided. c The University of the West Indies
page 2. Section A Questions Q1, Q2, and Q3 refer to the following piece of code for the PIC16F877, to multiply two numbers M1 and M2. mul clrw loop addwf M2,W btfsc incf decfsz goto movwf STATUS,C R_hi,F M1,F loop R_lo Q1. The code will perform multiplication of the initial values in M1 and M2 using: (a) partial sums; result in R_hi and R_lo. (b) partial sums; result in M2, M1. (c) repeated addition; result in M2, M1. (d) repeated addition; result in R_hi and R_lo. (e) repeated addition; result in the working register. Q2. To use this code eectively, we MUST: Q2-I ensure that M1 is unsigned Q2-II ensure that M2 is unsigned Q2-III initialize R_hi to zero Q2-IV initialise R_lo to zero Choose the most appropriate combination: (a) Q2-I and Q2-II only (b) Q2-I and Q2-III only (c) Q2-I and Q2-IV only (d) Q2-II and Q2-III only (e) Q2-II and Q2-IV only Q3. How many machine cycles will this routine take to run (ignoring the cycles for the call)? (a) 1 + 5 (M1 1) + 6 (b) 1 + 5 (M1 1) + 7 (c) 1 + 5 (M2 1) + 6 (d) 1 + 6 (M1 1) + 8 (e) 1 + 6 (M2 1) + 8
page 3. Q4. Three sets of C code have been written to perform the multiplication of 255 and 27 using repeated addition. Q4-I Q4-II Q4-III void main() int k,m1,m2; long int R; void main() int k,m1,m2; long int R; void main() int k,m1,m2; long int R; R=0; M2=27; M1=255; for (k=0;k<=m1;k++) R=R+M2; R=0; M2=27; M1=255; for (k=m1;k>0;k++) R=R+M2; R=M2=27; M1=254; for (k=0;k<=m1;k++) R=R+M2; For the compiler used, integers are represented using 8 bits, and long integers are represented using 16 bits. When run, Program Q4-I never stops. Programs Q4-II and Q4-III, however, run as expected. The MOST PLAUSIBLE explanation for the failure of Program Q4-I is that: (a) R is not big enough to hold the result of the operation. (b) R should also be declared as an integer. (c) the number 255 cannot be represented using 8 bits. (d) the termination test requires representing the number 256 in 8 bits. (e) there is a aw in the compiler it generates bad code sometimes. Q5. A normally-closed push button switch needs to be interfaced to the PIC16F877, so that we can determine if it is pressed or not by checking the state of the input pin. Which of the following connections are viable alternatives? Q5-I tie the switch between PORTB pin and ground with internal pull-ups enabled Q5-II tie the switch between PORTB pin and ground with an external pull-up resistor Q5-III tie the switch between PORTB pin and V DD with internal pull-ups enabled Q5-IV tie the switch between PORTB pin and V DD with an external pull-down resistor (a) none of these are viable alternatives (b) Q5-I and Q5-IV only (c) Q5-I, Q5-II, and Q5-III only (d) Q5-I, Q5-II, and Q5-IV only (e) Q5-I, Q5-II, Q5-III, and Q5-IV
page 4. Q6. Select the most appropriate combination of words to complete the sentence. A programmer used the Q6-I instruction instead of the Q6-II instruction at the end of an Interrupt Service Routine for the PIC16F877. The program failed to work because Q6-III. (a) Q6-I: retfie; Q6-II:; Q6-III: interrupts were not re-enabled. (b) Q6-I: retfie; Q6-II:; Q6-III: PCL was not restored. (c) Q6-I: retfie; Q6-II:; Q6-III: the stack overowed. (d) Q6-I: ; Q6-II:retfie; Q6-III: interrupts were not re-enabled. (e) Q6-I: ; Q6-II:retfie; Q6-III: the stack overowed. Q7. A 4 bit timer/counter module is connected so that one of its inputs is a 100Hz signal, and the other is a signal whose frequency is to be determined. The frequency ranges which can be measured in counter mode are: (a) min: 0Hz; max: 1.5kHz; resolution ±100Hz (b) min: 0Hz; max: 1.6kHz; resolution ±50Hz (c) min: 100Hz; max: 1.5kHz; resolution ±100Hz (d) min: 100Hz; max: 1.5kHz; resolution ±50Hz (e) min: 100Hz; max: 1.6kHz; resolution ±100Hz Q8. You have a circuit in which the timer/counter module inside the PIC16F877 is used to count each falling edge from an externally supplied signal. You notice that the module counts twice on each falling edge. In order to discover the cause of the problem, the MOST appropriate action is to: (a) check the supply voltage for noise using a multimeter. (b) replace the PIC16F877 and see if the problem disappears. (c) use a pull-down resistor on the input pin and see if the problem disappears. (d) increase the frequency of the external signal and see if the problem disappears. (e) visually inspect for loose wires. Q9. Timer0 (8-bit timer) of the PIC16F877 is congured to increment on the rising edge of the internal machine cycle clock (CLKOUT) with a pre-scaler value of 1:64. What is the interval between successive overows of Timer0 if the PIC16F877 is clocked using a 10M Hz oscillator? (a) 64µs (b) 102.4µs (c) 256µs (d) 6553.6µs (e) 16384µs
page 5. The following information is to be used for questions Q10 to Q12. A rotary optical incremental encoder (rotary pulse generator) has two outputs, channel A and channel B. Channel A is used to generate external interrupts on a PIC16F877 at each rising edge. Channel B is connected to another pin on the PIC16F877. An Interrupt Service Routine (ISR) is used to determine the direction of rotation clockwise (rising edge of A coincides with a low on B) or anticlockwise (rising edge of A coincides with a high on B) and the distance turned (counting number of pulses on Channel A). Several "strange behaviours" may be observed with this system: P-I : counting less than the actual number pulses P-II : counting more than the actual number of pulses P-III : deducing the wrong direction of motion Q10. Which of the "strange behaviours" listed can occur if interrupts occur before the previous interrupt is handled? (a) P-III only (b) P-I and P-II only (c) P-I and P-III only (d) P-II and P-III only (e) P-I, P-II, and P-III Q11. Which of the "strange behaviours" listed can occur if the encoder output does not rise quickly enough? (a) P-III only (b) P-I and P-II only (c) P-I and P-III only (d) P-II and P-III only (e) P-I, P-II, and P-III Q12. Which of the "strange behaviours" listed can occur if the voltage supplying the PIC16F877 and the encoder uctuates? (a) P-III only (b) P-I and P-II only (c) P-I and P-III only (d) P-II and P-III only (e) P-I, P-II, and P-III
page 6. Q13. You are told that the 80C51 and PIC16F877 both have data transfer instructions. The 80C51 can move a literal to any register using one instruction: MOV Reg, #6. The PIC16F877 can move a literal to the working register, and subsequently to any register using two instructions: movlw 6 followed by movwf Reg. Based on this information ONLY, we can assume that an 80C51 program involving several literalto-register data transfers will probably: Q13-I have less lines of code Q13-II require less program storage space Q13-III run faster Which combination of stated assumptions is appropriate? (a) none of the above assumptions are appropriate. (b) Q13-I only (c) Q13-I and Q13-II only (d) Q13-I and Q13-III only (e) Q13-I, Q13-II and Q13-III Q14. Select the most appropriate combination of words to complete the sentence. The design of an embedded system starts with the Q14-I, which is followed by the Q14-II and then the Q14-III. (a) Q14-I: conceptual design; Q14-II: function statement; Q14-III: requirements list (b) Q14-I: conceptual design; Q14-II: requirements list; Q14-III: function statement (c) Q14-I: function statement; Q14-II: conceptual design; Q14-III: requirements list (d) Q14-I: function statement; Q14-II: requirements list; Q14-III: conceptual design (e) Q14-I: requirements list; Q14-II: function statement; Q14-III: conceptual design Q15. When writing routines for a microprocessor, you have been advised to always initialize your registers, even if the power-on-default value is the value you want. Which of the following statements is an INVALID reason for this guideline? (a) The microprocessor may be reset without re-powering up. (b) The microprocessor may power-up incorrectly. (c) The routine may run after the value was changed by another routine. (d) The routine may be interrupted while it is running. (e) The routine may work incorrectly if the register has a non-default value.
page 7. Section B The following information should be used for all questions in Section B. You are a member of a development team which is working on a new microprocessor-controlled refrigerator, using a PIC16F877 with a 700 Hz clock. The refrigerator consists of a cooling unit, a defrost unit, and a thermostat, which are connected to PORTD pins <2>, <1> and <0> respectively. loop: The cooling unit is switched on when PORTD<2> is high. The defrost unit is switched on when PORTD<1> is low. PORTD<0> has an external pull-up resistor. The thermostat pulls PORTD<0> low when the fridge temperature is above c + 2 degrees Celsius, and keeps the line low until the fridge temperature drops below c 2 degrees Celsius; where c is the temperature setting for the fridge. The defrost unit is activated for approximately 6 minutes each day. The main program algorithm is: configure pins if defrost unit is on else if defrost-time over turn off defrost unit start timing defrost-delay (239) if defrost-delay over else turn on defrost unit start timing defrost-time (1) if thermostat line active else turn on cooling unit turn off cooling unit Q1. Write a routine named Init which will congure PORTD<2>, PORTD<1> as outputs and PORTD<0> as an input. Your routine should NOT aect the conguration state of the other PORTD pins. You should at the end of your routine. label/; 3 marks correct conguration of TRISD register (lose each if wrong register/bank, if bad bits or if alters other bits). Sample answers: 4 marks Init BANKSEL TRISD bsf TRISD,0 bcf TRISD,1 bcf TRISD,2 BANKSEL PORTD Init BANKSEL TRISD movlw 0x01 iorwf TRISD,F movlw 0xF9 andwf TRISD,F BANKSEL PORTD
page 8. Q2. Write an interrupt handler for Timer0 named TmrHndler which clears the Timer0 ag, decrements the value in Cnt, and disables the Timer0 interrupt if Cnt is 0. You should at the end of your interrupt handler. label/; clear ag; disable timer interrupt only if Cnt is 0; decrement Cnt (with correct destination)) Sample answer: 4 marks TmrHndlr bcf decfsz bcf INTCON, T0IF Cnt,F INTCON, T0IE Q3. Write a routine named StrtTimer which takes the value in the working register, places it in a register called Cnt, clears the Timer0 register, congures Timer0 for 1:256 pre-scaler, and internal clock, (i.e. 700Hz clock signal; timer expires every 6.24 minutes) enables the Timer0 interrupt, and global interrupts You should at the end of your routine. move W into Cnt; appropriate banking for OPTION_REG and TMR0; 2 marks appropriate bits set in OPTION_REG (1:64 pre-scaler, CLKOUT) clear timer 0, 2 marks appropriate bits in INTCON (GIE, T0IF, and T0IE) Sample answer: 7 marks StrtTimer movwf Cnt BANKSEL OPTION_REG movlw B'xx0x0111' movwf OPTION_REG BANKSEL TMR0 clrf TMR0 movlw B'1x1xx0xx' movwf INTCON
page 9. Q4. Write an assembly language main program for the PIC16F877 which performs the algorithm provided. Your main program should indicate where the preceding routines Init and StrtTimer, the Interrupt Service Routine and handlers are to be placed, but you do NOT need to rewrite them. Your main program should use appropriate preprocessor directives. Max. 15 marks. use of directives LIST, INCLUDE. use of sleep or END directive. choice of data location for Cnt. 2 marks choice of program location & use of reset/interrupt vector. 2 marks place value (1 or 239) in W prior to call of delay routine. 2 marks use of call for routines, and goto for loops/vectors. 4 marks correct ow for each of the decision-statements. 2 marks turn on/o cooling and defrost units. 2 marks identify & correct aw in algorithm (cool/defrost simultaneously). 15 marks LIST p=16f877 INCLUDE <p16f877.inc> Cnt EQU 0x20 ORG 0x00 goto start ORG 0x04 goto ISR ORG 0x10 start call Init bcf PORTD,2 ; turn o cooling unit bsf PORTD,1 ; turn o defrost unit loop btfsc PORTD,1 ; if defrost unit is o goto dfsto dfston movf Cnt,F ; if defrost-time not over keep looping btfss STATUS,Z bsf PORTD,1 ; otherwise turn o defrost unit movlw D'239' ; and start timing defrost-delay call StrtTimer dfsto movf Cnt,F ; if defrost-delay not over check thermostat btfss STATUS,Z goto chkthm bcf PORTD,1 ; otherwise turn on defrost unit bcf PORTD,2 ; turn o cooling unit movlw D'1' ; and start timing defrost-time call StrtTimer chkthm btfss PORTD,0 ; if thermostat line is active bsf PORTD,2 ; turn on cooling unit btfsc PORTD,0 ; if thermostat line is not-active bcf PORTD,2 ; turn o cooling unit sleep ; put the two routines: Init and StrtTimer followed by the ISR and handlers here!! END