我有嵌入式系统项目,车载监控系统我们在microC程序上使用C语言。
我决定编写部分,然后对其进行测试,我的想法是当我按下并释放(switch0)时,汽车将工作并且"电源"将出现在LCD屏幕上,然后我必须按下switch1的安全带。
如果 switch1=1,那么它将显示 "BF",否则它将浸入 "BO"。当我们进入 switch0 时,它会消失 POWER 和 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);
}
这是因为您的第二个条件(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);