Professional Documents
Culture Documents
#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:
Here's how we arrived at the answer B0 B4 for displaying a 3 on the 7-segment display:
|---|---|
| 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]
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) 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
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
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
#include <stdio.h>
int main() {
if (GetBit(inputA, i) == 1) {
occupiedSpaces++;
return 0;
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.
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() {
Serial.begin(9600);
mySerial.begin(9600);
if (mySerial.available()) {
if (startByte == 0x53) {
Serial.println(counterValue, HEX);
} else {
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.