我有這個C程序,我正在編寫代碼作曲家工作室。爲什麼我的C程序跳過這個if語句?
#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
}
}
}
當如果statment在調試模式下獲取到這個
if (R5_SW & BIT1) {// rotate left
R6_LED << 1;
} else {
R6_LED >> 1;
} // rotate right
它會跳過它,它不運行,如果還是else塊。此時在代碼R5_SW
中是22
這是0010 0010
二進制,所以R5_SW & BIT1
應評估爲true。我在這裏錯過了什麼?
你肯定正在運行if或else之一,它只是你沒有存儲你的計算。我認爲你的意思是'R6_LED << = 1;'和'R6_LED >> = 1;'。 – quamrana
十進制中的'22'是'00010110'。 '0x22'是'00100010' –