0

我有嵌入式系统项目,汽车监控系统,我们在 microC 程序上使用 c 语言。

我决定写一部分然后测试它我的想法是当我按下并释放(开关0)时,汽车会工作并且“电源”会出现在液晶屏上,然后我必须按下开关1来系好安全带。

如果 switch1=1 那么它将显示“BF”,否则它将显示“BO”。当我们进入switch0时,它会消失POWER AND BO,因为我们没有按下switch1。但是,即使我按下 switch1 也不会出现 BF,除非我同时按下 switch0 和 switch1 !

这是我的代码:

sbit LCD_RS at RA1_bit;
sbit LCD_RW at RA2_bit;
sbit LCD_EN at RA3_bit;


sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;

sbit LCD_RS_Direction at TRISA1_bit;
sbit LCD_RW_Direction at TRISA2_bit;
sbit LCD_EN_Direction at TRISA3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;

sbit LED0 at RC0_bit;
sbit LED1 at RC1_bit;
sbit Switch0 at RB0_bit;
sbit Switch1 at RB1_bit;
sbit Switch2 at RB2_bit;
sbit Switch3 at RB3_bit;
int Num;


void main() {

ADCON1 = 0X07;               //a port as ordinary i/o.
TRISA = 0X00;                //a port as output.
TRISD = 0X00;                //d port as output.
TRISC = 0X00;
TRISB = 0X0F;
PORTC = 0b00000001;


Lcd_Init();                        // Initialize LCD
Delay_ms(200);
Lcd_Cmd(_LCD_CLEAR);                // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);

LED0 = 0;
LED1= 0;

do {      
  if (Switch0) // if switch (RB1) is pressed
  {
      Delay_ms(20); // pause 20 mS
      while(Switch0); // Wait for release of the button
      Delay_ms(10);

      Lcd_Out(1, 7, "power");

      if (Switch1)
      {
          Delay_ms(10); // pause 20 mS
          while (Switch1); // Wait for release of the butto
          Delay_ms(10);
          Lcd_Out(2, 6, "BF");
          LED0 = 0;
      }
      else
      {
          Delay_ms(20);
          Lcd_Cmd(_LCD_CLEAR);
          Lcd_Out(2,1,"BO");
          LED0 = ~LED0;    
      }   
  }
} while(1);

}
4

1 回答 1

0

那是因为您的第二个条件(switch1 检查)位于第一个条件(switch0)内,因此在未按下电源按钮时无法检查皮带状态。您应该创建变量来存储汽车电源状态并在第一个条件下对其进行修改。第二个条件应该独立于第一个条件,并使用您的变量来检查汽车的电源状态。例如:

 bool bPowerState;
//... 
   do {
      if (Switch0) // if switch (RB1) is pressed
      {
          bPowerState = !bPowerState;
          Delay_ms(20); // pause 20 mS
          while(Switch0); // Wait for release of the button
          Delay_ms(10);

       if(bPowerState)
         Lcd_Out(1,7,"power");
      }

      if (Switch1 && bPowerState)
      {
         Delay_ms(10); // pause 20 mS
         while (Switch1); // Wait for release of the butto
         Delay_ms(10);
         Lcd_Out(2,6,"BF");
         LED0=0;
       }else{
         Delay_ms(20);
         Lcd_Cmd(_LCD_CLEAR);
         Lcd_Out(2,1,"BO");
         LED0=~LED0;
       }
    }while(1);
于 2014-11-28T16:12:57.627 回答