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

PWM01.c module

ME 218B Project Hats Off

Team Firefox

March 7, 2010

This code initializes the PWM for Channel 0 and gives the public functions:

  - initPWM0(unsigned char pol, unsigned char postScale) to initialize PWM

    for Channel 0.

    Polarity and PWM Clock Select are parameters

  - SetPWM0Period(unsigned int period) to set the period

  - SetPWM0DutyCycle(unsigned int dutyCycle) to set the duty cycle.

 

When used in main(), the user still needs to

  - Set the Prescale value

    (ex: PWMPRCLK = (PWMPRCLK &~ _S12_PCKA2) | _S12_PCKA1 | _S12_PCKA0;)

  - Set the Postscale value (ex: PWMSCLA = 75;)

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

 

 

/*---------------------------- Include Files ------------------------------*/

#include "PWM01.h"

 

 

/*-------------------------- Module Code -----------------------------*/

//Initializes PWM for Channel 0 and takes in the parameters to determine PWM

//Output Polarity and PWM Clock Select (just prescale or prescale & postscale.)

void PWM0_Init(unsigned char pol, unsigned char postScale){

  PWME = PWME | _S12_PWME0;         //Enable PWM 0

  MODRR = MODRR | _S12_MODRR0;      //Map PWM0 to Port T

 

  if (pol)

    PWMPOL = PWMPOL | _S12_PPOL0;   //Set polarity for PWM0(output initially hi)

  else

    PWMPOL = PWMPOL &~ _S12_PPOL0;  // 0

 

  if (postScale)                    //if postScale = 1 = preScale and postScale

    PWMCLK = PWMCLK | _S12_PCLK0;   //PWM0 use clock SA = 1

  else

    PWMCLK = PWMCLK &~ _S12_PCLK0;  //PWM0 use clock SA = 0 = prescaled only

 

  setPWM0DutyCycle(0);

}

 

 

//Set the number of clock counts in one period

void setPWM0Period(unsigned int period){

  PWMPER0 = period;

}

 

//Set the number of clock counts for the active part of the duty cycle

void setPWM0DutyCycle(unsigned int dutyCycle){

  PWMDTY0 = dutyCycle;

}

 

 

 

 

 

//Initializes PWM for Channel 1 and takes in the parameters to determine PWM

//Output Polarity and PWM Clock Select (just prescale or prescale & postscale.)

void PWM1_Init(unsigned char pol, unsigned char postScale){

  PWME = PWME | _S12_PWME1;         //Enable PWM 0

  MODRR = MODRR | _S12_MODRR1;      //Map PWM0 to Port T

 

  if (pol)

    PWMPOL = PWMPOL | _S12_PPOL1;   //Set polarity for PWM1(output initially hi)

  else

    PWMPOL = PWMPOL &~ _S12_PPOL1;  // 0

 

  if (postScale)                    //if postScale = 1 = preScale and postScale

    PWMCLK = PWMCLK | _S12_PCLK1;   //PWM1 use clock SA = 1

  else

    PWMCLK = PWMCLK &~ _S12_PCLK1;  //PWM1 use clock SA = 0 = prescaled only

 

  setPWM1DutyCycle(0);

}

 

//Set the number of clock counts in one period

void setPWM1Period(unsigned int period){

  PWMPER1 = period;

}

 

//Set the number of clock counts for the active part of the duty cycle

void setPWM1DutyCycle(unsigned int dutyCycle){

  PWMDTY1 = dutyCycle;

}