Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEASE ADD COMMENTS TO EVERY LINE OF THE CODE AND FIX IT AND READ WHATS IN BOLD!

ID: 3786121 • Letter: P

Question

PLEASE ADD COMMENTS TO EVERY LINE OF THE CODE AND FIX IT AND READ WHATS IN BOLD!!!

#include <msp430g2553.h>

/*
* Fix this code! You may NOT delete or add code, you may only change values being stored in a variable or register.
*/

void DELAY();
unsigned int CLS(unsigned int x);
unsigned int CRS(unsigned int x);

int main(void) {
WDTCTL = WDTPW | WDTHOLD;

volatile unsigned int m = 1;
volatile unsigned int s = 0;
volatile unsigned int p = 65535;

P1DIR |= m;

while(s)
{
   p = CRS(p);
   P1OUT = p;
   DELAY();
}
}

void DELAY(){
   volatile unsigned int i = 25;
   volatile unsigned int j = 0;
   volatile unsigned int k = 1;
   volatile unsigned int l = 0;
  
   for(; i > 0; i--)
   {
       j += 2;
       k += 2;
   }

   l = CLS(j + k);
}

unsigned int CLS(unsigned int x){
   return (x << 1) | (x >> 15);
}

unsigned int CRS(unsigned int x){
   return (x >> 1) | (x << 15);
}

Explanation / Answer

#include <msp430g2553.h> //including the directory here means the directory itself contains some logic ,so we are just using it

void DELAY(); //declaring a function name as DELAY with return type as void ,means this function don't return a value
unsigned int CLS(unsigned int x);//declaring CLS function by passing an unsigned int as an argument to it and unsigned int as return type that returns an unsigned int
unsigned int CRS(unsigned int x); //declaring CRS function by passing an unsigned int as an argument to it and unsigned int as return type that returns an unsigned int

int main(void) { // main function with int as return type
WDTCTL = WDTPW | WDTHOLD; //WDTCTL a variable that holds a value with logical condition

volatile unsigned int m = 1; // here declaring and initialising three variables m,s and p
volatile unsigned int s = 0;
volatile unsigned int p = 65535;

P1DIR |= m; //here assigning that initialized "m" value to P1DIR variable

while(s) //while loop with passing initialized variable "s"
{
   p = CRS(p); //here passing "p" value to CRS function and storing that returned value from that funtion to a variable"p"
   P1OUT = p; // above returned value to P1OUT variable
   DELAY();// caling delay() function
}
}

void DELAY(){ // implementation of delay() function
   volatile unsigned int i = 25; // declaring and initializing four variables i,j,k,l
   volatile unsigned int j = 0;
   volatile unsigned int k = 1;
   volatile unsigned int l = 0;
  
   for(; i > 0; i--) // for loop with iteration i=25 and i>0 which is true so j and k values increment by 2

so in first iteration the values of j=2,k=3 and then i value decrements by 1 so i =24

this process continues till the condition becomes false
   {
       j += 2;
       k += 2;
   }

   l = CLS(j + k); // calling CLS function and storing that returned value from that function into "l"


}

unsigned int CLS(unsigned int x){ //implementation of CLS function
   return (x << 1) | (x >> 15); //to return a value based on the "x" value ,a check with comparision of value each time with x value
}

unsigned int CRS(unsigned int x){    //implementation of CLS function
   return (x >> 1) | (x << 15);   //to return a value based on the "x" value ,a check with comparision of value each time with x value
}