some errors in microC embedded system

128 Views Asked by At

we have a project we complete 60% of the program but we faced 2 problem Our first problem is to make engine should be cooling for 5 second then it should be heated for 10 second then it should stop like it not work again unless i on the switch again

this is our project :-

switch 0 controls the operation of the car (0 no-operation) (1 car is operating) we are done with this part

switch 1 is for seat belt: we are done with this part

switch 2 is for Door: we are done with this part

in this part we make it but the problem it should be done 1 time only !! but because we have a while loop forever it will not stop !! so what we should do ?!

while the car is operating, engine will take 15s to be heated: at the beginning --> 'HH' will be displayed on LCD, heater led on the application board will be on and at the same time motor will operate in the forward direction for 5 seconds to cool it. SO 'HN' will be displayed on LCD After that, engine will take 10s to be heated.

this is our second problem we cant do it !! we think about making a loop inside a loop inside another loop but it will not work also we try to do it by timer and inside it another timer !! we should do it by timer or interrupt we cant use delay

we will have 4 leds to represent fuel level. Every 10s one led will turn off . when the last led remains, warning will appear: (1) 'FL’ 'will be displayed on line 2 on LCD. if switch 3 becomes on fuel will be full otherwise car will switch off.

this is our 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;

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

// this is wrong

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

}
1

There are 1 best solutions below

3
On

Not an adequate answer, sorry, but too much to say for a comment.

You have not stated your two problems clearly. There is no code for the first problem, which appears to be something about heating the engine. About the second problem you just say "it does not work", without saying what.

I would also like to throw a spanner in your works and ask what will happen if the door is opened, or the seatbelt is released during your operational phases. It can sometimes be a good idea to maintain a single status variable that has a bit field for each value such a "door closed", "seatbelt fastened", "power on" etc.

I do notice that you have used delay() although you say you cannot. In the real world the only way a process controller could successfully use a delay() function is if there are other threads, or interrupt routines, looking after the I/O and scheduling events. One essential feature of any embedded controller is a timer tick which is serviced under interrupt, allowing you to delay without blocking other processes (the same handler can also poll and debounce keyboard and button inputs). Suppose your regular timer interrupt increments a variable called unsigned ticks. As an example (vaguely related to your tasks):

unsigned mark, elapsed;
int heating = 0;
while (1) {                      // main operational loop
    if (buttonpress) {           // pseudo code
        heating = 1;             // flag stage one of heater
        mark = ticks;            // start a delay
    }

    ...                          // service the fuel usage
    ...                          // check the door
    ...                          // check the seat belt

    if (heating) {
        elapsed = ticks - mark;  // don't directly compare...
        if (elapsed >= 1000) {   // ...because of counter wrap
            ...                  // heater jobs
            heating = 0;         // clear flag
        }
    }
}                                // repeat main loop

This can be expanded to give the heater process several stages.