/*
** ###################################################################
**
**     THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**
**     Filename  : PPG1.C
**
**     Project   : Full1
** 
**     Processor : MC68HC908AZ60MFU
**
**     Beantype  : PPG
**
**     Version   : Bean 02.061, Driver 01.02, CPU db: 2.81.019
**
**     Compiler  : Metrowerks HC08 C Compiler V-5.0.13
**
**     Date/Time : 2.4.2002, 9:36
**
**     Abstract  :
**
**         This bean "PPG" implements a programmable
**         pulse generator that generates signal with variable
**         duty and variable cycle (period).
**
**     Settings  :
**
**         Used output pin             : 
**             ----------------------------------------------------
**                Number (on package)  |    Name
**             ----------------------------------------------------
**                       15            |  PTE2_TACH0
**             ----------------------------------------------------
**
**         Timer name                  : TIMA_PPG [16-bit] 
**           PWM signal generation     : unbuffered
**
**         Counter                     : TACNT     [34]
**         Mode register               : TASC      [32]
**         Run register                : TASC      [32]
**         Prescaler                   : TASC      [32]
**         Compare 1 register          : TAMOD     [36]
**         Compare 2 register          : TACH0     [39]
**         Flip-flop 1 register        : TASC      [32]
**         Flip-flop 2 register        : TASC0     [38]
**
**         User handling procedure     : not specified
**
**         Output pin
**
**         Port name                   : PTE
**         Bit number (in port)        : 2
**         Bit mask of the port        : 4
**         Port data register          : PTE       [8]
**         Port control register       : DDRE      [12]
**         Port function register      : SPCR      [16]
**
**         Runtime setting period      : none
**         Runtime setting ratio       : calculated
**         Initialization:
**              Output level           : low
**              Timer                  : Enabled
**              Event                  : Enabled
**         High-speed CPU mode
**             Prescaler               : divide-by-4
**             Clock                   : 500000 Hz
**           Initial value of            period        pulse width (ratio 25%)
**             Xtal ticks              : 1024000       256000
**             microseconds            : 128000        32000
**             milliseconds            : 128           32
**             seconds (real)          : 0.1280000     0.0320000
**
**
**     Contents  :
**
**         SetRatio16 - byte PPG1_SetRatio16(word Ratio);
**         SetDutyUS  - byte PPG1_SetDutyUS(word Time);
**         SetDutyMS  - byte PPG1_SetDutyMS(word Time);
**
**
**     (c) Copyright UNIS, spol. s r.o. 1997-2002
**
**     UNIS, spol. s r.o.
**     Jundrovska 33
**     624 00 Brno
**     Czech Republic
**
**     http      : www.processorexpert.com
**     mail      : info@processorexpert.com
**
** ###################################################################
*/


/* MODULE PPG1. */

#include "PE_Error.h"
#include "PPG1.h"

#pragma MESSAGE DISABLE C2705          /* WARNING C2705: Possible loss of data */
#pragma MESSAGE DISABLE C5919          /* WARNING C5919: Conversion of floating to unsigned integral */

static word RatioStore;                /* Ratio of L-level to H-level */


/*
** ===================================================================
**     Method      :  PPG1_SetPV (bean PPG)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
static void SetPV(byte Val)
{
  TASC_PS = Val;                       /* Store given value to the prescaler */
  TASC_TRST = 1;                       /* Reset counter */
}

/*
** ===================================================================
**     Method      :  PPG1_SetRatio (bean PPG)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
static void SetRatio(void)
{
  TWREG duty;                          /* Union used for correct access to the 16-bit IO register */
  TWREG period;                        /* Union used for correct access to the 16-bit IO register */

  period.b.high = TAMODH;              /* Load of actual value of the modulo register */
  period.b.low = TAMODL;
  duty.w = (period.w * (dword)RatioStore) >> 16; /* Calculate new value according to the given ratio */
  TACH0H = duty.b.high;                /* Store result to the compare register */
  TACH0L = duty.b.low;
}

/*
** ===================================================================
**     Method      :  PPG1_SetRatio16 (bean PPG)
**
**     Description :
**         This method sets a new duty-cycle ratio.
**     Parameters  :
**         NAME       - DESCRIPTION
**         Ratio      - Ratio is expressed as an 16-bit unsigned integer
**                      number. 0 - 65535 value is proportional
**                      to ratio 0 - 100%
**         Note: Calculated duty-cycle ratio depends on the timer
**               possibilities and on the selected period.
**     Returns     :
**         ---        - Error code
** ===================================================================
*/
byte PPG1_SetRatio16(word Ratio)
{
  RatioStore = Ratio;                  /* Store new value of the ratio */
  SetRatio();                          /* Calculate and set up new appropriate values of the compare and modulo registers */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  PPG1_SetDutyUS (bean PPG)
**
**     Description :
**         This method sets the new duty value of the output signal. The
**         duty is expressed in microseconds as a 16-bit unsigned integer
**         number.
**     Parameters  :
**         NAME       - DESCRIPTION
**         Time       - Duty to set [in microseconds]
**                      (0 to 65535 us in high speed CPU mode)
**     Returns     :
**         ---        - Error code
** ===================================================================
*/
byte PPG1_SetDutyUS(word Time)
{
  dlong rtval;                         /* Result of two 32-bit numbers multiplication */

  PE_Timer_LngMul((dword)Time,2199023256,&rtval); /* Multiply given value and high speed CPU mode coefficient */
  if (PE_Timer_LngHi4(rtval[0],rtval[1],&RatioStore)) /* Is the result greater or equal than 65536 ? */
    RatioStore = 65535;                /* If yes then use maximal possible value */
  SetRatio();                          /* Calculate and set up new appropriate values of the compare and modulo registers */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  PPG1_SetDutyMS (bean PPG)
**
**     Description :
**         This method sets the new duty value of the output signal. The
**         duty is expressed in milliseconds as a 16-bit unsigned integer
**         number.
**     Parameters  :
**         NAME       - DESCRIPTION
**         Time       - Duty to set [in milliseconds]
**                      (0 to 128 ms in high speed CPU mode)
**     Returns     :
**         ---        - Error code
** ===================================================================
*/
byte PPG1_SetDutyMS(word Time)
{
  dlong rtval;                         /* Result of two 32-bit numbers multiplication */

  if (Time >= 128)                     /* Is the given value out of range? */
    return ERR_RANGE;                  /* If yes then error */
  PE_Timer_LngMul((dword)Time,33554432,&rtval); /* Multiply given value and high speed CPU mode coefficient */
  if (PE_Timer_LngHi2(rtval[0],rtval[1],&RatioStore)) /* Is the result greater or equal than 65536 ? */
    RatioStore = 65535;                /* If yes then use maximal possible value */
  SetRatio();                          /* Calculate and set up new appropriate values of the compare and modulo registers */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  PPG1_Init (bean PPG)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
void PPG1_Init(void)
{
  TASC = 48;                           /* Set up output compare mode */
  TASC0 = 18;                          /* Set up unbuffered PWM mode with output signal level low */
  TASC0 = 30;
  RatioStore = 16384;                  /* Store initial value of the ratio */
  TACH0H = (byte)(16000 >> 8);         /* Store initial value to the duty-compare register */
  TAMODH = (byte)(64000 >> 8);         /* and to the period-modulo register */
  TACH0L = (byte)16000;
  TAMODL = (byte)64000;
  SetPV(2);                            /* Set prescaler register according to the selected high speed CPU mode */
  TASC_TSTOP = 0;                      /* Run counter */
}

/* END PPG1. */

/*
** ###################################################################
**
**     This file was created by UNIS Processor Expert 02.84 for 
**     the Motorola HC08 series of microcontrollers.
**
** ###################################################################
*/
