Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] mikro c button function code is not working

Status
Not open for further replies.

Qaisar Azeemi

Full Member level 5
Full Member level 5
Joined
Feb 11, 2011
Messages
315
Helped
16
Reputation
32
Reaction score
15
Trophy points
1,298
Location
Peshawar, Pakistan, Pakistan
Activity points
3,829
Hi;
i am using four buttons with my PIC16F877A microcontroller. i am using button function if( Button(&PORTD,0,100,0)) in order to detect Logic 0 by a keypress but it is not detecting it. here is my code:

Code:
// INPUTS
#define enter PORTD.F0
#define reset PORTD.F1
#define up PORTD.F2
#define down PORTD.F3

//CONSTANTS
#define ms 100   // for button debounce

int scroll_up( int , int );
int scroll_down( int , int );

void main()
{
    unsigned int i=0;  //i=loops
 // INPUTS:
  TRISD.F0=1;    // ENTER
  TRISD.F1=1;   // RESET
  TRISD.F2=1;    // UP
  TRISD.F3=1;    // DOWN
  TRISA = 0xff;     // port A as input

 while(1)
  {



                    if(Button(&PORTD,0,ms,0))  //when Enter is pressed
                     {
                          int itrue=1;
                          x=0;
                          Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                          Lcd_Cmd(_LCD_CURSOR_OFF);
                          Lcd_Out(1,1,CopyConst2Ram(msg,ep));   //enter passward
                          Delay_ms(t);
                             while(Button(&PORTD,0,ms,0)) // press enter
                             {    x++;
                               Lcd_Out(2,x,CopyConst2Ram(msg,asteric));  //*

                                 if(x==4)
                                 {
                                   Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                                   Lcd_Cmd(_LCD_CURSOR_OFF);
                                   Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                                   Lcd_Out(1,1,CopyConst2Ram(msg,pw));   //please wait
                                   Delay_ms(1000);

                                   Lcd_Cmd(_LCD_CLEAR);
                                   Lcd_Out(1,1,CopyConst2Ram(msg,gwt));  // Generator warm up time

                                   inc=0;  //for selection of variable
                                   itrue=1;   // Entring and Exiting main menu
                                   opt=1;    // option b/w selection number or variable

                                             while(itrue)
                                             {
                                                  if(Button(&PORTD,0,ms,0))  // pressing enter to enter time
                                                  {
                                                   opt=0;
                                                   Lcd_Out(2,1,CopyConst2Ram(msg,sp));
                                                  }


                                                  if(Button(&PORTD,1,ms,0)) //press reset to exit
                                                  itrue=0;

                                                  if(Button(&PORTD,2,ms,0))  //up
                                                  inc=scroll_up(opt,inc);

                                                  if(Button(&PORTD,3,ms,0))   // down
                                                  inc=scroll_down(opt,inc);
                                             }

                                              x++;
                                 }   //if
                             }    //while
                          Delay_ms(1000);
                     }

  }    // while

}      // main

please tell me where i am going wrong in Button function. please don't care of lcd or scroll functions code missing; but i think i am missing some thing in button function. i am just having the problem in button function. my hardware is functioning well, means no problem in there in soldering or wiring connections.
waiting for your kind help, and any help will be much appreciated.

Thanking you in anticipation

Regards
Qaisar Azeemi
 

Where is your scroll_up() and scroll_down() functions?

i didn't make the code for these functions now. because these functions based on the button function... and i have the problem with button function....... so first i want to set button function.................. my keypress is not detected by this button function....... is there any problem with its deceleration? kindly check the mistake in Button function... is it need to defined before main?
 

i didn't make the code for these functions now. because these functions based on the button function... and i have the problem with button function....... so first i want to set button function.................. my keypress is not detected by this button function....... is there any problem with its deceleration? kindly check the mistake in Button function... is it need to defined before main?

In your code
Code:
 if(Button(&PORTD,2,ms,0))  //up
                                                  inc=scroll_up(opt,inc);

                                                  if(Button(&PORTD,3,ms,0))   // down
                                                  inc=scroll_down(opt,inc);
the scroll_up() and scroll_down() functions assigns the value ti inc variable. So you should code the functions first. What do you mean by buttons not working. What should the buttons do?

Change
Code:
 #define ms 100   // for button debounce
to
Code:
 #define ms 20   // for button debounce
 
Last edited:

simply when i press the button the code
Code:
                                         if(Button(&PORTD,2,ms,0))  //up
                                                  inc=scroll_up(opt,inc);

must detect the logic zero and execute the statment

Code:
 inc=scroll_up(opt,inc);

i will write the function for scroll_up and scroll_down.. and both of these contains the same button functions and and statments after that as the above code.. now i just declared a variable in the function body to tell the compilar that the function is present.
 

Post your proteus .dsn file.

According to the code
Code:
 while(itrue)
                                             {
                                                  if(Button(&PORTD,0,ms,0))  // pressing enter to enter time
                                                  {
                                                   opt=0;
                                                   Lcd_Out(2,1,CopyConst2Ram(msg,sp));
                                                  }


                                                  if(Button(&PORTD,1,ms,0)) //press reset to exit
                                                  itrue=0;

                                                  if(Button(&PORTD,2,ms,0))  //up
                                                  inc=scroll_up(opt,inc);

                                                  if(Button(&PORTD,3,ms,0))   // down
                                                  inc=scroll_down(opt,inc);
                                             }

all the buttons work only when itrue = 1.

First you have to press ENTER button 4 times to make x = 4. when x = 4 only then itrue will become 1.. then only the other buttons work.
 
Last edited:

Yes Quite right..... and by default itrue is 1. when i press the button reset it will make itrue=0 to exit from the main routine "while(itrue)". but again the problem is that it is not detecting the button press.

here is my proteus file image: i am sorry i don't know how to attach my .dsn file here :-?
ATS Test.png
 
Last edited:

Yes Quite right..... and by default itrue is 1. when i press the button reset it will make itrue=0 to exit from the main routine "while(itrue)". but again the problem is that it is not detecting the button press.

here is my proteus file image: i am sorry i don't know how to attach my .dsn file here :-?
View attachment 79282

When you press the reset button itrue becomes 0. So you have to press ENTER button 4 times to make x = 4 and itrue = 1. Then only the buttons work.

Tell me what the buttons should do?

- - - Updated - - -

Check this.

I have coded such that on pressing the buttons, they do something.

You have to zip your .dsn file. Then click Manage Attachments, select the file to upload and then upload it and then click the Done button and then click save changes.
 

Attachments

  • pic16f877a_buttons.rar
    17.5 KB · Views: 90
Last edited:

Thats great, ....... your file is working fine.... Kindly post your source code here.... i want to compare it for my mistakes..

- - - Updated - - -

Tell me what the buttons should do?

The buttons should do the same thing as you code. but with a few changes. let me explain it to you step by step.

1) when i press enter button it should enter the scrolling routine..
2) just like enter when i press the reset button at any point in the scrolling routine , it must get out of that specific routine.
3)the scroll up and scroll down have the same function as you code but after scroll to a specific/desired number i want to press enter and display saving..... for a second or half to show an effect on viewer the the number is being saved with his command/key press... :)
 

Here is the button code.

Add any extra code you want to it.

Code:
if((ENTER == 0) && (UP == 1) && (DOWN == 1) && (RESET == 1)) {
              Delay_ms(150);
              if((ENTER == 0) && (UP == 1) && (DOWN == 1) && (RESET == 1)) {
                 Lcd_Cmd(_LCD_CLEAR);
                 Lcd_Out(2,1,"ENTER Pressed");
              }
            }

            if((ENTER == 1) && (UP == 0) && (DOWN == 1) && (RESET == 1)) {
              Delay_ms(150);
              if((ENTER == 1) && (UP == 0) && (DOWN == 1) && (RESET == 1)) {
                  counter = counter + 1;
                  Lcd_Cmd(_LCD_CLEAR);
                  IntToStr(counter, string);
                  Lcd_Out(2,10,string);
                  
              }
            }
            
            if((ENTER == 1) && (UP == 1) && (DOWN == 0) && (RESET == 1)) {
              Delay_ms(150);
              if((ENTER == 1) && (UP == 1) && (DOWN == 0) && (RESET == 1)) {
                  counter = counter - 1;
                  Lcd_Cmd(_LCD_CLEAR);
                  IntToStr(counter, string);
                  Lcd_Out(2,10,string);
              }
            }
            
            if((ENTER == 1) && (UP == 1) && (DOWN == 1) && (RESET == 0)) {
              Delay_ms(150);
              if((ENTER == 1) && (UP == 1) && (DOWN == 1) && (RESET == 0)) {
                   Lcd_Cmd(_LCD_CLEAR);
                   Lcd_Out(2,1,"RESET Pressed");
              }
            }

Check this.

----------update----------------

Here is the updated code.

Code:
if((ENTER == 0) && (UP == 1) && (DOWN == 1) && (RESET == 1) && (btn_pressed == 0)) {
              Delay_ms(150);
              if((ENTER == 0) && (UP == 1) && (DOWN == 1) && (RESET == 1) && (btn_pressed == 0)) {
                 Lcd_Cmd(_LCD_CLEAR);
                 Lcd_Out(2,1,"ENTER Pressed");
                 btn_pressed = 0;
              }
            }

            if((ENTER == 0) && (UP == 1) && (DOWN == 1) && (RESET == 1) && (btn_pressed == 1)) {
              Delay_ms(150);
              if((ENTER == 0) && (UP == 1) && (DOWN == 1) && (RESET == 1) && (btn_pressed == 1)) {
                 Lcd_Cmd(_LCD_CLEAR);
                 Lcd_Out(2,1,"Saving...");
                 Delay_ms(1000);
                 Lcd_Cmd(_LCD_CLEAR);
                 btn_pressed = 0;
              }
            }
            
            if((ENTER == 1) && (UP == 0) && (DOWN == 1) && (RESET == 1)) {
              Delay_ms(150);
              if((ENTER == 1) && (UP == 0) && (DOWN == 1) && (RESET == 1)) {
                  counter = counter + 1;
                  Lcd_Cmd(_LCD_CLEAR);
                  IntToStr(counter, string);
                  Lcd_Out(2,10,string);
                  btn_pressed = 1;
                  
              }
            }
            
            if((ENTER == 1) && (UP == 1) && (DOWN == 0) && (RESET == 1)) {
              Delay_ms(150);
              if((ENTER == 1) && (UP == 1) && (DOWN == 0) && (RESET == 1)) {
                  counter = counter - 1;
                  Lcd_Cmd(_LCD_CLEAR);
                  IntToStr(counter, string);
                  Lcd_Out(2,10,string);
                  btn_pressed = 1;
              }
            }
            
            if((ENTER == 1) && (UP == 1) && (DOWN == 1) && (RESET == 0)) {
              Delay_ms(150);
              if((ENTER == 1) && (UP == 1) && (DOWN == 1) && (RESET == 0)) {
                   Lcd_Cmd(_LCD_CLEAR);
                   Lcd_Out(2,1,"RESET Pressed");
                   btn_pressed = 0;
              }
            }

-----------------update----------------------

Check with this hex file.
 

Attachments

  • pic16f877a_buttons_v2.rar
    17.6 KB · Views: 74
  • pic16f877a_hex.rar
    1.7 KB · Views: 77
Last edited:
It is better to program without using buttons function. You have to consider multiple button presses. You cannot monitor for multiple button presses using button library.

If multiple buttons are pressed, code for all the buttons gets executed one after the other. You have to check the conditions of all the buttons every time.

Check this hex file.
 

Attachments

  • pic16f877a v3.rar
    2 KB · Views: 69
Last edited:

Check the simulation with this code. Press all the buttons and see.

Code:
void main() {
          unsigned int counter;
          char string[7];
          unsigned int btn_pressed;

          while(1) {
                  // ENTER
            if((Button(&PORTD,0,100,0)) && (btn_pressed == 0)) {
                Lcd_Cmd(_LCD_CLEAR);
                Lcd_Out(2,1,"ENTER Pressed");
                Delay_ms(1000);
                Lcd_Cmd(_LCD_CLEAR);
                IntToStr(counter, string);
                Lcd_Out(1,2,"Press UP/DOWN");
                Lcd_Out(2,10,string);
                btn_pressed = 0;
            }
            
            // ENTER
            if((Button(&PORTD,0,100,0)) && (btn_pressed == 1)) {
                Lcd_Cmd(_LCD_CLEAR);
                Lcd_Out(2,1,"Saving...");
                Delay_ms(1000);
                Lcd_Cmd(_LCD_CLEAR);
                btn_pressed = 0;
            }
            // RESET
            if(Button(&PORTD,1,100,0)) {
               Lcd_Cmd(_LCD_CLEAR);
               Lcd_Out(2,1,"RESET Pressed");
               counter = 0;
               btn_pressed = 0;
            }
            // UP
            if(Button(&PORTD,2,100,0)) {
               counter = counter + 1;
               Lcd_Cmd(_LCD_CLEAR);
               IntToStr(counter, string);
               Lcd_Out(1,2,"Press UP/DOWN");
               Lcd_Out(2,10,string);
               btn_pressed = 1;
            }
            // DOWN
            if(Button(&PORTD,3,100,0)) {
               counter = counter - 1;
               Lcd_Cmd(_LCD_CLEAR);
               IntToStr(counter, string);
               Lcd_Out(1,2,"Press UP/DOWN");
               Lcd_Out(2,10,string);
               btn_pressed = 1;
            }
          }
}
 

Thanks Internetuser2k12, your code is working fine on my hardware. but now here i have a few problems....

1) I want to save the incremented or decremented number permanently in the controller's RAM or ROM. i.e the value of counter must be saved in the microcontroller even if i remove the power.

2) i have written the same code but it is not working: kindly check my code for decleration mistakes...... i have decleard portd bits 4 and 5 as an output for lcd control signals and the pis 0,1,2, and 3 as inputs of same port... it is as follows:

lcd pin decleration:
Code:
//============Start LCD Connections========================================
sbit LCD_RS at RD5_bit;
sbit LCD_EN at RD4_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D7 at RC7_bit;

sbit LCD_RS_Direction at TRISD5_bit;
sbit LCD_EN_Direction at TRISD4_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D7_Direction at TRISC7_bit;
//=======End LCD Connections=============================================

portD pin declerations
Code:
#define enter PORTD.F0
#define reset PORTD.F1
#define up PORTD.F2
#define down PORTD.F3

 // INPUTS:
 /* TRISD.F0=0xff;    // ENTER
  TRISD.F1=0xff;  // RESET
  TRISD.F2=0xff;    // UP
  TRISD.F3=0xff;   // DOWN   */
  TRISA = 0xff;     // port A as input
  TRISD = 0xff;     // port D as input

and here is the full code:

Code:
// OUTPUTS
#define gen_cont PORTE.f2
#define led PORTB.f2
#define main_cont PORTC.F0
#define chowk PORTC.F1
#define ignition PORTC.F2
#define self PORTC.F3

// INPUTS
#define genr PORTA.F0
#define mains PORTA.F1

#define enter PORTD.F0
#define reset PORTD.F1
#define up PORTD.F2
#define down PORTD.F3

//CONSTANTS
#define t 100   //DELAYS
#define P 1     //FOR MAIN AND gen INPUTS
#define A 0     //FOR MAIN AND gen INPUTS
#define ms 150   // for button debounce
//#define pressed 0 // to detect the button press
//#define unpressed 1 // to detect the button unpress
//============Start LCD Connections========================================
sbit LCD_RS at RD5_bit;
sbit LCD_EN at RD4_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D7 at RC7_bit;

sbit LCD_RS_Direction at TRISD5_bit;
sbit LCD_EN_Direction at TRISD4_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D7_Direction at TRISC7_bit;
//=======End LCD Connections=============================================

//========== Start Button connections =======================================
/*unsigned short Button(unsigned short *PORTD, unsigned short  , unsigned short , unsigned short );
sbit Button_Pin           at PIND.F0;   // Input pin, PINx register is used //RD0_bit;
sbit Button_Pin_Direction at DDRD.F0;
sbit Button_Pin           at PIND.F1; // RD1_bit;
sbit Button_Pin_Direction at DDRD.F1;
sbit Button_Pin           at PIND.F2;   //  RD2_bit;
sbit Button_Pin_Direction at DDRD.F2;
sbit Button_Pin           at PIND.F3;  // RD3_bit;
sbit Button_Pin_Direction at DDRD.F3;    */
//=============== End Button connections ====================================

//============= START RAM SAVER ===========================================
const char wu[] = "Warming up.....";
const char fv[] = "Frequency  Volts";
const char error[] = "< ERROR >";
const char f2s[] = "Fails to start";
const char st[] = "Shenztech";
const char pl[] = "Pvt Ltd";
const char mna[] = "Mains not avail";
const char sg[] = "Starting Genset";
const char mok[] = "Mains OK..";
const char mol[] = "Mains Overload";
const char pw[] = "Please Wait...";
const char gwt[] = "Gen Warm up time";
const char sont[] = "Self ON Time";
const char soft[] = "Self OFF time";
const char ep[] = "Enter Passward";
const char asteric[] = "*";
const char sp[] = "scroll please";
const char sav[] = "Saving.......";
//============= END RAM SAVER ===========================================
 int x=0, inc=0, opt=1, gen_wt=10, self_ont=2, self_oft=5;
char msg[17]; //declare array set 16 bytes of RAM aside for copying into
 // copy const to ram string
char  *CopyConst2Ram(char * dest, const char * src)
{
  char * d ;
  d = dest;
  for(;*dest++ = *src++;);
  return d;
}

 unsigned char cnt;
 void gen(void);
void start(void);
int scroll_up( int opt, int inc);
int scroll_down( int opt, int inc );



void interrupt()
{
     if(TMR0IF_bit)
     {  cnt++;
        TMR0IF_bit=0;


        if(cnt>=10)
        {
           led=~led;
           cnt=0;
        }

       TMR0=0;
     }

}
  //-----------------------scroll up ...........................................

     scroll_up(int opt ,int inc )
     {
       int j=1;
       inc++;


             if(opt==1)
             {
                 Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                 Lcd_Cmd(_LCD_CURSOR_OFF);
                 Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);

                        if(inc==0)
                         Lcd_Out(1,1,CopyConst2Ram(msg,gwt));

                          if(inc==1)
                         Lcd_Out(1,1,CopyConst2Ram(msg,sont));

                          if(inc==2)
                         Lcd_Out(1,1,CopyConst2Ram(msg,soft));
             } //end  if opt = 1

             if(opt==0)
             {
                Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                Lcd_Cmd(_LCD_CURSOR_OFF);
                Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);

             //..................inc=0...gen_wt...............................
                        if(inc==0)
                        {
                           x=gen_wt;
                           Lcd_out(2,1,x);
                           do
                           {
                               if((enter==1) && (reset==1) && (up==0) && (down==1) && j)//(Button(&PORTD,2,ms,0)&&j) // up pressed
                                 {   Delay_ms(ms); // wait for switch debounce
                                     x++;
                                     Lcd_out(2,1,x);
                                     Delay_ms(500);
                                 }// end if

                                                   if((enter==0) && (reset==1) && (up==1) && (down==1))//( Button(&PORTD,0,ms,0)) //Enter pressed to save
                                                    {   Delay_ms(ms); // wait for switch debounce
                                                       gen_wt=x;
                                                        Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                                                        Lcd_Cmd(_LCD_CURSOR_OFF);
                                                        Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                                                        Delay_ms(500);
                                                        Lcd_Out(1,1,CopyConst2Ram(msg,sav));
                                                        Delay_ms(1000);
                                                        j=0;
                                                     } // end saving

                        } while(j);
                  }// if inc=0
                  //....................inc = 1......self_ont..............
                   if(inc==1)
                        {
                           x=self_ont;
                           Lcd_out(2,1,x);
                           do
                           {
                               if((enter==1) && (reset==1) && (up==0) && (down==1) && j)//(Button(&PORTD,2,ms,0)&&j) // up pressed
                                 {     Delay_ms(ms); // wait for switch debounce
                                     x++;
                                     Lcd_out(2,1,x);
                                     Delay_ms(500);
                                 }// end if

                                                   if((enter==0) && (reset==1) && (up==1) && (down==1))//( Button(&PORTD,0,ms,0)) //Enter pressed to save
                                                    {    Delay_ms(ms); // wait for switch debounce
                                                        self_ont=x;
                                                        Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                                                        Lcd_Cmd(_LCD_CURSOR_OFF);
                                                        Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                                                        Delay_ms(500);
                                                        Lcd_Out(1,1,CopyConst2Ram(msg,sav));
                                                        Delay_ms(1000);
                                                        j=0;
                                                     } // end saving

                        } while(j);
                  }// if inc=1
                  //.................inc=2..........self_oft..................................
                   if(inc==2)
                        {
                           x=self_oft;
                           Lcd_out(2,1,x);
                           do
                           {
                               if((enter==1) && (reset==1) && (up==0) && (down==1) && j)//(Button(&PORTD,2,ms,0)&&j) // up pressed
                                 {   Delay_ms(ms); // wait for switch debounce
                                     x++;
                                     Lcd_out(2,1,x);
                                     Delay_ms(500);
                                 }// end if

                                                   if((enter==0) && (reset==1) && (up==1) && (down==1))//( Button(&PORTD,0,ms,0)) //Enter pressed to save
                                                    {    Delay_ms(ms); // wait for switch debounce
                                                       self_oft=x;
                                                        Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                                                        Lcd_Cmd(_LCD_CURSOR_OFF);
                                                        Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                                                        Delay_ms(500);
                                                        Lcd_Out(1,1,CopyConst2Ram(msg,sav));
                                                        Delay_ms(1000);
                                                        j=0;
                                                     } // end saving
                                                     
                                                     inc=0;

                        } while(j);
                  }// if inc=2
                  
                  
             } // if opt = 0

              return (inc);

     } // end scroll up

 //-----------------------scroll Down ...........................................

     scroll_down( int opt , int inc )
     {
       int j=1;
       inc--;


             if(opt==1)
             {
                 Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                 Lcd_Cmd(_LCD_CURSOR_OFF);
                 Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);

                        if(inc==0)
                         Lcd_Out(1,1,CopyConst2Ram(msg,gwt));

                         if(inc==1)
                         Lcd_Out(1,1,CopyConst2Ram(msg,sont));

                          if(inc==2)
                          
                         Lcd_Out(1,1,CopyConst2Ram(msg,soft));
             } //end  if opt = 1

             if(opt==0)
             {
                Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                Lcd_Cmd(_LCD_CURSOR_OFF);
                Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
               //..................inc=0....gen_wt......................
                        if(inc==0)
                        {
                           x=gen_wt;
                           Lcd_out(2,1,x);
                           do
                           {
                               if((enter==1) && (reset==1) && (up==1) && (down==0) && j)//(Button(&PORTD,2,ms,0)&&j) // down pressed
                                 {     Delay_ms(ms); // wait for switch debounce
                                     x--;
                                     Lcd_out(2,1,x);
                                     Delay_ms(500);
                                 }// end if

                                                   if((enter==0) && (reset==1) && (up==1) && (down==1))//( Button(&PORTD,0,ms,0)) //Enter pressed to save
                                                    {   Delay_ms(ms); // wait for switch debounce
                                                        gen_wt=x;
                                                        Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                                                        Lcd_Cmd(_LCD_CURSOR_OFF);
                                                        Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                                                        Delay_ms(500);
                                                        Lcd_Out(1,1,CopyConst2Ram(msg,sav));
                                                        Delay_ms(1000);
                                                        j=0;
                                                     } // end saving

                        } while(j);
                  }// if inc=0
                  
                    //....................inc = 1......self_ont..............
                   if(inc==1)
                        {
                           x=self_ont;
                           Lcd_out(2,1,x);
                           do
                           {
                               if((enter==1) && (reset==1) && (up==1) && (down==0) && j)//(Button(&PORTD,2,ms,0)&&j) // down pressed
                                 {    Delay_ms(ms); // wait for switch debounce
                                     x--;
                                     Lcd_out(2,1,x);
                                     Delay_ms(500);
                                 }// end if

                                                   if((enter==0) && (reset==1) && (up==1) && (down==1))//( Button(&PORTD,0,ms,0)) //Enter pressed to save
                                                    {   Delay_ms(ms); // wait for switch debounce
                                                        self_ont=x;
                                                        Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                                                        Lcd_Cmd(_LCD_CURSOR_OFF);
                                                        Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                                                        Delay_ms(500);
                                                        Lcd_Out(1,1,CopyConst2Ram(msg,sav));
                                                        Delay_ms(1000);
                                                        j=0;
                                                     } // end saving

                        } while(j);
                  }// if inc=1
                  //.................inc=2..........self_oft..................................
                   if(inc==2)
                        {
                           x=self_oft;
                           Lcd_out(2,1,x);
                           do
                           {
                               if((enter==1) && (reset==1) && (up==1) && (down==0) && j)//(Button(&PORTD,2,ms,0)&&j) // down pressed
                                 {     Delay_ms(ms); // wait for switch debounce
                                     x--;
                                     Lcd_out(2,1,x);
                                     Delay_ms(500);
                                 }// end if

                                                   if((enter==0) && (reset==1) && (up==1) && (down==1))//( Button(&PORTD,0,ms,0)) //Enter pressed to save
                                                    {   Delay_ms(ms); // wait for switch debounce
                                                       self_oft=x;
                                                        Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                                                        Lcd_Cmd(_LCD_CURSOR_OFF);
                                                        Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                                                        Delay_ms(500);
                                                        Lcd_Out(1,1,CopyConst2Ram(msg,sav));
                                                        Delay_ms(1000);
                                                        j=0;
                                                     } // end saving

                                                     inc=0;

                        } while(j);
                  }// if inc=2
                  
             } // if opt = 0

             return (inc);
     } // end scroll down
     
     
     
//..............generator routine...............................................
gen()
{
      int i;
     main_cont=0;
      ignition=1;
      Delay_ms(2000);
      chowk=1;
      Delay_ms(5000);//chowk on for 5 seconds
      chowk=0;
      for(i=0; i<3; i++)
      {
               if(mains==P)     //if Main resumes
             {
                 i=3;
                 gen_cont=0;
                 ignition=0;
                 self=0;
                 Delay_ms(2000);  //self on for 2 seconds
                 main_cont=1;
             }

               if(i==2)
               {
                   chowk=1;
                   Delay_ms(5000);//chowk on for 5 seconds
                   chowk=0;
               }

       self=1;
       Delay_ms(2000);  //self on for 2 seconds
       self=0;
       Delay_ms(4000);  //self off for 4 seconds




             if(genr==P)     //if generator ON
             {
                 i=3;
             }
      }

      if(genr==P && mains==A)     // if generator ON
      {
       Delay_ms(1000);
      Lcd_Cmd(_LCD_CLEAR);                    // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);
      Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
      Lcd_Out(1,1,CopyConst2Ram(msg,wu));   // Write text in first row
       Delay_ms(5000);
       gen_cont=1;      // transfer load on genset after warmup

      Delay_ms(300);

      Lcd_Cmd(_LCD_CLEAR);                    // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);
      Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
      Lcd_Out(1,1,CopyConst2Ram(msg,fv));   // Write text in first row
      Lcd_Out(2,1," . Hz     . V");
      // lcd_init();
      Delay_ms(2000);

      }
      else
      if(genr==A && mains==A)    // if generator is still off; i.e; can't start
      {
      Delay_ms(1000);

      Lcd_Cmd(_LCD_CLEAR);                    // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);
      Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
      Lcd_Out(1,2,CopyConst2Ram(msg,error));
      Lcd_Out(2,1,CopyConst2Ram(msg,f2s));   // Write text in first row

      ignition=0;        //Turn off the genset
      main_cont=0;
      while(mains==A && genr==A); //stuck here untill reset/restart the controller board (IF main and generator both are absent)
      Lcd_Cmd(_LCD_CLEAR);                    // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);

      }

      for(;mains==A;)   //untill main absent
      {

          if (mains==A)      // if main absent
            gen_cont=1;     // Turn on generator contactor
            main_cont=0;    // Turn off main contactor
            while(mains==A); // stay here until main resumes

         if (mains==P)       //if main presents or after main resumes
           {
             gen_cont=0;
             Delay_ms(1000);
             ignition=0; //Turn off the genset
             Delay_ms(2000);
             main_cont=1;
             Lcd_Cmd(_LCD_CLEAR);                    // Clear display
             Lcd_Out(1,3,CopyConst2Ram(msg,st));   // Write text in first row
             Lcd_Out(2,4,CopyConst2Ram(msg,pl));
           }
      }
}

void start()
 {

  //IRP_bit = 1;
 //STATUS.IRP=1;
 // asm BSF STATUS,RP1
 // asm BSF STATUS,RP0  // ACCESS BANK 2


  gen_cont=0;
      main_cont=0;
      Lcd_Cmd(_LCD_CLEAR);                    // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);
      Lcd_Out(1,1,CopyConst2Ram(msg,mok));   // Write text in first row
      Lcd_Out(2,1,CopyConst2Ram(msg,pw));    //please wait
      Delay_ms(3000);

      main_cont=1;
      Lcd_Cmd(_LCD_CLEAR);                    // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);
      Lcd_Out(1,1,CopyConst2Ram(msg,mok));   // Write text in first row
      Lcd_Out(2,1,CopyConst2Ram(msg,mol));    //mains overload
         Delay_ms(2000);

         Lcd_Cmd(_LCD_CLEAR);                    // Clear display
         Lcd_Cmd(_LCD_CURSOR_OFF);               // Cursor off
         Lcd_Out(1,3,CopyConst2Ram(msg,st));   // Write text in first row
         Lcd_Out(2,4,CopyConst2Ram(msg,pl));   //pvt ltd
 }


void main()
{
    unsigned int i=0, itrue=1;  //i=loops ; x=passward ; J=While lock

 //..............INITIALIZATIONS............................................

/*   ANSEL  = 0;                        // Configure AN pins as digital I/O
  ANSELH = 0;
  C1ON_bit = 0;                      // Disable comparators
  C2ON_bit = 0;     */
  // IRP_bit = 0;

  // CONFIGURING SFRs:
  cnt=0;
  TMR0=0;   // loading vlaue of timer0 interrupt
  OPTION_REG=0x87;  // for timer 0; presscaller 1; 256
  ADCON1=0x06;   // adc reg initialization
  INTCON=0xA0;  // for timer0  ; interrupt reg initilization
 // STATUS.F7=0;
  //STATUS.IRP=0;
  //asm BCF STATUS,IRP


  // OUTPUTS:
  TRISB.f2 = 0x00;   //led out put
  TRISE.F2=0;        // gen cont output
  TRISC=0x00;        // portC output For LCD
  PORTC=0X00;        //  initilization port c

  // INPUTS:
 /* TRISD.F0=0xff;    // ENTER
  TRISD.F1=0xff;  // RESET
  TRISD.F2=0xff;    // UP
  TRISD.F3=0xff;   // DOWN   */
  TRISA = 0xff;     // port A as input
  TRISD = 0xff;     // port D as input

    // TRISD.F4=0XFF;      ALREADY DIRECTED AT LCD INITILIZATION
    //  TRISD.F5=0XFF;
 //................................................................................


  lcd_init();
  Delay_ms(500);


    if(mains==P)    // if main is Present.
    {
       start();

    }

  while(1)
  {



                    if ((enter==0) && (reset==1) && (up==1) && (down==1))// (Button(&PORTD,0,ms,0))  //when Enter is pressed
                     {
                          Delay_ms(ms); // wait for switch debounce
                          itrue=1;
                          x=0;
                          Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                          Lcd_Cmd(_LCD_CURSOR_OFF);
                          Lcd_Out(1,1,CopyConst2Ram(msg,ep));   //enter passward
                          delay_ms(t);
                             while ((enter==0) && (reset==1) && (up==1) && (down==1))//(Button(&PORTD,0,ms,0)) // press enter
                             {   Delay_ms(ms); // wait for switch debounce
                                 x++;
                               Lcd_Out(2,x,CopyConst2Ram(msg,asteric));  //*

                                 if(x==4)
                                 {
                                   Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                                   Lcd_Cmd(_LCD_CURSOR_OFF);
                                   Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                                   Lcd_Out(1,1,CopyConst2Ram(msg,pw));   //please wait
                                   Delay_ms(1000);

                                   Lcd_Cmd(_LCD_CLEAR);
                                   Lcd_Out(1,1,CopyConst2Ram(msg,gwt));  // Generator warm up time

                                   inc=0;  //for selection of variable
                                   itrue=1;   // Entring and Exiting main menu
                                   opt=1;    // option b/w selection number or variable

                                             while(itrue)
                                             {
                                                  if ((enter==0) && (reset==1) && (up==1) && (down==1))//(Button(&PORTD,0,ms,0))  // pressing enter to enter time
                                                  {
                                                   Delay_ms(ms); // wait for switch debounce
                                                   opt=0;
                                                   Lcd_Out(2,1,CopyConst2Ram(msg,sp));
                                                  }


                                                  if ((enter==1) && (reset==0) && (up==1) && (down==1))//(Button(&PORTD,1,ms,0)) //press reset to exit
                                                  {  Delay_ms(ms); // wait for switch debounce
                                                     itrue=0;
                                                  }
                                                  if ((enter==1) && (reset==1) && (up==0) && (down==1))//(Button(&PORTD,2,ms,0))  //up
                                                  {   Delay_ms(ms); // wait for switch debounce
                                                      inc=scroll_up(opt,inc);
                                                  }

                                                  if ((enter==1) && (reset==1) && (up==1) && (down==0))//(Button(&PORTD,3,ms,0))   // down
                                                   {
                                                      Delay_ms(ms); // wait for switch debounce
                                                      inc=scroll_down(opt,inc);
                                                   }
                                             }

                                              x++;
                                 }   //if
                             }    //while
                          Delay_ms(1000);
                     }



    if(mains==A)    // if main is absent.
    {
     Delay_ms(4000); // wait for 4 seconds to ensure power failure
    if(mains==P)     //check again
      continue;

     //Delay_ms(1000);
     Lcd_Cmd(_LCD_CLEAR);                    // Clear display
     Lcd_Cmd(_LCD_CURSOR_OFF);
     Lcd_Out(1,1,CopyConst2Ram(msg,mna));    //mains not available
     Lcd_Out(2,1,CopyConst2Ram(msg,sg));     //starting genset
     Delay_ms(1000);
     gen();

    }
    else           // if main is present .........
    {

      Lcd_Cmd(_LCD_CLEAR);                    // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);
      Lcd_Out(1,1,CopyConst2Ram(msg,mok));   // Write text in first row
      Lcd_Out(2,1,CopyConst2Ram(msg,mol));    // mains overload
      Delay_ms(2000);
     //  gen_cont=0;   //Turn off generator contactor
       //main_cont=1;   //Turn on main contactor
      Lcd_Cmd(_LCD_CLEAR);                    // Clear display
      Lcd_Out(1,3,CopyConst2Ram(msg,st));   // Write text in first row
      Lcd_Out(2,4,CopyConst2Ram(msg,pl));     // pvt ltd
      Delay_ms(1000);
    }



              for(i=0; i<96; i++)
              {
                       Lcd_Cmd(_LCD_SHIFT_RIGHT);
                       Delay_ms(t);
                        if (enter==0)
                            i=96;
                        
                        if(mains==A)
                        {
                          i=96;
                        }
              }


   }    // while

}      // main
//..............................;;;;;;;.........................................
 
Last edited:

Which UC are you using? PIC or ATMEGA? The code you have written is for ATMEGA

To save the number you have to use eeprom. You have to write to eeprom and read from eeprom on start.

remove this

Code:
//========== Start Button connections =======================================
/*unsigned short Button(unsigned short *PORTD, unsigned short  , unsigned short , unsigned short );
sbit Button_Pin           at PIND.F0;   // Input pin, PINx register is used //RD0_bit;
sbit Button_Pin_Direction at DDRD.F0;
sbit Button_Pin           at PIND.F1; // RD1_bit;
sbit Button_Pin_Direction at DDRD.F1;
sbit Button_Pin           at PIND.F2;   //  RD2_bit;
sbit Button_Pin_Direction at DDRD.F2;
sbit Button_Pin           at PIND.F3;  // RD3_bit;
sbit Button_Pin_Direction at DDRD.F3;    */
//=============== End Button connections ====================================

-----------------update--------------

Why have you complicated it so much. I just can't understand your code.

Check this link for using eeprom. http://www.mikroe.com/forum/viewtopic.php?f=147&t=48450

How are you entering password. There is no keypad.
 
Last edited:

Which UC are you using? PIC or ATMEGA? The code you have written is for ATMEGA

To save the number you have to use eeprom. You have to write to eeprom and read from eeprom on start.

remove this

Code:
//========== Start Button connections =======================================
/*unsigned short Button(unsigned short *PORTD, unsigned short  , unsigned short , unsigned short );
sbit Button_Pin           at PIND.F0;   // Input pin, PINx register is used //RD0_bit;
sbit Button_Pin_Direction at DDRD.F0;
sbit Button_Pin           at PIND.F1; // RD1_bit;
sbit Button_Pin_Direction at DDRD.F1;
sbit Button_Pin           at PIND.F2;   //  RD2_bit;
sbit Button_Pin_Direction at DDRD.F2;
sbit Button_Pin           at PIND.F3;  // RD3_bit;
sbit Button_Pin_Direction at DDRD.F3;    */
//=============== End Button connections ====================================

-----------------update--------------

Why have you complicated it so much. I just can't understand your code.

Check this link for using eeprom. http://www.mikroe.com/forum/viewtopic.php?f=147&t=48450

How are you entering password. There is no keypad.

i am usign pic.... the coad is commented so it is removed.

passward is just a number of astarics(*)................. its not a specific number.l
 

i am usign pic.... the coad is commented so it is removed.

passward is just a number of astarics(*)................. its not a specific number.l

If you tell me what exactly the scroll up and scroll down functions do. I can try to code that into the buttons.
 

If you tell me what exactly the scroll up and scroll down functions do. I can try to code that into the buttons.

let me explain you the whole situation in less words.... :)

it is autometic transfer switch and its work is to start the Generator when there is load shedding.... now question rises here is how to start the generator engine.... so the engine is started in such a way that first ats will ON the ignition of the engine by suppling it 12V from its battery and then after an appropriate time it will supply the self to start the engine. there is also the chowk for engine. i think you know better about how engine works. now i need to store the values in the eeprom of the controller for these delays....
i.e; self on time.. self off time... generator warm up time ..... chowk on time..... etc..
now scroll has two functions to perform.. that is to scroll these choices and then after pressing enter to select one of all choices and then again scroll up or down to set appropriate delay for that function.

hope you understand the whole situation.....


please try to under stand my above posted code once again...
 

Did you check the eeprom code. That is what you need to store integer variables.

Code:
void EEPROM_Write(unsigned short eeprom_internal_address, unsigned long int eeprom_integer_value);
void EEPROM_Read(unsigned short eeprom_internal_address);

void EEPROMWriteInt(unsigned short eeprom_internal_address, unsigned long int eeprom_integer_value) {
        lowByte = Lo(eeprom_integer_value);
        highByte = Hi(eeprom_integer_value);
        higherByte = Higher(eeprom_integer_value);
        highestByte = Highest(eeprom_integer_value);

        EEPROM_write(eeprom_internal_address, lowByte);
        eeprom_internal_address++;
        delay_ms(20);
        EEPROM_write(eeprom_internal_address, highByte);
        eeprom_internal_address++;
        delay_ms(20);
        EEPROM_write(eeprom_internal_address, higherByte);
        eeprom_internal_address++;
        delay_ms(20);
        EEPROM_write(eeprom_internal_address, highestByte);
        eeprom_internal_address++;
        delay_ms(20);

        lowByte = 0x00;
        highByte = 0x00;
        higherByte = 0x00;
        highestByte = 0x00;

}

unsigned long int EEPROMReadInt(unsigned short eeprom_internal_address)
        {
        Lo(eeprom_integer_value) = EEPROM_read(eeprom_internal_address);
        eeprom_internal_address++;
        delay_ms(20);
        Hi(eeprom_integer_value) = EEPROM_read(eeprom_internal_address);
        eeprom_internal_address++;
        delay_ms(20);
        Higher(eeprom_integer_value) = EEPROM_read(eeprom_internal_address);
        eeprom_internal_address++;
        delay_ms(20);
        Highest(eeprom_integer_value) = EEPROM_read(eeprom_internal_address);
        eeprom_internal_address++;
        delay_ms(20);

        return eeprom_integer_value;

}

unsigned short eeprom_intenal_address, eeprom_external_address, lowByte, highByte, higherByte, highestByte;
unsigned long int eeprom_integer_value, SelfONTime, SelfOFFTime, GenWarmUpTime, ChowkONTime;

void main() {

    
     	unsigned int counter;
     	unsigned int btn_pressed;
	char string[7];


	while(1) {
		if((ENTER == 0) && (UP == 1) && (DOWN == 1) && (RESET == 1) && (btn_pressed == 0)) {
              Delay_ms(150);
              if((ENTER == 0) && (UP == 1) && (DOWN == 1) && (RESET == 1) && (btn_pressed == 0)) {
                 Lcd_Cmd(_LCD_CLEAR);
                 Lcd_Out(2,1,"ENTER Pressed");
                 Delay_ms(1000);
                 Lcd_Cmd(_LCD_CLEAR);
                 IntToStr(counter, string);
                 Lcd_Out(1,2,"Press UP/DOWN");
                 Lcd_Out(2,10,string);
                 btn_pressed = 0;
              }
            }

            if((ENTER == 0) && (UP == 1) && (DOWN == 1) && (RESET == 1) && (btn_pressed == 1)) {
              Delay_ms(150);
              if((ENTER == 0) && (UP == 1) && (DOWN == 1) && (RESET == 1) && (btn_pressed == 1)) {
                 Lcd_Cmd(_LCD_CLEAR);
                 Lcd_Out(2,1,"Saving...");
                 Delay_ms(1000);
                 Lcd_Cmd(_LCD_CLEAR);
                 btn_pressed = 0;
              }
            }

            if((ENTER == 1) && (UP == 0) && (DOWN == 1) && (RESET == 1)) {
              Delay_ms(150);
              if((ENTER == 1) && (UP == 0) && (DOWN == 1) && (RESET == 1)) {
                  counter = counter + 1;
                  Lcd_Cmd(_LCD_CLEAR);
                  IntToStr(counter, string);
                  Lcd_Out(1,2,"Press UP/DOWN");
                  Lcd_Out(2,10,string);
                  btn_pressed = 1;

              }
            }

            if((ENTER == 1) && (UP == 1) && (DOWN == 0) && (RESET == 1)) {
              Delay_ms(150);
              if((ENTER == 1) && (UP == 1) && (DOWN == 0) && (RESET == 1)) {
                  counter = counter - 1;
                  Lcd_Cmd(_LCD_CLEAR);
                  IntToStr(counter, string);
                  Lcd_Out(1,2,"Press UP/DOWN");
                  Lcd_Out(2,10,string);
                  btn_pressed = 1;
              }
            }

            if((ENTER == 1) && (UP == 1) && (DOWN == 1) && (RESET == 0)) {
              Delay_ms(150);
              if((ENTER == 1) && (UP == 1) && (DOWN == 1) && (RESET == 0)) {
                   Lcd_Cmd(_LCD_CLEAR);
                   Lcd_Out(2,1,"RESET Pressed");
                   counter = 0;
                   btn_pressed = 0;
              }
            }

	    
	    SelfONTime = 9;
            SelfOFFTime = 5;
            GenWarmUpTime = 20;
            ChowkONTime = 45;
            
            eeprom_external_address = 0x00
            eeprom_integer_value = 0;
            EEPROMWriteInt(eeprom_external_address, SelfONTime);
            eeprom_external_address = eeprom_external_address + 4;
            EEPROMWriteInt(eeprom_external_address, SelfOFFTime);
            eeprom_external_address = eeprom_external_address + 4;
            EEPROMWriteInt(eeprom_external_address, GenWarmUpTime);
            eeprom_external_address = eeprom_external_address + 4;
            EEPROMWriteInt(eeprom_external_address, ChowkONTime);
            eeprom_external_address = eeprom_external_address + 4;

            SelfONTime = 0;
            SelfOFFTime = 0;
            GenWarmUpTime = 0;
            ChowkONTime = 0;

            eeprom_external_address = 0x00;
            SelfONTime = EEPROMReadInt(eeprom_external_address);
            eeprom_external_address = eeprom_external_address + 4;
            SelfOFFTime = EEPROMReadInt(eeprom_external_address);
            eeprom_external_address = eeprom_external_address + 4;
            GenWarmUpTime = EEPROMReadInt(eeprom_external_address);
            eeprom_external_address = eeprom_external_address + 4;
            ChowkONTime = 0; = EEPROMReadInt(eeprom_external_address);
            eeprom_external_address = eeprom_external_address + 4;
	}

This code is for eeprom. It assigns some values to variables. Then writes the variables to eeprom. After that clears the variables. Then again reads the eeprom data and assigns to the variables. You put the codes at the proper place.

-----------update--------------

Code:
// OUTPUTS
#define GEN_CONT PORTE.F2
#define LED PORTB.F2
#define MAIN_CONT PORTC.F0
#define CHOWK PORTC.F1
#define IGNITION PORTC.F2
#define SELF PORTC.F3

// INPUTS
#define GEN PORTA.F0
#define MAIN PORTA.F1

// SWITCHES
#define ENTER PORTD.F0
#define RESET PORTD.F1
#define UP PORTD.F2
#define DOWN PORTD.F3

//CONSTANTS
#define T 100   //DELAYS
#define P 1     //FOR MAIN AND GEN INPUTS
#define A 0     //FOR MAIN AND GEN INPUTS

sbit LCD_RS at RD5_bit;
sbit LCD_EN at RD4_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D7 at RC7_bit;

sbit LCD_RS_Direction at TRISD5_bit;
sbit LCD_EN_Direction at TRISD4_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D7_Direction at TRISC7_bit;

void EEPROM_Write(unsigned short eeprom_internal_address, unsigned long int eeprom_integer_value);
void EEPROM_Read(unsigned short eeprom_internal_address);

void EEPROMWriteInt(unsigned short eeprom_internal_address, unsigned long int eeprom_integer_value) {
        lowByte = Lo(eeprom_integer_value);
        highByte = Hi(eeprom_integer_value);
        higherByte = Higher(eeprom_integer_value);
        highestByte = Highest(eeprom_integer_value);

        EEPROM_write(eeprom_internal_address, lowByte);
        eeprom_internal_address++;
        delay_ms(20);
        EEPROM_write(eeprom_internal_address, highByte);
        eeprom_internal_address++;
        delay_ms(20);
        EEPROM_write(eeprom_internal_address, higherByte);
        eeprom_internal_address++;
        delay_ms(20);
        EEPROM_write(eeprom_internal_address, highestByte);
        eeprom_internal_address++;
        delay_ms(20);

        lowByte = 0x00;
        highByte = 0x00;
        higherByte = 0x00;
        highestByte = 0x00;

}

unsigned long int EEPROMReadInt(unsigned short eeprom_internal_address)
        {
        Lo(eeprom_integer_value) = EEPROM_read(eeprom_internal_address);
        eeprom_internal_address++;
        delay_ms(20);
        Hi(eeprom_integer_value) = EEPROM_read(eeprom_internal_address);
        eeprom_internal_address++;
        delay_ms(20);
        Higher(eeprom_integer_value) = EEPROM_read(eeprom_internal_address);
        eeprom_internal_address++;
        delay_ms(20);
        Highest(eeprom_integer_value) = EEPROM_read(eeprom_internal_address);
        eeprom_internal_address++;
        delay_ms(20);

        return eeprom_integer_value;

}

unsigned short eeprom_intenal_address, eeprom_external_address, lowByte, highByte, higherByte, highestByte;
unsigned long int eeprom_integer_value, SelfONTime, SelfOFFTime, GenWarmUpTime, ChowkONTime;

void main() {

         eeprom_external_address = 0x00;
         SelfONTime = EEPROMReadInt(eeprom_external_address);
         eeprom_external_address = eeprom_external_address + 4;

         while(1) {
         
                if((ENTER == 0) && (UP == 1) && (DOWN == 1) && (RESET == 1) && (btn_pressed == 0)) {
              Delay_ms(150);
              if((ENTER == 0) && (UP == 1) && (DOWN == 1) && (RESET == 1) && (btn_pressed == 0)) {
                 Lcd_Cmd(_LCD_CLEAR);
                 Lcd_Out(1,1,"SELECT AND:);
                 Lcd_Out(2,1,"SET VALUE");
                 Delay_ms(2000);
                 Lcd_Cmd(_LCD_CLEAR);
                 IntToStr(counter, string);
                 Lcd_Out(1,2,"Press UP/DOWN");
                 Lcd_Out(2,10,string);
                 btn_pressed = 0;
              }
            }

            if((ENTER == 0) && (UP == 1) && (DOWN == 1) && (RESET == 1) && (btn_pressed == 1)) {
              Delay_ms(150);
              if((ENTER == 0) && (UP == 1) && (DOWN == 1) && (RESET == 1) && (btn_pressed == 1)) {
                 Lcd_Cmd(_LCD_CLEAR);
                 Lcd_Out(2,1,"Saving...");
                 Delay_ms(1000);
                 Lcd_Cmd(_LCD_CLEAR);
                 btn_pressed = 0;
                 
                 SelfONTime = counter;
                 
                 eeprom_external_address = 0x00
                 eeprom_integer_value = 0;
                 EEPROMWriteInt(eeprom_external_address, SelfONTime);
                 eeprom_external_address = eeprom_external_address + 4;

              }
            }

            if((ENTER == 1) && (UP == 0) && (DOWN == 1) && (RESET == 1)) {
              Delay_ms(150);
              if((ENTER == 1) && (UP == 0) && (DOWN == 1) && (RESET == 1)) {
                  counter = counter + 1;
                  Lcd_Cmd(_LCD_CLEAR);
                  IntToStr(counter, string);
                  Lcd_Out(1,2,"Press UP/DOWN");
                  Lcd_Out(2,10,string);
                  btn_pressed = 1;

              }
            }

            if((ENTER == 1) && (UP == 1) && (DOWN == 0) && (RESET == 1)) {
              Delay_ms(150);
              if((ENTER == 1) && (UP == 1) && (DOWN == 0) && (RESET == 1)) {
                  counter = counter - 1;
                  Lcd_Cmd(_LCD_CLEAR);
                  IntToStr(counter, string);
                  Lcd_Out(1,2,"Press UP/DOWN");
                  Lcd_Out(2,10,string);
                  btn_pressed = 1;
              }
            }

            if((ENTER == 1) && (UP == 1) && (DOWN == 1) && (RESET == 0)) {
              Delay_ms(150);
              if((ENTER == 1) && (UP == 1) && (DOWN == 1) && (RESET == 0)) {
                   Lcd_Cmd(_LCD_CLEAR);
                   Lcd_Out(2,1,"RESET Pressed");
                   counter = 0;
                   btn_pressed = 0;
              }
            }
         }
}

You have to code a menu system, which gives option to select, SelfONTime, SelfOFFTime, etc., After selecting the variable, its value should be incremented or decremented. After that if ENTER is pressed, the value should be saved to eeprom.

----------------update----------------------

Code:
// OUTPUTS
#define GEN_CONT PORTE.F2
#define LED PORTB.F2
#define MAIN_CONT PORTC.F0
#define CHOWK PORTC.F1
#define IGNITION PORTC.F2
#define SELF PORTC.F3

// INPUTS
#define GEN PORTA.F0
#define MAIN PORTA.F1

// SWITCHES
#define ENTER PORTD.F0
#define RESET PORTD.F1
#define UP PORTD.F2
#define DOWN PORTD.F3

//CONSTANTS
#define T 100   //DELAYS
#define P 1     //FOR MAIN AND GEN INPUTS
#define A 0     //FOR MAIN AND GEN INPUTS

sbit LCD_RS at RD5_bit;
sbit LCD_EN at RD4_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D7 at RC7_bit;

sbit LCD_RS_Direction at TRISD5_bit;
sbit LCD_EN_Direction at TRISD4_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D7_Direction at TRISC7_bit;

#define Lo(param) ((char *)&param)[0]
#define Hi(param) ((char *)&param)[1]
#define Higher(param) ((char *)&param)[2]
#define Highest(param) ((char *)&param)[3]


void EEPROM_Write(unsigned short eeprom_internal_address, unsigned long int eeprom_integer_value);
void EEPROM_Read(unsigned short eeprom_internal_address);

unsigned short eeprom_intenal_address, eeprom_external_address, lowByte, highByte, higherByte, highestByte;
unsigned long int eeprom_integer_value, SelfONTime, SelfOFFTime, GenWarmUpTime, ChowkONTime;
unsigned int varcount = 0;

void EEPROMWriteInt(unsigned short eeprom_internal_address, unsigned long int eeprom_integer_value) {
        lowByte = Lo(eeprom_integer_value);
        highByte = Hi(eeprom_integer_value);
        higherByte = Higher(eeprom_integer_value);
        highestByte = Highest(eeprom_integer_value);

        EEPROM_write(eeprom_internal_address, lowByte);
        eeprom_internal_address++;
        delay_ms(20);
        EEPROM_write(eeprom_internal_address, highByte);
        eeprom_internal_address++;
        delay_ms(20);
        EEPROM_write(eeprom_internal_address, higherByte);
        eeprom_internal_address++;
        delay_ms(20);
        EEPROM_write(eeprom_internal_address, highestByte);
        eeprom_internal_address++;
        delay_ms(20);

        lowByte = 0x00;
        highByte = 0x00;
        higherByte = 0x00;
        highestByte = 0x00;

}

unsigned long int EEPROMReadInt(unsigned short eeprom_internal_address)
        {
        Lo(eeprom_integer_value) = EEPROM_read(eeprom_internal_address);
        eeprom_internal_address++;
        delay_ms(20);
        Hi(eeprom_integer_value) = EEPROM_read(eeprom_internal_address);
        eeprom_internal_address++;
        delay_ms(20);
        Higher(eeprom_integer_value) = EEPROM_read(eeprom_internal_address);
        eeprom_internal_address++;
        delay_ms(20);
        Highest(eeprom_integer_value) = EEPROM_read(eeprom_internal_address);
        eeprom_internal_address++;
        delay_ms(20);

        return eeprom_integer_value;

}

void main() {
         unsigned int btn_pressed, counter;
         char string[7];
         
         btn_pressed = 0;
         
         Lcd_Init();
         Lcd_Cmd(_LCD_CLEAR);
         Lcd_Cmd(_LCD_CURSOR_OFF);
         
         /*
         eeprom_external_address = 0x00;
         SelfONTime = EEPROMReadInt(eeprom_external_address);
         eeprom_external_address = eeprom_external_address + 4;
         */
         
         while(1) {
         
              if((ENTER == 0) && (UP == 1) && (DOWN == 1) && (RESET == 1) && (btn_pressed == 0)) {
              Delay_ms(150);
              if((ENTER == 0) && (UP == 1) && (DOWN == 1) && (RESET == 1) && (btn_pressed == 0)) {
                 Lcd_Cmd(_LCD_CLEAR);
                 Lcd_Out(1,1,"SELECT AND");
                 Lcd_Out(2,1,"SET VALUE");
                 Delay_ms(2000);
                 Lcd_Cmd(_LCD_CLEAR);
                 IntToStr(counter, string);
                 Lcd_Out(1,2,"Press UP/DOWN");
                 Lcd_Out(2,10,string);
                 btn_pressed = 0;
              }
            }

              if((ENTER == 0) && (UP == 1) && (DOWN == 1) && (RESET == 1) && (btn_pressed == 1)) {
              Delay_ms(150);
                if((ENTER == 0) && (UP == 1) && (DOWN == 1) && (RESET == 1) && (btn_pressed == 1)) {
                 Lcd_Cmd(_LCD_CLEAR);
                 Lcd_Out(2,1,"Saving...");
                 Delay_ms(1000);
                 Lcd_Cmd(_LCD_CLEAR);
                 btn_pressed = 0;
                 
                 if(varcount == 0) {
                        SelfONTime = counter;
                        eeprom_external_address = 0x00;
                        eeprom_integer_value = 0;
                        EEPROMWriteInt(eeprom_external_address, SelfONTime);
                        eeprom_external_address = eeprom_external_address + 4;
                 }
                 else if(varcount == 1) {
                        SelfOFFTime = counter;
                        eeprom_external_address = 0x00;
                        eeprom_integer_value = 0;
                        EEPROMWriteInt(eeprom_external_address, SelfONTime);
                        eeprom_external_address = eeprom_external_address + 4;
                 }
                 else if(varcount == 2) {
                        GenWarmUpTime = counter;
                        eeprom_external_address = 0x00;
                        eeprom_integer_value = 0;
                        EEPROMWriteInt(eeprom_external_address, SelfONTime);
                        eeprom_external_address = eeprom_external_address + 4;
                 }
                 else if(varcount == 3) {
                        ChowkONTime = counter;
                        eeprom_external_address = 0x00;
                        eeprom_integer_value = 0;
                        EEPROMWriteInt(eeprom_external_address, SelfONTime);
                        eeprom_external_address = eeprom_external_address + 4;
                 }
              }
            }

            if((ENTER == 1) && (UP == 0) && (DOWN == 1) && (RESET == 1)) {
              Delay_ms(150);
              if((ENTER == 1) && (UP == 0) && (DOWN == 1) && (RESET == 1)) {
                  counter = counter + 1;
                  Lcd_Cmd(_LCD_CLEAR);
                  IntToStr(counter, string);
                  btn_pressed = 1;

                  if(varcount > 2) {
                       varcount = 0;
                  }
                  
                  if(varcount == 0) {
                         Lcd_Out(1,2,"Self ON Time");
                         Lcd_Out(2,1,"Self OFF Time");
                  }
                  else if(varcount == 1) {
                         Lcd_Out(1,2,"Self OFF Time");
                         Lcd_Out(2,1,"Gen Warm Up Time");
                  }
                  else if(varcount == 2) {
                         Lcd_Out(1,2,"Gen Warm Up Time");
                         Lcd_Out(2,1,"Chowk ON Time");
                  }
                  varcount = varcount + 1;

              }
            }

            if((ENTER == 1) && (UP == 1) && (DOWN == 0) && (RESET == 1)) {
              Delay_ms(150);
              if((ENTER == 1) && (UP == 1) && (DOWN == 0) && (RESET == 1)) {
                  counter = counter - 1;
                  Lcd_Cmd(_LCD_CLEAR);
                  IntToStr(counter, string);
                  btn_pressed = 1;
                  
                  varcount = varcount - 1;
                  
                  if(varcount < 0) {
                       varcount = 2;
                  }

                  if(varcount == 0) {
                         Lcd_Out(1,2,"Self ON Time");
                         Lcd_Out(2,1,"Self OFF Time");
                  }
                  else if(varcount == 1) {
                         Lcd_Out(1,2,"Self OFF Time");
                         Lcd_Out(2,1,"Gen Warm Up Time");
                  }
                  else if(varcount == 2) {
                         Lcd_Out(1,2,"Gen Warm Up Time");
                         Lcd_Out(2,1,"Chowk ON Time");
                  }
              }
            }

            if((ENTER == 1) && (UP == 1) && (DOWN == 1) && (RESET == 0)) {
              Delay_ms(150);
              if((ENTER == 1) && (UP == 1) && (DOWN == 1) && (RESET == 0)) {
                   Lcd_Cmd(_LCD_CLEAR);
                   Lcd_Out(2,1,"RESET Pressed");
                   counter = 0;
                   btn_pressed = 0;
              }
            }
         }
}
 
Last edited:

thank you internetuser2k12. i will check your above given code soon. but my program cade is highly frustating me... every thing is seems to be true as i decleared portD as input but still the input buttons are not working with my code ... i can't understand where i am going wrong.................
plz check my code :

Code:
// OUTPUTS
#define gen_cont PORTE.f2
#define led PORTB.f2
#define main_cont PORTC.F0
#define chowk PORTC.F1
#define ignition PORTC.F2
#define self PORTC.F3

// INPUTS
#define genr PORTA.F0
#define mains PORTA.F1

#define enter PORTD.F0
#define reset PORTD.F1
#define up PORTD.F2
#define down PORTD.F3

//CONSTANTS
#define t 100   //DELAYS
#define P 1     //FOR MAIN AND gen INPUTS
#define A 0     //FOR MAIN AND gen INPUTS
#define ms 150   // for button debounce
//#define pressed 0 // to detect the button press
//#define unpressed 1 // to detect the button unpress
//============Start LCD Connections========================================
sbit LCD_RS at RD5_bit;
sbit LCD_EN at RD4_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D7 at RC7_bit;

sbit LCD_RS_Direction at TRISD5_bit;
sbit LCD_EN_Direction at TRISD4_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D7_Direction at TRISC7_bit;
//=======End LCD Connections=============================================

//========== Start Button connections =======================================
/*unsigned short Button(unsigned short *PORTD, unsigned short  , unsigned short , unsigned short );
sbit Button_Pin           at PIND.F0;   // Input pin, PINx register is used //RD0_bit;
sbit Button_Pin_Direction at DDRD.F0;
sbit Button_Pin           at PIND.F1; // RD1_bit;
sbit Button_Pin_Direction at DDRD.F1;                  THIS IS 4 AVR CONTROLLERS
sbit Button_Pin           at PIND.F3;  // RD3_bit;
sbit Button_Pin_Direction at DDRD.F3;    */
//=============== End Button connections ====================================

//============= START RAM SAVER ===========================================
const char wu[] = "Warming up.....";
const char fv[] = "Frequency  Volts";
const char error[] = "< ERROR >";
const char f2s[] = "Fails to start";
const char st[] = "Shenztech";
const char pl[] = "Pvt Ltd";
const char mna[] = "Mains not avail";
const char sg[] = "Starting Genset";
const char mok[] = "Mains OK..";
const char mol[] = "Mains Overload";
const char pw[] = "Please Wait...";
const char gwt[] = "Gen Warm up time";
const char sont[] = "Self ON Time";
const char soft[] = "Self OFF time";
const char ep[] = "Enter Passward";
const char asteric[] = "*";
const char sp[] = "scroll please";
const char sav[] = "Saving.......";
//============= END RAM SAVER ===========================================
 int x=0, inc=0, opt=1, gen_wt=10, self_ont=2, self_oft=5;
char msg[17]; //declare array set 16 bytes of RAM aside for copying into
 // copy const to ram string
char  *CopyConst2Ram(char * dest, const char * src)
{
  char * d ;
  d = dest;
  for(;*dest++ = *src++;);
  return d;
}

 unsigned char cnt;
 void gen(void);
void start(void);
int scroll_up( int opt, int inc);
int scroll_down( int opt, int inc );



void interrupt()
{
     if(TMR0IF_bit)
     {  cnt++;
        TMR0IF_bit=0;


        if(cnt>=10)
        {
           led=~led;
           cnt=0;
        }

       TMR0=0;
     }

}
  //-----------------------scroll up ...........................................

     scroll_up(int opt ,int inc )
     {
       int j=1;
       inc++;


             if(opt==1)
             {
                 Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                 Lcd_Cmd(_LCD_CURSOR_OFF);
                 Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);

                        if(inc==0)
                         Lcd_Out(1,1,CopyConst2Ram(msg,gwt));

                          if(inc==1)
                         Lcd_Out(1,1,CopyConst2Ram(msg,sont));

                          if(inc==2)
                         Lcd_Out(1,1,CopyConst2Ram(msg,soft));
             } //end  if opt = 1

             if(opt==0)
             {
                Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                Lcd_Cmd(_LCD_CURSOR_OFF);
                Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);

             //..................inc=0...gen_wt...............................
                        if(inc==0)
                        {
                           x=gen_wt;
                           Lcd_out(2,1,x);
                           do
                           {
                               if((enter==1) && (reset==1) && (up==0) && (down==1) && j==1)//(Button(&PORTD,2,ms,0)&&j) // up pressed
                                 {   Delay_ms(ms); // wait for switch debounce
                                     x++;
                                     Lcd_out(2,1,x);
                                     Delay_ms(500);
                                 }// end if

                                                   if((enter==0) && (reset==1) && (up==1) && (down==1))//( Button(&PORTD,0,ms,0)) //Enter pressed to save
                                                    {   Delay_ms(ms); // wait for switch debounce
                                                       gen_wt=x;
                                                        Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                                                        Lcd_Cmd(_LCD_CURSOR_OFF);
                                                        Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                                                        Delay_ms(500);
                                                        Lcd_Out(1,1,CopyConst2Ram(msg,sav));
                                                        Delay_ms(1000);
                                                        j=0;
                                                     } // end saving

                        } while(j);
                  }// if inc=0
                  //....................inc = 1......self_ont..............
                   if(inc==1)
                        {
                           x=self_ont;
                           Lcd_out(2,1,x);
                           do
                           {
                               if((enter==1) && (reset==1) && (up==0) && (down==1) && j==1)//(Button(&PORTD,2,ms,0)&&j) // up pressed
                                 {     Delay_ms(ms); // wait for switch debounce
                                     x++;
                                     Lcd_out(2,1,x);
                                     Delay_ms(500);
                                 }// end if

                                                   if((enter==0) && (reset==1) && (up==1) && (down==1))//( Button(&PORTD,0,ms,0)) //Enter pressed to save
                                                    {    Delay_ms(ms); // wait for switch debounce
                                                        self_ont=x;
                                                        Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                                                        Lcd_Cmd(_LCD_CURSOR_OFF);
                                                        Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                                                        Delay_ms(500);
                                                        Lcd_Out(1,1,CopyConst2Ram(msg,sav));
                                                        Delay_ms(1000);
                                                        j=0;
                                                     } // end saving

                        } while(j);
                  }// if inc=1
                  //.................inc=2..........self_oft..................................
                   if(inc==2)
                        {
                           x=self_oft;
                           Lcd_out(2,1,x);
                           do
                           {
                               if((enter==1) && (reset==1) && (up==0) && (down==1) && j==1)//(Button(&PORTD,2,ms,0)&&j) // up pressed
                                 {   Delay_ms(ms); // wait for switch debounce
                                     x++;
                                     Lcd_out(2,1,x);
                                     Delay_ms(500);
                                 }// end if

                                                   if((enter==0) && (reset==1) && (up==1) && (down==1))//( Button(&PORTD,0,ms,0)) //Enter pressed to save
                                                    {    Delay_ms(ms); // wait for switch debounce
                                                       self_oft=x;
                                                        Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                                                        Lcd_Cmd(_LCD_CURSOR_OFF);
                                                        Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                                                        Delay_ms(500);
                                                        Lcd_Out(1,1,CopyConst2Ram(msg,sav));
                                                        Delay_ms(1000);
                                                        j=0;
                                                     } // end saving
                                                     
                                                     inc=0;

                        } while(j);
                  }// if inc=2
                  
                  
             } // if opt = 0

              return (inc);

     } // end scroll up

 //-----------------------scroll Down ...........................................

     scroll_down( int opt , int inc )
     {
       int j=1;
       inc--;


             if(opt==1)
             {
                 Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                 Lcd_Cmd(_LCD_CURSOR_OFF);
                 Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);

                        if(inc==0)
                         Lcd_Out(1,1,CopyConst2Ram(msg,gwt));

                         if(inc==1)
                         Lcd_Out(1,1,CopyConst2Ram(msg,sont));

                          if(inc==2)
                          
                         Lcd_Out(1,1,CopyConst2Ram(msg,soft));
             } //end  if opt = 1

             if(opt==0)
             {
                Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                Lcd_Cmd(_LCD_CURSOR_OFF);
                Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
               //..................inc=0....gen_wt......................
                        if(inc==0)
                        {
                           x=gen_wt;
                           Lcd_out(2,1,x);
                           do
                           {
                               if((enter==1) && (reset==1) && (up==1) && (down==0) && j==1)//(Button(&PORTD,2,ms,0)&&j) // down pressed
                                 {     Delay_ms(ms); // wait for switch debounce
                                     x--;
                                     Lcd_out(2,1,x);
                                     Delay_ms(500);
                                 }// end if

                                                   if((enter==0) && (reset==1) && (up==1) && (down==1))//( Button(&PORTD,0,ms,0)) //Enter pressed to save
                                                    {   Delay_ms(ms); // wait for switch debounce
                                                        gen_wt=x;
                                                        Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                                                        Lcd_Cmd(_LCD_CURSOR_OFF);
                                                        Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                                                        Delay_ms(500);
                                                        Lcd_Out(1,1,CopyConst2Ram(msg,sav));
                                                        Delay_ms(1000);
                                                        j=0;
                                                     } // end saving

                        } while(j);
                  }// if inc=0
                  
                    //....................inc = 1......self_ont..............
                   if(inc==1)
                        {
                           x=self_ont;
                           Lcd_out(2,1,x);
                           do
                           {
                               if((enter==1) && (reset==1) && (up==1) && (down==0) && j==1)//(Button(&PORTD,2,ms,0)&&j) // down pressed
                                 {    Delay_ms(ms); // wait for switch debounce
                                     x--;
                                     Lcd_out(2,1,x);
                                     Delay_ms(500);
                                 }// end if

                                                   if((enter==0) && (reset==1) && (up==1) && (down==1))//( Button(&PORTD,0,ms,0)) //Enter pressed to save
                                                    {   Delay_ms(ms); // wait for switch debounce
                                                        self_ont=x;
                                                        Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                                                        Lcd_Cmd(_LCD_CURSOR_OFF);
                                                        Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                                                        Delay_ms(500);
                                                        Lcd_Out(1,1,CopyConst2Ram(msg,sav));
                                                        Delay_ms(1000);
                                                        j=0;
                                                     } // end saving

                        } while(j);
                  }// if inc=1
                  //.................inc=2..........self_oft..................................
                   if(inc==2)
                        {
                           x=self_oft;
                           Lcd_out(2,1,x);
                           do
                           {
                               if((enter==1) && (reset==1) && (up==1) && (down==0) && j==1)//(Button(&PORTD,2,ms,0)&&j) // down pressed
                                 {     Delay_ms(ms); // wait for switch debounce
                                     x--;
                                     Lcd_out(2,1,x);
                                     Delay_ms(500);
                                 }// end if

                                                   if((enter==0) && (reset==1) && (up==1) && (down==1))//( Button(&PORTD,0,ms,0)) //Enter pressed to save
                                                    {   Delay_ms(ms); // wait for switch debounce
                                                       self_oft=x;
                                                        Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                                                        Lcd_Cmd(_LCD_CURSOR_OFF);
                                                        Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                                                        Delay_ms(500);
                                                        Lcd_Out(1,1,CopyConst2Ram(msg,sav));
                                                        Delay_ms(1000);
                                                        j=0;
                                                     } // end saving

                                                     inc=0;

                        } while(j);
                  }// if inc=2
                  
             } // if opt = 0

             return (inc);
     } // end scroll down
     
     
     
//..............generator routine...............................................
gen()
{
      int i;
     main_cont=0;
      ignition=1;
      Delay_ms(2000);
      chowk=1;
      Delay_ms(5000);//chowk on for 5 seconds
      chowk=0;
      for(i=0; i<3; i++)
      {
               if(mains==P)     //if Main resumes
             {
                 i=3;
                 gen_cont=0;
                 ignition=0;
                 self=0;
                 Delay_ms(2000);  //self on for 2 seconds
                 main_cont=1;
             }

               if(i==2)
               {
                   chowk=1;
                   Delay_ms(5000);//chowk on for 5 seconds
                   chowk=0;
               }

       self=1;
       Delay_ms(2000);  //self on for 2 seconds
       self=0;
       Delay_ms(4000);  //self off for 4 seconds




             if(genr==P)     //if generator ON
             {
                 i=3;
             }
      }

      if(genr==P && mains==A)     // if generator ON
      {
       Delay_ms(1000);
      Lcd_Cmd(_LCD_CLEAR);                    // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);
      Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
      Lcd_Out(1,1,CopyConst2Ram(msg,wu));   // Write text in first row
       Delay_ms(5000);
       gen_cont=1;      // transfer load on genset after warmup

      Delay_ms(300);

      Lcd_Cmd(_LCD_CLEAR);                    // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);
      Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
      Lcd_Out(1,1,CopyConst2Ram(msg,fv));   // Write text in first row
      Lcd_Out(2,1," . Hz     . V");
      // lcd_init();
      Delay_ms(2000);

      }
      else
      if(genr==A && mains==A)    // if generator is still off; i.e; can't start
      {
      Delay_ms(1000);

      Lcd_Cmd(_LCD_CLEAR);                    // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);
      Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
      Lcd_Out(1,2,CopyConst2Ram(msg,error));
      Lcd_Out(2,1,CopyConst2Ram(msg,f2s));   // Write text in first row

      ignition=0;        //Turn off the genset
      main_cont=0;
      while(mains==A && genr==A); //stuck here untill reset/restart the controller board (IF main and generator both are absent)
      Lcd_Cmd(_LCD_CLEAR);                    // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);

      }

      for(;mains==A;)   //untill main absent
      {

          if (mains==A)      // if main absent
            gen_cont=1;     // Turn on generator contactor
            main_cont=0;    // Turn off main contactor
            while(mains==A); // stay here until main resumes

         if (mains==P)       //if main presents or after main resumes
           {
             gen_cont=0;
             Delay_ms(1000);
             ignition=0; //Turn off the genset
             Delay_ms(2000);
             main_cont=1;
             Lcd_Cmd(_LCD_CLEAR);                    // Clear display
             Lcd_Out(1,3,CopyConst2Ram(msg,st));   // Write text in first row
             Lcd_Out(2,4,CopyConst2Ram(msg,pl));
           }
      }
}

void start()
 {

  //IRP_bit = 1;
 //STATUS.IRP=1;
 // asm BSF STATUS,RP1
 // asm BSF STATUS,RP0  // ACCESS BANK 2


  gen_cont=0;
      main_cont=0;
      Lcd_Cmd(_LCD_CLEAR);                    // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);
      Lcd_Out(1,1,CopyConst2Ram(msg,mok));   // Write text in first row
      Lcd_Out(2,1,CopyConst2Ram(msg,pw));    //please wait
      Delay_ms(3000);

      main_cont=1;
      Lcd_Cmd(_LCD_CLEAR);                    // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);
      Lcd_Out(1,1,CopyConst2Ram(msg,mok));   // Write text in first row
      Lcd_Out(2,1,CopyConst2Ram(msg,mol));    //mains overload
         Delay_ms(2000);

         Lcd_Cmd(_LCD_CLEAR);                    // Clear display
         Lcd_Cmd(_LCD_CURSOR_OFF);               // Cursor off
         Lcd_Out(1,3,CopyConst2Ram(msg,st));   // Write text in first row
         Lcd_Out(2,4,CopyConst2Ram(msg,pl));   //pvt ltd
 }


void main()
{
    unsigned int i=0, itrue=1;  //i=loops ; x=passward ; J=While lock

 //..............INITIALIZATIONS............................................

/*   ANSEL  = 0;                        // Configure AN pins as digital I/O
  ANSELH = 0;
  C1ON_bit = 0;                      // Disable comparators
  C2ON_bit = 0;     */
  // IRP_bit = 0;

  // CONFIGURING SFRs:
  cnt=0;
  TMR0=0;   // loading vlaue of timer0 interrupt
  OPTION_REG=0x87;  // for timer 0; presscaller 1; 256
  ADCON1=0x06;   // adc reg initialization
  INTCON=0xA0;  // for timer0  ; interrupt reg initilization
 // STATUS.F7=0;
  //STATUS.IRP=0;
  //asm BCF STATUS,IRP


  // OUTPUTS:
  TRISB.f2 = 0x00;   //led out put
  TRISE.F2=0;        // gen cont output
  TRISC=0x00;        // portC output For LCD
  PORTC=0X00;        //  initilization port c

  // INPUTS:
  TRISD.F0=0xff;    // ENTER
  TRISD.F1=0xff;  // RESET
  TRISD.F2=0xff;    // UP
  TRISD.F3=0xff;   // DOWN   */
  TRISA = 0xff;     // port A as input
  TRISD = 0xff;     // port D as input

    // TRISD.F4=0XFF;      ALREADY DIRECTED AT LCD INITILIZATION
    //  TRISD.F5=0XFF;
 //................................................................................


  lcd_init();
  Delay_ms(500);


    if(mains==P)    // if main is Present.
    {
       start();

    }

  while(1)
  {



                    if ((enter==0) && (reset==1) && (up==1) && (down==1))// (Button(&PORTD,0,ms,0))  //when Enter is pressed
                     {
                          Delay_ms(ms); // wait for switch debounce
                          itrue=1;
                          x=0;
                          Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                          Lcd_Cmd(_LCD_CURSOR_OFF);
                          Lcd_Out(1,1,CopyConst2Ram(msg,ep));   //enter passward
                          delay_ms(t);
                             while ((enter==0) && (reset==1) && (up==1) && (down==1))//(Button(&PORTD,0,ms,0)) // press enter
                             {   Delay_ms(ms); // wait for switch debounce
                                 x++;
                               Lcd_Out(2,x,CopyConst2Ram(msg,asteric));  //*

                                 if(x==4)
                                 {
                                   Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                                   Lcd_Cmd(_LCD_CURSOR_OFF);
                                   Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                                   Lcd_Out(1,1,CopyConst2Ram(msg,pw));   //please wait
                                   Delay_ms(1000);

                                   Lcd_Cmd(_LCD_CLEAR);
                                   Lcd_Out(1,1,CopyConst2Ram(msg,gwt));  // Generator warm up time

                                   inc=0;  //for selection of variable
                                   itrue=1;   // Entring and Exiting main menu
                                   opt=1;    // option b/w selection number or variable

                                             while(itrue==1)
                                             {
                                                  if ((enter==0) && (reset==1) && (up==1) && (down==1))//(Button(&PORTD,0,ms,0))  // pressing enter to enter time
                                                  {
                                                   Delay_ms(ms); // wait for switch debounce
                                                   opt=0;
                                                   Lcd_Out(2,1,CopyConst2Ram(msg,sp));
                                                  }


                                                  if ((enter==1) && (reset==0) && (up==1) && (down==1))//(Button(&PORTD,1,ms,0)) //press reset to exit
                                                  {  Delay_ms(ms); // wait for switch debounce
                                                     itrue=0;
                                                  }
                                                  if ((enter==1) && (reset==1) && (up==0) && (down==1))//(Button(&PORTD,2,ms,0))  //up
                                                  {   Delay_ms(ms); // wait for switch debounce
                                                      inc=scroll_up(opt,inc);
                                                  }

                                                  if ((enter==1) && (reset==1) && (up==1) && (down==0))//(Button(&PORTD,3,ms,0))   // down
                                                   {
                                                      Delay_ms(ms); // wait for switch debounce
                                                      inc=scroll_down(opt,inc);
                                                   }
                                             }

                                              x++;
                                 }   //if
                             }    //while
                          Delay_ms(1000);
                     }



    if(mains==A)    // if main is absent.
    {
     Delay_ms(4000); // wait for 4 seconds to ensure power failure
    if(mains==P)     //check again
      continue;

     //Delay_ms(1000);
     Lcd_Cmd(_LCD_CLEAR);                    // Clear display
     Lcd_Cmd(_LCD_CURSOR_OFF);
     Lcd_Out(1,1,CopyConst2Ram(msg,mna));    //mains not available
     Lcd_Out(2,1,CopyConst2Ram(msg,sg));     //starting genset
     Delay_ms(1000);
     gen();

    }
    else           // if main is present .........
    {

      Lcd_Cmd(_LCD_CLEAR);                    // Clear display
      Lcd_Cmd(_LCD_CURSOR_OFF);
      Lcd_Out(1,1,CopyConst2Ram(msg,mok));   // Write text in first row
      Lcd_Out(2,1,CopyConst2Ram(msg,mol));    // mains overload
      Delay_ms(2000);
     //  gen_cont=0;   //Turn off generator contactor
       //main_cont=1;   //Turn on main contactor
      Lcd_Cmd(_LCD_CLEAR);                    // Clear display
      Lcd_Out(1,3,CopyConst2Ram(msg,st));   // Write text in first row
      Lcd_Out(2,4,CopyConst2Ram(msg,pl));     // pvt ltd
      Delay_ms(1000);
    }



              for(i=0; i<96; i++)
              {
                       Lcd_Cmd(_LCD_SHIFT_RIGHT);
                       Delay_ms(t);
                        if (enter==0)
                            i=96;
                        
                        if(mains==A)
                        {
                          i=96;
                        }
              }


   }    // while

}      // main
//..............................;;;;;;;.........................................
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top