embedded C:跳过函数调用

时间:2015-10-13 00:55:51

标签: c function embedded

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

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; 
}
}

2 个答案:

答案 0 :(得分:1)

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

switch_press == 0;

应该是

switch_press = 0;

答案 1 :(得分: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;
}