//-----------------------------------------------------------------------------
// System_Init(void)
//-----------------------------------------------------------------------------
//
// Return Value - None
// Parameters - None
//
// This top-level initialization routine calls all support routine.
//
// ----------------------------------------------------------------------------
void System_Init (void)
{
   PCA0MD &= ~0x40;                    // Disable Watchdog timer
   PCA0MD  = 0x00;
   Sysclk_Init ();                     // initialize system clock
   Port_Init ();	                   // configure cross bar
}
Esempio n. 2
0
//-----------------------------------------------------------------------------
// Main Routine
//-----------------------------------------------------------------------------
void main(void)  
{
	char cmd;

   PCA0MD &= ~0x40;                    // Disable Watchdog timer

   Sysclk_Init();                      // Initialize oscillator
   Port_Init();                        // Initialize crossbar and GPIO
   P2=0xFF;  // tie high to turn off pulldowns for open drain devices connected
   Usb0_Init();                        // Initialize USB0
   Timer_Init();                       // Initialize timer2
	LedOn(); 

  	//RSTSRC =0x80;	;				   // enable USB and
                                       // missing clock detector reset sources
									   // if we enabled missing clock detector then the device doesn't work, don't know why

    // watchdog
	// load watchdog offset 
	PCA0CPL4=0xff; // maximum offset so that watchdog takes a good long time to time out
	// enable watchdog 
  PCA0MD |= 0x44;   // WDT enabled, PCA clock source is timer0 overflow
  PCA0CPH4 = 0x00;  // write value to WDT PCA to start watchdog                   

	
/*	Servo0=0;
	Servo1=0;
	Servo2=0;
	Servo3=0;
*/
   while (1)
   {
    // It is possible that the contents of the following packets can change
    // while being updated.  This doesn't cause a problem in the sample
    // application because the bytes are all independent.  If data is NOT
    // independent, packet update routines should be moved to an interrupt
    // service routine, or interrupts should be disabled during data updates.


	  PCA0CPH4 = 355;  // write value to WDT PCA to reset watchdog
	  
	             
		
		EA=0; // disable ints
	//	LedToggle();
		cmd=Out_Packet[0];
		switch(cmd){
		case CMD_SET_SERVO:
			Out_Packet[0]=0; // command is processed
			LedToggle();
			pwmNumber=Out_Packet[1];
			switch(pwmNumber)
			{
			// big endian 16 bit value to load into PWM controller
				case 0:
				{ // servo0
					pwmh1=Out_Packet[2]; // store the PCA compare value for later interrupt to load
					pwml1=Out_Packet[3];
					PCA0CPM0 |= 0x49; // enable compare function and match and interrupt for match for pca
					
				}
				break;
				case 1:
				{ // servo1
					pwmh2=Out_Packet[2];
					pwml2=Out_Packet[3];
					PCA0CPM1 |= 0x49; // enable compare function and enable match and interrupt for match for pca
				}
				break;
				case 2:
				{ // servo1
					pwmh3=Out_Packet[2];
					pwml3=Out_Packet[3];
					PCA0CPM2 |= 0x49; // enable compare function and enable match and interrupt for match for pca
				}
				break;
				case 3:
				{ // servo1
					pwmh4=Out_Packet[2];
					pwml4=Out_Packet[3];
					PCA0CPM3 |= 0x49; // enable compare function and enable match and interrupt for match for pca
				}
			}			
			EIE1 |= 0x10; // enable PCA interrupt

			break;
			case CMD_DISABLE_SERVO:
	      	//cmd: CMD, SERVO
			{
				Out_Packet[0]=0; // command is processed
				LedToggle();
				pwmNumber=Out_Packet[1];
				switch(pwmNumber)
				{
				// big endian 16 bit value to load into PWM controller
					case 0:
					{ // servo0
						PCA0CPM0 &= ~0x40; // disable compare function
					}
					break;
					case 1:
					{ // servo1
						PCA0CPM1 &= ~0x40; // disable compare function
					}
					break;
					case 2:
					{ // servo2
						PCA0CPM2 &= ~0x40; // disable compare function
					}
					break;
					case 3:
					{ // servo3
						PCA0CPM3 &= ~0x40; // disable compare function
					}
				}			
			}
			break;
			case CMD_SET_ALL_SERVOS: // cmd: CMD, PWMValue0 (2 bytes bigendian), PWMValue1 (2 bytes bigendian)
			{
				Out_Packet[0]=0; // command is processed
				LedToggle();
				
				// TODO the set all servos probably doesn't work because it doesn't save the values for the ISR to later load

				PCA0CPL0=Out_Packet[2];
				PCA0CPH0=Out_Packet[1]; // store the PCA compare value for later interrupt to load, write low value FIRST
				PCA0CPM0 |= 0x49; // enable compare function and match and interrupt for match for pca
				
				PCA0CPL1=Out_Packet[4];
				PCA0CPH1=Out_Packet[3];
				PCA0CPM1 |= 0x49; // enable compare function and enable match and interrupt for match for pca
				
				PCA0CPL2=Out_Packet[6];
				PCA0CPH2=Out_Packet[5];
				PCA0CPM2 |= 0x49; // enable compare function and enable match and interrupt for match for pca
				
				PCA0CPL3=Out_Packet[8];
				PCA0CPH3=Out_Packet[7];
				PCA0CPM3 |= 0x49; // enable compare function and enable match and interrupt for match for pca
			}	
			EIE1 |= 0x10; // enable PCA interrupt
			break;
			case CMD_DISABLE_ALL_SERVOS: 	//cmd: CMD
			{
				Out_Packet[0]=0; // command is processed
				LedToggle();
				PCA0CPM0 &= ~0x40; // disable compare function
				PCA0CPM1 &= ~0x40; // disable compare function
				PCA0CPM2 &= ~0x40; // disable compare function
				PCA0CPM3 &= ~0x40; // disable compare function
			}
			
			break;
			case CMD_SET_TIMER0_RELOAD_VALUE: 	
			{
				Out_Packet[0]=0; // command is processed
				LedToggle();
				TH0=255-Out_Packet[1]; // timer0 reload value, 2 for 60Hz, 1 for 91Hz servo pulse rate, 0 for 180 Hz
			}
			break;
			case CMD_SET_PCA0MD_CPS: // bit are ORed with 0x7, left shifted by one, and set to the PCA0MD bits 3:1
			{
				Out_Packet[0]=0; // command is processed
				LedToggle();
				// must disable watchdog before changing these bits
				PCA0MD&=~0x40;	

				PCA0MD = (PCA0MD & 0xf1) | (  (0x7&Out_Packet[1])  <<1); // this is the PCA control register, bits 3:1 are the CPS bits that control the PCA clock source
				// CPS = 0 sysclk/12
				// CPS = 1 sysclk/4
				// CPS = 2 timer0 overflow

				PCA0MD|=0x40; // re-enable watchdog
				PCA0CPH4 = 0xff;  // write value to WDT PCA to start watchdog                   

			}
			break;
			case CMD_SET_PORT2:
			{
				Out_Packet[0]=0; //ack
				LedToggle();
				P2=Out_Packet[1];
				break;
			}
			case CMD_SEND_WOWWEE_RS_CMD:
			{
				// P2.0 is high
				Out_Packet[0]=0; // cmd has been processed
				LedToggle();
				
				rsv2_cmd.cmd = (Out_Packet[1])|(Out_Packet[2]<<8);
				//rsv2_cmd.msb = (Out_Packet[2]);

				rsv2_precmd = 1; // we're sending the start 'bit'
				rsv2_cyclesleft = 517; // 8/1200
				rsv2_cmdidx = 12; // 12 bits
				rsv2_sendcmd = 1; // we're sending a cmd state
				EIE1|=0x80; // enable timer 3 interrupts, disabled at end of cmd
				break;
			}			
			case CMD_SET_PORT_DOUT:
			{
				Out_Packet[0]=0; // cmd has been processed
				LedToggle();
				
				P1MDOUT= (Out_Packet[1]); // setting bit to 1 makes port pin push-pull, 0 makes it open drain
				P2MDOUT= (Out_Packet[2]);
				break;
			}

		} // switch
		EA=1; // enable interrupts
		//LedOn();
	} // while(1)
}