📄 ad1.c
字号:
AD1_HWEnDi(); /* Enable the device */
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : AD1_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 AD1_GetChanValue(byte Channel,void* Value)
{
if (Channel >= 4) { /* Is channel number greater than or equal to 4 */
return ERR_RANGE; /* If yes then error */
}
if (ADCSC2_ADTRG || (!(OutFlg & Table[Channel]))) { /* Is trigger mode or output flag set? */
if (ADCSC1_COCO) {
AD1_OutV[SumChan] = ADCRL; /* Save measured value */
OutFlg |= Table[SumChan]; /* Value of measured channel is available */
if(!ADCSC2_ADTRG) { /* If the device is not in trigger mode */
ADCSC1 = 0x1F; /* Stop the device */
ModeFlg = STOP; /* Set the device to the stop mode */
}
if (!(OutFlg & Table[Channel])) { /* Is output flag set? */
return ERR_NOTAVAIL; /* If no then error */
}
}
else {
return ERR_NOTAVAIL; /* If no then error */
}
}
*(byte*)Value = AD1_OutV[Channel]; /* Save measured values to the output buffer */
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : AD1_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 AD1_GetChanValue8(byte Channel,byte *Value)
{
if (Channel >= 4) { /* Is channel number greater than or equal to 4 */
return ERR_RANGE; /* If yes then error */
}
if (ADCSC2_ADTRG || (!(OutFlg & Table[Channel]))) { /* Is trigger mode or output flag set? */
if (ADCSC1_COCO) {
AD1_OutV[SumChan] = ADCRL; /* Save measured value */
OutFlg |= Table[SumChan]; /* Value of measured channel is available */
if(!ADCSC2_ADTRG) { /* If the device is not in trigger mode */
ADCSC1 = 0x1F; /* Stop the device */
ModeFlg = STOP; /* Set the device to the stop mode */
}
if (!(OutFlg & Table[Channel])) { /* Is output flag set? */
return ERR_NOTAVAIL; /* If no then error */
}
}
else {
return ERR_NOTAVAIL; /* If no then error */
}
}
*Value = AD1_OutV[Channel]; /* Save measured values to the output buffer */
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : AD1_EnableIntChanTrigger (bean ADC)
**
** Description :
** Enables the internal trigger mode. A conversion of one
** required channel will be launched by internal sync pulse.
** If the <Number of conversions> property 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 EnableIntChanTrigger method is available
** only when the <Internal trigger> property is enabled.
** Parameters :
** NAME - DESCRIPTION
** Channel - Channel number which will be
** measured at internal trigger control. If
** only one channel in the bean is set then
** this parameter is ignored.
** Returns :
** --- - Error code, possible codes:
** ERR_OK - OK
** ERR_BUSY - A conversion is already
** running
** ERR_RANGE - Parameter "Channel" out of
** range
** ===================================================================
*/
byte AD1_EnableIntChanTrigger(byte Channel)
{
if (Channel >= 4) { /* Is channel number greater than or equal to 4 */
return ERR_RANGE; /* If yes then error */
}
if (ModeFlg != STOP) { /* Is the device in different mode than "stop"? */
return ERR_BUSY; /* If yes then error */
}
ModeFlg = SINGLE; /* Set state of device to the measure mode */
SumChan = Channel; /* Set required channel */
ADCSC1 = 0x1F; /* Block the conversion */
WaitForRes = 0; /* Disable wait for result*/
ADCSC2_ADTRG = 1; /* Select HW trigger */
AD1_HWEnDi(); /* Enable the device */
return ERR_OK;
}
/*
** ===================================================================
** Method : AD1_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 AD1_Init(void)
{
OutFlg = 0; /* No measured value */
SumChan = 0;
ModeFlg = STOP; /* Device isn't running */
/* ADCSC2: ADACT=0,ADTRG=0,ACFE=0,ACFGT=0,??=0,??=0,??=0,??=0 */
setReg8(ADCSC2, 0x00); /* Disable HW trigger and autocompare */
/* ADCCFG: ADLPC=0,ADIV1=1,ADIV0=1,ADLSMP=1,MODE1=0,MODE0=0,ADICLK1=0,ADICLK0=0 */
setReg8(ADCCFG, 0x70); /* Set prescaler bits */
/* ADCSC1: COCO=0,AIEN=0,ADCO=0,ADCH4=1,ADCH3=1,ADCH2=1,ADCH1=1,ADCH0=1 */
setReg8(ADCSC1, 0x1F); /* Disable the module */
}
/* END AD1. */
/*
** ###################################################################
**
** This file was created by UNIS Processor Expert 3.00 [03.89]
** for the Freescale HCS08 series of microcontrollers.
**
** ###################################################################
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -