/*
Matt Hobson
Amy Thompson
Adeola Arije
ELEN 598
Spring, 2006
File Name: ServoPWM.c
File Purpose: Initialize the registers related to PWM for operation with  
              hobby servos.  The 8, 8-bit PWM channels are being concatenated 
			  to form 4, 16-bit PWM channels to control the corresponding 4
			  hobby servos.  The servos require a 50Hz refresh signal.  This
			  code is written for the Axiom CML12S256 board which features a 
			  4MHz crystal (2MHz E-Clock(bus speed)).  The on-chip PLL is 
			  being used to bump the bus speed up to 24MHz.
			  
*/

//********************************** INCLUDE ***********************************

#include <mc9s12dp256.h>
#include <stdio.h>

//******************************** PROTOTYPES **********************************

extern void delay (int);  //Delay function

//********************************** DEFINE ************************************

//Using 24MHz bus frequency:

#define PERIOD 60000  //Clock A count required to yield 20ms period (50Hz)
#define GATEDOWN 1630  //Clock count required to place gate servos in down pos. 1630
#define GATEUP   4300  //Clock count required to place gate servos in up pos.  4300
#define d 25           //Delay constant for servos

//********************************* VARIABLES **********************************

unsigned int i = 0;  //Counter Variable

//*************************** SERVOPWM_INIT FUNCTION ***************************

void ServoPWM_Init (void)
{
  PWMCLK = 0x00;    //Select clocks A and B
  PWMPRCLK = 0x33;  //Set prescale for A and B to (bus frequency) / 8   
  PWMCTL = 0xF0;    //Concatenate all channels to form 4 16-bit PWMs
  PWMPOL = 0xAA;    //Set channel[7,5,3,1] polarity as high (normal PWM operation)
  PWMCAE = 0x00;    //Operate all channels in left-aligned mode
  PWMPER01 = PERIOD; //Set the period register to generate a 50Hz signal
  PWMPER23 = PERIOD; //"   "
  PWMPER45 = PERIOD; //"   "
  PWMPER67 = PERIOD; //"   " 
  PWMDTY01 = GATEDOWN;   //Set duty cycle to place servos in the down position 
  PWMDTY23 = GATEDOWN;   //"  "
  PWMDTY45 = GATEDOWN;   //"  "
  PWMDTY67 = GATEDOWN;   //"  "
  PWME = 0xAA;       //Enable the concatenated PWM channels
}

//***************************** GATESTART FUNCTION *****************************

void gateStart (void)
{
   for(i=GATEDOWN; i<GATEUP; i++)  //Raise all gates
   {
      PWMDTY01 = i;
	  PWMDTY23 = i;
	  PWMDTY45 = i;
	  PWMDTY67 = i;
	  delay(d);
   }
   delay(500);
   for(i=GATEUP; i>GATEDOWN; i--)  //Lower all gates
   {
      PWMDTY01 = i;
	  PWMDTY23 = i;
	  PWMDTY45 = i;
	  PWMDTY67 = i;
	  delay(d);
   }   
}

//***************************** GATEDOWN FUNCTION ******************************

void gateDown (unsigned char gate)
{
   if(gate == 0)
    {
      for(i=GATEUP; i>GATEDOWN; i--)
       {
	     PWMDTY01 = i;
		 delay(d);
	   }
    }
   if(gate == 1)
    {
      for(i=GATEUP; i>GATEDOWN; i--)
       {
	     PWMDTY23 = i;
		 delay(d);
	   }
    }   
   if(gate == 2)
    {
      for(i=GATEUP; i>GATEDOWN; i--)
       {
	     PWMDTY45 = i;
		 delay(d);
	   }
    }      
   if(gate == 3)
    {
      for(i=GATEUP; i>GATEDOWN; i--)
       {
	     PWMDTY67 = i;
		 delay(d);
	   }
    }
}

//****************************** GATEUP FUNCTION *******************************

void gateUp (unsigned char gate)
{
   if(gate == 0)
    {
      for(i=GATEDOWN; i<GATEUP; i++)
       {
	     PWMDTY01 = i;
		 delay(d);
	   }
    }
   if(gate == 1)
    {
      for(i=GATEDOWN; i<GATEUP; i++)
       {
	     PWMDTY23 = i;
		 delay(d);
	   }
    }     
   if(gate == 2)
    {
      for(i=GATEDOWN; i<GATEUP; i++)
       {
	     PWMDTY45 = i;
		 delay(d);
	   }
    }      
   if(gate == 3)
    {
      for(i=GATEDOWN; i<GATEUP; i++)
       {
	     PWMDTY67 = i;
		 delay(d);
	   }
    }
}

//******************************************************************************