c - 为什么我的 C 程序会跳过这个 if 语句?

标签 c microcontroller code-composer

我有这个 C 程序,是我在 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
         }
    }
}

当它在 Debug模式下达到这个 if 语句时

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

它跳过它,它不运行 if 或 else block 。此时代码中的 R5_SW22,它是二进制的 0010 0010 所以 R5_SW & BIT1 应该评估为真的。我在这里缺少什么?

最佳答案

如果您使用类似 << 的操作或 >>如果不分配它,那么结果将被丢弃。试试这个:

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

或者,为简洁起见:

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

关于c - 为什么我的 C 程序会跳过这个 if 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40661370/

相关文章:

c - 这个 C 函数是不是写得很糟糕?

assembly - 使用 TI 代码编写器工作室(用于 ARM)在 C 代码中进行内联汇编

code-generation - 如何编译使用不同版本的代码生成工具创建的 Code Composer 项目?

c++ - 代码 Composer 工作室 : fatal error: could not open source file "types.h"

c - 为什么 GCC 不能为 int 除法生成正确的汇编代码?

c - 函数中的参数

c - STM32外部中断只在 Debug模式下响应

c - 为什么 gmtime() 函数返回 NULL?

将值分配给结构中的地址时出现 C 段错误

c++ - c/c++ 中什么是类型安全的