Microchip Technology SW006021-1N 用户手册
MPLAB
®
XC8 C Compiler User’s Guide
DS52053B-page 194
2012 Microchip Technology Inc.
5.10
MAIN, RUNTIME STARTUP AND RESET
The identifier main is special. You must always have one and only one function called
main()
main()
in your programs. This is the first function to execute in your program.
Code associated with main(); however, is not the first code to execute after Reset.
Additional code provided by the compiler, and known as the runtime startup code, is
executed first and is responsible for transferring control to the main() function. The
actions and control of this code is described in the following sections.
Additional code provided by the compiler, and known as the runtime startup code, is
executed first and is responsible for transferring control to the main() function. The
actions and control of this code is described in the following sections.
The compiler inserts special code at the end of main() which is executed if this func-
tion ends, i.e. a return statement inside main() is executed, or code execution
reaches the main()’s terminating right brace. This special code causes execution to
jump to address 0, the Reset vector for all 8-bit PIC devices. This essentially performs
a software Reset. Note that the state of registers after a software Reset may be differ-
ent to that after a hardware Reset.
tion ends, i.e. a return statement inside main() is executed, or code execution
reaches the main()’s terminating right brace. This special code causes execution to
jump to address 0, the Reset vector for all 8-bit PIC devices. This essentially performs
a software Reset. Note that the state of registers after a software Reset may be differ-
ent to that after a hardware Reset.
It is recommended that the main() function does not end. Add a loop construct (such
as a while(1)) that will never terminate either around your code in main() or at the
end of your code, so that execution of the function will never terminate. For example,
as a while(1)) that will never terminate either around your code in main() or at the
end of your code, so that execution of the function will never terminate. For example,
void main(void)
{
// your code goes here
// finished that, now just wait for interrupts
while(1)
continue;
}
5.10.1
Runtime Startup Code
A C program requires certain objects to be initialized and the device to be in a particular
state before it can begin execution of its function main(). It is the job of the runtime
startup code to perform these tasks, specifically (and in no particular order):
state before it can begin execution of its function main(). It is the job of the runtime
startup code to perform these tasks, specifically (and in no particular order):
• Initialization of global variables assigned a value when defined
• Clearing of non-initialized global variables
• General setup of registers or device state
Rather than the traditional method of linking in a generic, precompiled routine, MPLAB
XC8 uses a more efficient method which actually determines what runtime startup code
is required from the user’s program. Details of the files used and how the process can
be controlled are described in Section 4.4.2 “Startup and Initialization”. The follow-
ing sections detail exactly what the runtime startup code actually does.
XC8 uses a more efficient method which actually determines what runtime startup code
is required from the user’s program. Details of the files used and how the process can
be controlled are described in Section 4.4.2 “Startup and Initialization”. The follow-
ing sections detail exactly what the runtime startup code actually does.
The runtime startup code is executed before main(), but If you require any special ini-
tialization to be performed immediately after Reset, you should use power-up feature
described later in Section 5.10.2 “The Powerup Routine”.
tialization to be performed immediately after Reset, you should use power-up feature
described later in Section 5.10.2 “The Powerup Routine”.
The following table lists the significant assembly labels used by the startup and pow-
erup code.
erup code.
TABLE 5-10:
SIGNIFICANT ASSEMBLY LABELS
Label
Location
reset_vec
At the Reset vector location (0x0)
powerup
The beginning of the powerup routine, if used
start
The beginning of the runtime startup code, in startup.as
start_initialization
The beginning of the C initialization startup code, in the C
output code.
output code.