Page 1 of 1

variable initialisation

Posted: Tue Dec 22, 2009 12:08 pm
by Adrien DG
Hello All !

i'm always working on a PIC - CPLD project, with SPI communication
I have to initialize an "unsigned long time = 0" variable in a macro, but i have a problem...
There is a while loop in my program, so it's always executed and i need to use this variable 'time',
but when i wrote Unisgned long time = 0 (init) i can work, with it (increment and decrement), but when i come back in this macro the variable is re-initialized.
So i've tried to put the initialisation out of the while loop, at the begining of the program, and at this time, i have a lot of error sayong "unknown variable 'time'"...
i don't know how can i resolve that !

one more time excuse me for my english.
Regards,

Adrien DG
France

Re: variable initialisation

Posted: Tue Dec 22, 2009 1:06 pm
by Sean
Hello Adrien

This looks like code from a C Code block. If this is the case, you might be able to declare and initialise the variable in the macro using the 'static' directive:

static unsigned long time = 0;

This should cause the variable to be initialized on the first call to the macro, and any changes retained on subsequent calls.

Re: variable initialisation

Posted: Tue Dec 22, 2009 1:24 pm
by Adrien DG
That runs, thank you so much ImageImage

I have learned that at school but i have forgoted it. Now i'll remember that !!

Merry Christmas at all and happy new year !!! Image