Car monitor system embedded project

170 Views Asked by At

I have embedded system project, car monitor system we work on c language on microC program.

I decide to write part part and then test it my idea is when I press and release (switch0) the car will work and "power" will appear on LCD screen, then I have to press switch1 for seat belt.

If switch1=1 then the it will display "BF" else it will dipslay "BO". When we enter the switch0, it will disapear POWER AND BO because we didnt press switch1. However even when I press switch1 it will not appear BF unless I press the switch0 and switch1 together !!

This is my code:

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);

}
1

There are 1 best solutions below

2
On

Thats because your second condition (switch1 check) lies inside the first condition (switch0), so belt status could not be checked while power button is not pressed. You should create variable to store car power status and modify it in the first condition. Second condition should be independent from the first and use your variable to check car power state. For example:

 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);