/*
** ###################################################################
**
**     THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**
**     Filename  : PWM1.C
**
**     Project   : Full1
** 
**     Processor : MC68HC908AZ60MFU
**
**     Beantype  : PWM
**
**     Version   : Bean 02.039, 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 implements a pulse-width modulation generator
**         that generates signal with variable duty and fixed cycle. 
**
**     Settings  :
**
**         Used output pin             : 
**             ----------------------------------------------------
**                Number (on package)  |    Name
**             ----------------------------------------------------
**                       8             |  PTF4_TBCH0
**             ----------------------------------------------------
**
**         Timer name                  : TIMB_PWM [16-bit] 
**           PWM signal generation     : unbuffered
**
**         Counter                     : TBCNT     [65]
**         Mode register               : TBSCR     [64]
**         Run register                : TBSCR     [64]
**         Prescaler                   : TBSCR     [64]
**         Compare register            : TBCH0     [70]
**         Flip-flop register          : TBSC0     [69]
**
**         User handling procedure     : not specified
**
**         Port name                   : PTF
**         Bit number (in port)        : 4
**         Bit mask of the port        : 16
**         Port data register          : PTF       [9]
**         Port control register       : DDRF      [13]
**
**         Initialization:
**              Output level           : low
**              Timer                  : Enabled
**              Event                  : Enabled
**         High-speed CPU mode
**             Prescaler               : divide-by-8
**             Clock                   : 250000 Hz
**           Initial value of            period     pulse width
**             Xtal ticks              : 2048000    1024000
**             microseconds            : 256000     128000
**             milliseconds            : 256        128
**             seconds (real)          : 0.2560000  0.1280000
**
**
**     Contents  :
**
**         SetRatio16 - byte PWM1_SetRatio16(word Ratio);
**         SetDutyUS  - byte PWM1_SetDutyUS(word Time);
**         SetDutyMS  - byte PWM1_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 PWM1. */

#include "PE_Error.h"
#include "PWM1.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      :  PWM1_SetPV (bean PWM)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
static void SetPV(byte Val)
{
  TBSCR_PS = Val;                      /* Store given value to the prescaler */
  TBSCR_TRST = 1;                      /* Reset counter */
}

/*
** ===================================================================
**     Method      :  PWM1_SetRatio (bean PWM)
**
**     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 = TBMODH;              /* Load of actual value of the modulo register */
  period.b.low = TBMODL;
  duty.w = (period.w * (dword)RatioStore) >> 16; /* Calculate new value according to the given ratio */
  TBCH0H = duty.b.high;                /* Store result to the compare register */
  TBCH0L = duty.b.low;
}

/*
** ===================================================================
**     Method      :  PWM1_SetRatio16 (bean PWM)
**
**     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%
**     Returns     :
**         ---        - Error code
** ===================================================================
*/
byte PWM1_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      :  PWM1_SetDutyUS (bean PWM)
**
**     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, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_MATH - Overflow during evaluation
**                           ERR_RANGE - Parameter out of range
** ===================================================================
*/
byte PWM1_SetDutyUS(word Time)
{
  dlong rtval;                         /* Result of two 32-bit numbers multiplication */

  PE_Timer_LngMul((dword)Time,1099511628,&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      :  PWM1_SetDutyMS (bean PWM)
**
**     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 256 ms in high speed CPU mode)
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_MATH - Overflow during evaluation
**                           ERR_RANGE - Parameter out of range
** ===================================================================
*/
byte PWM1_SetDutyMS(word Time)
{
  dlong rtval;                         /* Result of two 32-bit numbers multiplication */

  if (Time >= 256)                     /* Is the given value out of range? */
    return ERR_RANGE;                  /* If yes then error */
  PE_Timer_LngMul((dword)Time,16777216,&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      :  PWM1_Init (bean PWM)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
void PWM1_Init(void)
{
  TBSCR = 48;                          /* Set up output compare mode */
  TBSC0 = 18;                          /* Set up PWM mode */
  TBSC0 = 30;
  RatioStore = 32768;                  /* Store initial value of the ratio */
  TBCH0H = (byte)(32000 >> 8);         /* Store initial value to the duty-compare register */
  TBMODH = (byte)(64000 >> 8);         /* and to the period-modulo register */
  TBCH0L = (byte)32000;
  TBMODL = (byte)64000;
  SetPV(3);                            /* Set prescaler register according to the selected high speed CPU mode */
  TBSCR_TSTOP = 0;                     /* Run counter */
}

/* END PWM1. */

/*
** ###################################################################
**
**     This file was created by UNIS Processor Expert 02.84 for 
**     the Motorola HC08 series of microcontrollers.
**
** ###################################################################
*/
