Discussion Forum

Why do my variables reset??

Next Thread | Thread List | Previous Thread Start a Thread | Settings

DetailsMessage
Read-Only
Author
Tom Stathes
Posted
12-Feb-2009 14:14 GMT
Toolset
C51
New! Why do my variables reset??

First off, let me say while im not new to c, i havent programmed in it for years, now im very quickly having to re-learn everything for work.

Here is my problem.

I have 2 files, lets say file Main.c an support.c

Main contains some initilizations and an infinite loop that just waits for the uart to trigger an interrupt.

support.c contains the uart ISR and some functions. There are a few global counter variables as well.

when the isr tripps a function is called, and a counter is incrimented. the next time the isr tripps the counter resets before the function that incriments it is even called.

Since global variables are by default static (and im defining them as static), any one of these functions should be able to modify the counter freely and the value should not reset when a function is called correct?.

i verified this by starting another project with a simple prgmm like this

int i;
void inci(){i++;}

main(){
int a;
for(a=0;a>100;a++){inci();}
}

The value of i does not reset just incriments like it should on the watch window.

One odd thing i also noticed, if i set a breakpoint on my counter init function and hit RUN it will break there everytime i hit run (it should break then continue and enter a while(1){;} loop the next time i hit run), when i step through using F11 it exectues once and ends up in the infinite loop waiting like it should.

If anyone could help, i would really appreciate it.

Read-Only
Author
Mike Kleshov
Posted
12-Feb-2009 14:26 GMT
Toolset
C51
New! RE: Why do my variables reset??

Watchdog timer, maybe? If it's on at power-up and you don't disable it, your program will keep restarting.

Read-Only
Author
erik malund
Posted
12-Feb-2009 16:50 GMT
Toolset
C51
New! why does no one think it's important ....

If it's on at power-up

.. to post which derivative they use?

Erik

Read-Only
Author
Tom Stathes
Posted
12-Feb-2009 20:52 GMT
Toolset
C51
New! RE: Why do my variables reset??

Thanks for the help guys, watchdog timer was the problem. Only change i had to make was disable it and now it works fine.

Read-Only
Author
Neil Kurzman
Posted
12-Feb-2009 19:05 GMT
Toolset
C51
New! RE: Why do my variables reset??

Since global variables are by default static (and im defining them as static), any one of these functions should be able to modify the counter freely and the value should not reset when a function is called correct?.

declaring a global variable static limits its scope to the current module. This allows you to have two different variables with the same name in different modules.
get rid of the static, declare the variable extern in the second file.

Next Thread | Thread List | Previous Thread Start a Thread | Settings