/*
** ###################################################################
**
**     THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**
**     Filename  : TI1.C
**
**     Project   : projRT1
** 
**     Processor : MC68HC908AZ60MFU
**
**     Beantype  : TimerInt
**
**     Version   : Bean 02.053, Driver 01.02, CPU db: 2.81.019
**
**     Compiler  : Metrowerks HC08 C Compiler V-5.0.13
**
**     Date/Time : 2.4.2002, 8:44
**
**     Abstract  :
**
**         This bean "TimerInt" implements a periodic interrupt.
**         When the bean and its events are enabled, the "OnInterrupt"
**         event is called periodically with the period that you specify.
**         TimerInt supports also changing the period in runtime.
**         The source of periodic interrupt can be timer compare or reload 
**         register or timer-overflow interrupt (of free running counter).
**
**     Settings  :
**
**         Timer name                  : TIM (16-bit)
**         Compare name                : TIMmod
**         Counter shared              : No
**
**         High-speed CPU mode
**             Prescaler               : divide-by-32
**           Initial period/frequency
**             Xtal ticks              : 8000000
**             microseconds            : 1000000
**             milliseconds            : 1000
**             seconds                 : 1
**             seconds (real)          : 1.0000000
**             Hz                      : 1
**
**         Runtime setting             : modes (list of settings)
**            -----------------------------------------------------------------------
**            |  period  |     Prescaler      |        Real period [seconds]        |
**     --------    or    |-----------------------------------------------------------
**     | mode | freqency | high | low  | slow |    high    |    low     |    slow   |
**     ------------------------------------------------------------------------------
**     |  0   |  1sec    | 32   | -    | -    | 1.0000000  | -          | -         | 
**     |  1   |  0_5sec  | 16   | -    | -    | 0.5000000  | -          | -         | 
**     |  2   |  2sec    | 64   | -    | -    | 2.0000000  | -          | -         | 
**     ------------------------------------------------------------------------------
**
**         Initialization:
**              Timer                  : Enabled
**              Events                 : Enabled
**
**         Timer registers
**              Counter                : TCNT      [76]
**              Mode                   : TSC       [75]
**              Run                    : TSC       [75]
**              Prescaler              : TSC       [75]
**
**         Compare registers
**              Compare                : TMOD      [78]
**
**         Flip-flop registers
**
**     Contents  :
**
**         SetPeriodMode - byte TI1_SetPeriodMode(byte Mode);
**
**
**     (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 TI1. */

#include "Events.h"
#include "PE_Error.h"
#include "TI1.h"

#pragma MESSAGE DISABLE C2705          /* WARNING C2705: Possible loss of data */
#pragma MESSAGE DISABLE C5919          /* WARNING C5919: Conversion of floating to unsigned integral */

static bool EnUser;                    /* Enable/Disable device by user */
static byte TMode;                     /* Number of actual timer modes */


/* Table of prescaler values for high speed CPU mode */
static const byte HighPresc[3] = {5,4,6};

/* Table of compare register values for high speed CPU mode */
static const word HighComp[3] = {62500,62500,62500};

/*
** ===================================================================
**     Method      :  TI1_SetCV (bean TimerInt)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
static void SetCV(word Val)
{
  TMODH = (*(TWREG*)&Val).b.high;      /* Store given value to the compare register */
  TMODL = (*(TWREG*)&Val).b.low;
}

/*
** ===================================================================
**     Method      :  TI1_SetPV (bean TimerInt)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
static void SetPV(byte Val)
{
  TSC_PS = Val;                        /* Store given value to the prescaler */
  TSC_TRST = 1;                        /* Reset counter */
}

/*
** ===================================================================
**     Method      :  TI1_HWEnDi (bean TimerInt)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
static void HWEnDi(void)
{
  if (EnUser) {                        /* Enable device? */
    TSC_TSTOP = 0;                     /* Run counter */
  }
  else {                               /* Disable device? */
    TSC_TSTOP = 1;                     /* Stop counter */
    TSC_TRST = 1;                      /* Reset counter */
  }
}

/*
** ===================================================================
**     Method      :  TI1_SetPeriodMode (bean TimerInt)
**
**     Description :
**         Switch the bean to the specified mode (changes the
**         period/frequency using a mode's values). This method
**         reduce the time needed for setting a new period value.
**         This method can be used only if you specify a list of
**         possible period settings at design time (see
**         Timing dialog box - Runtime setting - from a list of
**         values). Each of these settings constitutes a mode and
**         Processor ExpertTM assigns them a mode identifier. The
**         prescaler and compare values corresponding to each mode
**         are calculated at design time. You may switch modes at
**         runtime by referring only to a mode identifier. No
**         run-time calculations are performed, all the calculations
**         are performed at design time.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Mode       - Mode to switch to (0 to 2)
**                         0.   1sec
**                         1.   0_5sec
**                         2.   2sec
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_VALUE - Value out of range,
**                           requested timing mode is not defined
** ===================================================================
*/
byte TI1_SetPeriodMode(byte Mode)
{
  bool TmrRun;                         /* Temporary flag enable/disable */

  if (Mode > 2)                        /* Is a values of mode identifier out of range? */
    return ERR_VALUE;                  /* If yes then error */
  TmrRun = EnUser;                     /* Store actual device state */
  if (EnUser) {                        /* Is the device enabled by user? */
    EnUser = FALSE;                    /* If yes then set the flag "device disabled" */
    HWEnDi();                          /* Enable/disable device according to status flags */
  }   
  TMode = Mode;                        /* Store given mode identifier to the variable TMode */
  SetCV(HighComp[Mode]);               /* Store appropriate value to the compare register according to the value of mode identifier if selected high speed CPU mode */
  SetPV(HighPresc[TMode]);             /* Set prescaler register according to the value of mode identifier if selected high speed CPU mode */
  if (TmrRun) {                        /* Was the device disabled? */
    EnUser=TRUE;                       /* If yes set flag "device enabled" */
    HWEnDi();                          /* Enable/disable device according to status flags */
  }
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  TI1_Init (bean TimerInt)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
void TI1_Init(void)
{
  TSC = 112;                           /* Set up (output) compare mode */
  TMode = 0;                           /* Set number of actual timer mode to 0 */
  EnUser = TRUE;                       /* Enable device */
  SetCV(HighComp[TMode]);              /* Store appropriate value to the compare register according to the value of mode identifier if selected high speed CPU mode */
  SetPV(HighPresc[TMode]);             /* Set prescaler register according to the value of mode identifier if selected high speed CPU mode */
  HWEnDi();                            /* Enable/disable device according to status flags */
}

/*
** ===================================================================
**     Method      :  TI1_Interrupt (bean TimerInt)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
__interrupt void TI1_Interrupt(void)
{
  TSC_TOF = 0;                         /* Reset interrupt request flag */
  TI1_OnInterrupt();                   /* Invoke user event */
}


/* END TI1. */

/*
** ###################################################################
**
**     This file was created by UNIS Processor Expert 02.84 for 
**     the Motorola HC08 series of microcontrollers.
**
** ###################################################################
*/
