CSE211s_2023_Final_with_asnwers
CSE211s_2023_Final_with_asnwers
CSE211s_2023_Final_with_asnwers
FACULTY OF ENGINEERING
Computer and Systems Engineering Department
Junior Level, Electrical power and Machines Engineering
Spring 2023 - Final Exam (Model Answer) Course Code: CSE 211s Time allowed: 2 Hrs.
INTRODUCTION TO EMBEDDED SYSTEMS
The Exam Consists of 50 Questions in Eleven Pages. Maximum Marks: 60 Marks 1 / 11
Important Rules: تعليمات هامة
• Having a )mobile -Smart Watch- earphones) inside the examination hall سماعة األذن) داخل لجنة- الساعات الذكية-• حيازة (املحمول
is forbidden and is considered as a cheating behavior. . المتحان يعتبر حالة غش تستوجب العقاب
• It is forbidden to have any references, notes, books, or any other
• ليسمح بدخول أي كتب أو مالزم أو أوراق داخل اللجنة
materials even if it is not related to the exam content with you in the
examination hall. .واملخالفة تعتبر حالة غش
For each of the following 50 multiple choice questions (MCQs), select ONLY the ONE correct answer.
Mark your choice on the answer bubble sheet. .................................... [The 50 MCQs are equal in weight]
1. Using Arduino Uno board, which of the following statements can be used to define PIN3 as an output
port and set it to one?
A) pinMode (3, OUTPUT); digitalWrite (1, 3); B) pinMode (3, OUTPUT_PULLUP); digitalWrite (3, HIGH);
C) pinMode (3, OUTPUT); digitalWrite (3, HIGH); D) pinMode (3, INPUT); digitalWrite (HIGH, 1);
2. Using Arduino Uno board, what are the best parameters to send to an analogWrite function to generate
a square wave on PIN4 with a 4% duty cycle?
A) 4,40 B) 4,0.04 C) 4,4 D) 4,10
3. Which expression can be used to check if the 3rd bit on an 8-bit variable X is one?
A) ((X & 0x04)>>2) ==1 B) ((X & 0x04)>>3) ==1 C) ((X & 0x03) <<2) ==1 D) ((X | 0x04) >>3) ==1
4. Using Arduino Uno board, what is the third parameter to send to the function attachInterrupt
(digitalPinToInterrupt(3), ISR, ________); to call a function named “ISR” on every change from HIGH to
LOW only on PIN 3?
A) FALLING B) LOW C) RISING D)CHANGE
5. Using Arduino Uno board, what is the value of variable “y” after executing the statements
AnalogReference(EXTERNAL); int y=analogRead(0);, given that VR =2.50 V and 1.15 V is applied on analog
pin 0?
A) 235 B) 511 C) 470 D) 115
6. Which of the following is not a correct statement from I2C specifications?
A) Easy to add on slave devices B) Supports 5MB/s in the ultra-fast mode
C) A simple two-wire communication protocol D) Has an SCL line for data and an SDA line for clock.
7. How many wires are needed to communicate between a one microcontroller as a master and 7 devices
as slaves (4 using SPI and 3 using I2c)?
A) 10 B) 12 C) 9 D) 7
8. Given that the orange color can be represented by 0xFF7F27 using RGB888 (24-bits color depth), what is
the orange color value in RGB565 (16-bits color depth)?
A) 0xFBA3 B) 0xEA44 C) 0xFA4E D) 0xFBE4
9. Which PID controller setting(s) has/have a major effect on steady-state error and can eliminate it?
A) Ki B) Kp, Kd C) Kp D) Kd
10. Using Sony remote control (12-bit SIRC, command in 7 bits followed by the address in 5 bits), what is the
command sent in the following pattern?
A) 68 B) 84 C) 17 D) 21
11. How many wires are common between master and all slaves, when using SPI in communication?
A) 2 B) 1 C) 3 D) 4
12. The main advantage of I2C over SPI is: “it is easy to add new slave devices onto the bus in I2C, unlike SPI
that needs a dedicated line for each slave device.” What is the name of that line that is needed in SPI for
each slave device?
A) MOSI B) SS/CS C) MISO D) SCLK
13. Which PID controller setting(s) has/have a major effect on settling time and can increase the damping on
the output?
A) Kp B) Ki C) Kd D) Kp, Ki
14. Which function can be used to read an analog input?
A) Read () B) AnalogGet () C) analogRead () D) ReadAnalog ()
15. In a C-language code, X and Y are defined as char variables, if X= ‘3’ and Y= X+1, then Y is equal to ___.
A) 4 B) 31 C) ’31’ D) ’4’
Questions 16—22
An Arduino Uno
microcontroller is used to
control a stepper motor for
solar tracking system. The
used motor has unipolar
configuration, as shown in
the figure. We can turn the
motor clockwise (CW) by
driving B-, A-, B+, A+ in
sequential order. The
resistance of the used LDR
is almost zero with
maximum light intensity
and increases to 100k in
darkness. If the intensity of
the light on the left LDR is more than the right LDR, the motor should rotate counterclockwise (CCW) and vice
versa. The speed of the motor should be direct proportional to the difference between the two intensities of
the light on both LDRs. The motor should stop if the difference is zero. PROG1 performs this functionality.
PROG1 Q16-Q22
1 #define MOTOR_OUT_PIN_BASE 3
2 #define RIGHT_ANALOG_IN 5
3 #define LEFT_ANALOG_IN 4
4 #define MIN_DELAY 100
5 #define MAX_DELAY 500
6 byte output_value=1;
7 void setup () {for (int i=0; i<4; i++) pinMode(MOTOR_OUT_PIN_BASE+i, OUTPUT); }
8 void loop(void) {
9 for (int i=0; i<4; i++) digitalWrite (MOTOR_OUT_PIN_BASE+i, (<EXPR1>));
10 int error=analogRead (RIGHT_ANALOG_IN) – analogRead (LEFT_ANALOG_IN);
11 int abs_error=<EXPR2>;
12 int delay_v=map (abs_error, <V1>, <V2>, MAX_DELAY, MIN_DELAY);
13 if (<C1>) {
14 if (error>0) output_value=(output_value==<V3>)? <V4>: output_value>>1; //CCW
15 else output_value=(output_value==<V5>)? <V6>: output_value<<1; //CW
16 }
17 delay(delay_v);
18 }
16. In PROG1 line 9 the <EXPR1> should be
A) output_value>i & 0x1 B) output_value<i & 0x1 C) output_value>>i & 0x1 D) output_value<<i & 1
17. In PROG1 line 11 the <EXPR2> should be
A) (error<0)? error: -error B) (error>0)? error: -error C) (error==0)? error: -error D) (error<=0)? error: -error
18. In PROG1 line 12 the best value for <V1> should be
A) -1023 B) 0 C) -1013 D) -1000
19. In PROG1 line 12 the best value for <V2> should be
A)255 B) 1023 C) 1013 D) 1000
20. In PROG1 line 13 <C1> should be
A) delay_v<MAX_DELAY B) delay_v==MAX_DELAY C) delay_v<MIN_DELAY D) delay_v>MIN_DELAY
21. In PROG1 line 14 <V3>, <V4> should be
A) 8,1 B) 0,1 C) 8,0 D) 1,8
22. In PROG1 line 15 <V5>, <V6> should be
A) 8,1 B) 0,1 C) 8,0 D) 1,8
Questions 23—29
Hint: PROG2 reads (VA2, VA3 and VA4), sorts them then calculates relative delay for each one.
PROG2 Q23-Q29
1 #include <MsTimer2.h>
2 #define ANALOG_IN_BASE 2
3 #define MOTOR_OUT_PIN_BASE 2
4 #define NO_OF_MOTORS 3
5 #define SWAP(A,B,temp) <EXPR1>
6 unsigned int Off_time [NO_OF_MOTORS], max_delay;
7 byte motor_index [NO_OF_MOTORS];
8 void sort_motor_delay () {
9 unsigned int temp;
10 for (byte k=0; k<NO_OF_MOTORS;k++) motor_index[k]=k;
11 for (byte i=0;<C1>; i++) {
12 for (byte <EXPR2>; j<NO_OF_MOTORS; j++) {
13 if(<C2>) {// sort the values ascending
14 SWAP(Off_time[i], Off_time[j], temp);
15 SWAP (motor_index[i], motor_index[j], temp);
16 }}}
17 }
18 void read_required_angle(){
19 for (byte k=0; k<NO_OF_MOTORS;k++) {
20 unsigned int v=analogRead (ANALOG_IN_BASE+k);
21 Off_time[k]=1000-map(v,0,1023,0,1000);
22 }}
23 void isr () {
24 read_required_angle (); sort_motor_delay();
25 max_delay=Off_time [NO_OF_MOTORS-1];
26 for (byte k=NO_OF_MOTORS-1; k>0; k--) <EXPR3>; //calculate relative delay
27 for (byte k=0; k<NO_OF_MOTORS;k++) {
28 delayMicroseconds (Off_time[k]);
29 digitalWrite(MOTOR_OUT_PIN_BASE+motor_index[k],1);
30 }
31 delayMicroseconds(<EXPR4>);
32 for (byte k=0; k<NO_OF_MOTORS; k++) digitalWrite(MOTOR_OUT_PIN_BASE+k,0);
33 }
34 void setup () {
35 for (byte k=0; k<NO_OF_MOTORS; k++) pinMode(MOTOR_OUT_PIN_BASE+k, OUTPUT);
36 MsTimer2::set(<V1>,isr);MsTimer2::start(); }
37 void loop(void) {}
23.3 In PROG2 line 5 the <EXPR1> should be
A) A=B; B=A; A=temp B) temp=B; A=B; B=temp C) B=temp;A=temp;B=A D) temp=A; A=B; B=temp
24. In PROG2 line 11 the <C1> should be
A) i<NO_OF_MOTORS-1 B) i<NO_OF_MOTORS C) i<NO_OF_MOTORS+1 D) i<NO_OF_MOTORS-2
25. In PROG2 line 12 the <EXPR2> should be
A) j=0 B) j=i C) j=i+1 D) j=i-1
26. In PROG2 line 13 the <C2> should be
A) Off_time[i]<Off_time[j] B) Off_time[i]==Off_time[j] C) Off_time[i]>Off_time[j] D) Off_time[i] !=Off_time[j]
27. In PROG2 line 26 the <EXPR3> should be
A) Off_time[k] +=Off_time[k-1] B) Off Off_time[k-1] + =Off_time[k]
C) Off Off_time[k-1] -=Off_time[k] D) Off_time[k] -=Off_time[k-1]
28. In PROG2 line 31 the <EXPR4> should be
A) 2000-max_delay B) 1000-max_delay C) 2000+max_delay D) 1000+max_delay
29. In PROG2 line 36 the <V1> should be
A) 1000 B) 18 C) 2000 D) 10
Questions 30—39
An Arduino Uno microcontroller is used to decode the received pattern from Sony remote controller using
IR-RCVR. As shown in the figure the pattern starts with the start bit followed by the command in 7 bits then
the address in 5 bits. An Uno microcontroller runs PROG3 to decode the received pattern into Command
and Address.
Note:
1) The IR-RCVR output level is inverted, such that the Start bit is received as logic “0” and the gap as logic
“1”.
2) Received signal timing durations can be increased/decreased with a tolerance 20%, i.e., exact value
for START bit duration equals to 2.4 ms and is acceptable to be (2.4±20%), that is 1.92—2.88 ms.
PROG3 Q30-Q39
1 #define IR_REC_PIN 3
2 #define START 2400 // 2.4 ms
3 #define ZERO_DURATION 600 // 0.6 ms
4 #define ONE_DURATION 1200 // 1.2 ms
5 #define TOL 0.2 // tolerance value 20%
6 #define COMMAND_N_BITS 7
7 #define ADDRESS_N_BITS 5
8 long time_duration;
9 byte rec_buffer [COMMAND_N_BITS+ADDRESS_N_BITS], rec_index=0;
10 void setup () {
11 pinMode (IR_REC_PIN, INPUT); Serial.begin(9600);
12 attachInterrupt (digitalPinToInterrupt (IR_REC_PIN), pin_isr,CHANGE);
13 }
14 void pin_isr(){
15 if (<C1>) {
16 time_duration= micros () - time_duration;
17 if ((time_duration> (1 - EXPR1) * EXPR2)) && (time_duration< (1+ EXPR1) * EXPR2)))
rec_index=0;
18 else if ((time_duration> (1- EXPR1) * EXPR3) && (time_duration< (1+ EXPR1) * EXPR3))
rec_buffer[rec_index++] =1;
19 else if ((time_duration> (1- EXPR1) * EXPR4) && (time_duration< (1+ EXPR1) *EXPR4))
rec_buffer[rec_index++] =0;
21 if (rec_index== EXPR5) {
Questions 40—50
The figure shows the connection between a water tank and its level controller. The controller has two inputs:
High-Level Sensor and Low-Level Sensor, which sense the level of the water inside the tank. Both sensors are
connected through signal conditioning circuits to output logic high/low voltage levels. High voltage level (logic
1) on the output of the signal conditioning circuits means that the water level reached the sensor. Controller
output (PUMP_PIN) is used to control a water pump. ON (Green) and ALARM (Red) LED indicators are used to
show the state of the pump. The controller runs PROG4 on Uno microcontroller. The pump should be turned
on, if the water level in the tank is below the low level until reaching the upper level, then it is turned off until
the water level drops again below the lower level. If it happens that the pump is on for 1 s without having the
water reaching the low-level sensor or 3 s without having the water reaching the high-level sensor, the pump
is turned off and the ALARM LED is switched on for 6 s. Then, the pump is switched on again (with ALARM LED
off), continuing as normal.
Note: On LED should be enabled only while switching on the pump
PROG4 Q40-Q50
1 #include<MsTimer2.h>
2 #define HIGH_LEVEL_PIN 2
3 #define LOW_LEVEL_PIN 3
4 #define ON_LED 4
5 #define ALARM_LED 5
6 #define PUMP_PIN 6
7 #define PUMP_OFF_STATE 0
8 #define PUMP_ON_STATE 1
9 #define PUMP_ALARM_STATE 2
12 byte PUMP_STATE=PUMP_OFF_STATE;
13 void timer_isr(){
14 MsTimer2::stop();
15 if (PUMP_STATE==PUMP_ALARM_STATE) {
16 PUMP_STATE=<EXPR1>;
17 on_state_counter=0;
18 } else {
19 on_state_counter+=1;
22 }
23 check_water_level_isr();
24 }
25 void check_water_level_isr(){
27 PUMP_STATE= <EXPR3>;
28 on_state_counter=0;
30 PUMP_STATE= <EXPR4>;
31 }
32 if (PUMP_STATE==PUMP_OFF_STATE) {
34 }
35 if (PUMP_STATE==PUMP_ON_STATE) {
37 MsTimer2::set(<V11>,timer_isr);MsTimer2::start();
38 }
39 if (PUMP_STATE==PUMP_ALARM_STATE) {
41 MsTimer2::set(<V12>,timer_isr);MsTimer2::start(); on_state_counter=0;
42 }
43 }
44 void setup () {
50 check_water_level_isr();
51 }
END of Exam