Question

In: Computer Science

Assignment (C language) We will simulate the status of 8 LEDs that are connected to a...

Assignment (C language)

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

Solutions

Expert Solution

Here is the code:

#include <bits/stdc++.h>

using namespace std;
void printBinary(char);

int main()
{
   char led_reg = '0';
   while(1){
       printBinary(led_reg);
       cout<<"\n0. Turn ON LED #4 .\n";
       cout<<"1. Turn OFF LED #4 .\n";
       cout<<"2. Add 1 to the LED Register .\n";
       cout<<"3. Turn ON all the even numbered LEDs .\n";
       cout<<"4. Turn OFF all the even numbered LEDs .\n";
       cout<<"5. Toggle the state of LED #3 .\n";
       cout<<"6. Shifte the values of the LED Register right by 1 .\n";
       cout<<"7. Quit.\n";
       int choice;
       cin>>choice;
       switch(choice)
       {
           case 0:
               led_reg = (led_reg | (1 << 2));            // All the bits that we count are from left side to right
               break;                                               //but the operations are performed in opposite dxn
           case 1:                                                   // so we do 7-i-1 for the ith bit from left
               led_reg = (led_reg & ~(1 << 2));        //hence we use 2 for 4 as 7-4-1=2
               break;
           case 2:
               led_reg++;
               break;
           case 3:
               for(int i = 0;i<8; i++){
                   if(i%2==0){
                       led_reg = (led_reg | (1 << i));
                       }
                   }
               break;
           case 4:
               for(int j = 0;j<8; j++){
                   if(j%2==0){
                       led_reg = (led_reg & ~(1 << j));
                       }
                   }
               break;
           case 5:
               led_reg = (led_reg ^ (1 << (5)));
               break;
           case 6:
               led_reg = led_reg>>1;
               break;
           case 7:
               exit(0);
               break;

           default:
               cout<<"Invalid Choice!!\n Try agin\n";
       }
   }
    return 0;
}

void printBinary(char c){
   /* step 1 */
    if (c > 1)
        printBinary(c/2);

    /* step 2 */
    cout << c % 2;
   }


Related Solutions

Programming Language: C++ Overview For this assignment, write a program that will simulate a single game...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice is equal to 7 or 11, the player wins immediately....
Write a C program to blink two LEDs connected to Raspberry pi. One must blink at...
Write a C program to blink two LEDs connected to Raspberry pi. One must blink at a rate of 1 Hz and the other at a rate of 2 HZ.
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
In this assignment, we will explore four subspaces that are connected to a linear transformation. For...
In this assignment, we will explore four subspaces that are connected to a linear transformation. For the questions below, assume A is an m×n matrix with rank r, so that T(~x) = A~x is a linear transformation from R n to R m. Consider the following definitions: • The transpose of A, denoted AT is the n × m matrix we get from A by switching the roles of rows and columns – that is, the rows of AT are...
C++ In this assignment you will use your Card class to simulate a deck of cards....
C++ In this assignment you will use your Card class to simulate a deck of cards. You will create a Deck class as follows: The constructor will create a full 52-card deck of cards. 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace for each suit: Clubs, Diamonds, Hearts, Spades Each card will be created on the heap and the deck will be stored using an STL vector of Card pointers. The destructor will free the...
Please code this in C In this project, we shall simulate the operations of an ATM...
Please code this in C In this project, we shall simulate the operations of an ATM machine. Suppose you’re in charge of this simulation and here is a scenario of what is required to do: The customer will be assigned a random number for his/her balance. First, the customer is prompted to enter his personal identification number pin (for this case study, we test only if this pin is formed by 4 digits! otherwise, a message like “Invalid PIN, try...
In Java, C, C++ or Python Develop a simulation program to simulate an 8-port Ethernet switch....
In Java, C, C++ or Python Develop a simulation program to simulate an 8-port Ethernet switch. The switch initially has no knowledge about the hosts connected to each port. It learns frame addresses and stores-and-forwards the frames. The input text file "in.txt" contains information of the incoming frames, one frame per line. There are 4 pieces of data per line: frame ID, arrival port, frame source address, and frame destination address. The frames arrive at the switch in the order...
In C language Assignment Extend the Game of Life assignment to load its configuration from a...
In C language Assignment Extend the Game of Life assignment to load its configuration from a file. Functional Requirements MUST load the initial state from text file MUST load the number of rounds to play from text file COULD load the grid size from the file Nonfunctional Requirements MUST compile without warnings and error Source code: life.h /* * life.h * *  Created on: Sep 20, 2020 *      Author: Joker Zhong */ #ifndef LIFE_H_ #define LIFE_H_ #define XSIZE   15 #define YSIZE   15 #define DEFAULTROUNDS...
Q1- Write code in C language(Atmel atmega32 avr microcontroller) (a)-Switches are connected to Port C and...
Q1- Write code in C language(Atmel atmega32 avr microcontroller) (a)-Switches are connected to Port C and Port B and LED’s with PORTD. Write a code to read input from PortB and C and perform following. Addition of both inputs, Subtraction, Multiplication, Division, And, Or, Nor,Xor. (b)- A switch is connected to PD0 and LED’s to Port B and Port C. Write a code to perform following: When switch is 0 send your roll number to Port B. When Switch is...
i'm working on this assignment where we must simulate a bank account. I am having a...
i'm working on this assignment where we must simulate a bank account. I am having a few issues. The first issue is that for menu option 1 i am struggling to make the program so that I can add the name and balance of the account all at once. For example I would type "mike 350" and it would update the dictionary with a new account and balance, right now i have to open the account then deposit a balance...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT