/*
** ###################################################################
**
**     THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**
**     Filename  : AD2.C
**
**     Project   : Full1
** 
**     Processor : MC68HC908AZ60MFU
**
**     Beantype  : ADconverter
**
**     Version   : Bean 02.024, 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 device "ADconverter" implements an A/D converter,
**         its control methods and interrupt/event handling procedure.
**
**     Settings  :
**
**         AD control register         : ADSCR       [56]
**         AD control register         : ADICLK      [58]
**         AD result register          : ADCres      [57]
**         AD result register          : ADCres      [57]
**         AD result register          : ADCres      [57]
**         AD result register          : ADCres      [57]
**         AD result register          : ADCres      [57]
**         AD result register          : ADCres      [57]
**         AD result register          : ADCres      [57]
**         AD result register          : ADCres      [57]
**         AD result register          : ADCres      [57]
**         AD result register          : ADCres      [57]
**         AD result register          : ADCres      [57]
**         AD result register          : ADCres      [57]
**         AD result register          : ADCres      [57]
**         AD result register          : ADCres      [57]
**         AD result register          : ADCres      [57]
**         Interrupt name              : INT_ADC 
**         Interrupt enable reg.       : ADSCR       [56]
**         User handling procedure     : AD2_OnEnd
**         Number of conversions       : 1
**
**         Input pins
**
**              Port name              : PTB
**              Bit number (in port)   : 1
**              Bit mask of the port   : 2
**              Port data register     : PTB         [1]
**              Port control register  : DDRB        [5]
**              
**              Port name              : PTB
**              Bit number (in port)   : 0
**              Bit mask of the port   : 1
**              Port data register     : PTB         [1]
**              Port control register  : DDRB        [5]
**              
**              Port name              : PTB
**              Bit number (in port)   : 2
**              Bit mask of the port   : 4
**              Port data register     : PTB         [1]
**              Port control register  : DDRB        [5]
**              
**              Port name              : PTB
**              Bit number (in port)   : 3
**              Bit mask of the port   : 8
**              Port data register     : PTB         [1]
**              Port control register  : DDRB        [5]
**              
**              Port name              : PTB
**              Bit number (in port)   : 4
**              Bit mask of the port   : 16
**              Port data register     : PTB         [1]
**              Port control register  : DDRB        [5]
**              
**              Port name              : PTB
**              Bit number (in port)   : 5
**              Bit mask of the port   : 32
**              Port data register     : PTB         [1]
**              Port control register  : DDRB        [5]
**              
**              Port name              : PTB
**              Bit number (in port)   : 6
**              Bit mask of the port   : 64
**              Port data register     : PTB         [1]
**              Port control register  : DDRB        [5]
**              
**              Port name              : PTB
**              Bit number (in port)   : 7
**              Bit mask of the port   : 128
**              Port data register     : PTB         [1]
**              Port control register  : DDRB        [5]
**              
**              Port name              : PTD
**              Bit number (in port)   : 0
**              Bit mask of the port   : 1
**              Port data register     : PTD         [3]
**              Port control register  : DDRD        [7]
**              
**              Port name              : PTD
**              Bit number (in port)   : 1
**              Bit mask of the port   : 2
**              Port data register     : PTD         [3]
**              Port control register  : DDRD        [7]
**              
**              Port name              : PTD
**              Bit number (in port)   : 2
**              Bit mask of the port   : 4
**              Port data register     : PTD         [3]
**              Port control register  : DDRD        [7]
**              
**              Port name              : PTD
**              Bit number (in port)   : 3
**              Bit mask of the port   : 8
**              Port data register     : PTD         [3]
**              Port control register  : DDRD        [7]
**              
**              Port name              : PTD
**              Bit number (in port)   : 4
**              Bit mask of the port   : 16
**              Port data register     : PTD         [3]
**              Port control register  : DDRD        [7]
**              
**              Port name              : PTD
**              Bit number (in port)   : 5
**              Bit mask of the port   : 32
**              Port data register     : PTD         [3]
**              Port control register  : DDRD        [7]
**              
**              Port name              : PTD
**              Bit number (in port)   : 6
**              Bit mask of the port   : 64
**              Port data register     : PTD         [3]
**              Port control register  : DDRD        [7]
**              
**         Initialization:
**              Conversion             : Enabled
**              Event                  : Enabled
**         High-speed CPU mode
**             Prescaler               : divide-by-16
**
**     Contents  :
**
**         Start    - byte AD2_Start(void);
**         Stop     - byte AD2_Stop(void);
**         Measure  - byte AD2_Measure(bool WaitForResult);
**         GetValue - byte AD2_GetValue(byte *Values);
**
**
**     (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
**
** ###################################################################
*/

#include "Events.h"
#include "PE_Error.h"
#include "PE_Cnvrt.h"
#include "AD2.h"

#define STOP       0
#define MEASURE    1
#define CONTINUOUS 2

static bool OutFlg;
static byte SumCnt;
static byte ModeFlg;
extern byte PE_Cnvrt_ADResults[];

static word SumV1;
static word SumV0;
static word SumV2;
static word SumV3;
static word SumV4;
static word SumV5;
static word SumV6;
static word SumV7;
static word SumV8;
static word SumV9;
static word SumV10;
static word SumV11;
static word SumV12;
static word SumV13;
static word SumV14;
static word OutV1;
static word OutV0;
static word OutV2;
static word OutV3;
static word OutV4;
static word OutV5;
static word OutV6;
static word OutV7;
static word OutV8;
static word OutV9;
static word OutV10;
static word OutV11;
static word OutV12;
static word OutV13;
static word OutV14;

/*
** ===================================================================
**     Method      :  AD2_ClrSumV (bean ADconverter)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
static void ClrSumV(void)
{
  SumV1 = 0;
  SumV0 = 0;
  SumV2 = 0;
  SumV3 = 0;
  SumV4 = 0;
  SumV5 = 0;
  SumV6 = 0;
  SumV7 = 0;
  SumV8 = 0;
  SumV9 = 0;
  SumV10 = 0;
  SumV11 = 0;
  SumV12 = 0;
  SumV13 = 0;
  SumV14 = 0;
}

/*
** ===================================================================
**     Method      :  AD2_SetOutV (bean ADconverter)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
static void SetOutV(void)
{
  if (++SumCnt == 1) {
    SumCnt = 0;
    OutFlg = TRUE;
    OutV1 = SumV1;
    OutV0 = SumV0;
    OutV2 = SumV2;
    OutV3 = SumV3;
    OutV4 = SumV4;
    OutV5 = SumV5;
    OutV6 = SumV6;
    OutV7 = SumV7;
    OutV8 = SumV8;
    OutV9 = SumV9;
    OutV10 = SumV10;
    OutV11 = SumV11;
    OutV12 = SumV12;
    OutV13 = SumV13;
    OutV14 = SumV14;
    ClrSumV();
    if (ModeFlg == MEASURE) {
      ModeFlg = STOP;
      PE_Cnvrt_RunSto(32767);
    }
    AD2_OnEnd();
  }
}

/*
** ===================================================================
**     Method      :  AD2_HWEnDi (bean ADconverter)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
static void  HWEnDi(void)
{
  SumCnt = 0;
  OutFlg = FALSE;
    ClrSumV();
  if (ModeFlg) {                   /* Enable device? */
    PE_Cnvrt_RunSta(32767);  /* Enable */
  }
  else {
    PE_Cnvrt_RunSto(32767);  /* Disable */
  }
}

/*
** ===================================================================
**     Method      :  AD2_AD2_Interrupt (bean ADconverter)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
void  AD2_Interrupt(void)
{
  if (ModeFlg != STOP) {
    SumV1 += PE_Cnvrt_ADResults[1];
    SumV0 += PE_Cnvrt_ADResults[0];
    SumV2 += PE_Cnvrt_ADResults[2];
    SumV3 += PE_Cnvrt_ADResults[3];
    SumV4 += PE_Cnvrt_ADResults[4];
    SumV5 += PE_Cnvrt_ADResults[5];
    SumV6 += PE_Cnvrt_ADResults[6];
    SumV7 += PE_Cnvrt_ADResults[7];
    SumV8 += PE_Cnvrt_ADResults[8];
    SumV9 += PE_Cnvrt_ADResults[9];
    SumV10 += PE_Cnvrt_ADResults[10];
    SumV11 += PE_Cnvrt_ADResults[11];
    SumV12 += PE_Cnvrt_ADResults[12];
    SumV13 += PE_Cnvrt_ADResults[13];
    SumV14 += PE_Cnvrt_ADResults[14];
    SetOutV();
  }
}


/*
** ===================================================================
**     Method      :  AD2_Start (bean ADconverter)
**
**     Description :
**         Start automatic measurements - they run one after another
**         (event OnEnd is called whenever a measurement is done).
**     Parameters  : None
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_BUSY - A conversion is already
**                           running
** ===================================================================
*/
byte  AD2_Start(void)
{
  if (ModeFlg != STOP)
    return ERR_BUSY;
  ModeFlg = CONTINUOUS;
  SumCnt = 0;
  OutFlg = FALSE;
  ClrSumV();
  PE_Cnvrt_RunSta(32767);
  return ERR_OK;
}

/*
** ===================================================================
**     Method      :  AD2_Stop (bean ADconverter)
**
**     Description :
**         Stop automatic measurements.
**     Parameters  : None
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_BUSY - A conversion is already
**                           running
** ===================================================================
*/
byte  AD2_Stop(void)
{
  if (ModeFlg != CONTINUOUS)
    return ERR_BUSY;
  PE_Cnvrt_RunSto(32767);
  ModeFlg = STOP;
  return ERR_OK;
}

/*
** ===================================================================
**     Method      :  AD2_Measure (bean ADconverter)
**
**     Description :
**         If the automatic measurements are not started, it starts
**         one measure, otherwise it uses current automatically
**         started measure. Then this method waits till the current
**         measurement is finished and returns the measured value
**         (you can determine by a parameter how much time the
**         procedure should wait till the end of the measurement
**         (loop). Take care that in this interval no other task is
**         performed - e.g. the watchdog cannot be cleared) .
**     Parameters  :
**         NAME            - DESCRIPTION
**         Wai             - Wait for result of
**                           the convertion
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_DISABLED - Device is disabled
**                           ERR_BUSY - a conversion is already
**                           running
** ===================================================================
*/
byte  AD2_Measure(bool WaitForResult)
{
  if (ModeFlg != STOP)
    return ERR_BUSY;
  ModeFlg = MEASURE;
  SumCnt = 0;
  OutFlg = FALSE;
    ClrSumV();
  PE_Cnvrt_RunSta(32767);
  if (WaitForResult)
    while (ModeFlg == MEASURE);
  return ERR_OK;
}

/*
** ===================================================================
**     Method      :  AD2_GetValue (bean ADconverter)
**
**     Description :
**         Return the last measured value.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Dat             - Pointer to array contained
**                           measured data
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_DISABLED - Device is disabled
**                           ERR_BUSY - A conversion is already
**                           running
**                           ERR_NOTAVAIL - Requested value not
**                           available
** ===================================================================
*/
byte AD2_GetValue(byte *Values)
{
  if (!OutFlg)
    return ERR_NOTAVAIL;
  *Values++ = (byte)OutV1;
  *Values++ = (byte)OutV0;
  *Values++ = (byte)OutV2;
  *Values++ = (byte)OutV3;
  *Values++ = (byte)OutV4;
  *Values++ = (byte)OutV5;
  *Values++ = (byte)OutV6;
  *Values++ = (byte)OutV7;
  *Values++ = (byte)OutV8;
  *Values++ = (byte)OutV9;
  *Values++ = (byte)OutV10;
  *Values++ = (byte)OutV11;
  *Values++ = (byte)OutV12;
  *Values++ = (byte)OutV13;
  *Values = (byte)OutV14;
  return ERR_OK;
}

/*
** ===================================================================
**     Method      :  AD2_Init (bean ADconverter)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
void AD2_Init(void)
{
  SumCnt = 0;
  OutFlg = FALSE;
  ModeFlg = STOP;
  PE_Cnvrt_SetPro(0, AD2_Interrupt);
  DDRB = 0;                                                   
  DDRD &= 128;                                       
  PE_Cnvrt_SetSpe(1, 128);
  PE_Cnvrt_SetSpe(0, 128);
  PE_Cnvrt_SetSpe(2, 128);
  PE_Cnvrt_SetSpe(3, 128);
  PE_Cnvrt_SetSpe(4, 128);
  PE_Cnvrt_SetSpe(5, 128);
  PE_Cnvrt_SetSpe(6, 128);
  PE_Cnvrt_SetSpe(7, 128);
  PE_Cnvrt_SetSpe(8, 128);
  PE_Cnvrt_SetSpe(9, 128);
  PE_Cnvrt_SetSpe(10, 128);
  PE_Cnvrt_SetSpe(11, 128);
  PE_Cnvrt_SetSpe(12, 128);
  PE_Cnvrt_SetSpe(13, 128);
  PE_Cnvrt_SetSpe(14, 128);
  HWEnDi();
}


/* END AD2. */


/*
** ###################################################################
**
**     This file was created by UNIS Processor Expert 02.84 for 
**     the Motorola HC08 series of microcontrollers.
**
** ###################################################################
*/
