Problem 2. Consider a light system that is controlled by an 8-bit register, whic
ID: 2267794 • Letter: P
Question
Problem 2. Consider a light system that is controlled by an 8-bit register, which carries a command to the light system. We call this register Light System Command or LSC. Here are the specifications of this register: . Bit 0-1 control the light intensity: 00 off, 01 low, 10 medium, 11 high. Bit 2-4 control the color: blue 000, green 001, red 010, yellow 011, white 100. Example: If we assign OxC3 to LSC, the light system will interpret it as "It should turn on lamp number # 6 with highest intensity in blue color."Explanation / Answer
Answer :- We can define the function as written below, but for datatype as COLOR and INTENSITY, we need to use ENUM as defined below-
typedef enum
{
BLUE,
GREEN,
RED,
YELLOW,
WHITE
} COLOR;
typedef enum
{
OFF_INTENSITY,
LOW_INTENSITY,
MEDIUM_INTENSITY,
HIGH_INTENSITY
} INTENSITY;
Now define the function-
int makeLSC(int lampNumber, COLOR newColor, INTENSITY newIntensity)
{
unsigned char temp = 0x00; //clear all the 8-bits
temp = (lampNumber << 5); //MSB 3-bits for lamp number
temp |= newIntensity; //LSB 2-bits for intensity
LSC = temp; //write the value to temp
temp = (newColor << 2); //write new color value to temp
LSC |= temp; //write bit 2, 3, and 4 to LSC
return LSC; //return final LSC value
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.