嵌入式 C:跳过函数调用

embedded C: Skips a function call

我正在对 RX63N 开发板进行编程,并尝试通过修改现有教程来执行以下操作: 最初板载 LED 的 pattern1 应该发光直到按下开关,如果按下任何开关 pattern2 应该启动然后按下任何开关所有 LED 应该打开 off.I 已经初始化了顶部的所有常量和函数原型但为了简洁起见没有包括在这里。 BLOCK_UNTIL_SWITCH_PRESS 应该启用无限循环,直到按下任何开关,如果在第一次出现后,模式 2 应该启动,然后在第二次出现后,所有 LED 都应该关闭。但是在这里,代码直接从模式 1 转到停止跳过模式 2.Can 有人帮忙吗? 这是代码:

void main(void){
cmt_init(); /* Initialize the CMT unit for application timing tasks. */ 

R_SWITCHES_Init(); /* Prepare the board switches for use. */

/* Set up the callback function on cmt channel 0 */   
cmt_callback_set(CHANNEL_0, &BLINK_RED_LEDS);

/* Start 400mS count on cmt channel 0. */
cmt_start(CHANNEL_0, TIMER_COUNT_400MS);

BLOCK_UNTIL_SWITCH_PRESS();
state = 0;

/* Stop counting on cmt channel 0. */
cmt_stop(CHANNEL_0);

/* Set up the callback function on cmt channel 0 */   
cmt_callback_set(CHANNEL_0, &PATTERN);

/* Start 200mS count on cmt channel 0. */
cmt_start(CHANNEL_0, TIMER_COUNT_200MS);
switch_press == 0;
BLOCK_UNTIL_SWITCH_PRESS();

gstate = 0;
/* Stop counting on cmt channel 0. */
cmt_stop(CHANNEL_0);


while (1)
{
    /* All done. Loop here forever. LEDs will continue to flash as 
       at a variable rate as the timer ISR executes. */
}
} /* End of function main(). */

/*Reset all LEDs */
void RESET_ALL_LEDS(void){
PORTD.PODR.BYTE = 0xFF;       /* Turn off all LEDs on port D.*/
PORTE.PODR.BYTE |= 0x0F;      /* Turn off all LEDs on port E.*/
state = 0;                   /*state of RED LEDs*/
gstate = 0;                  /*state of greeen LEDs*/
}

/*All RED LEDS ON */
void ALL_RED_LEDS_ON(void){
PORTD.PODR.BYTE = ~0xC7;  /*Turns on all the RED LEDS on port D*/
PORTE.PODR.BYTE = ~0x01;    /*Turns on the RED LED on port E */
state = 1;                  /*update state*/
}

/*BLOCK UNTIL SWITCH PRESS:
g_swX_press is  predefined bool to detect a switch press and initiated false*/
void BLOCK_UNTIL_SWITCH_PRESS(void){
while(switch_press == 0){
    if(g_sw1_press == true){
        switch_press = 1 ;
        }
    else if(g_sw2_press == true){
        switch_press = 1 ;
    }
    else if(g_sw3_press == true){
        switch_press = 1 ;
    }   
}
g_sw1_press == false;
g_sw2_press == false;
g_sw3_press == false;
}

/*BLINK RED LEDS */
void BLINK_RED_LEDS(void){
if (state == 1){
    ALL_RED_LEDS_ON();
    state = 0;
}
else if(state == 0){
    RESET_ALL_LEDS();
    state = 1;  
}   
}

void ALL_GREEN_LEDS_ON(void){
PORTD.PODR.BYTE = ~0x38;     /*Turns on all the Green LEDS on port D*/
PORTE.PODR.BYTE = ~0xFE;    /*Turns on all the Green LEDS on port E*/
gstate = 1;
}

/*CUSTOM PATTERN */
void PATTERN (void){
if (gstate == 1){
    ALL_GREEN_LEDS_ON();
    gstate = 0;
}
else if(gstate == 0){
    RESET_ALL_LEDS();
    gstate = 1; 
}
}

函数调用前的赋值BLOCK_UNTIL_SWITCH_PRESS();错误

switch_press == 0;

应该是

switch_press = 0;

正如@jayant 已经回答的那样,通过编写 switch_press == 0,您不会将零分配给 switch_press,而是评估布尔表达式(就像编写 if 函数时一样)。你在几个地方做了这个。

为了补充他的答案,您可能还想将阻止功能简化为:

// it looks like you don't need "switch_press" at all
void BLOCK_UNTIL_SWITCH_PRESS(void) {

    // loop until one of the switches is pressed
    while (!g_sw1_press && !g_sw2_press && !g_sw3_press)
    { 
       // do nothing (or you can put the cpu to sleep for a while)
    }

    // do you even need to reset these?
    g_sw1_press = false;
    g_sw2_press = false;
    g_sw3_press = false;
}