Qaisar Azeemi
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
// 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
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?
if(Button(&PORTD,2,ms,0)) //up
inc=scroll_up(opt,inc);
if(Button(&PORTD,3,ms,0)) // down
inc=scroll_down(opt,inc);
#define ms 100 // for button debounce
#define ms 20 // for button debounce
if(Button(&PORTD,2,ms,0)) //up
inc=scroll_up(opt,inc);
inc=scroll_up(opt,inc);
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);
}
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
Tell me what the buttons should do?
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");
}
}
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;
}
}
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;
}
}
}
//============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=============================================
#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
// 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
//..............................;;;;;;;.........................................
//========== 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 ====================================
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
If you tell me what exactly the scroll up and scroll down functions do. I can try to code that into the buttons.
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;
}
// 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;
}
}
}
}
// 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 *)¶m)[0]
#define Hi(param) ((char *)¶m)[1]
#define Higher(param) ((char *)¶m)[2]
#define Highest(param) ((char *)¶m)[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;
}
}
}
}
// 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
//..............................;;;;;;;.........................................
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?