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.

Variable corruption after a statement

Status
Not open for further replies.

ucsam

Advanced Member level 4
Joined
Oct 12, 2010
Messages
119
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
kathmandu,nepal
Activity points
2,058
I am writing code for a keypad system. The problem that is occuring is very strange and ridiculous!

so here is the problem:

  • When I enter a key, the system would compare it with a predefined user-key key and let me in or out like all keypad system does.
  • But when i once enter the wrong key, the predefined user-key gets corrupted! I hook up LCD to the key and found out two extra character are also being added to the original one!
  • AND the same goes while checking with the master key.

Possible solution I though would help
  • Disabling the interrupt. But it didn't work as well.
  • Using data keyword while declaring the variable. But it didn't work too

I though that it was some kind of corruption problem so i gave it the topic. But how does a string gets corrupted just after an IF-ELSE statement.

I am using KEIL and AT89s52.

Thanks,
 

I'm not sure how we can help without seeing the code but you can try to declare the predefined code as const, if any part of the code tries to change the content of the constant then you 'll get a compile error and it may help you locate where this is happening.
If not then this is probably some RAM/stack/heap related problem

Alex
 

You may have used = instead of == in the comparison. As Alex says - show your code.

Keith
 

I can keep the master password constant so i did, but it got worse. I will post the code.

idata volatile char user_password[max_userkey_length]="1234";

void main(void)
{

char entered_key[4];
buzzer_pin=0;

#ifdef debug
Initialize_LCD();
// printf("Keypad debug\n");
debug_use(1,"Keypad debug");
#endif
indicate_power_up();
ini_all();
get_current_settings(); //via EEPROM
buzz_special(1);
TR0=0; // start counting
TR1=0;

pwm_value=25;
while(1)
{
indicate_power_up();
get_key(entered_key,max_userkey_length);

#ifdef debug
cls();
//printf("\n String entered %s",entered_key);
lcd_text(1,2,"String entered:");
lcd_text(2,6,entered_key);
delay_ms(500);
cls();
#endif
if(string_length(entered_key)==max_userkey_length)
{
delay_sec(1);
if(trial<legal_trial)
{
if(check_string(entered_key,user_password)) // here the checking takes place
{
#ifdef debug
debug_use(1,"Corct pass entry");
#endif
pwrLED(on);
modeLED(off);
okLED(on);
delay_ms(200);
okLED(off);
trial=0;
if(!freeze_mode)
password_correct_flag=1;
}
else
{
password_correct_flag=0;
pwrLED(off);
buzz_special(3);
pwrLED(on);
trial++;
#ifdef debug
debug_use(1,"Wrng pass entry");
debug_use(1,"User key:");
debug_use(2,user_password);
#endif

}
}

/* this function does the checking */
char check_string(unsigned char *to,unsigned char *with)
{
char i,j,temp=0;

temp=0;

#ifdef debug
cls();
debug_use(1,"to");
Position_LCD(1,6);
Write_LCD_Text(to);
delay_sec(1);
debug_use(2,"with");
Position_LCD(2,6);
Write_LCD_Text(with);
delay_sec(1);
cls();
#endif
i=string_length(to);
j=string_length(with);
if(i!=j)
temp=0;
else if(i==j)
{
for(i=0;i<j;i++)
{
if(to==with)
temp++;
else
temp=0;
}
if(temp!=j)
temp=0;
else if(temp==j)
temp=1;
else
temp=2;
}
return temp;
}
 

Need to see your GetKey function, but at first glance, it seems that you try to store a 4 character string in a 4 character array - no space for NULL character (0x00), identifying the end of a string, and then you use string operation functions, which require null terminated string.
 

My function doesn't overwrites on the last index so i have the place for null character.
The main function is not complete. I am just showing the part where i am having trouble
void get_key(unsigned char *string,char limit)
{
char temp;
char_length=0;
do
{
if(key_pressed_flag)
{
temp=key_pressed;
key_pressed_flag=0;

if(temp=='#' && char_length !=0 )
{
delay_ms(100);
buzz(2);
}
else if(temp=='#' && char_length==0)
{
buzz(2);
//sys_edit_mode=1;
break;
}
else if(temp=='*' && char_length!=0)
{
char_length=0;
buzz(2);
}
else if(temp=='*' && char_length==0)
{
buzz(1);
go_back=1;
break;
}
else if(temp=='o')
{
if(open_cmd_legal_flag)
{
act('o');
}
else
buzz_special(3);
}
else if(temp=='c'||temp=='s')
{
act(temp);
buzz(2);
}
else
{
string[char_length]=temp;
char_length++;
buzz(1);
}
#ifdef debug
lcd_text(1,2,"Pressed key:");
Write_LCD_Data(temp);
#endif
}

}
while(char_length<limit);
string[char_length]='\0';
}
 

I can keep the master password constant so i did, but it got worse. I will post the code.

What do you mean , how did things gotten worse with const?
The problem was that the user_password was changed so now that const prevents that what changes to the worse?


My function doesn't overwrites on the last index so i have the place for null character.
The main function is not complete. I am just showing the part where i am having trouble

So you mean the in
Code:
idata volatile char user_password[max_userkey_length]="1234";
max_userkey_length is 5?
 

Your

Code:
char entered_key[4];

defined in main is to small to keep 4 character array + '/0' character

Should be:

Code:
char entered key_[5];
;

Or, in other words,

Code:
char entered_key[max_userkey_length+1];

BTW - the right way for this:

Code:
idata volatile char user_password[max_userkey_length]="1234";

would be

Code:
idata volatile char user_password[]="1234";
//Let the compiler do the job.. :)
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top