View previous topic :: View next topic |
Author |
Message |
Study
Joined: 09 Apr 2009 Posts: 29
|
Changing global var on a function that doesn't use it !!! |
Posted: Wed Nov 10, 2010 3:17 am |
|
|
Hi friends
I have a program that has one global var (unsigned int16) and I'm using 18F452.
I use this variable in main and timer1_int. I have some function too. When I call one function it does its work fine but changes my global variable too!!! (I never use this variable on that function).
I mean before calling that function my global variable is ok in main but after come back from the function it changes (I don't use global var on that function).
I have looked at asm program in lst file too. I didn't see any work on global var by that function but it changes!!
What's the problem?
Regards _________________ Sorry if i have much mistakes, my english is not good.
Thanks a lot for your helps.
Have a good time |
|
|
vinniewryan
Joined: 29 Jul 2009 Posts: 154 Location: at work
|
|
Posted: Wed Nov 10, 2010 4:23 am |
|
|
Please post the code and your compiler version.
Try writing a test code and comment out certain lines of code in the 'function in question' to narrow down exactly what function or line of code is changing your global variable.
I can't say much more without seeing the code. _________________ Vinnie Ryan |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Wed Nov 10, 2010 4:30 am |
|
|
Most likely, something going beyond the area it is meant to.
Do you use any arrays, or pointer in the function?.
The 'classic' problem is (for example), having an array of eight characters, and writing an 8 character string to this. Forgetting that a 'string', always has an extra character for the terminator. This then results in the next variable in memory getting corrupted....
So, open the .sym file. This shows 'where' variables are stored in memory. Find where the global variable is stored. Then look at what is stored immediately in front of this. Is this a variable in the problem function?. Then look at how this is declared, and used. The variables immediately in front, are the 'most likely' to cause a problem.
Beyond this, are you using pointers anywhere in the function?. If so, triple check that declarations are correct for these, and indexes are correct. Then the same for arrays. Another 'classic', is forgetting that in C, arrays are 'zero referenced', so if you create an array of twenty elements, you can use a[0] to a[19]. If you accidentally use a[20], this results in the next variable getting corrupted....
Best Wishes |
|
|
Study
Joined: 09 Apr 2009 Posts: 29
|
|
Posted: Wed Nov 10, 2010 5:25 am |
|
|
Thanks a lot Ttelmah
I think the problem is what you say, I have global array too that i use it on function, it stors at: 019-01C and my global var is on 01D-01E.
what should i do now? _________________ Sorry if i have much mistakes, my english is not good.
Thanks a lot for your helps.
Have a good time |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Wed Nov 10, 2010 6:36 am |
|
|
Check your counter (index) for traversing the Array.
Its probably going out of bounds.
Most likely in the function that over writes your variable contains a loop.
Check its limits.
I think I just said the same thing twice. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Study
Joined: 09 Apr 2009 Posts: 29
|
|
Posted: Wed Nov 10, 2010 9:28 am |
|
|
I got answer, Thanks a lot for your helps friends _________________ Sorry if i have much mistakes, my english is not good.
Thanks a lot for your helps.
Have a good time |
|
|
|