PIC16F877A Code ERROR

Thread Starter

nalakan2

Joined Nov 20, 2013
4
Dear friends, I had made a program to control lab reaction hood function using PIC. In the code there is a timer part and keypad to control a blower,tube light,UV lamp,power socket and door. code is working perfectly until I add error handling statements. If I add one line to the code it reset the program when I simulate the program in Proteus. please help to get rid of this problem.

Here is the code in mikroC:
When this code is simulated in Proteus it restart to beginning if I added the last part of the code..


Rich (BB code):
// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

// Variable declaration
bit Unit_Button,Start_Button,Stop_Button,System_Start,fanSW,lightSW,socketSW,uvSW;

unsigned short i, j, unit=0, ten=0, time,System_Ready = 0, keypad = 0;
// End variable declaration

// Messages
char Message1[]="Timer by NALAKA";
char Message2[]="SYSTEM READY IN";
char Message3[]="SYSTEM READY";
char Message4[]="Set Time:    min";
char Message5[]="nalakan2@gmail.com";
char Message6[]="iGnat Troniks";
char Message7[]="Please set time";
char Message8[]="& press START";
char *digit = "00";
// End messages

// Display digits function
void Display_Digits()
{
 digit[1] = unit + 48;
 Lcd_Out(2,11,digit);
}

// Beep tone
void Beep()
{
 Sound_Play(500, 50);
 Delay_ms(100);
 Sound_Play(500, 50);
}

// Timer function
void start_timer(unsigned short MinVal)
{
 unsigned short temp1,temp2,temp3,temp4;
 //Transsision();
 Beep();
 RC0_bit = 1;
 Lcd_Cmd(_LCD_CLEAR);                        // Clear LCD
 Lcd_Out(1,1,Message2);                      // Show msg1 on LCD
 OPTION_REG = 0x80;                          // Enable PORTB Pull Up
 INTCON = 0x90;                              // Enable Globle Interrupt(GIE) & RB0/INT External Interrupt

 for (i=0; i<MinVal; i++)
 {
      temp1 = (MinVal-i)%10 ;
      temp2 = (MinVal-i)/10 ;
      Lcd_Chr(2, 7, temp2+48);
      Lcd_Chr(2, 8, (temp1-1)+48);
      j=60;

      do
         {
           if (j>=10)
              {
               temp3 = (j%10);
              }
           else
               {
                temp3 = j;
               }
           if (j>=10)
              {
                temp4 = (j/10);
              }
           else
              {
               temp4 = 0;
              }
           j--;
           Lcd_Chr(2,9,':');
           Lcd_Chr(2,10,temp4+48);
           Lcd_Chr(2,11,temp3+48);
           Delay_ms(1000);

         } while(j<=60 && Stop_Button ==0);

  if (i==MinVal)
     {
      goto stop;
     }

  if (Stop_Button == 1)
     {
      INTCON = 0x00;
      goto stop;
     }

 }

 stop:

      Beep();
      Lcd_Cmd(_LCD_CLEAR);
      Lcd_out(1,3,Message3);
      RC0_bit = 1;
      RC1_bit = 1;
      unit = 0;
      ten = 0;
      Stop_Button = 0;
      System_Ready = 1;


}

//Interrupt
void interrupt(void)
 {
      if (INTCON.INTF == 1)
         {
          Stop_Button = 1;
          INTCON.INTF = 0;
         }
 }

//Error beep
void Error()
{
 Sound_Play(500, 100);
 RC5_bit = 1;                              
 Delay_ms(500);                           
 RC5_bit = 0;
}

//Main function
void main()
{

  TRISB = 0x03;
  TRISC = 0x00;
  TRISD = 0xFF;
  PORTB = 0x00;
  PORTC = 0x00;
  PORTD = 0x00;
  Start_Button = 0;
  System_Start = 1;
  fanSW = 0;
  lightSW = 0;
  uvSW = 0;
  socketSW = 0;
  Lcd_Init();
  Sound_Init(&PORTC,6);

  Startup_Display();
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Cmd(_LCD_CURSOR_OFF);
  Lcd_Out(1,1,Message1);
  Lcd_Out(2,1,Message4);
  Display_Digits();

// Timer control buttons 
do
 {
     if (Button(&PORTB,1,1,1))
         Unit_Button = 1;
     if (Unit_Button && Button(&PORTB,1,1,0))
        {
          unit ++;
          if(unit==10)
          unit=0;
          Display_Digits();
          Unit_Button = 0;
         }

      if (Button(&PORTB,0,1,1))
         Start_Button = 1;
      if (Start_Button && Button(&PORTB,0,1,0))
         {
          time = unit;
          if(time > 0)
          {
           start_timer(time);
           Start_Button = 0;
          }
          goto aaa;
         }


 } while(System_Start == 1);


   aaa:
       System_Start = 0;


// Key pad
   if (System_Ready == 1)
   {
       do
       {

        if (RC3_bit == 0)                                // When UV off;
           {
              if (Button(&PORTD,0,1,1))             
                 fanSW = 1;                              
              if (fanSW && Button(&PORTD,0,1,0))   // Turn on fan
                 {                                 
                  RC0_bit = ~RC0_bit;              
                  fanSW = 0;                       
                 }

         //------------------------------------------------
              if (Button(&PORTD,1,1,1))            //
                 lightSW = 1;                      //
              if (lightSW && Button(&PORTD,1,1,0)) // Turn on lights
                 {                                 //
                  RC1_bit = ~RC1_bit;              //
                  lightSW = 0;                     //
                 }
            }

         //------------------------------------------------
           
              if (RC0_bit == 0 && RC1_bit == 0)                // When fan AND lights off;
              if (RD4_bit == 1)                                // if slash closed;
                 {
                  if (Button(&PORTD,3,1,1))            //
                     uvSW = 1;                         //
                  if (uvSW && Button(&PORTD,3,1,0))    // Turn on UV
                     {                                 //
                      RC3_bit = ~RC3_bit;              //
                      uvSW = 0;                        //
                     }
                  }

 //------------------------------------------------
              if (Button(&PORTD,2,1,1))                        //
                 socketSW = 1;                                 //
              if (socketSW && Button(&PORTD,2,1,0))            // Turn on internal power socket
                 {                                             //
                  RC2_bit = ~RC2_bit;                          //
                  socketSW = 0;                                //
                 }
 //------------------------------------------------
              if (RD4_bit == 1)                                // if slash is closed
                 {
                  RC4_bit = 1;                                 // Turn on slash indicator
                 }
              else                                             // if slash is open
                  {
                   RC4_bit = 0;                                // Turn off slash indicator
                  }

  //------------------------------------------------
              if (RC3_bit == 1)                                // When UV is on;
                 {
                  if (RD4_bit == 0)                            // if slash opened,
                  RC3_bit = 0;                                 // Turn off UV
                 }

 //------------------------------------------------

           if (RC3_bit == 1)                                 // When UV on AND slash closed;
           {

                  if (Button(&PORTD,0,1,1))                     // if fan switch pressed

                     {
                      Error();                              //
                     }


                if (Button(&PORTD,1,1,1))                     // if lights switch pressed
                     {
                      Error();                              //
                     }         
                     
            }

 //------------------------------------------------
           if (RC0_bit == 1 || RC1_bit == 1 )               // When fan OR lights on;
           {
                  if (Button(&PORTD,3,1,1))                     // if UV switch pressed
                     {
                      Error();                              //
                     }
           }


           if (RD4_bit == 0)                           // or when slash open;
           {
            if (uvSW == 1)
                                                      // if UV switch pressed
               {
                Error();                              //
               }
           }


       } while (System_Ready == 1);
   }
}
 
Last edited by a moderator:

Art

Joined Sep 10, 2007
806
and what is the last part of your code?
Everything after the first line, or everything before the last line?
 

MrSoftware

Joined Oct 29, 2013
2,200
Are you sure you're not running out of memory? I'm not familiar with the chip that you're using, but I used an Arduino for a recent project and when we ran out of memory the behaviors got really odd.
 
Top