garfield

飞思卡尔dsc mc56f8366 ad

0
阅读(3428)

#include "Events.h"
#include "AD1.h"

#define IDLE            0              /* IDLE state           */
#define MEASURE         1              /* MESURE state         */
#define CONTINUOUS      2              /* CONTINUOUS state     */
#define SINGLE          3              /* SINGLE state         */
static const byte Table[] = {          /* Table of mask constants */
1,2};
static const byte Channels[] = {       /* Number of the channel for each used sample */
0,1};
/*lint -esym(765,AD1_EnUser) Disable MISRA rule (23) checking for symbols (AD1_EnUser). */
bool AD1_EnUser;                       /* Enable/Disable device */
static bool EnEvent;                   /* Enable/Disable events */
static byte OutFlg;                    /* Measurement finish flags */
static byte SumChan;                   /* Number of the measured channels */
/*lint -esym(765,AD1_ModeFlg) Disable MISRA rule (23) checking for symbols (AD1_ModeFlg). */
volatile byte AD1_ModeFlg;             /* Current state of device */
static word AD1_OutV[2];               /* Sum of measured values */
static word HighLimitSample0;          /* High limit value for sample 0 */
static word LowLimitSample0;           /* Low limit value for sample 0 */
static word OffsetSample0;             /* Offset value for sample 0 */
/*
** ===================================================================
**     Method      :  AD1_InterruptCC (component ADC)
**
**     Description :
**         The method services the conversion complete interrupt of the
**         selected peripheral(s) and eventually invokes the beans
**         event(s).
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
#pragma interrupt alignsp saveall
void AD1_InterruptCC(void)
{
  setRegBits(ADCA_ADSTAT,2048);        /* Clear EOSI flag */
  if (AD1_ModeFlg != SINGLE) {
    AD1_OutV[0] = getReg(ADCA_ADRSLT0); /* Save measured values to the output buffer */
    AD1_OutV[1] = getReg(ADCA_ADRSLT1); /* Save measured values to the output buffer */
    OutFlg = 3;                        /* Measured values are available */
    if (AD1_ModeFlg == CONTINUOUS) {   /* Running in continuous mode? */
      setRegBit(ADCA_ADCR1,START);     /* Launching of conversion */
    }
    else {
      AD1_ModeFlg = IDLE;              /* Set the bean to the idle mode */
    }
    if (EnEvent) {                     /* Are events enables? */
      AD1_OnEnd();                     /* If yes then invoke user event */
    }
  }
  else {
    AD1_OutV[SumChan] = getReg(ADCA_ADRSLT0); /* Save measured values to the output buffer */
    OutFlg |= Table[SumChan];          /* Measured values are available */
    AD1_ModeFlg = IDLE;                /* Set the bean to the idle mode */
    if (EnEvent) {                     /* Are events enables? */
      AD1_OnEnd();                     /* If yes then invoke user event */
    }
  }
}
/*
** ===================================================================
**     Method      :  HWEnDi (component ADC)
**
**     Description :
**         Enables or disables the peripheral(s) associated with the bean.
**         The method is called automatically as a part of Enable,
**         Disable and Init methods and several internal methods.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
static void HWEnDi(void)
{
  volatile word i;
  if (AD1_EnUser) {                    /* Enable device? */
    if (getRegBits(ADCA_ADPOWER,(ADCA_ADPOWER_PSTS0_MASK|ADCA_ADPOWER_PSTS2_MASK))) { /* Is device enabled? */
      /* ADCA_ADPOWER: ??=0,??=0,??=0,PSTS2=0,PSTS1=0,PSTS0=0,PUDELAY=13,PSM=0,PD2=0,PD1=1,PD0=0 */
      setReg(ADCA_ADPOWER,210);        /* Enable device */
      for (i=0;i<100;i++) {}           /* Recovery time of voltage reference */
      while (ADCA_ADPOWER&((ADCA_ADPOWER_PSTS0_MASK|ADCA_ADPOWER_PSTS2_MASK))) {} /* Wait for device powered up */
    }
    if (AD1_ModeFlg) {                 /* Launch measurement? */
      if (AD1_ModeFlg != SINGLE) {
        OutFlg = 0;                    /* Measured values are available */
      }
      else {
        OutFlg &= ~Table[SumChan];     /* Output value isn't available */
      }
      clrRegBit(ADCA_ADCR1,STOP);      /* Normal operation mode */
      setRegBit(ADCA_ADCR1,START);     /* Launching of conversion */
    }
    else {
      setRegBit(ADCA_ADCR1,STOP);      /* Stop command issued */
    }
  }
  else {
    setRegBit(ADCA_ADCR1,STOP);        /* Stop command issued */
    /* ADCA_ADPOWER: ??=0,??=0,??=0,PSTS2=0,PSTS1=0,PSTS0=0,PUDELAY=13,PSM=0,PD2=1,PD1=1,PD0=1 */
    setReg(ADCA_ADPOWER,215);          /* Disable device */
  }
}
/*
** ===================================================================
**     Method      :  AD1_Enable (component ADC)
**
**     Description :
**         Enables A/D converter bean. <Events> may be generated
**         (<DisableEvent>/<EnableEvent>). If possible, this method
**         switches on A/D converter device, voltage reference, etc.
**     Parameters  : None
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte AD1_Enable(void)
{
  if (AD1_EnUser) {                    /* Is the device enabled by user? */
    return ERR_OK;                     /* If yes then set the flag "device enabled" */
  }
  AD1_EnUser = TRUE;                   /* Set the flag "device enabled" */
  HWEnDi();                            /* Enable the device */
  return ERR_OK;                       /* OK */
}
/*
** ===================================================================
**     Method      :  AD1_Disable (component ADC)
**
**     Description :
**         Disables A/D converter bean. No <events> will be generated.
**         If possible, this method switches off A/D converter device,
**         voltage reference, etc.
**     Parameters  : None
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte AD1_Disable(void)
{
  if (!AD1_EnUser) {                   /* Is the device disabled by user? */
    return ERR_OK;                     /* If yes then OK */
  }
  AD1_EnUser = FALSE;                  /* If yes then set the flag "device disabled" */
  HWEnDi();                            /* Enable the device */
  return ERR_OK;                       /* OK */
}
/*
** ===================================================================
**     Method      :  AD1_EnableEvent (component ADC)
**
**     Description :
**         Enables the <events>.
**     Parameters  : None
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte AD1_EnableEvent(void)
{
  EnEvent = TRUE;                      /* Set the flag "events enabled" */
  return ERR_OK;                       /* OK */
}
/*
** ===================================================================
**     Method      :  AD1_DisableEvent (component ADC)
**
**     Description :
**         Disables the <events>.
**     Parameters  : None
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte AD1_DisableEvent(void)
{
  EnEvent = FALSE;                     /* Set the flag "events disabled" */
  return ERR_OK;                       /* OK */
}
/*
** ===================================================================
**     Method      :  AD1_Start (component ADC)
**
**     Description :
**         This method starts continuous conversion on all channels
**         that are set in the bean inspector. When each measurement on
**         all channels has finished the <OnEnd > event may be invoked.
**         This method is not available if the <interrupt service> is
**         disabled and the device doesn't support the continuous mode.
**         Note: If time of measurement is too short and the
**         instruction clock is too slow then the conversion complete
**         interrupt and its handler may cause a system overflow.
**     Parameters  : None
**     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 AD1_Start(void)
{
  if (!AD1_EnUser) {                   /* Is the device disabled by user? */
    return ERR_DISABLED;               /* If yes then error */
  }
  if (AD1_ModeFlg != IDLE) {           /* Is the device in running mode? */
    return ERR_BUSY;                   /* If yes then error */
  }
  setReg(ADCA_ADHLMT0,HighLimitSample0); /* Set high limit value for sample 0 */
  setReg(ADCA_ADLLMT0,LowLimitSample0); /* Set low limit value for sample 0 */
  setReg(ADCA_ADOFS0,OffsetSample0);   /* Set offset value for sample 0 */
  /* ADCA_ADLST1: ??=0,SAMPLE3=0,??=0,SAMPLE2=0,??=0,SAMPLE1=1,??=0,SAMPLE0=0 */
  setReg(ADCA_ADLST1,16);              /* Set ADC channel list reg. */
  /* ADCA_ADSDIS: TEST=0,??=0,??=0,??=0,??=0,??=0,??=0,DS7=1,DS6=1,DS5=1,DS4=1,DS3=1,DS2=1,DS1=0,DS0=0 */
  setReg(ADCA_ADSDIS,252);             /* Enable/disable of samples */
  AD1_ModeFlg = CONTINUOUS;            /* Set state of device to the continuous mode */
  HWEnDi();                            /* Enable the device */
  return ERR_OK;                       /* OK */
}
/*
** ===================================================================
**     Method      :  AD1_Stop (component ADC)
**
**     Description :
**         This method stops the continuous measurement or disables
**         a trigger mode (if supported by HW), which has been
**         started by one of the following methods:
**         Version specific information for Freescale 56800
**         derivatives ]
**         - <Start>
**         - <EnableIntTrigger>
**         - <EnableIntChanTrigger>
**         The Stop method is available if one of the previously
**         mentioned methods is supported by A/D converter device
**         and is enabled to be generated.
**     Parameters  : None
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_BUSY - No continuous measurement is
**                           running. Neither internal trigger nor
**                           external trigger have been enabled (if
**                           these are supported by HW).
** ===================================================================
*/
byte AD1_Stop(void)
{
  if (AD1_ModeFlg != CONTINUOUS) {     /* Is the device in different mode from "continuous"? */
    return ERR_BUSY;                   /* If yes then error */
  }
  setRegBit(ADCA_ADCR1,STOP);          /* Stop command issued */
  AD1_ModeFlg = IDLE;                  /* Set state of device to the stop mode */
  return ERR_OK;                       /* OK */
}
/*
** ===================================================================
**     Method      :  AD1_Measure (component ADC)
**
**     Description :
**         This method performs one measurement on all channels that
**         are set in the bean inspector. (Note: If the <number of
**         conversions> is more than one the conversion of A/D channels
**         is performed specified number of times.)
**     Parameters  :
**         NAME            - DESCRIPTION
**         WaitForResult   - Wait for a result of a
**                           conversion. If <interrupt service> is
**                           disabled, A/D peripheral doesn't support
**                           measuring all channels at once or Autoscan
**                           mode property isn't enabled and at the same
**                           time the <number of channel> is greater
**                           than 1, then the WaitForResult parameter is
**                           ignored and the method waits for each
**                           result every time. If the <interrupt
**                           service> is disabled and a <number of
**                           conversions> is greater than 1, the
**                           parameter is ignored and the method also
**                           waits for each result every time.
**     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 AD1_Measure(bool WaitForResult)
{
  if (!AD1_EnUser) {                   /* Is the device disabled by user? */
    return ERR_DISABLED;               /* If yes then error */
  }
  if (AD1_ModeFlg != IDLE) {           /* Is the device in running mode? */
    return ERR_BUSY;                   /* If yes then error */
  }
  setReg(ADCA_ADHLMT0,HighLimitSample0); /* Set high limit value for sample 0 */
  setReg(ADCA_ADLLMT0,LowLimitSample0); /* Set low limit value for sample 0 */
  setReg(ADCA_ADOFS0,OffsetSample0);   /* Set offset value for sample 0 */
  /* ADCA_ADLST1: ??=0,SAMPLE3=0,??=0,SAMPLE2=0,??=0,SAMPLE1=1,??=0,SAMPLE0=0 */
  setReg(ADCA_ADLST1,16);              /* Set ADC channel list reg. */
  /* ADCA_ADSDIS: TEST=0,??=0,??=0,??=0,??=0,??=0,??=0,DS7=1,DS6=1,DS5=1,DS4=1,DS3=1,DS2=1,DS1=0,DS0=0 */
  setReg(ADCA_ADSDIS,252);             /* Enable/disable of samples */
  AD1_ModeFlg = MEASURE;               /* Set state of device to the measure mode */
  HWEnDi();                            /* Enable the device */
  if (WaitForResult) {                 /* Is WaitForResult TRUE? */
    while (AD1_ModeFlg == MEASURE) {}  /* If yes then wait for end of measurement */
  }
  return ERR_OK;                       /* OK */
}
/*
** ===================================================================
**     Method      :  AD1_MeasureChan (component ADC)
**
**     Description :
**         This method performs measurement on one channel. (Note: If
**         the <number of conversions> is more than one the conversion
**         of the A/D channel is performed specified number of times.)
**     Parameters  :
**         NAME            - DESCRIPTION
**         WaitForResult   - Wait for a result of
**                           conversion. If the <interrupt service> is
**                           disabled and at the same time a <number of
**                           conversions> is greater than 1, the
**                           WaitForResult parameter is ignored and the
**                           method waits for each result every time.
**         Channel         - Channel number. If only one
**                           channel in the bean is set this parameter
**                           is ignored, because the parameter is set
**                           inside this method.
**     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_RANGE - Parameter "Channel" out of range
** ===================================================================
*/
byte AD1_MeasureChan(bool WaitForResult,byte Channel)
{
  if (Channel >= 2) {                  /* Is channel number out of range? */
    return ERR_RANGE;                  /* If yes then error */
  }
  if (!AD1_EnUser) {                   /* Is the device disabled by user? */
    return ERR_DISABLED;               /* If yes then error */
  }
  if (AD1_ModeFlg != IDLE) {           /* Is the device in running mode? */
    return ERR_BUSY;                   /* If yes then error */
  }
  if (Channel) {                       /* Is it non-zero channel number? */
    setReg(ADCA_ADHLMT0,(&ADCA_ADHLMT0)[Channel]); /* Copy high limit value to sample 0 */
    setReg(ADCA_ADLLMT0,(&ADCA_ADLLMT0)[Channel]); /* Copy low limit value to sample 0 */
    setReg(ADCA_ADOFS0,(&ADCA_ADOFS0)[Channel]); /* Copy offset value to sample 0 */
  }
  else {
    setReg(ADCA_ADHLMT0,HighLimitSample0); /* Set high limit value for sample 0 */
    setReg(ADCA_ADLLMT0,LowLimitSample0); /* Set low limit value for sample 0 */
    setReg(ADCA_ADOFS0,OffsetSample0); /* Set offset value for sample 0 */
  }
  setReg(ADCA_ADLST1,Channels[Channel]); /* Set ADC channel list reg. */
  /* ADCA_ADSDIS: TEST=0,??=0,??=0,??=0,??=0,??=0,??=0,DS7=1,DS6=1,DS5=1,DS4=1,DS3=1,DS2=1,DS1=1,DS0=0 */
  setReg(ADCA_ADSDIS,254);             /* Enable/disable of sample 0 */
  AD1_ModeFlg = SINGLE;                /* Set state of device to the measure mode */
  SumChan = Channel;                   /* Set required channel */
  HWEnDi();                            /* Enable the device */
  if (WaitForResult) {                 /* Is WaitForResult TRUE? */
    while (AD1_ModeFlg == SINGLE) {}   /* If yes then wait for end of measurement */
  }
  return ERR_OK;                       /* OK */
}
/*
** ===================================================================
**     Method      :  AD1_GetValue (component ADC)
**
**     Description :
**         Returns the last measured values for all channels. Format
**         and width of the value is a native format of the A/D
**         converter.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Values          - Pointer to the array that contains
**                           the measured data. Data type is a byte, a
**                           word or an int. It depends on the supported
**                           modes, resolution, etc. of the AD converter.
**                           See the Version specific information for
**                           the current CPU in <General Info>.
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_NOTAVAIL - Requested value not
**                           available
**                           ERR_OVERRUN - External trigger overrun flag
**                           was detected after the last value(s) was
**                           obtained (for example by GetValue). This
**                           error may not be supported on some CPUs
**                           (see generated code).
** ===================================================================
*/
byte AD1_GetValue(void* Values)
{
  register word *pValue=(word*)Values; /* Temporary pointer to the user buffer */
  if (OutFlg != 3) {                   /* Is measured value(s) available? */
    return ERR_NOTAVAIL;               /* If no then error */
  }
  *pValue++ = AD1_OutV[0];             /* Store value from output to user buffer */
  *pValue = AD1_OutV[1];               /* Store value from output to user buffer */
  return ERR_OK;                       /* OK */
}
/*
** ===================================================================
**     Method      :  AD1_GetChanValue (component ADC)
**
**     Description :
**         Returns the last measured value of the required channel.
**         Format and width of the value is a native format of the A/D
**         converter.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Channel         - Channel number. If only one
**                           channel in the bean is set then this
**                           parameter is ignored.
**       * Value           - Pointer to the measured value. Data
**                           type is a byte, a word or an int. It
**                           depends on the supported modes, resolution,
**                           etc. of the AD converter. See the Version
**                           specific information for the current CPU in
**                           <General Info>.
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_NOTAVAIL - Requested value not
**                           available
**                           ERR_RANGE - Parameter "Channel" out of
**                           range
**                           ERR_OVERRUN - External trigger overrun flag
**                           was detected after the last value(s) was
**                           obtained (for example by GetValue). This
**                           error may not be supported on some CPUs
**                           (see generated code).
** ===================================================================
*/
byte AD1_GetChanValue(byte Channel,void* Value)
{
  register word *pValue=(word*)Value;  /* Temporary pointer to the user buffer */
  if (Channel >= 2) {                  /* Is channel number out of range? */
    return ERR_RANGE;                  /* If yes then error */
  }
  if (!(OutFlg & Table[Channel])) {    /* Is measured value(s) available? */
    return ERR_NOTAVAIL;               /* If no then error */
  }
  *pValue = AD1_OutV[Channel];         /* Store value from output to user buffer */
  return ERR_OK;                       /* OK */
}
/*
** ===================================================================
**     Method      :  AD1_GetValue8 (component ADC)
**
**     Description :
**         This method returns the last measured values of all channels
**         justified to the left. Compared with <GetValue> method this
**         method returns more accurate result if the <number of
**         conversions> is greater than 1 and <AD resolution> is less
**         than 8 bits. In addition, the user code dependency on <AD
**         resolution> is eliminated.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Values          - Pointer to the array that contains
**                           the measured data.
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_NOTAVAIL - Requested value not
**                           available
**                           ERR_OVERRUN - External trigger overrun flag
**                           was detected after the last value(s) was
**                           obtained (for example by GetValue). This
**                           error may not be supported on some CPUs
**                           (see generated code).
** ===================================================================
*/
byte AD1_GetValue8(byte *Values)
{
  if (OutFlg != 3) {                   /* Is measured value(s) available? */
    return ERR_NOTAVAIL;               /* If no then error */
  }
  *Values++ = (byte)((AD1_OutV[0] + OffsetSample0) >> 7); /* Store value from output to user buffer */
  *Values = (byte)((AD1_OutV[1] + getReg(ADCA_ADOFS1)) >> 7); /* Store value from output to user buffer */
  return ERR_OK;                       /* OK */
}
/*
** ===================================================================
**     Method      :  AD1_GetChanValue8 (component ADC)
**
**     Description :
**         This method returns the last measured value of required
**         channel justified to the left. Compared with <GetChanValue>
**         method this method returns more accurate result if the
**         <number of conversions> is greater than 1 and <AD resolution>
**         is less than 8 bits. In addition, the user code dependency
**         on <AD resolution> is eliminated.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Channel         - Channel number. If only one
**                           channel in the bean is set then this
**                           parameter is ignored.
**       * Value           - Pointer to the measured value.
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_NOTAVAIL - Requested value not
**                           available
**                           ERR_RANGE - Parameter "Channel" out of
**                           range
**                           ERR_OVERRUN - External trigger overrun flag
**                           was detected after the last value(s) was
**                           obtained (for example by GetValue). This
**                           error may not be supported on some CPUs
**                           (see generated code).
** ===================================================================
*/
byte AD1_GetChanValue8(byte Channel,byte *Value)
{
  register volatile word *pTempOffset = &ADCA_ADOFS0; /* Temporary pointer to begin of offset values */
  if (Channel >= 2) {                  /* Is channel number out of range? */
    return ERR_RANGE;                  /* If yes then error */
  }
  if (!(OutFlg & Table[Channel])) {    /* Is measured value(s) available? */
    return ERR_NOTAVAIL;               /* If no then error */
  }
  if (Channel) {                       /* Is it non-zero channel number? */
    *Value = (byte)((AD1_OutV[Channel] + pTempOffset[Channel]) >> 7); /* Store value from output to user buffer */
  }
  else {
    *Value = (byte)((AD1_OutV[0] + OffsetSample0) >> 7); /* Store value from output to user buffer */
  }
  return ERR_OK;                       /* OK */
}
/*
** ===================================================================
**     Method      :  AD1_GetValue16 (component ADC)
**
**     Description :
**         This method returns the last measured values of all channels
**         justified to the left. Compared with <GetValue> method this
**         method returns more accurate result if the <number of
**         conversions> is greater than 1 and <AD resolution> is less
**         than 16 bits. In addition, the user code dependency on <AD
**         resolution> is eliminated.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Values          - Pointer to the array that contains
**                           the measured data.
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_NOTAVAIL - Requested value not
**                           available
**                           ERR_OVERRUN - External trigger overrun flag
**                           was detected after the last value(s) was
**                           obtained (for example by GetValue). This
**                           error may not be supported on some CPUs
**                           (see generated code).
** ===================================================================
*/
byte AD1_GetValue16(word *Values)
{
  if (OutFlg != 3) {                   /* Is measured value(s) available? */
    return ERR_NOTAVAIL;               /* If no then error */
  }
  *Values++ = (AD1_OutV[0] + OffsetSample0) << 1; /* Store value from output to user buffer */
  *Values = (AD1_OutV[1] + getReg(ADCA_ADOFS1)) << 1; /* Store value from output to user buffer */
  return ERR_OK;                       /* OK */
}
/*
** ===================================================================
**     Method      :  AD1_GetChanValue16 (component ADC)
**
**     Description :
**         This method returns the last measured value of the required
**         channel justified to the left. Compared with <GetChanValue>
**         method this method returns more accurate result if the
**         <number of conversions> is greater than 1 and <AD resolution>
**         is less than 16 bits. In addition, the user code dependency
**         on <AD resolution> is eliminated.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Channel         - Channel number. If only one
**                           channel in the bean is set then this
**                           parameter is ignored.
**       * Value           - Pointer to the measured value.
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_NOTAVAIL - Requested value not
**                           available
**                           ERR_RANGE - Parameter "Channel" out of
**                           range
**                           ERR_OVERRUN - External trigger overrun flag
**                           was detected after the last value(s) was
**                           obtained (for example by GetValue). This
**                           error may not be supported on some CPUs
**                           (see generated code).
** ===================================================================
*/
byte AD1_GetChanValue16(byte Channel,word *Value)
{
  register volatile word *pTempOffset = &ADCA_ADOFS0; /* Temporary pointer to begin of offset values */
  if (Channel >= 2) {                  /* Is channel number out of range? */
    return ERR_RANGE;                  /* If yes then error */
  }
  if (!(OutFlg & Table[Channel])) {    /* Is measured value(s) available? */
    return ERR_NOTAVAIL;               /* If no then error */
  }
  if (Channel) {                       /* Is it non-zero channel number? */
    *Value = (AD1_OutV[Channel] + pTempOffset[Channel]) << 1; /* Store value from output to user buffer */
  }
  else {
    *Value = (AD1_OutV[0] + OffsetSample0) << 1; /* Store value from output to user buffer */
  }
  return ERR_OK;                       /* OK */
}
/*
** ===================================================================
**     Method      :  AD1_SetHighChanLimit (component ADC)
**
**     Description :
**         Set a high limit for required channel. If ADC result
**         (without subtraction of the offset value) is greater than
**         this value, the event <OnHighLimit> can be invoked.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Channel         - Channel number. If only one
**                           channel in the bean is set then this
**                           parameter is ignored, because the parameter
**                           is set inside this method.
**         Limit           - The high limit 12-bit value must be
**                           placed from the third to the fourteenth bit
**                           of a 16-bit value.
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_RANGE - Parameter "Channel" out of
**                           range
**                           ERR_BUSY - A conversion is already running
** ===================================================================
*/
byte AD1_SetHighChanLimit(byte Channel,word Limit)
{
  register volatile word *pTemp = &ADCA_ADHLMT0; /* Temporary pointer to begin of high limit values */
  if (Channel >= 2) {                  /* Is channel number out of range? */
    return ERR_RANGE;                  /* If yes then error */
  }
  if (AD1_ModeFlg != IDLE) {           /* Is the device in different mode from "stop"? */
    return ERR_BUSY;                   /* If yes then error */
  }
  if (Channel) {
    pTemp[Channel] = Limit;            /* Set high limit */
  }
  else {
    HighLimitSample0 = Limit;          /* Set high limit */
  }
  return ERR_OK;                       /* OK */
}
/*
** ===================================================================
**     Method      :  AD1_SetLowChanLimit (component ADC)
**
**     Description :
**         Set a low limit for a required channel. If ADC result
**         (without subtraction of the offset value) is less than this
**         value, the event <OnLowLimit> can be invoked.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Channel         - Channel number. If it is set only
**                           one channel in the bean then this parameter
**                           is ignored, because the parameter is set
**                           inside this method.
**         Limit           - The low limit 12-bit value must be
**                           placed from the third to the fourteenth bit
**                           of a 16-bit value.
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_RANGE - Parameter "Channel" out of
**                           range
**                           ERR_BUSY - A conversion is already running
** ===================================================================
*/
byte AD1_SetLowChanLimit(byte Channel,word Limit)
{
  register volatile word *pTemp = &ADCA_ADLLMT0; /* Temporary pointer to begin of low limit values */
  if (Channel >= 2) {                  /* Is channel number out of range? */
    return ERR_RANGE;                  /* If yes then error */
  }
  if (AD1_ModeFlg != IDLE) {           /* Is the device in different mode from "stop"? */
    return ERR_BUSY;                   /* If yes then error */
  }
  if (Channel) {
    pTemp[Channel] = Limit;            /* Set low limit */
  }
  else {
    LowLimitSample0 = Limit;           /* Set low limit */
  }
  return ERR_OK;                       /* OK */
}
/*
** ===================================================================
**     Method      :  AD1_SetChanOffset (component ADC)
**
**     Description :
**         Set an offset value for required channel. The offset value
**         is subtracted from the ADC result. In order to obtain
**         unsigned results (by <GetValue> or <GetChanValue> method),
**         this value should be set to 0, with a result within the
**         range 0 to 32760. The results returned by <GetValue8>,
**         <GetChanValue8>, <GetValue16> and <GetChanValue16> methods
**         are not affected by these offset values.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Channel         - Channel number. If only one
**                           channel in the bean is set then this
**                           parameter is ignored, because the parameter
**                           is set inside this method.
**         Offset          - Offset 12-bit value placed from
**                           third to fourteenth bit of 16-bit value.
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_RANGE - Parameter "Channel" out of
**                           range
**                           ERR_BUSY - A conversion is already running
** ===================================================================
*/
byte AD1_SetChanOffset(byte Channel,word Offset)
{
  register volatile word *pTemp = &ADCA_ADOFS0; /* Temporary pointer to begin of offset values */
  if (Channel >= 2) {                  /* Is channel number out of range? */
    return ERR_RANGE;                  /* If yes then error */
  }
  if (AD1_ModeFlg != IDLE) {           /* Is the device in different mode from "stop"? */
    return ERR_BUSY;                   /* If yes then error */
  }
  if (Channel) {
    pTemp[Channel] = Offset;           /* Set offset */
  }
  else {
    OffsetSample0 = Offset;            /* Set offset */
  }
  return ERR_OK;                       /* OK */
}
/*
** ===================================================================
**     Method      :  AD1_GetHighLimitStatus (component ADC)
**
**     Description :
**         This method returns a high limit status of all samples.
**     Parameters  : None
**     Returns     :
**         ---             - A High limit status of all samples.
** ===================================================================
*/
word AD1_GetHighLimitStatus(void)
{
  register word Temp = getReg(ADCA_ADLSTAT) >> 8;
  setReg(ADCA_ADLSTAT,65280);          /* Clear high limit status flags */
  return Temp;                         /* Return high limit status */
}
/*
** ===================================================================
**     Method      :  AD1_GetLowLimitStatus (component ADC)
**
**     Description :
**         This method returns low limit status of all samples.
**     Parameters  : None
**     Returns     :
**         ---             - A Low limit status of all samples.
** ===================================================================
*/
word AD1_GetLowLimitStatus(void)
{
  register word Temp = getReg(ADCA_ADLSTAT) & 255;
  setReg(ADCA_ADLSTAT,255);            /* Clear low limit status flags */
  return Temp;                         /* Return low limit status */
}
/*
** ===================================================================
**     Method      :  AD1_Init (component ADC)
**
**     Description :
**         Initializes the associated peripheral(s) and the beans
**         internal variables. The method is called automatically as a
**         part of the application initialization code.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
void AD1_Init(void)
{
  AD1_EnUser = TRUE;                   /* Enable device */
  EnEvent = TRUE;                      /* Enable events */
  OutFlg = 0;                          /* No measured value */
  AD1_ModeFlg = IDLE;                  /* Device isn't running */
  /* ADCA_ADCR1: ??=0,STOP=1,START=0,SYNC=0,EOSIE=1,ZCIE=0,LLMTIE=0,HLMTIE=0,CHNCFG=0,??=0,SMODE=0 */
  setReg(ADCA_ADCR1,18432);            /* Set control register 1 */
  OffsetSample0 = 0;                   /* Initial set offset value for sample 0 */
  /* ADCA_ADOFS1: ??=0,OFFSET=0,??=0,??=0,??=0 */
  setReg(ADCA_ADOFS1,0);               /* Set offset reg. 1 */
  HighLimitSample0 = 32760;            /* Initial set high limit value for sample 0 */
  /* ADCA_ADHLMT1: ??=0,HLMT=4095,??=0,??=0,??=0 */
  setReg(ADCA_ADHLMT1,32760);          /* Set high limit reg. 1 */
  LowLimitSample0 = 0;                 /* Initial set low limit value for sample 0 */
  /* ADCA_ADLLMT1: ??=0,LLMT=0,??=0,??=0,??=0 */
  setReg(ADCA_ADLLMT1,0);              /* Set low limit reg. 1 */
  /* ADCA_ADZCSTAT: ??=1,??=1,??=1,??=1,??=1,??=1,??=1,??=1,ZCS7=1,ZCS6=1,ZCS5=1,ZCS4=1,ZCS3=1,ZCS2=1,ZCS1=1,ZCS0=1 */
  setReg(ADCA_ADZCSTAT,65535);         /* Clear zero crossing status flags */
  /* ADCA_ADLSTAT: HLS7=1,HLS6=1,HLS5=1,HLS4=1,HLS3=1,HLS2=1,HLS1=1,HLS0=1,LLS7=1,LLS6=1,LLS5=1,LLS4=1,LLS3=1,LLS2=1,LLS1=1,LLS0=1 */
  setReg(ADCA_ADLSTAT,65535);          /* Clear high and low limit status */
  /* ADCA_ADSTAT: CIP=0,??=0,??=0,??=0,EOSI=1,ZCI=0,LLMTI=0,HLMTI=0,RDY7=0,RDY6=0,RDY5=0,RDY4=0,RDY3=0,RDY2=0,RDY1=0,RDY0=0 */
  setReg(ADCA_ADSTAT,2048);            /* Clear EOSI flag */
  /* ADCA_ADZCC: ZCE7=0,ZCE6=0,ZCE5=0,ZCE4=0,ZCE3=0,ZCE2=0,ZCE1=0,ZCE0=0 */
  setReg(ADCA_ADZCC,0);                /* Set zero crossing control reg. */
  /* ADCA_ADCR2: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,DIV=30 */
  setReg(ADCA_ADCR2,30);               /* Set prescaler */
  HWEnDi();                            /* Enable/disable device according to the status flags */
}
/* END AD1. */