Philadelphia University Faculty of Engineering Course Title: Embedded Systems (630414) Instructor: Eng. Anis Nazer Dept. of Computer Engineering Final Exam, First Semester: 2016/2017 Student Name: Student Number Date: 6/1/2017, No. of Pages: 3 Time Allowed: 2 hours Instructions: ALLOWED: Non-programmable calculator, pens and drawing tools (no red colour). NOT ALLOWED: Papers, literatures and any handouts. Otherwise, it will lead to the non-approval of your examination. Shut down Telephones, and other communication devices. Please note: Write your name and your ID number on every page of the solution sheets. All solutions together with solution methods (explanatory statement) must be inserted in the labeled position on the solution sheets. Support your answer with diagrams, equations and examples when possible You can submit your exam after the first hour. ASSUME THAT THE PIC16F84A MICROCONTROLLER IS USED IN ALL QUESTIONS UNLESS SPECIFIED OTHERWISE Question 1 (4 marks) Answer the following questions briefly: a) What is the name of your project in the course? b) Name one of the configuration bits and explain its purpose CP: code protect, to prevent read/write of program memory after downloading code FOSC1, FOSC0 : to select oscillator type and frequency WTDE: to enable the watchdog timer PWRTE: to enable the power up timer c) What is the purpose of the stack memory in the PIC microcontroller To hold the return address after a call or interrupt d) Draw a proper connection for a button connected to the MCLR input of the PIC16F84A
Question 2 (11 marks) It is required to design the controller for a washing machine. The system is composed of the following components: PIC16F84A microcontroller A DC motor, and an H bridge An electronic valve to control the flow of water A switch to select the washing cycle type, we have a cycle for white clothes and a cycle for colored clothes A push button to start the washing cycle A heating element to heat the water The washing machine operates as follows: 1) The system waits for the start button to be pushed 2) The heater is turned on for a period of time: 30 seconds for colored clothes and 60 seconds for white clothes 3) The water valve is turned on for 60 seconds 4) Motor is rotated clockwise for 10 seconds 5) Motor is rotated counter clockwise for 10 seconds. 6) Repeat steps (4) and (5) for a period of time: 30 minutes for colored clothes, and 60 minutes for white clothes Answer the following questions and state any assumptions you make: a) Specify the inputs and outputs of the microcontroller Inputs: switch to select the washing cycle, button to start the washing Outputs: DC Motor, Heater, Water valve b) Draw a simplified block diagram of the system, showing the components as blocks (2 marks) PIC16F84A Switch: 0: closed, white 1: open, colored RA0 RB0 Water valve Button: 0: pushed, start 1: open RA1 RB1 RB2 H bridge Motor RB3 Heater
c) Draw a flow chart of the program to control the system (3 marks) Start Is button pushed no Heater ON delay 30s White? delay 30s no Heater OFF OPEN Water valve delay 60s CLOSE Water valve T = 90 no White? T = 180 Rotate Motor clockwise delay 10s Rotate Motor counter clockwise delay 10s T = T-1 Motor OFF no T > 0
d) Write a program in C or assembly to operate the system, Assume a delay of 10 seconds is available (5 marks) Assembly: #include "p16f84a.inc" c EQU 0x0C ; counter for motor ; select inputs and outputs BSF STATUS, RP0 CLRF TRISB MOVLW 0x03 MOVWF TRISA BCF STATUS, RP0 CLRF PORTB ; wait for start start BTFSC PORTA, RA1 GOTO start ; heater BSF PORTB, 3 ; heater on CALL delay_30s BTFSC PORTA, 0 CALL delay_30s BCF PORTB, 3 ; heater off ; water BSF PORTB, 0 CALL delay_60s BCF PORTB, 0 ; motor BTFSC PORTA, 0 MOVLW D'90' BTFSS PORTA, 0 MOVLW D'180' MOVWF C loop BSF PORTB, 1 ; clockwise BCF PORTB, 2 BCF PORTB, 1 ; counterclockwise BSF PORTB, 2 DECFSZ T, 1 GOTO loop C #include "p16f84a.inc" void main() int c = 0; TRISB = 0; // output TRISA = 3; // input while ( 1 ) // wait for start while ( PORTA.F1 == 1 ) ; // heater PORTB.F3 = 1; if ( PORTA.F0 == 1 ) Delay_ms(30000); else Delay_ms(60000); PORTB.F3 = 0; // water PORTB.F0 = 1; Delay_ms(60000); PORTB.F0 = 0; // motor if ( PORTA.F0 == 1 ) c = 90; else c = 180; do PORTB = 0x02; Delay_ms(10000); PORTB = 0x04; Delay_ms(10000); c = c-1; while ( c > 0 ) ; PORTB = 0; CLRF PORTB GOTO start ; motor off ; repeat cycle delay_30s RETURN delay_60s CALL delay_30s CALL delay_30s RETURN END
Question 3 (8 marks) You need to design an electronic die نرد) (حجر. The system is composed of a common anode 7 segment display and a switch. When the switch is closed the 7 segment display shows a number between 1 and 6. The flow chart of the program is shown. a) Draw a circuit diagram showing the detailed connection for the PIC Start microcontroller, the switch, and the 7 segment display N = 1 display N Is switch closed No Increment N No N = 7? N = 1 b) Write the lookup table in assembly language for the numbers from 1 to 6 N RB6 g RB5 f RB4 e 1 1 1 1 1 0 0 1 2 0 1 0 0 1 0 0 3 0 1 1 0 0 0 0 4 0 0 1 1 0 0 1 5 0 0 1 0 0 1 0 6 0 0 0 0 0 1 0 RB3 d RB2 c RB1 b RB0 a (2 mark) table ADDWF PCL,1 RETLW 0xFF ; this entry is because N starts from 1 RETLW 0x79 RETLW 0x24 RETLW 0x30 RETLW 0x19 RETLW 0x12 RETLW 0x02
c) Write a complete program in assembly language that will implement the required program. (4 marks) #include "p16f84a.inc" N EQU 0x0C ; counter ; select inputs and outputs BSF STATUS, RP0 CLRF TRISB BSF TRISA, 0 BCF STATUS, RP0 CLRF PORTB MOVLW 1 MOVWF N main MOVF N, 0 CALL table MOVWF PORTB BTFSS PORTA, 0 INCF N, 1 MOVLW D'7' XORWF N, 0 BTFSC STATUS, Z CALL reset reset MOVLW 1 MOVWF N RETURN table ADDWF PCL,1 RETLW 0xFF ; This entry is because N starts from 1 RETLW 0x79 RETLW 0x24 RETLW 0x30 RETLW 0x19 RETLW 0x12 RETLW 0x02 END
Question 4 (8 marks) I always hit the wall when parking my car :( I need your help in designing a system that will solve this problem. You proposed to use the following components: An analog sensor to measures the distance between the car and the wall. The sensor gives a voltage between 1V and 5V that is proportional to the distance. If the distance is 10cm, the sensor gives 1V, if the distance is 50cm the sensor gives 5V Three LEDs: green, orange, and red PIC16F877 microcontroller which has a 10 bit ADC converter. The green LED is on if the distance is larger than 40cm, the orange LED is on if the distance is between 20cm and 40cm, and the red LED is on if the distance is less than 20cm a) What is the output voltage of the sensor if the distance is 40cm? 10cm 1V 50cm 5V so, 40cm 4V b) What is output of the ADC if the input voltage is 4V? step size = (5-1) / 1023 = 4/1023 = 0.00391 Output = (4-1) / 0.00391 = 767.25 => output = 767 = 0b1011111111 or 768 = 0b1100000000 c) What input voltage of the ADC would result in an output of 0b1000000000? if ADC output = 0b1000000000 = 512 Input = 1 + 512*0.00391 = 3V d) What values should be stored in ADCON0 and ADCON1 to operate the ADC? ADCON0 = 0bxx0000x1 ADCON1 = 0bxxxx1000 or 0bxxxx1011 or 0bxxxx1100 or 0bxxxx1101 or 0bxxxx1111 (2 marks)
e) Write a program in C or assembly to operate the system as described. (3 marks) #include "P16F877.inc" BSF STATUS, RP0 CLRF TRISB BSF TRISA, 0 BSF TRISA, 2 BSF TRISA, 3 MOVLW 0x88 MOVWF ADCON1 BCF STATUS, RP0 MOVLW 0x01 MOVWF ADCON0 void main() unsigned int N=0; TRISB = 0; TRISA = 0x0D; ADCON1 = 0x88; ADCON0 = 0x01; while (1) ADCON0.GO_DONE = 1; while ( ADCON0.GO_DONE == 1) ; main BSF ADCON0, GO_DONE wait BTFSC ADCON0, GO_DONE GOTO wait MOVLW 0 XORWF ADRESH, 0 BTFSC STATUS, Z GOTO RED MOVLW 0x03 XORWF ADRESH, 0 BTFSC STATUS, Z GOTO GREEN N = ADRESH; N = N << 8; N = N + ADRESL; if ( N < 256 ) PORTB = 0x04; else if ( N < 768 ) PORTB = 0x02; else PORTB = 0x01; GOTO ORANGE RED MOVLW 0x04 MOVWF PORTB ORANGE MOVLW 0x02 MOVWF PORTB GREEN MOVLW 0x01 MOVWF PORTB END
Question 5 (9 marks) Choose the most correct answer: Question 1) Which of the following is true? be honest :) A) I LOVE the course B) I HATE the course C) I have mixed feelings?!? D) I don't know how I feel!!!???? 2) The maximum size of a lookup table in the PIC16F84A is : A) 1000 value B) 100 value C) a) 2000 value D) 256 value Answer A D 3) Assume RA2 is configured as output, and the PIC executed the instruction BSF PORTA, 2 which of the following is true? A) The pull up transistor is ON B) The pull down transistor is ON C) Both pull up and pull down transistors are ON D) Both pull up and pull down transistors are OFF 4) Which of the following is true about the PIC microcontroller? (I) It has a CISC instruction set (II) It is a Harvard architecture A) (I) only B) (II) only C) (I) and (II) D) Both are false 5) Assume an 8MHz clock is used, what is the approximate delay of the following code? L1 MOVLW D'200' MOVWF counter NOP NOP DECFSZ counter, 1 GOTO L1 A B C A) 1000s B) 125 s C) 0.5ms D) 1s 6) Which of the following INTCON values will enable the external and timer interrupts of the PIC16F84A? A) 0xB0 B) 0x30 C) 0x86 D) 0x36 7) Which of the following interrupt sources cannot be disabled? A) external interrupt B) timer interrupt C) PORTB interrupt D) None of the choices 8) Assume a 10MHz oscillator is connected to the PIC16F84A, how long would it take for the timer0 to overflow if you store the value of 0xC3 in the OPTION register and start the timer from 0 A) 1.6384ms B) 3.2768ms C) 0.1024ms D) 12.8 s 9) Assume a 10MHz oscillator is connected to the PIC16F84A and only the timer0 interrupt is enabled. What is the maximum possible time between interrupts generated by the timer0 interrupt? A) 1 s B) 0.5 s C) 26.2 ms D) 6.55 ms A D A C Lcd_Out(1,1," GOOD LUCK :) ");