Question

(In c code only, not c++) We will simulate the status of 8 LEDs that are...

(In c code only, not c++)

We will simulate the status of 8 LEDs that are connected to a microcontroller. Assume that the state of each LED (ON or OFF) is determined by each of the bits (1 or 0) in an 8-bit register (high-speed memory).

  1. Declare a char variable called led_reg and initialize it to 0.
    Assume that the least-significant bit (lsb) controls LED#0 and the most-significant bit (msb) controls LED#7.
  2. In the main function, build and present a menu to the user and enclose it in a loop that ends when the Quit option is chosen. Use if-else or switch-case statements to implement the operations described:

    Menu:
    0. Turn ON LED#4
    1. Turn OFF LED#4
    2. Add 1 to the LED Register
    3. Turn ON all the even numbered LEDs
    4. Turn OFF all the even numbered LEDs
    5. Toggle the state of LED#3.
    6. Shift the values of the LED Register right by 1
    7. Quit
  3. Create a function called: printBinary() with the following properties:

    • one input argument of type char
    • returns nothing
    • prints the binary representation (in 8-bits) of the char input value.
  4. Use the printBinary() function to display the value of the LED register before the menu.

HINTS:

  • Each operation can be performed with a single expression (line of code).
  • Append 0b or 0x to numerical constants to denote binary or hexadecimal values in source code, respectively.
    101010102 = 0b10101010
    FF16 = 0xFF

Homework Answers

Answer #1

Here is the code:

#include <stdio.h>

void printBinary (unsigned char ch) {
    int i, digit[8];
    
    printf ("0b");
    for (i = 0; i < 8; i++) {
        digit[i] = (ch % 2 == 0 ? 0 : 1);
        ch = ch >> 1;
    }
    for (i = 7; i >= 0; i--)
            printf ("%d", digit[i]);
    printf ("\n");
    
}

int main()
{
    unsigned char led_reg = 0;
    int menu_choice = -1;
    
    printBinary (led_reg);
    while (menu_choice != 7) {
        printf ("Menu:\n");
        printf ("0. Turn ON LED#4\n");
        printf ("1. Turn OFF LED#4\n");
        printf ("2. Add 1 to the LED Register\n");
        printf ("3. Turn ON all the even numbered LEDs\n");
        printf ("4. Turn OFF all the even numbered LEDs\n");
        printf ("5. Toggle the state of LED#3.\n");
        printf ("6. Shift the values of the LED Register right by 1\n");
        printf ("7. Quit\n");
        scanf ("%d[^\n]\n", &menu_choice);
        switch (menu_choice) {
            case 0:
                led_reg |= (1 << 4);
                printBinary (led_reg);
                break;
            case 1:
                led_reg &= (0 << 4);
                printBinary (led_reg);
                break;
            case 2:
                led_reg += 1;
                break;
            case 3:
                led_reg = led_reg | 0x55; // alternate bits set
                printBinary (led_reg);
                break;
            case 4:
                led_reg = led_reg & 0xAA;
                printBinary (led_reg);
                break;
            case 5:
                led_reg = (led_reg & 0x8) ? (led_reg & ~(0x8)) : (led_reg | 0x8);
                printBinary (led_reg);
                break;
            case 6:
                led_reg = (led_reg >> 1);
                printBinary (led_reg);
                break;
                
        }
    }
    return 0;
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT