> Please can someone faimilar with C kindly help clear up my confusion here? Well I don't really claim familiarity with C, but have done a little bit with it. > I'm declaring a couple of SPI function prototypes, see below. Not > wishing to waste too much time testing awkward things, shouldn't the > common variables (sync_mode, bus_mode etc.) be specified more uniquely > for each SPI channel? Will the following, although it compiles ok, cause > ambiguity when it comes to execution? I can't tell whether prototyping a > function is the same as declaring a variable by name, nor how it's > allocating the names following "unsigned char". > > Many thanks, > Matt > > > > 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 > ); 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. -- Scanned by iCritical. -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist