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...
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...
Assignment (C language, not C ++) Start a new project to store grade information for 5...
Assignment (C language, not C ++) Start a new project to store grade information for 5 students using structures. Declare a structure called student_t that contains : First name Last name Student ID Number Percentage grade Letter grade Create the following functions: getStudentInfo(void)- Declares a single student object - Uses printf()/scanf() to get keyboard input for Name, SID, and Percentage (not Letter). - Returns a single student structure calcStudentGrade(student_t *st_ptr)- Takes the pointer to a student (to avoid making a...
C Programming Language. A chessboard consists of 8 squares x 8 squares for a total of...
C Programming Language. A chessboard consists of 8 squares x 8 squares for a total of 64 squares. The squares of the chessboard are identified, from the perspective of the player with the white pieces, by the letters a – h for the 8 columns or files (starting from that player’s left), and 1 – 8 for the 8 rows or ranks (starting closest to that player). One of the chess pieces, the knight, can move in any direction by...
In C/C++ programming language, write down the lines of codes (and figure) to show- a) assignment-by-sharing...
In C/C++ programming language, write down the lines of codes (and figure) to show- a) assignment-by-sharing b) dangling reference
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT