text: #include <C8051F38x.h>
  #include <stdlib.h>
  #include "robot_header.h"
  
  char _c51_external_startup (void) {
  	PCA0MD&=(~0x40) ;    // DISABLE WDT: clear Watchdog Enable bit
  	VDM0CN=0x80; // enable VDD monitor
  	RSTSRC=0x02|0x04; // Enable reset on missing clock detector and VDD
  
  	// CLKSEL&=0b_1111_1000; // Not needed because CLKSEL==0 after reset
  	#if (SYSCLK == 12000000L)
  		//CLKSEL|=0b_0000_0000;  // SYSCLK derived from the Internal High-Frequency Oscillator / 4
  	#elif (SYSCLK == 24000000L)
  		CLKSEL|=0b_0000_0010; // SYSCLK derived from the Internal High-Frequency Oscillator / 2.
  	#elif (SYSCLK == 48000000L)
  		CLKSEL|=0b_0000_0011; // SYSCLK derived from the Internal High-Frequency Oscillator / 1.
  	#else
  		#error SYSCLK must be either 12000000L, 24000000L, or 48000000L
  	#endif
  	OSCICN |= 0x03; // Configure internal oscillator for its maximum frequency
  
  	// Configure UART0
  	SCON0 = 0x10;
  #if (SYSCLK/BAUDRATE/2L/256L < 1)
  	TH1 = 0x10000-((SYSCLK/BAUDRATE)/2L);
  	CKCON &= ~0x0B;                  // T1M = 1; SCA1:0 = xx
  	CKCON |=  0x08;
  #elif (SYSCLK/BAUDRATE/2L/256L < 4)
  	TH1 = 0x10000-(SYSCLK/BAUDRATE/2L/4L);
  	CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 01
  	CKCON |=  0x01;
  #elif (SYSCLK/BAUDRATE/2L/256L < 12)
  	TH1 = 0x10000-(SYSCLK/BAUDRATE/2L/12L);
  	CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 00
  #else
  	TH1 = 0x10000-(SYSCLK/BAUDRATE/2/48);
  	CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 10
  	CKCON |=  0x02;
  #endif
  	TL1 = TH1;      // Init Timer1
  	TMOD &= ~0xf0;  // TMOD: timer 1 in 8-bit autoreload
  	TMOD |=  0x20;
  	TR1 = 1; // START Timer1
  	TI = 1;  // Indicate TX0 ready
  
  	// Configure the pins used for square output
  	P2MDOUT |= 0b_0000_0011;
  	P0MDOUT |= 0x10; // Enable UTX as push-pull output OR IT MIGHT BE THIS LINE GOING WRONG
  	XBR0     = 0x01; // Enable UART on P0.4(TX) and P0.5(RX)
  	XBR1     = 0x50; // Enable crossbar and weak pull-ups IF SOMETHING GOES WRONG IT'S RIGHT HERE!!!
  	XBR2 	 = 0x00;
  
  	// Initialize timer 2 for periodic interrupts
  	TMR2CN=0x00;   // Stop Timer2; Clear TF2;
  	CKCON|=0b_0001_0000;
  	TMR2RL=(-(SYSCLK/(2*48))/(100L)); // Initialize reload value
  	TMR2=0xffff;   // Set to reload immediately
  	ET2=1;         // Enable Timer2 interrupts
  	TR2=1;         // Start Timer2
  
  	EA=1; // Enable interrupts
  
  	return 0;
  }
  
  void Timer3us(unsigned char us) {
  	unsigned char i;               // usec counter
  	
  	// The input for Timer 3 is selected as SYSCLK by setting T3ML (bit 6) of CKCON:
  	CKCON|=0b_0100_0000;
  	
  	TMR3RL = (-(SYSCLK)/1000000L); // Set Timer3 to overflow in 1us.
  	TMR3 = TMR3RL;                 // Initialize Timer3 for first overflow
  	
  	TMR3CN = 0x04;                 // Sart Timer3 and clear overflow flag
  	for (i = 0; i < us; i++)       // Count <us> overflows
  	{
  		while (!(TMR3CN & 0x80));  // Wait for overflow
  		TMR3CN &= ~(0x80);         // Clear overflow indicator
  	}
  	TMR3CN = 0 ;                   // Stop Timer3 and clear overflow flag
  }
  
  void waitms (unsigned int ms) {
  	unsigned int j;
  	for(j=ms; j!=0; j--)
  	{
  		Timer3us(249);
  		Timer3us(249);
  		Timer3us(249);
  		Timer3us(250);
  	}
  }
  
  void TIMER0_Init(void)
  {
  	TMOD&=0b_1111_0000; // Set the bits of Timer/Counter 0 to zero
  	TMOD|=0b_0000_0001; // Timer/Counter 0 used as a 16-bit timer
  	TR0=0; // Stop Timer/Counter 0
  }
  
  