View Single Post
  #4   Spotlight this post!  
Unread 25-02-2007, 16:50
Dave K.'s Avatar
Dave K. Dave K. is offline
Engineer/Mentor
FRC #0930
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 2005
Location: WI
Posts: 91
Dave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to behold
Re: Real time clocks, out of the question?

Quote:
Originally Posted by evan_wilson View Post
Alright. I have another question about how the timer variables work in all of these. I've used storage classes/qualifiers occasionally but I don't understand the significance of using, say, extern and volatile to store the timer variables. Could anyone shed some light on this?
'extern' is much like a function prototype, meaning that it is allowing you declare the variable within the local scope such that the compiler will know what the variable type is and can generate the appropriate code. When the linker pulls all of the object files together, it will resolve the external dependancies of each of the object files, and if you haven't declared the variable itself anywhere within the program, will generate an error and abort.

'volatile' tells the compiler that the variable may change on its own and to generate code as appropriate. For optimization purposes, a compiler may read a variable from memory into a temporary register, and subsequent read/write operations to that variable _may_ only occur to the register without updating the memory location. Telling the compiler that the variable is volatile causes the compiler to re-read the variable's memory location each time it is used, and to write it back to memory each time it is updated.

'volatile' would be used for things like hardware periphial registers, as well as things like variables that might change inside of an interrupt routine.

Since the compiler can't know why you are telling it that the variable is volatile, it wouldn't generate additional instructions to disable interrupts during critical regions to ensure that a multi-byte variable read/write operation is performed without the potential for another interrupt routine corrupting the operation. Pre-emptive multi-tasking operating systems generally pose the same potential for corruption that an interrupt routine does.

The 'static' qualifier allows variables to be declared within a local, rather than global, scope and simply tells the compiler to declare the variable in a memory location that is static, rather than allocating it on the stack frame (or emulated equivilent thereof) of the local function.

Any variable declared outside of a function is normally considered a global variable, but unless other source files provide an 'extern' reference, the variable's use will remain limited to that source file, and is sometimes referred to as a 'local global'. The caveat here is that if you were to declare another 'local global' in another source file, the linker will (usually) have a problem with the duplicate declarations, and generate an error.



Recognizing that Microchips documentation already presumes a certain familiarity with the "C" language and doesn't really get into all of the specifics, for those that already have a general grasp on the "C" language, two books that I would recommend are:

The C Programming Language by Brian Kernighan and Dennis Ritchie, who are the origional authors of "C".
A book on C by Al Kelly and Ira Pohl.

K&R's book is very terse with minimal examples, but is the bible. The 2nd one is also a bit terse, and goes into depth more than K&R's book.

I've included links to Amazon's website as a convienent reference, but retailers such as Barnes & Noble also generally carry these and other good reference books where you can browse before you buy.
__________________
--Dave