我们有一个项目,我们完成了 60% 的项目,但我们面临 2 个问题我们的第一个问题是让发动机冷却 5 秒,然后加热 10 秒,然后它应该停止,除非我在再次切换
这是我们的项目:-
开关 0 控制汽车的运行(0 不运行)(1 辆汽车正在运行)我们完成了这部分
开关 1 用于安全带:我们完成了这部分
开关 2 用于门:我们完成了这部分
在这部分我们做到了,但问题应该只做 1 次!!但是因为我们永远有一个while循环,它不会停止!那我们该怎么办?!
在汽车运行时,发动机需要 15 秒加热:开始时 --> LCD 上将显示“HH”,应用板上的加热器 LED 将亮起,同时电机正向运行5 秒钟使其冷却。SO 'HN' 将显示在 LCD 上之后,发动机需要 10 秒才能加热。
这是我们做不到的第二个问题!!我们考虑在另一个循环内的一个循环内创建一个循环,但它不会工作,我们也尝试通过计时器来完成它,并在其中另一个计时器!我们应该通过定时器或中断来完成,我们不能使用延迟
我们将有 4 个 LED 来代表燃料水平。每 10 秒一个 LED 将关闭。当最后一个 LED 剩余时,会出现警告: (1) LCD 的第 2 行将显示“FL”。如果开关 3 接通,燃油将充满,否则汽车将熄火。
这是我们的代码!!
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;
int i;
sbit LED0 at RC0_bit;
sbit LED1 at RC1_bit;
sbit LED2 at RC2_bit;
sbit LED4 at RC4_bit;
sbit LED5 at RC5_bit;
sbit LED6 at RC6_bit;
sbit LED7 at RC7_bit;
sbit Switch0 at RB0_bit;
sbit Switch1 at RB1_bit;
sbit Switch2 at RB2_bit;
sbit Switch3 at RB3_bit;
int Num;
void Move_Delay() { // Function used for text moving
Delay_ms(1000); // You can change the moving speed here
}
void main() {
ADCON1 = 0X06; //a port as ordinary i/o.
TRISA=0X00; //a port as output.
TRISD=0X00; //d port as output.
TRISC=0X00;
TRISB=0X0F;
PORTC = 0b00000000;
OPTION_REG = 0xD2;
Num = 0; //clear the number of overflows
OPTION_REG = 0x82; //Timer, Internal cycle clock (Fosc/4)
//Prescaler is assigned to the TMR0 timer/counter
//Prescaler (1:128) is assigned to the timer TMR0
TMR0 = 56; //Timer T0 counts from 39 to 255
INTCON.T0IF=0;
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)
{
Delay_ms(200); // pause 20 mS
if(INTCON.T0IF) //check for TMR0 register overflow
{
Num ++; // overflow causes Num to be incremented by 1
TMR0 = 56; // TMR0 returns to its initial value
INTCON.T0IF = 0 ; // Bit T0IF is cleared
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,2,"cooling");
}
if(Num ==108)
{
Lcd_Cmd(_LCD_CLEAR);
LED0=~LED0;
Lcd_Out(1,2,"heater ");
Delay_ms(1000);
}
}
else
Lcd_Cmd(_LCD_CLEAR);
if (switch1)
{
Delay_ms(20); // pause 20 mS
Lcd_Out(2,1,"BO");
LED1=0;
}
else
{
if(INTCON.T0IF) //check for TMR0 register overflow
{
Num ++; // overflow causes Num to be incremented by 1
TMR0 = 39; // TMR0 returns to its initial value
INTCON.T0IF = 0 ; // Bit T0IF is cleared
/*Lcd_Cmd(_LCD_CLEAR);*/
Lcd_Out(2,1,"BF ");
LED1=~LED1;
}
if(Num == 108)
{ //after 108 overflows
Num = 0;
}
}
if (switch2)
{
Lcd_Out(2,5,"DO");
LED2=0;
}
else
{
if(INTCON.T0IF) //check for TMR0 register overflow
{
Num ++; // overflow causes Num to be incremented by 1
TMR0 = 39; // TMR0 returns to its initial value
INTCON.T0IF = 0 ; // Bit T0IF is cleared
/*Lcd_Cmd(_LCD_CLEAR);*/
Lcd_Out(2,5,"DF");
LED2=~LED2;
}
if(Num == 108)
{ //after 108 overflows
Num = 0;
}
}
// 这是错误的
if(switch3)
{
Delay_ms(500); // Clear display
//Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1," FFFFFFFFFF"); // Write text in first row
Delay_ms(500);
for(i=0; i<15; i++) { // Move text to the right 7 times
Lcd_Cmd(_LCD_SHIFT_RIGHT);
Move_Delay();
if(i==14)
{
Lcd_Cmd(_LCD_CLEAR); // Cursor off
Lcd_Out(1,1," warning !! ");
Delay_ms(1000);
}
}
}
} while(1);
}