sách gpt4 ai đã đi

c - vòng lặp while thoát sớm

In lại 作者:行者123 更新时间:2023-12-02 06:29:52 29 4
mua khóa gpt4 Nike

我已经编写了读取开关状态的代码,如果按 3 次 # 则退出。

void allkeypadTest(void)
{
static uint8_t modeKeyCount=0;

LÀM
{
uint8_t key=getKeyStatus();
if(key)
{
if(key=='#')
{
modeKeyCount++;
//pulseIODevice(LED1,1,500,200);
}
khác
{
pulseIODevice(LED1,key-0x30,500,200);
}
}
}while(modeKeyCount<3);
}

但是只要我输入一次#key,循环就会退出。如果我按其他键,则行为正常。但是,如果我取消注释 if(key=='#') 部分下的 pulseIODevice,行为是正常的。 pulseIODevice 将在特定时间点切换 LED 特定时间并将 PWM 传递给它。我对我的代码出了什么问题感到困惑。请注意,如果未检测到任何键,getKeyStatus 将返回 '\0'(空),并返回 1x4 键盘键的 ASCII 值(ASCII 值为 3、6、9并且只有#)

1 Câu trả lời

在检测到按键状态后,您可能希望等到它变回正常(没有按键)后再继续

这样做改变

uint8_t key=getKeyStatus();

成为

uint8_t key = getKeyStatus();
while (0 != getKeyStatus())
{
/* Do nothing.
Shouldn't loop too long if not abused by holding the key pressed. */
/* if available add some milli sec delay here. */
}

一点点效率更高,甚至更准确

uint8_t key = getKeyStatus();
if ( 0 != key)
{
LÀM
{
/* Do nothing.
Shouldn't loop too long if not abused by holding the key pressed. */
/* If available add some milli sec delay here. */
} while (0 != getKeyStatus());
}

背景:

根据定义,要根据状态的变化(此处为“按下”、“向上”)检测事件(此处为“按键”),您需要测试不止一次。

关于c - 做 while 循环过早退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41648214/

29 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com