1

I have this C program that I am writing in code composer studio.

#include <msp430.h> 

/*
 * main.c
 */
int main(void)
{
    WDTCTL = WDTPW | WDTHOLD;    // Stop watchdog timer

    int R5_SW=0, R6_LED=0, temp=0;

    P1OUT = 0b00000000;     // mov.b    #00000000b,&P1OUT
    P1DIR = 0b11111111;     // mov.b    #11111111b,&P1DIR
    P2DIR = 0b00000000;     // mov.b    #00000000b,&P2DIR

    while (1)
    {
        // read all switches and save them in R5_SW
    R5_SW = P2IN;

    // check for read mode
        if (R5_SW & BIT0)
          {
            R6_LED = R5_SW & (BIT3 | BIT4 | BIT5); // copy the pattern from the switches and mask
            P1OUT = R6_LED;            // send the pattern out
          }

        // display rotation mode
        else
            {
            R6_LED = R5_SW & (BIT3|BIT4|BIT5);
            // check for direction
            if (R5_SW & BIT1) {// rotate left
                R6_LED << 1;
            } else {
                R6_LED >> 1;
            }   // rotate right

            // mask any excessive bits of the pattern and send it out
            R6_LED &= 0xFF;             // help clear all bits beyound the byte so when you rotate you do not see garbage coming in
                P1OUT = R6_LED;

                // check for speed
            if (R5_SW & BIT2)   {__delay_cycles( 40000); }  //fast
                else            {__delay_cycles(100000); }  //slow
         }
    }
}

When it gets to this if statment in debug mode

if (R5_SW & BIT1) {// rotate left
    R6_LED << 1;
} else {
    R6_LED >> 1;
}   // rotate right

It skips over it, it doesn't run the if or the else block. At this point in the code R5_SW is 22 which is 0010 0010 in binary so R5_SW & BIT1 should evaluate to true. What am I missing here?

Matt
  • 2,232
  • 8
  • 35
  • 64
  • 1
    You are definitely running one of the if or else, its just that you are not storing your calculations. I think you mean `R6_LED <<= 1;` and `R6_LED >>=1;`. – quamrana Nov 17 '16 at 17:30
  • `22` in decimal is `00010110`. `0x22` is `00100010` – Eugene Sh. Nov 17 '16 at 17:32

2 Answers2

5

If you use an operation like << or >> without assigning it then the result is just discarded. Try this:

if (R5_SW & BIT1) 
{
    R6_LED = R6_LED << 1;
}
else
{
    R6_LED = R6_LED >> 1;
}

Or, for brevity:

if (R5_SW & BIT1) 
{
    R6_LED <<= 1;
}
else
{
    R6_LED >>= 1;
}   
Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
3

This piece of code...

if (R5_SW & BIT1) {// rotate left
    R6_LED << 1;
} else {
    R6_LED >> 1;
}   // rotate right

evaluates either the expression R6_LED >> 1 or the expression R6_LED << 1 but it doesn't do anything with the result of it, so the compiler chooses to throw it away the entire IF statement.

A line such as a>>b by itself doesn't modify a. It is not like for example a++ which does modify a.

mcleod_ideafix
  • 11,128
  • 2
  • 24
  • 32