我想执行一个while循环,直到达到一个条件,条件是由一个用户按钮触发的中断给出的,但是当我按下按钮时while循环没有结束,奇怪的是如果我在里面放一个延迟循环然后它工作
//does not works:
while( 1 )
{
PRINTF("hello\n\r");
while (button_state==0)
{
//do something
if(button_state==1)
break;
}
button_state=0;
}
//works:
while( 1 )
{
PRINTF("hello\n\r");
while (button_state==0)
{
HAL_Delay(500);//i don't know why needs this to work
//do something
}
button_state=0;
}
//does not works:
while( 1 )
{
PRINTF("hello\n\r");
while (button_state==0)
{
//do something
}
button_state=0;
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
button_state = 1;
}
程序以“hello”开始,然后进入while循环,我按下按钮,此时中断将button_state设置为1,我希望while循环结束,到达我重置条件“button_state = 0;" 并再次看到“你好”,但没有发生这种情况。如果我在循环内插入延迟,则所有预期都已实现