我有嵌入式系统项目,汽车监控系统我们在microC程序上使用c语言。
我决定写零件然后测试它我的想法是当我按下并释放(switch0)汽车将工作和"电源"将出现在液晶屏上,然后我必须按开关1作为安全带。
如果switch1 = 1,那么它将显示" BF"否则它会淹没" BO"。当我们进入switch0时,它会消除POWER AND BO,因为我们没有按下switch1。然而,即使按下开关1,除非我同时按下开关0和开关1,否则它不会出现BF!
这是我的代码:
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);
}
答案 0 :(得分: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);