At 05:03 AM 27/09/2009, you wrote: >William "Chops" Westfield wrote: > > On Sep 26, 2009, at 12:39 PM, Quintin Beukes wrote: > > > >> The problem can be reproduced, however, by finish reading from the > >> serial interface, then later one doing an exit(0). After doing that a > >> few times all serial comms in the future are messed up. > > > > So you are undoubtably running into the differences between doing a > > "real" reset and the partial re-start that happens (perhaps) when you > > do exit(0). A "real" reset will return most of the peripherals to a > > default state, as well as restarting the program. A "software reset" > > might ONLY return the program to its start address, > >Now, I do not remember what device this is, but executing the RESET >instruction on a PIC18 (or one of the later PIC16 that has that >instruction) is doing all register initializations that a hard >reset does. What we do not know is if the exit(0) realy executes >a real RESET instruction. If it could, depending on the device... >-- MPSIM is a really good (and free) tool for figuring out what the compiler and (in this case) the start-up code are up to. MCC18 sets up the stack pointer, does the usual C initialization such as clearing static variables, and then has a flow like this: // initialization is complete at this point loop: __init(); // despite the name, by default this calls a null function. main(); // call the user main function goto loop; As you can see, it behaves nothing like a RESET when you execute an exit(0)-- nothing at all is re-initialized. To the extent exit() is defined on a freestanding system (without a "host" or operating system), it is supposed to call atexit-registered functions, flush buffers and close streams, then return from main(); Calling exit() more than once is specifically indicated to cause undefined behavior in the ANSI/ISO C99 standard. I don't see any obvious and appropriate applications for exit() in a program that runs on this kind of system. Best regards, Spehro Pefhany --"it's the network..." "The Journey is the reward" speff@interlog.com Info for manufacturers: http://www.trexon.com Embedded software/hardware/analog Info for designers: http://www.speff.com -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist