gpt4 book ai didi

车载监控系统嵌入式项目

转载 作者:行者123 更新时间:2023-11-30 17:24:49 25 4
gpt4 key购买 nike

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

我决定写部分部分,然后测试它,我的想法是,当我按下并释放(switch0)时,汽车就会工作,并且液晶屏幕上会出现“power”,然后我必须按下switch1来系安全带。

如果 switch1=1 则显示“BF”,否则显示“BO”。当我们进入switch0时,POWER和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);

}

最佳答案

那是因为您的第二个条件(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);

关于车载监控系统嵌入式项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27192196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com