On 22/07/2010 20:28, alan.b.pearce@stfc.ac.uk wrote: [snip] >> void OpenSPI1 >> ( >> unsigned char sync_mode, >> unsigned char bus_mode, >> unsigned char smp_phase >> ); >> unsigned char WriteSPI1(unsigned char data_out ); >> >> void OpenSPI2 >> ( >> unsigned char sync_mode, >> unsigned char bus_mode, >> unsigned char smp_phase >> ); >> unsigned char WriteSPI2(unsigned char data_out ); >> >> void Open1USART >> ( >> unsigned char config, >> unsigned int spbrg >> ); >> =20 > Note that the variables declared inside each function are not declared > 'static' so each time the function gets called they are recreated on the > stack, and are unique to that function. So the variables inside OpenSPI1 > are different variables to OpenSPI2, even though they have the same > name. The scope of each name is limited to the code inside that > function. The variables are cleared to all zeros on creation on the > stack if I understand the C standard correctly, but may need correction > here. If you attempt to look at the values of these variables with a > debugger such as MPLAB/ICD combination, you get strange results unless > you are single stepping through the code inside that function. > > When the function terminates by executing a 'return' the variables are > removed from the stack, and no longer exist. At this stage your debugger > will again go strange with the values of these variables (IIRC it gives > an error message that says they are 'Out of Scope'). If the other > routine is now called the variables for that function are automatically > created on the stack, and are unique to that function, even if they > happened to reside at the same position in the stack. > > If the variables are declared 'static' in addition to the other > attributes they have, then they are created in the normal RAM area, and > are persistent between calls to the function, i.e. they keep their last > value from the last time the function was called, or are uninitialized > before the first call of the function unless initialisation values are > declared. But they are still separate variables even though they have > the same name. I don't know how the debugger identifies which one you > wish to watch, I haven't had occasion to go down this road at this > stage. > =20 Thanks Alan, this really hits the root of my question. Very helpful indeed. Regards, Matt --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .