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.

 

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
 

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.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…