Embedded Systems-Sm
Embedded Systems-Sm
SECTION A
b) Study the provided code and answer the questions that follow:
c
#include<REGX51.H>
sbit led = P2^0;
void main(void) {
unsigned int delay = 0;
while(1) {
led = 1; // LED ON
for(delay = 0; delay < 20000; delay++); // delay
led = 0; // LED OFF
for(delay = 0; delay < 20000; delay++); // delay
}
}
i. Explain the effect after the delay has been changed from 20000 to
40000 (4 marks)
• The delay loops control how long the LED stays ON and OFF.
Increasing the delay from 20000 to 40000 doubles the time the loop
iterates for each state (ON and OFF).
• Effect:
• The LED will remain ON for twice as long (longer illumination
period).
• The LED will remain OFF for twice as long (longer dark period).
• The overall blinking frequency decreases, making the blinking
slower and more noticeable.
• The duty cycle (ON/OFF ratio) remains unchanged, but the total
blink cycle time doubles.
ii. Explain the impact of LED (ON and OFF) assigned to 1 instead of 0
and 1 (4 marks)
• If led = 1 is used for both ON and OFF states (instead of led = 1 for ON
and led = 0 for OFF):
• Impact:
• The LED will remain ON continuously because led = 1
keeps the LED pin high in both states.
• There will be no blinking effect, as the OFF state (led = 0)
is never triggered.
• The circuit will not exhibit the intended alternating
ON/OFF behavior.
• This defeats the purpose of the code, which is to create a
blinking LED.
void setup() {
pinMode(LEDpin, OUTPUT); // Set LED pin as output
}
void loop() {
digitalWrite(LEDpin, HIGH); // Turn LED ON
delay(DelayT); // Wait for DelayT milliseconds
digitalWrite(LEDpin, LOW); // Turn LED OFF
delay(DelayT); // Wait for DelayT milliseconds
}
Code Process Explanation:
• Variable Declaration:
• LEDpin = 23 assigns pin 23 to control the LED.
• DelayT = 1000 sets a delay of 1000ms (1 second) for ON and
OFF states.
• Setup Function:
• pinMode(LEDpin, OUTPUT) configures pin 23 as an output to
control the LED.
• Loop Function:
• digitalWrite(LEDpin, HIGH) sets pin 23 to HIGH, turning the LED
ON.
• delay(DelayT) pauses for 1000ms with the LED ON.
• digitalWrite(LEDpin, LOW) sets pin 23 to LOW, turning the LED
OFF.
• delay(DelayT) pauses for 1000ms with the LED OFF.
• The loop repeats, causing the LED to blink ON and OFF every 1
second.
Effect of Changing DelayT to 2000:
• When DelayT = 2000 (2000ms or 2 seconds):
• The LED stays ON for 2 seconds (instead of 1 second).
• The LED stays OFF for 2 seconds (instead of 1 second).
• The total blink cycle time doubles from 2 seconds (1s ON + 1s
OFF) to 4 seconds (2s ON + 2s OFF).
• The blinking frequency decreases, making the LED blink slower
and more noticeable.
b) Explain the two methods used to solve the problem of shared data in
a Real-Time Operating System, giving their functions (6 marks)
• Semaphores:
• Function: A synchronization mechanism to control access to
shared resources in an RTOS, preventing concurrent access by
multiple tasks.
• Explanation: Semaphores use a counter to track resource
availability. A task must acquire the semaphore (lock) to
access the shared data and release it when done. Types
include binary semaphores (for mutual exclusion) and counting
semaphores (for multiple resource instances). This prevents
race conditions and ensures data integrity.
• Example: Used in RTOS to protect shared memory access in a
multi-tasking environment.
• Message Passing:
• Function: Enables tasks to exchange data without direct
access to shared memory, avoiding conflicts by passing data
through queues or mailboxes.
• Explanation: Tasks communicate by sending and receiving
messages via RTOS-managed queues. This isolates tasks,
reducing the risk of data corruption and simplifying
synchronization. It’s particularly useful for distributed systems
or when tasks run on different cores.
• Example: Used in RTOS for inter-task communication in IoT
devices to share sensor data.
d) Use the provided Arduino code snippet for a traffic control system to
answer the following questions:
cpp
1. void setup() {
2. pinMode(PIN_RED, OUTPUT);
3. pinMode(PIN_YELLOW, OUTPUT);
4. pinMode(PIN_GREEN, OUTPUT);
5. }
6. void loop() {
7. // red light on
8. digitalWrite(PIN_RED, HIGH); // turn on
9. digitalWrite(PIN_YELLOW, LOW); // turn off
10. digitalWrite(PIN_GREEN, LOW); // turn off
11. delay(RED_TIME); // keep red light on during a period of time
12. digitalWrite(PIN_RED, LOW); // turn off
13. digitalWrite(PIN_YELLOW, HIGH); // turn on
14. digitalWrite(PIN_GREEN, LOW); // turn off
15. delay(YELLOW_TIME); // keep yellow light on during a period of time
16. }
i. Explain the functions of lines 8, 9, and 11 (4 marks)
• Line 8: digitalWrite(PIN_RED, HIGH);
• Sets the red light pin to HIGH, turning ON the red traffic light.
This signals vehicles to stop.
• Line 9: digitalWrite(PIN_YELLOW, LOW);
• Sets the yellow light pin to LOW, turning OFF the yellow traffic
light. This ensures the yellow light is not active during the red
light phase.
• Line 11: delay(RED_TIME);
• Pauses the program execution for a duration specified by
RED_TIME (in milliseconds), keeping the red light ON for the
defined period. This simulates the time vehicles must stop at
the traffic signal.
ii. Explain the function of the following functions (2 marks)
• a. delay() (1 mark)
• Pauses the program execution for a specified number of
milliseconds. In the context of the code, it controls how long a
traffic light (e.g., red or yellow) remains ON before transitioning
to the next state.
• Example: delay(RED_TIME) keeps the red light ON for
RED_TIME milliseconds.
• b. digitalWrite() (1 mark)
• Sets the state of a specified digital pin to either HIGH (ON) or
LOW (OFF). In the code, it is used to turn traffic lights ON or
OFF by controlling the voltage on the respective pins (e.g., red,
yellow, green).
• Example: digitalWrite(PIN_RED, HIGH) turns the red light ON.