LCD Programming Example using C Language

By Youssef Edward
March 17, 2019


The following program is an example of LCD based interface where the user enter data using 16*2 LCD. The Microcontroller used is PIC16F877A. The idea is to pic time, power and operate for the time picked

The parameters that the user input are as follow:

  1. Working time.
  2. ramping time.
  3. prepare time
  4. power

Once the user input those parameters using the LCD and press start, the microcontroller send start order to second microcontroller to start running. if the user press stop or the time is over, the microcontroller sends stop command. the start and stop conditions are displayed on LCD. The used IDE is Microc

sbit LCD_RS at RC5_bit;
sbit LCD_EN at RC4_bit;
sbit LCD_D4 at RC0_bit;
sbit LCD_D5 at RC1_bit;
sbit LCD_D6 at RC2_bit;
sbit LCD_D7 at RC3_bit;

sbit LCD_RS_Direction at TRISC5_bit;
sbit LCD_EN_Direction at TRISC4_bit;
sbit LCD_D4_Direction at TRISC0_bit;
sbit LCD_D5_Direction at TRISC1_bit;
sbit LCD_D6_Direction at TRISC2_bit;
sbit LCD_D7_Direction at TRISC3_bit;

  char index=6;
  char row=1;
    char a_l=6;
    char a_u=1;

            char hour_h=0;
  char hour_l=0;
  char minute_h=0;
  char minute_l=0;
  char second_h=0;
  char second_l=4;

  char v_t=0  ;
  char v_s=2;
  char v_f=5;

  char timemode=0;  //0 socking,  1 rising1  2 working

    char pwmmode=1;  //0 current mode   1 voltage mode

              char riseincrement=0;
       char currentreached=0;
      char amperevalue=16;
      char voltagevalue=25;

      char secondvalue_s=8;
      char minutevalue_s=0;
      char hourvalue_s=0;

         char secondvalue_r=0;
      char minutevalue_r=0;
      char hourvalue_r=0;

         char secondvalue_w=0;
      char minutevalue_w=0;
      char hourvalue_w=0;

        char correctionindex=0;
         char displayindex=0;

         char writevoltagedisable=0;

//write number on LCD at current position
     void writenumber(char n)
     {
      if(n==0)      Lcd_Chr_Cp('0');
      else  if(n==1)      Lcd_Chr_Cp('1');
       else  if(n==2)      Lcd_Chr_Cp('2');
        else  if(n==3)      Lcd_Chr_Cp('3');
         else  if(n==4)      Lcd_Chr_Cp('4');
          else  if(n==5)      Lcd_Chr_Cp('5');
           else  if(n==6)      Lcd_Chr_Cp('6');
            else  if(n==7)      Lcd_Chr_Cp('7');
             else  if(n==8)      Lcd_Chr_Cp('8');
              else  if(n==9)      Lcd_Chr_Cp('9');

                           Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);
     }
//write number on LCD at specifed position named j
            void writenumber_pos(char n,char j)
     {
      if(n==0)     Lcd_Chr(1, j, '0');
      else  if(n==1)       Lcd_Chr(1, j, '1');
       else  if(n==2)       Lcd_Chr(1, j, '2');
        else  if(n==3)      Lcd_Chr(1, j, '3');
         else  if(n==4)       Lcd_Chr(1, j, '4');
          else  if(n==5)     Lcd_Chr(1, j, '5');
           else  if(n==6)      Lcd_Chr(1, j, '6');
            else  if(n==7)      Lcd_Chr(1, j, '7');
             else  if(n==8)      Lcd_Chr(1, j, '8');
              else  if(n==9)      Lcd_Chr(1, j, '9');


     }
//display second value on LCD
             void displaysecond(char sec)
       {
        char sec_u,sec_l;
        sec_u=sec/10;
        sec_l=sec%10;
                writenumber_pos(sec_u,15);
                writenumber_pos(sec_l,16 );

       }
//display minute value on LCD
                 void displayminute(char sec)
       {
        char sec_u,sec_l;
        sec_u=sec/10;
        sec_l=sec%10;
                writenumber_pos(sec_u,12);
                writenumber_pos(sec_l,13);


       }
//display hour value on LCD
         void displayhour(char sec)
       {
        char sec_u,sec_l;
        sec_u=sec/10;
        sec_l=sec%10;
                writenumber_pos(sec_u,9);
                writenumber_pos(sec_l,10);
       }

//write threedigit current value on LCD in amperes
         void writeamperevalue(char value)
     {
     char t=0;
     char s=0;
     char f=0;  char j=0;
     t=value/100;
     s=value/10;
     f=value%10;

           if(value==100)
          {
           t=1; s=0; f=0;
           }

              j=5;
            if(t==0)     Lcd_Chr(1, j, '0');
      else  if(t==1)       Lcd_Chr(1, j, '1');
       else  if(t==2)       Lcd_Chr(1, j, '2');
        else  if(t==3)      Lcd_Chr(1, j, '3');
         else  if(t==4)       Lcd_Chr(1, j, '4');
          else  if(t==5)     Lcd_Chr(1, j, '5');
           else  if(t==6)      Lcd_Chr(1, j, '6');
            else  if(t==7)      Lcd_Chr(1, j, '7');
             else  if(t==8)      Lcd_Chr(1, j, '8');
              else  if(t==9)      Lcd_Chr(1, j, '9');

                     j=6;
                  if(s==0)     Lcd_Chr(1, j, '0');
      else  if(s==1)       Lcd_Chr(1, j, '1');
       else  if(s==2)       Lcd_Chr(1, j, '2');
        else  if(s==3)      Lcd_Chr(1, j, '3');
         else  if(s==4)       Lcd_Chr(1, j, '4');
          else  if(s==5)     Lcd_Chr(1, j, '5');
           else  if(s==6)      Lcd_Chr(1, j, '6');
            else  if(s==7)      Lcd_Chr(1, j, '7');
             else  if(s==8)      Lcd_Chr(1, j, '8');
              else  if(s==9)      Lcd_Chr(1, j, '9');

                     j=7;
                  if(f==0)     Lcd_Chr(1, j, '0');
      else  if(f==1)       Lcd_Chr(1, j, '1');
       else  if(f==2)       Lcd_Chr(1, j, '2');
        else  if(f==3)      Lcd_Chr(1, j, '3');
         else  if(f==4)       Lcd_Chr(1, j, '4');
          else  if(f==5)     Lcd_Chr(1, j, '5');
           else  if(f==6)      Lcd_Chr(1, j, '6');
            else  if(f==7)      Lcd_Chr(1, j, '7');
             else  if(f==8)      Lcd_Chr(1, j, '8');
              else  if(f==9)      Lcd_Chr(1, j, '9');
     }
//write the specified voltage value
                    void writevoltagevalue(char value)
     {
     char t=0;
     char s=0;
     char f=0;  char j=0;
     t=value/100;
     s=value/10;
     f=value%10;

           if(value==100)
          {
           t=1; s=0; f=0;
           }

              j=3;
            if(t==0)     Lcd_Chr(2, j, '0');
      else  if(t==1)       Lcd_Chr(2, j, '1');
       else  if(t==2)       Lcd_Chr(2, j, '2');
        else  if(t==3)      Lcd_Chr(2, j, '3');
         else  if(t==4)       Lcd_Chr(2, j, '4');
          else  if(t==5)     Lcd_Chr(2, j, '5');
           else  if(t==6)      Lcd_Chr(2, j, '6');
            else  if(t==7)      Lcd_Chr(2, j, '7');
             else  if(t==8)      Lcd_Chr(2, j, '8');
              else  if(t==9)      Lcd_Chr(2, j, '9');

                     j=4;
                  if(s==0)     Lcd_Chr(2, j, '0');
      else  if(s==1)       Lcd_Chr(2, j, '1');
       else  if(s==2)       Lcd_Chr(2, j, '2');
        else  if(s==3)      Lcd_Chr(2, j, '3');
         else  if(s==4)       Lcd_Chr(2, j, '4');
          else  if(s==5)     Lcd_Chr(2, j, '5');
           else  if(s==6)      Lcd_Chr(2, j, '6');
            else  if(s==7)      Lcd_Chr(2, j, '7');
             else  if(s==8)      Lcd_Chr(2, j, '8');
              else  if(s==9)      Lcd_Chr(2, j, '9');

                     j=5;
                  if(f==0)     Lcd_Chr(2, j, '0');
      else  if(f==1)       Lcd_Chr(2, j, '1');
       else  if(f==2)       Lcd_Chr(2, j, '2');
        else  if(f==3)      Lcd_Chr(2, j, '3');
         else  if(f==4)       Lcd_Chr(2, j, '4');
          else  if(f==5)     Lcd_Chr(2, j, '5');
           else  if(f==6)      Lcd_Chr(2, j, '6');
            else  if(f==7)      Lcd_Chr(2, j, '7');
             else  if(f==8)      Lcd_Chr(2, j, '8');
              else  if(f==9)      Lcd_Chr(2, j, '9');


     }

    
//upper button press routine, executed when UP button is pressed
    void upperpressed()
    {
     if(index==6)     //amper upper
     {
     if(a_u==9) return;

         a_u++;

           amperevalue=a_u*10+a_l;
                  if(amperevalue>30) { a_u--;   amperevalue=a_u*10+a_l; return;}

                 eeprom_write(6,amperevalue);
         writenumber(a_u);

     }
     else if(index==7)    //amper lower
     {
               if(a_l==9) return;

           a_l++;

                  amperevalue=a_u*10+a_l;
               if(amperevalue>30) { a_l--;   amperevalue=a_u*10+a_l; return;}
                    eeprom_write(6,amperevalue);
           writenumber(a_l);

     }

      else if(index==9)    //hour upper
     {
               if(hour_h==9) return;
                        hour_h++;

               if(timemode==0)
               {
                      hourvalue_s=hour_h*10+hour_l;
               if(hourvalue_s>5) { hour_h--;    hourvalue_s=hour_h*10+hour_l; return;}
               }
               else if(timemode==1)
               {
                     hourvalue_r=hour_h*10+hour_l;
               if(hourvalue_r>5) { hour_h--;    hourvalue_r=hour_h*10+hour_l; return;}
               }
               else if(timemode==2)
               {
                    hourvalue_w=hour_h*10+hour_l;
               if(hourvalue_w>5) { hour_h--;    hourvalue_w=hour_h*10+hour_l; return;}
                eeprom_write(5,hourvalue_w);
               }


           writenumber(hour_h);
     }

       else if(index==10)    //hour lower
     {
               if(hour_l==9) return;
           hour_l++;

             if(timemode==0)
               {
                      hourvalue_s=hour_h*10+hour_l;
               if(hourvalue_s>5) { hour_l--;    hourvalue_s=hour_h*10+hour_l; return;}
               }
               else if(timemode==1)
               {
                     hourvalue_r=hour_h*10+hour_l;
               if(hourvalue_r>5) { hour_l--;    hourvalue_r=hour_h*10+hour_l; return;}
               }
               else if(timemode==2)
               {
                    hourvalue_w=hour_h*10+hour_l;
               if(hourvalue_w>5) { hour_l--;    hourvalue_w=hour_h*10+hour_l; return;}
                eeprom_write(5,hourvalue_w);
               }


           writenumber(hour_l);
     }

         else if(index==12)    //minute upper
     {
               if(minute_h==9) return;
           minute_h++;

             if(timemode==0)
               {
                      minutevalue_s=minute_h*10+minute_l;
               if(minutevalue_s>59) { minute_h--;    minutevalue_s=minute_h*10+minute_l;return;}
               }
               else if(timemode==1)
               {
                   minutevalue_r=minute_h*10+minute_l;
               if(minutevalue_r>59) { minute_h--;   minutevalue_r=minute_h*10+minute_l; return;}

               }
               else if(timemode==2)
               {
                    minutevalue_w=minute_h*10+minute_l;
               if(minutevalue_w>59) { minute_h--;    minutevalue_w=minute_h*10+minute_l; return;}
                 eeprom_write(4,minutevalue_w);
               }

           writenumber(minute_h);
     }

       else if(index==13)    //minute lower
     {
               if(minute_l==9) return;
           minute_l++;

             if(timemode==0)
               {
                      minutevalue_s=minute_h*10+minute_l;
               if(minutevalue_s>59) { minute_l--;    minutevalue_s=minute_h*10+minute_l;return;}
               }
               else if(timemode==1)
               {
                   minutevalue_r=minute_h*10+minute_l;
               if(minutevalue_r>59) { minute_l--;   minutevalue_r=minute_h*10+minute_l; return;}
               }
               else if(timemode==2)
               {
                    minutevalue_w=minute_h*10+minute_l;
               if(minutevalue_w>59) { minute_l--;    minutevalue_w=minute_h*10+minute_l; return;}
                eeprom_write(4,minutevalue_w);
               }


           writenumber(minute_l);
     }

        else if(index==15)    //second upper
     {
               if(second_h==9) return;
          second_h++;

            if(timemode==0)
               {
                      secondvalue_s=second_h*10+second_l;
               if(secondvalue_s>59) { second_h--;    secondvalue_s=second_h*10+second_l;return;}
                    eeprom_write(1,secondvalue_s);
               }
               else if(timemode==1)
               {
                       secondvalue_r=second_h*10+second_l;
               if(secondvalue_r>59) {second_h--;   secondvalue_r=second_h*10+second_l; return;}
                eeprom_write(2,secondvalue_r);
               }
               else if(timemode==2)
               {
                          secondvalue_w=second_h*10+second_l;
               if(secondvalue_w>59) { second_h--;    secondvalue_w=second_h*10+second_l; return;}
                eeprom_write(3,secondvalue_w);
               }


           writenumber(second_h);
     }
        else if(index==16)    //second lower
     {
               if(second_l==9) return;
           second_l++;

            if(timemode==0)
               {
                      secondvalue_s=second_h*10+second_l;
               if(secondvalue_s>59) { second_l--;    secondvalue_s=second_h*10+second_l;return;}
                eeprom_write(1,secondvalue_s);
               }
               else if(timemode==1)
               {
                       secondvalue_r=second_h*10+second_l;
               if(secondvalue_r>59) {second_l--;   secondvalue_r=second_h*10+second_l; return;}
                eeprom_write(2,secondvalue_r);
               }
               else if(timemode==2)
               {
                          secondvalue_w=second_h*10+second_l;
               if(secondvalue_w>59) { second_l--;    secondvalue_w=second_h*10+second_l; return;}
                eeprom_write(3,secondvalue_w);
               }


           writenumber(second_l);
     }

       else if(index==19)    //v third
     {
            /*if(v_t==9) return;
           v_t++;

              voltagevalue=v_t*100+v_s*10+v_f;
               if(voltagevalue>100) { v_t--;    voltagevalue=v_t*100+v_s*10+v_f; return;}


               eeprom_write(0,voltagevalue);
           writenumber(v_t);*/
     }

      else if(index==20)    //v sec
     {
               if(v_s==8) return;
           v_s++;

                 voltagevalue=v_t*100+v_s*10+v_f;
               if(voltagevalue>80) { v_s--;    voltagevalue=v_t*100+v_s*10+v_f; return;}
                       eeprom_write(0,voltagevalue);
           writenumber(v_s);
     }

      else if(index==21)    //v first
     {
               if(v_f==9) return;
           v_f++;

           voltagevalue=v_t*100+v_s*10+v_f;
               if(voltagevalue>80) { v_f--;    voltagevalue=v_t*100+v_s*10+v_f; return;}
                         eeprom_write(0,voltagevalue);
           writenumber(v_f);
     }
            else if(index==23)    //mode
     {
               if(timemode==0)
               {
               Lcd_Out_Cp("RISING");
                 secondvalue_s=second_h*10+second_l;
                    minutevalue_s=minute_h*10+minute_l;
                     hourvalue_s=hour_h*10+hour_l;
                 timemode=1;



       if( eeprom_read(2)>0&& eeprom_read(2)<=59)
      {
              secondvalue_r=  eeprom_read(2);
   displaysecond (secondvalue_r);

       second_h= secondvalue_r/10;
      second_l=secondvalue_r%10;

      }
            displayminute( 0); displayhour( 0);
         minute_h=0;  minute_l=0;
            hour_h=0;hour_l=0;
                }
                else    if(timemode==1)
                {
                     Lcd_Out_Cp("WORKING");
                       secondvalue_r=second_h*10+second_l;
                    minutevalue_r=minute_h*10+minute_l;
                     hourvalue_r=hour_h*10+hour_l;
                 timemode=2;



       if(eeprom_read(3)>=0&&eeprom_read(3)<=59)
      {
        secondvalue_w= eeprom_read(3) ;
      displaysecond( secondvalue_w);

       second_h= secondvalue_w/10;
      second_l=secondvalue_w%10;

      }


        if(eeprom_read(4)>=0&&eeprom_read(4)<=59)
      {
        minutevalue_w= eeprom_read(4) ;
      displayminute( minutevalue_w);

       minute_h= minutevalue_w/10;
      minute_l=minutevalue_w%10;

      }


        if(eeprom_read(5)>=0&&eeprom_read(5)<=2)
      {
        hourvalue_w= eeprom_read(5) ;
      displayhour( hourvalue_w);

      hour_h= hourvalue_w/10;
     hour_l=hourvalue_w%10;

      }
                }
                  else    if(timemode==2)
                {
                       Lcd_Out_Cp("SOCKING");
                         secondvalue_w=second_h*10+second_l;
                    minutevalue_w=minute_h*10+minute_l;
                     hourvalue_w=hour_h*10+hour_l;
                 timemode=0;

                         if(eeprom_read(1)>0&&eeprom_read(1)<=59)
      {
        secondvalue_s= eeprom_read(1) ;
      displaysecond( secondvalue_s);

       second_h= secondvalue_s/10;
      second_l=secondvalue_s%10;

      }

         displayminute( 0); displayhour( 0);
         minute_h=0;  minute_l=0;
            hour_h=0;hour_l=0;
                }
               Lcd_Cmd(_LCD_SECOND_ROW);
           Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
     }

       else if(index==32)    //pwm mode
     {
         if(pwmmode==0)
         {
          pwmmode=1;
               Lcd_Out_Cp("V");
                           eeprom_write(7,pwmmode);
                  Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);
         }
         else   if(pwmmode==1)
         {
          pwmmode=0;
                      eeprom_write(7,pwmmode);
               Lcd_Out_Cp("A");



                  Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);
         }
     }

    }


//down button routine
     void lowerpressed()
    {
     if(index==6)     //amper upper
     {
          if(a_u==0) return;
         a_u--;
         writenumber(a_u);

               amperevalue=a_u*10+a_l;
                        eeprom_write(6,amperevalue);

     }
     else if(index==7)    //amper lower
     {
          if(a_l==0) return;
           a_l--;
           writenumber(a_l);
                   amperevalue=a_u*10+a_l;
                            eeprom_write(6,amperevalue);

     }
                else if(index==9)    //hour upper
     {
               if(hour_h==0) return;
           hour_h--;

              if(timemode==0)
               {
                      hourvalue_s=hour_h*10+hour_l;

               }
               else if(timemode==1)
               {
                     hourvalue_r=hour_h*10+hour_l;

               }
               else if(timemode==2)
               {
                    hourvalue_w=hour_h*10+hour_l;
                      eeprom_write(5,hourvalue_w);

               }

           writenumber(hour_h);
     }

       else if(index==10)    //hour lower
     {
               if(hour_l==0) return;
           hour_l--;
             if(timemode==0)
               {
                      hourvalue_s=hour_h*10+hour_l;

               }
               else if(timemode==1)
               {
                     hourvalue_r=hour_h*10+hour_l;

               }
               else if(timemode==2)
               {
                    hourvalue_w=hour_h*10+hour_l;
                     eeprom_write(5,hourvalue_w);

               }
           writenumber(hour_l);
     }

         else if(index==12)    //minute upper
     {
               if(minute_h==0) return;
           minute_h--;

             if(timemode==0)
               {
                      minutevalue_s=minute_h*10+minute_l;

               }
               else if(timemode==1)
               {
                   minutevalue_r=minute_h*10+minute_l;

               }
               else if(timemode==2)
               {
                    minutevalue_w=minute_h*10+minute_l;
                      eeprom_write(4,minutevalue_w);

               }


           writenumber(minute_h);
     }

       else if(index==13)    //minute lower
     {
               if(minute_l==0) return;
           minute_l--;
            if(timemode==0)
               {
                      minutevalue_s=minute_h*10+minute_l;

               }
               else if(timemode==1)
               {
                   minutevalue_r=minute_h*10+minute_l;

               }
               else if(timemode==2)
               {
                    minutevalue_w=minute_h*10+minute_l;
                      eeprom_write(4,minutevalue_w);

               }

           writenumber(minute_l);
     }

        else if(index==15)    //second upper
     {
               if(second_h==0) return;
          second_h--;

           if(timemode==0)
               {
                      secondvalue_s=second_h*10+second_l;
                        eeprom_write(1,secondvalue_s);

               }
               else if(timemode==1)
               {
                       secondvalue_r=second_h*10+second_l;
                         eeprom_write(2,secondvalue_r);

               }
               else if(timemode==2)
               {
                          secondvalue_w=second_h*10+second_l;
                            eeprom_write(3,secondvalue_w);

               }

           writenumber(second_h);
     }

       else if(index==16)    //second lower
     {
               if(second_l==0) return;
           second_l--;
             if(timemode==0)
               {
                      secondvalue_s=second_h*10+second_l;
                        eeprom_write(1,secondvalue_s);

               }
               else if(timemode==1)
               {
                       secondvalue_r=second_h*10+second_l;
                         eeprom_write(2,secondvalue_r);

               }
               else if(timemode==2)
               {
                          secondvalue_w=second_h*10+second_l;
                            eeprom_write(3,secondvalue_w);

               }
           writenumber(second_l);
     }

      else if(index==19)    //v third
     {
               if(v_t==0) return;
           v_t--;
                   voltagevalue=v_t*100+v_s*10+v_f;
                             eeprom_write(0,voltagevalue);
           writenumber(v_t);
     }

      else if(index==20)    //v sec
     {
               if(v_s==0) return;
           v_s--;
                  voltagevalue=v_t*100+v_s*10+v_f;
                        eeprom_write(0,voltagevalue);
           writenumber(v_s);
     }

      else if(index==21)    //v first
     {
               if(v_f==0) return;
           v_f--;
                 voltagevalue=v_t*100+v_s*10+v_f;
                              eeprom_write(0,voltagevalue);
           writenumber(v_f);
     }

            else if(index==23)    //mode
     {
               if(timemode==0)
               {
               Lcd_Out_Cp("WORKING");
                 secondvalue_s=second_h*10+second_l;
                    minutevalue_s=minute_h*10+minute_l;
                     hourvalue_s=hour_h*10+hour_l;
                 timemode=2;

                     if(eeprom_read(3)>=0&&eeprom_read(3)<=59)
      {
        secondvalue_w= eeprom_read(3) ;
      displaysecond( secondvalue_w);

       second_h= secondvalue_w/10;
      second_l=secondvalue_w%10;

      }


        if(eeprom_read(4)>0&&eeprom_read(4)<=59)
      {
        minutevalue_w= eeprom_read(4) ;
      displayminute( minutevalue_w);

       minute_h= minutevalue_w/10;
      minute_l=minutevalue_w%10;

      }


        if(eeprom_read(5)>=0&&eeprom_read(5)<=2)
      {
        hourvalue_w= eeprom_read(5) ;
      displayhour( hourvalue_w);

      hour_h= hourvalue_w/10;
     hour_l=hourvalue_w%10;

      }
                }
                else    if(timemode==1)
                {
                     Lcd_Out_Cp("SOCKING");
                       secondvalue_r=second_h*10+second_l;
                    minutevalue_r=minute_h*10+minute_l;
                     hourvalue_r=hour_h*10+hour_l;
                 timemode=0;

                                   if(eeprom_read(1)>=0&&eeprom_read(1)<=59)
      {
        secondvalue_s= eeprom_read(1) ;
      displaysecond( secondvalue_s);

       second_h= secondvalue_s/10;
      second_l=secondvalue_s%10;

      }

         displayminute( 0); displayhour( 0);
         minute_h=0;  minute_l=0;
            hour_h=0;hour_l=0;
                }
                  else    if(timemode==2)
                {
                       Lcd_Out_Cp("RISING");
                         secondvalue_w=second_h*10+second_l;
                    minutevalue_w=minute_h*10+minute_l;
                     hourvalue_w=hour_h*10+hour_l;
                 timemode=1;


                                  if( eeprom_read(2)>0&& eeprom_read(2)<=59)
      {
              secondvalue_r=  eeprom_read(2);
      displaysecond(secondvalue_r);

       second_h= secondvalue_r/10;
      second_l=secondvalue_r%10;

      }

         displayminute( 0); displayhour( 0);
         minute_h=0;  minute_l=0;
            hour_h=0;hour_l=0;
                }
               Lcd_Cmd(_LCD_SECOND_ROW);
           Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
     }

            else if(index==32)    //pwm mode
     {
         if(pwmmode==0)
         {
          pwmmode=1;
                          eeprom_write(7,pwmmode);
               Lcd_Out_Cp("V");

                  Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);
         }
         else   if(pwmmode==1)
         {
          pwmmode=0;
                        eeprom_write(7,pwmmode);
               Lcd_Out_Cp("A");

                  Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);
         }
     }
    }

//right button routine
 void rightpressed()
 {


                if(index==6)
            {           Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                             index=7;
            }

               else  if(index==7)
            {           Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                             index=9;
            }

             else  if(index==9)
            {           Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                             index=10;
            }

            else  if(index==10)
            {           Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                             index=12;
            }

              else  if(index==12)
            {           Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                             index=13;
            }
              else  if(index==13)
            {           Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT); Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                             index=15;
            }

               else  if(index==15)
            {           Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                             index=16;
            }
                   else  if(index==16)
            {      
                      Lcd_Cmd(_LCD_SECOND_ROW);
                 Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);      Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                             index=19;
            }
                     else  if(index==19)
            {

                 Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                             index=20;
            }
              else  if(index==20)
            {

                 Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                             index=21;
            }

             else  if(index==21)
            {

                 Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);    Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                             index=23;
            }

            else  if(index==23)
            {

                 Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);    Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT); Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);    Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT); Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);    Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT); Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);    Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT); Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                             index=32;
            }

                   else  if(index==32)
            {
                     Lcd_Cmd(_LCD_FIRST_ROW);
                 Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);       Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);        Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);        Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);       Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                             index=6;
            }

 }
//left button routine
     void leftpressed()
     {

                if(index==7)
            {           Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);
                             index=6;
            }

               else  if(index==9)
            {           Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);
                             index=7;
            }

             else  if(index==10)
            {           Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);
                             index=9;
            }

            else  if(index==12)
            {           Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);
                             index=10;
            }

              else  if(index==13)
            {           Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);
                             index=12;
            }
              else  if(index==15)
            {           Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT); Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);
                             index=13;
            }

               else  if(index==16)
            {           Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);
                             index=15;
            }
                   else  if(index==19)
            {
                      Lcd_Cmd(_LCD_FIRST_ROW);
                 Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);    Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);    Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);    Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                   Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);    Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);    Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);    Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                     Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);    Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);    Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);    Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                       Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);    Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);    Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                             index=16;
            }
                     else  if(index==20)
            {

                 Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);
                             index=19;
            }
              else  if(index==21)
            {

                 Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);
                             index=20;
            }

             else  if(index==23)
            {

                 Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);    Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);
                             index=21;
            }

            else  if(index==32)
            {

                 Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);    Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT); Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);    Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT); Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);    Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT); Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);    Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT); Lcd_Cmd(_LCD_MOVE_CURSOR_LEFT);
                             index=23;
            }
     }


//soft based UART routine to receive byte
               unsigned short receivebyte()
            {
            const unsigned short delay=85;
            unsigned short    receivedresult1=0;
                delay_us( 135);
                    tmr0.f0=portb.f5;
                    delay_us(delay);
                     tmr0.f1=portb.f5;
                     delay_us(delay);
                     tmr0.f2=portb.f5;
                     delay_us(delay);
                     tmr0.f3=portb.f5;
                     delay_us(delay);
                     tmr0.f4=portb.f5;

                     delay_us(delay);
                     tmr0.f5=portb.f5;
                     delay_us(delay);
                     tmr0.f6=portb.f5;
                     delay_us(delay);
                     tmr0.f7=portb.f5;
                     delay_us(170);
                        receivedresult1=tmr0;
                     return receivedresult1;
            }

//send voltage value to slave processor, an active low pulse represent one voltage.
                           void sendvoltage(unsigned short value)
                           {
                           char gg=0;
                             while(1)
                             {
                                 portb.f7=0;
                                 delay_ms(5);
                                   portb.f7=1;
                                          delay_ms(5);
                                          gg++;
                                          if(gg==value) break;
                             }
                           }
//send timevalue to slave processor, an active low pulse represent one second.
                                    void sendtime(unsigned short value)
                           {
                           char gg=0;
                             while(1)
                             {
                                 portb.f5=0;
                                 delay_ms(5);
                                   portb.f5=1;
                                          delay_ms(5);
                                          gg++;
                                          if(gg==value) break;
                             }
                           }

//UART based routine in software to send one byte.
              void sendbyte(unsigned short value)
            {
               SPBRG=value;

portb.f7=0;
delay_us(100);

portb.f7=spbrg.f0;

delay_us(100);

 portb.f7=spbrg.f1;

delay_us(100);

portb.f7=spbrg.f2;
 delay_us(100);

 portb.f7=spbrg.f3;

delay_us(100);

 portb.f7=spbrg.f4;
delay_us(100);

 portb.f7=spbrg.f5;

delay_us(100);

portb.f7=spbrg.f6;

delay_us(100);

portb.f7=spbrg.f7;

delay_us(100);

 portb.f7=1;

delay_ms(2);
}

//send all data to slave processor including voltage and time
void senddata()
{

        sendvoltage(voltagevalue);
        delay_ms(50);
        sendtime(secondvalue_r);




          /*sendbyte(0) ;       delay_ms(50);
            sendbyte(voltagevalue);
                    delay_ms(50);

                 sendbyte(4) ;    delay_ms(50);
            sendbyte(secondvalue_r);
                      delay_ms(50);*/



        }
          void initializesettings()
          { 
                writevoltagedisable=0;
              riseincrement=0;
           portb.f6=0;
              porta.f2=0;
              portb.f7=1;
               index=6;
               displayindex=0;
               correctionindex=0;

                    timemode=0;


                              hour_h=0; hour_l=0;
                              minute_h=0;  minute_l=0;

                          secondvalue_s=  eeprom_read(1);

       if(secondvalue_s>0&&secondvalue_s<=59)
      {
      displaysecond( secondvalue_s);

       second_h= secondvalue_s/10;
      second_l=secondvalue_s%10;

      }             
       writevoltagevalue(voltagevalue);

                       Lcd_Cmd(_LCD_BLINK_CURSOR_ON);
                Lcd_Cmd(_LCD_FIRST_ROW);

                   Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);  Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);  Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);  Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);  Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);

                  currentreached=0;

          }


     unsigned int getadcresult()
   {
    return adresh.f1*512+  adresh.f0*256+adresl.f7*128+adresl.f6*64+adresl.f5*32+adresl.f4*16+adresl.f3*8+adresl.f2*4+adresl.f1*2+adresl.f0*1;
   }





                void readcurrentvalue()
        {

        unsigned int g=0; unsigned short gg=0;
                adcon0.f5=0;  adcon0.f4=0;  adcon0.f3=0;    //channel 0
                delay_ms(10);
           adcon0.f2=1;

              while(1)
              {
               if(!adcon0.f2)
               break;
              }
              g=getadcresult();
              if(g<=5&&g>=3)  g=5;
               g=g/5;
               gg= (unsigned short)g;


              if(pwmmode==1)
              {
                writecurrentvalue(gg);
                return;
              }

                  displayindex++;
                if(displayindex==3)
                {
                displayindex=0;

              writecurrentvalue(gg);
              }


                                      if(gg>=amperevalue)    currentreached=1;
                     if(g<3&&riseincrement>=20&&currentreached==0) 
                     {
                     writevoltagevalue(0);
                      return;

                      }



               if(gg<amperevalue)
                        {
                             portb.f5=0;      //rise voltage
               delay_ms(10);
                portb.f5=1;
                riseincrement++;

                        }


                        else      if(gg>amperevalue)
                        {
                              portb.f7=0;      //lower voltage
               delay_ms(10);
                portb.f7=1;
                 currentreached=1;
                 riseincrement--;
                        }

               }


                void readvoltagevalue()
        {

        unsigned int g=0; unsigned short gg=0;
                adcon0.f5=0;  adcon0.f4=0;  adcon0.f3=1;    //channel 1
                delay_ms(10);
           adcon0.f2=1;

              while(1)
              {
               if(!adcon0.f2)
               break;
              }
              g=getadcresult();
               g=g/4;
               gg= (unsigned short)g;


                  if(pwmmode==0)

                    {

                  writevoltagevalue(gg);

                  }


                  else  if(pwmmode==1)
                    {
                       displayindex++;
                if(displayindex==4)
                {
                displayindex=0;

                     if(writevoltagedisable==0)
              writevoltagevalue(gg);
              }

               correctionindex++;
               if(correctionindex==5)
               {
               correctionindex=0;

    if(gg<voltagevalue)
              {
               portb.f5=0;      //rise voltage
               delay_ms(10);
                portb.f5=1;
              }
              else if(gg>voltagevalue)
              {
                   portb.f7=0;      //lower voltage
               delay_ms(10);
                portb.f7=1;
              }

               }
                }

        }



       void execute_a ()
       {
            signed  char sec,min,hour;
  unsigned int adresult=0;
       double cons=0.488;
      double advoltageresult=0;
      unsigned short advoltageresult1=0;
  char voltageslope=0;    char currentvalue=0;


            currentreached=0;
            riseincrement=0;
          porta.f2=1;
       delay_ms(1000);


                   voltagevalue=5;           secondvalue_r=1;

             sendvoltage(voltagevalue);
        delay_ms(50);
        sendtime(secondvalue_r);
          delay_ms(50);



    sec=secondvalue_s;
    min=minutevalue_s;
     hour=hourvalue_s;



               Lcd_Cmd(_LCD_CURSOR_OFF);


                    writevoltagevalue( 0);        writecurrentvalue( 0);
              while(1)
               {
                     Lcd_Out(2, 7, "Socking");
                   displaysecond(sec);   displayminute(min);  displayhour(hour);
                           if(sec== 0&&min==0&&hour==0) break;
                   sec--;
                 if(sec<0)
                 {
                 min--;     sec=59;
                 if(min<0)
                 {
                  hour--;
                  min=59;
                 }
                 }
                    if( !portb.f1)
                    {
                    initializesettings();  delay_ms(2000);
                     return;
                    }
                   delay_ms(1000);
               }



                       portb.f6=1;
                sec=secondvalue_r;
    min=minutevalue_r;
     hour=hourvalue_r;

     displaysecond(sec);   displayminute(min);  displayhour(hour);
                    delay_ms(500);
               while(1)
               {
                     Lcd_Out(2, 7, "Rising ");


                          if( !portb.f1)
                    {
                    initializesettings();
                       delay_ms(2000);
                     return;
                    }


                   delay_ms(50);

                    readcurrentvalue();


                         if(currentreached==1)  break;
               }

                  delay_ms(100);


                sec=secondvalue_w;
    min=minutevalue_w;
     hour=hourvalue_w;


               while(1)
               {
                     Lcd_Out(2, 7, "WORKING");
                   displaysecond(sec);   displayminute(min);  displayhour(hour);
                           if(sec== 0&&min==0&&hour==0) break;
                   sec--;
                 if(sec<0)
                 {
                 min--;     sec=59;
                 if(min<0)
                 {
                  hour--;
                  min=59;
                 }
                 }
                      if( !portb.f1)
                    {
                    initializesettings();  delay_ms(2000);
                     return;
                    }


     readcurrentvalue();
                   delay_ms(250);

                      readcurrentvalue();
                   delay_ms(250);

                      readcurrentvalue();
                   delay_ms(250);

                         readvoltagevalue();
                         delay_ms(250);



               }


                  portb.f6=0;
                    initializesettings();
       }



     void execute()
     {
  signed  char sec,min,hour;
  unsigned int adresult=0;
       double cons=0.488;
      double advoltageresult=0;
      unsigned short advoltageresult1=0;unsigned short qqq=0;
  char voltageslope=0;    char currentvalue=0;

        char slopeflag=0;
        char slopeindex=0;
         unsigned int t1=0;  unsigned int t2    =0; unsigned int t3    =0;
         char workingindex=0;

          porta.f2=1;
       delay_ms(1000);

          if(  voltagevalue>=   secondvalue_r )
          {

voltageslope=voltagevalue/ secondvalue_r;
slopeflag=0;

  }

  else  if(  secondvalue_r>   voltagevalue )
  {
      t1=  (unsigned int)(secondvalue_r);
        t2=  (unsigned int)(voltagevalue);
            t3=   (t1*5)/t2;

        voltageslope=   (char)(t3);
  slopeflag=1;
  }


            if(voltagevalue>=50)
              {
               qqq=  voltagevalue-10;
             sendvoltage(qqq);
             }
                else  if(voltagevalue>=25)
              {
               qqq=  voltagevalue-5;
             sendvoltage(qqq);
             }
             else  sendvoltage(voltagevalue);

        delay_ms(50);
        sendtime(secondvalue_r);
          delay_ms(50);



    sec=secondvalue_s;
    min=minutevalue_s;
     hour=hourvalue_s;



               Lcd_Cmd(_LCD_CURSOR_OFF);


                    writevoltagevalue( 0);  writecurrentvalue( 0);
                            delay_ms(100);
              while(1)
               {
                     Lcd_Out(2, 7, "Socking");
                   displaysecond(sec);   displayminute(min);  displayhour(hour);
                           if(sec== 0&&min==0&&hour==0) break;
                   sec--;
                 if(sec<0)
                 {
                 min--;     sec=59;
                 if(min<0)
                 {
                  hour--;
                  min=59;
                 }
                 }
                    if( !portb.f1)
                    {
                    initializesettings();  delay_ms(2000);
                     return;
                    }
                   delay_ms(1000);
               }

                       portb.f6=1;
                sec=secondvalue_r;
    min=minutevalue_r;
     hour=hourvalue_r;

               while(1)
               {
                     Lcd_Out(2, 7, "Rising ");
                   displaysecond(sec);   displayminute(min);  displayhour(hour);
                           if(sec== 0&&min==0&&hour==0) break;
                   sec--;
                 if(sec<0)
                 {
                 min--;     sec=59;
                 if(min<0)
                 {
                  hour--;
                  min=59;
                 }
                 }
                          if( !portb.f1)
                    {
                    initializesettings();
                       delay_ms(2000);
                     return;
                    }



                  delay_ms(200);
                      if(slopeflag==1)
                  {
                  slopeindex++;
                  if(slopeindex==   voltageslope)
                  {
                        currentvalue++;
                        slopeindex=0;
                  }
                writevoltagevalue( currentvalue);
                     }


                      delay_ms(200);
                      if(slopeflag==1)
                  {
                  slopeindex++;
                  if(slopeindex==   voltageslope)
                  {
                        currentvalue++;
                        slopeindex=0;
                  }
                writevoltagevalue( currentvalue);
                     }


                      delay_ms(200);
                    if(slopeflag==1)
                  {
                  slopeindex++;
                  if(slopeindex==   voltageslope)
                  {
                        currentvalue++;
                        slopeindex=0;
                  }
                writevoltagevalue( currentvalue);
                     }

                      delay_ms(200);

                       if(slopeflag==1)
                  {
                  slopeindex++;
                  if(slopeindex==   voltageslope)
                  {
                        currentvalue++;
                        slopeindex=0;
                  }
                writevoltagevalue( currentvalue);
                     }

                      delay_ms(200);
                    if(slopeflag==0)
                  {
                                currentvalue=currentvalue+voltageslope;
                                writevoltagevalue( currentvalue);
                   }
                   else    if(slopeflag==1)
                  {
                  slopeindex++;
                  if(slopeindex==   voltageslope)
                  {
                        currentvalue++;
                        slopeindex=0;
                  }
                writevoltagevalue( currentvalue);
                     }


               }

                delay_ms(100);
                          writevoltagevalue( voltagevalue);

                sec=secondvalue_w;
    min=minutevalue_w;
     hour=hourvalue_w;


               while(1)
               {
                     Lcd_Out(2, 7, "WORKING");
                   displaysecond(sec);   displayminute(min);  displayhour(hour);
                           if(sec== 0&&min==0&&hour==0) break;
                   sec--;
                 if(sec<0)
                 {
                 min--;     sec=59;
                 if(min<0)
                 {
                  hour--;
                  min=59;
                 }
                 }
                      if( !portb.f1)
                    {
                    initializesettings();  delay_ms(2000);
                     return;
                    }

                      if(workingindex<5)
                      {
                      workingindex++;
                      writevoltagedisable=1;
                      }
                      else
                      {
                         writevoltagedisable=0;
                      }

     readvoltagevalue();
                   delay_ms(250);

                  readvoltagevalue();
                   delay_ms(250);

                    readvoltagevalue();
                   delay_ms(250);

                    readvoltagevalue();
                   delay_ms(250);


                 // readcurrentvalue();

               }


                  portb.f6=0;
                    initializesettings();


     }

     void loadeeprom()
     {

        char h=0;


      voltagevalue=eeprom_read(0);
      amperevalue= eeprom_read(6);
      if(voltagevalue>0&&voltagevalue<=80)
      {
              writevoltagevalue( voltagevalue);
//   writevoltagevalue( voltagevalue,2,3,4,5);
      if(voltagevalue==100)
      {
       v_t=1;
       v_s=0;
       v_f=0;
      }
      else
      {
       v_t=0;
       v_s= voltagevalue/10;
       v_f=voltagevalue%10;
      }


      }
      else voltagevalue=25;


        if(amperevalue>0&&amperevalue<=30)
      {
            writecurrentvalue( amperevalue);
               a_u= amperevalue/10;
       a_l=amperevalue%10;
      }
      else amperevalue=16;


      pwmmode= eeprom_read(7);

           if(pwmmode==0)
      {
         Lcd_Chr(2, 16, 'A');
       }

       else  if(pwmmode==1)
      {
            Lcd_Chr(2, 16, 'V');
       }

         else
             {
         pwmmode=1;
              Lcd_Chr(2, 16, 'V');
         }


     secondvalue_s=  eeprom_read(1);

       if(secondvalue_s>0&&secondvalue_s<=59)
      {
      displaysecond( secondvalue_s);

       second_h= secondvalue_s/10;
      second_l=secondvalue_s%10;

      }
      else secondvalue_s=4;




       if( eeprom_read(2)>0&& eeprom_read(2)<=59)
      {
          secondvalue_r=  eeprom_read(2);

      }

       if( eeprom_read(3)>=0&& eeprom_read(3)<=59)
      {
          secondvalue_w=  eeprom_read(3);

      }

        if( eeprom_read(4)>=0&& eeprom_read(4)<=59)
      {
          minutevalue_w=  eeprom_read(4);

      }
          if( eeprom_read(5)>=0&& eeprom_read(5)<=2)
      {
         hourvalue_w=  eeprom_read(5);

      }

     }

void main() {

        unsigned short g=0;
        unsigned short  receivedresult=0;

         resetvector:
          TRISA.F2=0;
    trisc.f7=1;
    trisc.f6=1;
    trisb.f1=1; trisb.f2=1; trisb.f3=1;   trisb.f4=1;

       trisb.f7=0;       //serial   and lower voltage
            trisb.f6=0;          // flag on off
                trisb.f5=0;   //feedback   rise voltage

                portb.f5=1;
            portb.f6=0;
            portb.f7=1;



              porta.f2=0;

     Lcd_Init();


                            Lcd_Cmd(_LCD_CLEAR);

Lcd_Cmd(_LCD_BLINK_CURSOR_ON);

          Lcd_Out(1, 1, "A=00016 00:00:04");
             Lcd_Out(2, 1, "V=025 SOCKING  V");

                        loadeeprom();

                             Lcd_Cmd(_LCD_FIRST_ROW);
                Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);


                                 trisa.f0=1; trisa.f1=1;
                         adcon0.f7=0;  adcon0.f6=1;   //conversion clock
                            adcon0.f5=0;  adcon0.f4=0;  adcon0.f3=1;    //channel 1

                          adcon1.f7=1;       //a/d result format
                                adcon1=0Xf4;

                              adcon0.f0=1;    //ad on bit

                    delay_ms(100);

             /*while(1)
  {
    portb.f6=1;
       delay_ms(1000);
       portb.f6=0;
       delay_ms(1000);
  }*/


  while(1)
  {
           if(!PORTC.f7)     //right
           {

                 rightpressed();
                      delay_ms(250);

           }


           else      if(!PORTb.f2)   //left
           {

      leftpressed();
                delay_ms(250);
           }


               else      if(!PORTb.f3)  //up
           {      

       UPPERPRESSED();
                   delay_ms(250);
           }


               else      if(!PORTC.f6)  //down
           {

          lowerpressed();
               delay_ms(250);
           }

               else      if(!PORTb.f1)     //exec
           {

               if(pwmmode==1)       //voltage
                {
           execute();
             goto   resetvector;
           }

           else if(pwmmode==0)          //current mode

              {
              //  execute_a();
                  // goto   resetvector;
                }

                 delay_ms(250);

           }
  }

}


          

Leave a Reply

Your email address will not be published. Required fields are marked *