The startup library provides a typical initialization regime for your programs. The startup library implements the __STARTUP() function described above. In particular, it initializes the COP8's I/O ports and configuration registers depending on symbols defined in your program. This method allows you to clearly specify the state of the microcontroller's I/O ports and CONTROL registers. The startup library header file has a list of the startup values that can be overridden from within the startup library.
If you need to make drastic customizations to __STARTUP(), simply write your own. For instance, some applications that run non-stop call this function to re-initialize the port states, to guard against external events that could disrupt the port configuration.
Figure 3-3. using the startup library init3.c
#include <startup.h> #define PORTL_STARTUP_DDR OOOOOOOO /* set all port B pins as output */ #define CNTRL_STARTUP_VAL 0x70; /* start timer 2 */ int i=5; void main(void) { while(1) /* loop forever */ PORTLD=TMR2LO; /* output low byte of timer T2 on port L */ }
This is an example of the startup.h library.
Figure 3-4. the generated code for the startup library init3.lst
void __STARTUP(void) { #ifdef PORTA_STARTUP_DATA PORTAD=PORTA_STARTUP_DATA; #endif #ifdef PORTA_STARTUP_DDR DDR(PORTA,PORTA_STARTUP_DDR); #endif #ifdef PORTB_STARTUP_DATA PORTBD=PORTB_STARTUP_DATA; #endif #ifdef PORTB_STARTUP_DDR DDR(PORTB,PORTB_STARTUP_DDR); #endif #ifdef PORTC_STARTUP_DATA PORTCD=PORTC_STARTUP_DATA; #endif #ifdef PORTC_STARTUP_DDR DDR(PORTC,PORTC_STARTUP_DDR); #endif #ifdef PORTD_STARTUP_DATA PORTDD=PORTD_STARTUP_DATA; #endif #ifdef PORTD_STARTUP_DDR DDR(PORTD,PORTD_STARTUP_DDR); #endif #ifdef PORTE_STARTUP_DATA PORTED=PORTE_STARTUP_DATA; #endif #ifdef PORTE_STARTUP_DDR DDR(PORTE,PORTE_STARTUP_DDR); #endif #ifdef PORTF_STARTUP_DATA PORTFD=PORTF_STARTUP_DATA; #endif #ifdef PORTF_STARTUP_DDR DDR(PORTF,PORTF_STARTUP_DDR); #endif #ifdef PORTG_STARTUP_DATA PORTGD=PORTG_STARTUP_DATA; #endif #ifdef PORTG_STARTUP_DDR DDR(PORTG,PORTG_STARTUP_DDR); #endif #ifdef PORTL_STARTUP_DATA PORTLD=PORTL_STARTUP_DATA; #endif #ifdef PORTL_STARTUP_DDR DDR(PORTL,PORTL_STARTUP_DDR); #endif #ifdef CNTRL_STARTUP_VAL 0205 BC D1 FF LD 0D1,#FF CNTRL=CNTRL_STARTUP_VAL; #endif #ifdef WDSVR_STARTUP_VAL WDSVR=WDSVR_STARTUP_VAL; #endif 0208 BC EE 70 LD 0EE,#70 }