You are on page 1of 11

UNIVERSITY EXAMINATIONS: 2020/2021

EXAMINATION FOR BACHELORS DEGREE IN APPLIED


COMPUTING/SOFTWARE DEVELOPMENT
BAC 3203/ BSD 3205: EMBEDDED SYSTEMS
INSTRUCTIONS: Question ONE IS COMPULSORY, choose TWO OTHER questions

QUESTION ONE (20 MARKS) COMPULSORY


a) Consider the following embedded system with a dial that can set A3..A0 to binary 0 to 9,and a
7-segment display (Wikipedia: 7-Segment Display) connected to B6..B0 as shown: Below is a
(partial) RIM C program that appropriately sets the display for the given dial
position:

#include “RIMS.h”
void main()
{
while (1) {
switch( A )
{
case 0 : B = 0x77; break; // 0111 0111 (0)
case 1 : B = 0x24; break; // 0010 0100 (1)
case 2 : B = 0x5D; break; // 0101 1101 (2)
//…
case 9 : B = 0x6F; break; // 0110 1111 (9)
default: B = 0x5B; break; // 0101 1011 (E for Error)
}
}
}
i) What B_ outputs should be set to 1 for case 3? List in ascending order separated
by spaces, e.g., B0 B2 … [5 marks]

The binary value for 3 is 0011. To display a 3 on a 7-segment display, the following B
outputs should be set to 1:

● B0 (Least Significant Bit)


● B4

In ascending order, this is B0 B4.

Here's how we arrived at the answer B0 B4 for displaying a 3 on the 7-segment display:

1. Binary representation of 3: The dial setting represents the number in binary. In


this case, 3 in binary is 0011.
2. 7-Segment Display Segments: A 7-segment display uses seven individual LED
segments (labeled a-g) to form different digits. Each segment needs to be turned
on or off to display specific numbers.
Mapping Digits to Segments: There's a specific pattern for which segments need to
be lit to display each digit (0-9). Here's a common mapping (though it might vary slightly
depending on the specific display):
| Digit | Segments lit |

|---|---|

| 0 | a, b, c, d, e, f |

| 1 | b, c |

| 2 | a, b, d, e, g |

| 3 | a, b, c, d, g |
| 4 | b, c, f, g |

| 5 | a, f, g, c, d |

| 6 | a, c, d, e, f, g |

| 7 | a, b, c |

| 8 | a, b, c, d, e, f, g |

3. | 9 | a, b, c, f, g |
4. Matching Binary with Segments: We look at the binary representation of 3
(0011) and compare it to the segment mapping. In this case:
○ The least significant bit (LSB) - B0 - corresponds to segment g. Since the
LSB is 1, segment g needs to be lit.
○ The third bit (counting from left) - B4 - corresponds to segment a. Since
the third bit is 1, segment a needs to be lit.
5. Output Selection: Therefore, to display a 3, we need to set B0 (segment g) and
B4 (segment a) to 1 in the output port B.

By following these steps and referencing the segment mapping, you can determine
which B outputs need to be set for any digit between 0 and 9.

ii) To what should B be set for case 3? B = ____; Use uppercase letters for the hex
literal. [5 marks]

● B0 and B4 need to be set to 1 for displaying a 3.


● In the given system, each B output corresponds to a single bit (B0 - LSB,
B6 - MSB).

To set specific bits to 1 in a hex value, we can use the concept of OR operation.

Steps to solve:
1. Identify bits to set: B0 and B4.
2. Set corresponding bits in a hex value:
○ We can start with a zero (0x00) as all bits are initially off.
○ To set B0 to 1, we perform a bitwise OR with 0x01 (which has only
LSB as 1).
○ Similarly, to set B4 to 1, we perform another OR with 0x10 (which has
only the 4th bit from the right as 1).

Solution:

B = 0x00 (initialize to 0)

B = B | 0x01 (set B0 to 1)

B = B | 0x10 (set B4 to 1)

Final Answer:

B = 0x11 (This value has only B0 and B4 set to 1).

b) State and explain the results of the following bitwise operation. [10 marks]
i) 00001111

10101010

The & operator performs a bitwise AND operation. It compares each bit of the first
operand to the corresponding bit of the second operand. If both bits are 1, the
corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

00001111

& 10101010
----------

00001010

ii) 00001111 | 10101010

The | operator performs a bitwise OR operation. It compares each bit of the first operand to the
corresponding bit of the second operand. If either bit is 1, the corresponding result bit is set to 1.
Otherwise, the corresponding result bit is set to 0

iii) 00001111 ^ 10101010

The ^ operator performs a bitwise XOR operation. It compares each bit of the first
operand to the corresponding bit of the second operand. If the bits are not identical, the
corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0..

00001111

^ 10101010

----------

10100101
iv) ~00001111

The ~ operator is a bitwise NOT operation. It inverts the bits of its operand.

~ 00001111

----------

11110000
v) Give a statement that sets B’s bits to the opposite of A’s bits, so if A is
11110000, B will be 00001111. End with ;

B = ~A;

In this statement, ~A performs a bitwise NOT operation on A, which flips all the bits of A.
The result is then assigned to B. So if A is 11110000, B will be 00001111. This statement ends
with a semicolon

QUESTION TW0 (15 MARKS)


A parking lot has eight parking spaces, each with a sensor connected to input A. Write a program
that sets B to the number of occupied spaces, by counting the number of 1s using the GetBit()
function. [15 marks]

#include <stdio.h>

// Function to get the value of a specific bit in a byte

int GetBit(unsigned char byte, int bitPosition) {

return (byte >> bitPosition) & 1;

int main() {

unsigned char inputA = 0b00101110; // Example state of parking spaces


int occupiedSpaces = 0;

// Iterate through each parking space

for (int i = 0; i < 8; i++) {

// Check if the bit representing the parking space is 1 (occupied)

if (GetBit(inputA, i) == 1) {

occupiedSpaces++;

// Set B to the number of occupied spaces

unsigned char outputB = occupiedSpaces;

// Print the number of occupied spaces

printf("Number of occupied spaces: %d\n", outputB);

return 0;

QESTION THREE (15 MARKS)


Given the following timing diagram and the light on/off SM, determine the value of B0 at
the specified times. [15 marks]

i) 0 s [3 marks]- 0
ii) 1 s [3 marks] -1
iii) 2 s [3 marks] -0
iv) 3 s [3 marks] -1
v) 4 s [3 marks] -1
QUESTION FOUR (15 marks)
a) Explain and give an example of an embedded system [5 marks]

A common example of an embedded system is a microwave oven. The digital control panel
of the microwave (which lets you input cooking time, select power level, etc.) is an
embedded system. It is designed to control the microwave’s operations, including turning
the microwave generator on and off, controlling the timer, and managing the display. The
control panel can’t be used for other tasks; it’s specifically designed and embedded into the
microwave to control its operation.

b) Consider the following schematic diagram for a serial communication

Two Arduino boards are used. The Arduino Uno on the left is the sender and the Arduino
Mega on the right is the receiver. We use the Mega to make it easier to display debugging
information on the computer. The Arduinos are connected together using digits 0 and 1
(RX and TX) on the Uno and digitals 16 and 17 (RX2 and TX2) on Mega. The receiver
one needs to be connected to the transmitter on the other and vice versa. The Arduinos
also need to have a common reference between the two, this is done by running a ground
wire. The first step in creating a serial communication system is to package the string to
be communicated. In general a packet is comprised of some start byte, a payload (the data
you wish to send), and a checksum to validate your data. Here, the packet is: [0x53]
+[counter value]+[static value]+ [checksum]. The sender code is shown below that
increments our counter and sends our packet.
Write the Receiver code for the above send information. [10 marks]

#include <SoftwareSerial.h>
SoftwareSerial mySerial(16, 17); // RX, TX

void setup() {

// Open serial communications:

Serial.begin(9600);

// Set the data rate for the SoftwareSerial port

mySerial.begin(9600);

void loop() { // run over and over

if (mySerial.available()) {

byte startByte = mySerial.read();

if (startByte == 0x53) {

byte counterValue = mySerial.read();

byte staticValue = mySerial.read();

byte checksum = mySerial.read();

if (checksum == (counterValue + staticValue)) {

Serial.print("Received valid packet with counter value: ");

Serial.println(counterValue, HEX);

} else {

Serial.println("Checksum error in packet");


}

This code uses the SoftwareSerial library to create a new serial port. In the setup() function, it
begins communication on both the built-in serial port (for debugging purposes) and the software
serial port. In the loop() function, it checks if there is any available data on the software serial
port. If data is available and the start byte matches 0x53, it reads the next three bytes as the
counter value, static value, and checksum. It then checks if the checksum matches the sum of the
counter value and static value. If the checksum is correct, it prints the counter value; otherwise, it
prints an error message.

You might also like