📄 ad3.c
字号:
** running
** ===================================================================
*/
byte AD3_Start(void)
{
if (!EnUser) { /* Is the device disabled by user? */
return ERR_DISABLED; /* If yes then error */
}
if (ModeFlg != STOP) { /* Is the device in running mode? */
return ERR_BUSY; /* If yes then error */
}
ModeFlg = CONTINUOUS; /* Set state of device to the continuos mode */
HWEnDi(); /* Start measurement */
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : AD3_Stop (bean ADC)
**
** Description :
** This method stops the continuous measurement, which had
** been started by <Start> method, or this method disables a
** trigger mode which has been enabled by <EnableInt(Chan)
** Trigger> or <EnableExt(Chan)Trigger> method (if these are
** supported by HW). This method is available if at least
** one of <Start>, <EnableInt(Chan)Trigger> or
** <EnableExt(Chan)Trigger> methods is supported by A/D
** converter device and it 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 AD3_Stop(void)
{
if (ModeFlg != CONTINUOUS) { /* Is the device in different mode than "continuos"? */
return ERR_BUSY; /* If yes then error */
}
/* ATD1CTL3: ??=0,S8C=0,S4C=0,S2C=0,S1C=1,FIFO=0,FRZ1=0,FRZ0=0 */
ATD1CTL3 = 8; /* Abort current measurement */
ModeFlg = STOP; /* Set state of device to the stop mode */
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : AD3_Measure (bean 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 AD3_Measure(bool WaitForResult)
{
if (!EnUser) { /* Is the device disabled by user? */
return ERR_DISABLED; /* If yes then error */
}
if (ModeFlg != STOP) { /* Is the device in different mode than "stop"? */
return ERR_BUSY; /* If yes then error */
}
ModeFlg = MEASURE; /* Set state of device to the measure mode */
HWEnDi(); /* Enable the device */
if (WaitForResult) { /* Is WaitForResult TRUE? */
while (ModeFlg == MEASURE) {} /* If yes then wait for end of measurement */
}
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : AD3_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 AD3_GetValue8(byte *Values)
{
if (!OutFlg) { /* Is measured value(s) available? */
return ERR_NOTAVAIL; /* If no then error */
}
*Values = AD3_OutV; /* Save measured values to the output buffer */
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : AD3_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 AD3_GetChanValue8(byte Channel,byte *Value)
{
if (!OutFlg) { /* Is measured value(s) available? */
return ERR_NOTAVAIL;
}
*Value = AD3_OutV; /* Save measured values to the output buffer */
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : AD3_Init (bean ADC)
**
** Description :
** Initializes the associated peripheral(s) and the bean's
** 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 AD3_Init(void)
{
HPRIO = 208; /* Set vector with the highest priority */
EnUser = TRUE; /* Enable device */
OutFlg = FALSE; /* No measured value */
ModeFlg = STOP; /* Device isn't running */
/* ATD1CTL4: SRES8=1,SMP1=1,SMP0=1,PRS4=0,PRS3=1,PRS2=1,PRS1=1,PRS0=0 */
ATD1CTL4 = 238; /* Set resolution, sample time and prescaler */
/* ATD1CTL3: ??=0,S8C=0,S4C=0,S2C=0,S1C=1,FIFO=0,FRZ1=0,FRZ0=0 */
ATD1CTL3 = 8; /* Set ATD control register 3 */
/* ATD1CTL2: ADPU=1,AFFC=1,AWAI=0,ETRIGLE=0,ETRIGP=0,ETRIGE=0,ASCIE=1,ASCIF=0 */
ATD1CTL2 = 194; /* Set ATD control register 2 */
}
/* END AD3. */
/*
** ###################################################################
**
** This file was created by UNIS Processor Expert 2.96 [03.76]
** for the Freescale HCS12 series of microcontrollers.
**
** ###################################################################
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -