The compiler looks for a special function named __STARTUP() when it compiles your source code. If it finds a function named '__STARTUP' (it is not required), the compiler causes it to be automatically called before global and static variables are initialized, and before main() is called. __STARTUP() is intended to initialize ports and data direction registers, and to make any other configuration before your program starts to run.
This example demonstrates the use of the __STARTUP() function. __STARTUP() modifies the default FUSE configuration, and sets the microcontroller's OPTION register.
Figure 3-1. source code init1.c
int i=5;
void __STARTUP(void)
{
DDR(PORTL,OOOOOOOO); /* set all port L pins as output */
T2CNTRL=0x70; /* start timer 2 */
}
void main(void)
{
while(1) /* loop forever */
PORTLD=TMR2LO; /* output low byte of timer T2 on port L */
}
Note: If an initial value is assigned to a global or static variable it may not be initialized as expected. Only variables located in bank 0 are initialized properly. To be sure the variable is initialized you must specify that the variable is to be located in bank 0. (i.e. int bank0 i = 0x55;). If one or more variables are initialized, the rest of RAM in bank 0 is cleared (set to zero).
Figure 3-2. compiler listing file output init1.lst
0008 05 int i=5;
void __STARTUP(void)
{
DDR(PORTB,OOOOOOOO); /* set all port B pins as output */
0200 BC A5 FF LD 0A5,#FF T2CNTRL=0x70; /* start timer 2 */
0203 BC C6 70 LD 0C6,#70 }
0206 8E RET
void main(void)
{
while(1) /* loop forever */
0207 9D C0 LD A,0C0 PORTLD=TMR2LO; /* output low byte of timer T2 on port L */
0209 9C D0 X A,0D0
020B FB JP 00207
}
0000 DD 6F LD SP,#06F
0002 AC 02 0C JMPL 0020C
020C AD 02 00 JSRL 00200
020F DF 00 LD S,#000
0211 DC 00 LD X,#000
0213 DE 6F LD B,#06F
0215 64 CLRA
0216 B2 X A,[X+]
0217 CE DRSZ B
0218 FC JP 00215
0219 BC 08 05 LD 008,#05
021C AC 02 07 JMPL 00207
ROM USAGE MAP
0000 to 0004 00FF to 0100 0200 to 021E
Total ROM used 0026