⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adcfd.c

📁 PWM同步
💻 C
📖 第 1 页 / 共 2 页
字号:
}

/*
** ===================================================================
**     Method      :  AdcFD_EnableIntTrigger (bean ADC)
**
**     Description :
**         Enables the internal trigger mode. A conversion of all
**         channels will be launched by internal sync pulse. If the
**         <Number of conversions> is greater than 1, a conversion
**         will be launched more than once (by an internal signal)
**         according to <Number of conversions>. It's possible to
**         disable the trigger mode by <Stop> method. This
**         EnableIntTrigger method is available only when the
**         <Internal trigger> property is enabled.
**     Parameters  : None
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_BUSY - A conversion is already
**                           running
** ===================================================================
*/
byte AdcFD_EnableIntTrigger(void)
{
  if (AdcFD_ModeFlg != IDLE)           /* Is the device in running mode? */
    return ERR_BUSY;                   /* If yes then error */
  setRegBitGroup(ADCB_ADCR1,SMODE,4);  /* Select 'Triggered Sequential' mode */
  AdcFD_ModeFlg = MEASURE;             /* Set state of device to the measure mode */
  HWEnDi();                            /* Enable the device */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  AdcFD_GetValue (bean 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 AdcFD_GetValue(void* Values)
{
  register word *pValue=(word*)Values; /* Temporary pointer to the user buffer */

  if (!OutFlg)                         /* Is measured value(s) available? */
    return ERR_NOTAVAIL;               /* If no then error */
  *pValue++ = getReg(ADCB_ADRSLT0);    /* Store value from result register of device to user buffer */
  *pValue = getReg(ADCB_ADRSLT1);      /* Store value from result register of device to user buffer */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  AdcFD_GetChanValue (bean 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 AdcFD_GetChanValue(byte Channel,void* Value)
{
  register volatile word *pTemp;       /* Temporary pointer */
  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)                         /* Is measured value(s) available? */
    return ERR_NOTAVAIL;               /* If no then error */
  pTemp = &ADCB_ADRSLT0;               /* Set temporary register */
  *pValue = pTemp[Channel];            /* Store value from result register of device to user buffer */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  AdcFD_GetValue8 (bean 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 AdcFD_GetValue8(byte *Values)
{
  if (!OutFlg)                         /* Is measured value(s) available? */
    return ERR_NOTAVAIL;               /* If no then error */
  *Values++ = (byte)((getReg(ADCB_ADRSLT0) + 0) >> 7); /* Store value from result register of device to user buffer */
  *Values = (byte)((getReg(ADCB_ADRSLT1) + 0) >> 7); /* Store value from result register of device to user buffer */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  AdcFD_GetChanValue8 (bean 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 AdcFD_GetChanValue8(byte Channel,byte *Value)
{
  register volatile word *pTemp = &ADCB_ADRSLT0; /* Temporary pointer to begin of AD results */

  if (Channel >= 2)                    /* Is channel number out of range? */
    return ERR_RANGE;                  /* If yes then error */
  if (!OutFlg)                         /* Is measured value(s) available? */
    return ERR_NOTAVAIL;               /* If no then error */
  *Value = (byte)((pTemp[Channel] + 0) >> 7); /* Store value from result register of device to user buffer */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  AdcFD_GetValue16 (bean 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 AdcFD_GetValue16(word *Values)
{
  if (!OutFlg)                         /* Is measured value(s) available? */
    return ERR_NOTAVAIL;               /* If no then error */
  *Values++ = (getReg(ADCB_ADRSLT0) + 0) << 1; /* Store value from result register of device to user buffer */
  *Values = (getReg(ADCB_ADRSLT1) + 0) << 1; /* Store value from result register of device to user buffer */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  AdcFD_Init (bean 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 AdcFD_Init(void)
{
  AdcFD_EnUser = TRUE;                 /* Enable device */
  OutFlg = FALSE;                      /* No measured value */
  AdcFD_ModeFlg = IDLE;                /* Device isn't running */
  /* ADCB_ADCR1: ??=0,STOP=1,START=0,SYNC=0,EOSIE=1,ZCIE=0,LLMTIE=0,HLMTIE=0,CHNCFG=0,??=0,SMODE=0 */
  setReg(ADCB_ADCR1,18432);            /* Set control register 1 */
  /* ADCB_ADOFS0: ??=0,OFFSET=0,??=0,??=0,??=0 */
  setReg(ADCB_ADOFS0,0);               /* Set offset reg. 0 */
  /* ADCB_ADOFS1: ??=0,OFFSET=0,??=0,??=0,??=0 */
  setReg(ADCB_ADOFS1,0);               /* Set offset reg. 1 */
  /* ADCB_ADHLMT0: ??=0,HLMT=4095,??=0,??=0,??=0 */
  setReg(ADCB_ADHLMT0,32760);          /* Set high limit reg. 0 */
  /* ADCB_ADHLMT1: ??=0,HLMT=4095,??=0,??=0,??=0 */
  setReg(ADCB_ADHLMT1,32760);          /* Set high limit reg. 1 */
  /* ADCB_ADLLMT0: ??=0,LLMT=0,??=0,??=0,??=0 */
  setReg(ADCB_ADLLMT0,0);              /* Set low limit reg. 0 */
  /* ADCB_ADLLMT1: ??=0,LLMT=0,??=0,??=0,??=0 */
  setReg(ADCB_ADLLMT1,0);              /* Set low limit reg. 1 */
  /* ADCB_ADZCSTAT: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,ZCS7=1,ZCS6=1,ZCS5=1,ZCS4=1,ZCS3=1,ZCS2=1,ZCS1=1,ZCS0=1 */
  setReg(ADCB_ADZCSTAT,255);           /* Clear zero crossing status flags */
  /* ADCB_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(ADCB_ADLSTAT,65535);          /* Clear high and low limit status */
  /* ADCB_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(ADCB_ADSTAT,2048);            /* Clear EOSI flag */
  /* ADCB_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(ADCB_ADSDIS,252);             /* Enable/disable of samples */
  /* ADCB_ADLST1: ??=0,SAMPLE3=0,??=0,SAMPLE2=0,??=0,SAMPLE1=3,??=0,SAMPLE0=0 */
  setReg(ADCB_ADLST1,48);              /* Set ADC channel list reg. */
  /* ADCB_ADZCC: ZCE7=0,ZCE6=0,ZCE5=0,ZCE4=0,ZCE3=0,ZCE2=0,ZCE1=0,ZCE0=0 */
  setReg(ADCB_ADZCC,0);                /* Set zero crossing control reg. */
  /* ADCB_ADCR2: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,DIV=11 */
  setReg(ADCB_ADCR2,11);               /* Set prescaler */
  HWEnDi();                            /* Enable/disable device according to the status flags */
}

/* END AdcFD. */

/*
** ###################################################################
**
**     This file was created by UNIS Processor Expert 2.97 [03.74]
**     for the Freescale 56800 series of microcontrollers.
**
** ###################################################################
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -