📄 freescale
字号:
/*
** ===================================================================
** Method : Sensor_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 Sensor_MeasureChan(bool WaitForResult,byte Channel)
{
if (!EnUser) { /* Is the device disabled by user? */
return ERR_DISABLED; /* If yes then error */
}
if (Channel >= 3) { /* Is channel number greater than or equal to 3 */
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 */
WaitForRes = WaitForResult; /* Save Wait for result flag */
Sensor_HWEnDi(); /* Enable the device */
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : Sensor_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 Sensor_GetChanValue(byte Channel,void* Value)
{
if (Channel >= 3) { /* Is channel number greater than or equal to 3 */
return ERR_RANGE; /* If yes then error */
}
if (!(OutFlg & Table[Channel])) { /* Is output flag set? */
if (ADCSC1_COCO) {
((TWREG*)(&Sensor_OutV[SumChan]))->b.high = ADCRH; /* Save measured value */
((TWREG*)(&Sensor_OutV[SumChan]))->b.low = ADCRL; /* Save measured value */
OutFlg |= Table[SumChan]; /* Value of measured channel is available */
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 */
}
}
*(word*)Value = Sensor_OutV[Channel]; /* Save measured values to the output buffer */
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : Sensor_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 Sensor_GetChanValue16(byte Channel,word *Value)
{
if (Channel >= 3) { /* Is channel number greater than or equal to 3 */
return ERR_RANGE; /* If yes then error */
}
if (!(OutFlg & Table[Channel])) { /* Is output flag set? */
if (ADCSC1_COCO) {
((TWREG*)(&Sensor_OutV[SumChan]))->b.high = ADCRH; /* Save measured value */
((TWREG*)(&Sensor_OutV[SumChan]))->b.low = ADCRL; /* Save measured value */
OutFlg |= Table[SumChan]; /* Value of measured channel is available */
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 = (word)((Sensor_OutV[Channel]) << 4); /* Save measured values to the output buffer */
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : Sensor_Init (component 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 Sensor_Init(void)
{
/* ADCSC1: COCO=0,AIEN=0,ADCO=0,ADCH4=1,ADCH3=1,ADCH2=1,ADCH1=1,ADCH0=1 */
setReg8(ADCSC1, 0x1F); /* Disable the module */
/* ADCSC2: ADACT=0,ADTRG=0,ACFE=0,ACFGT=0,??=0,??=0,??=0,??=0 */
setReg8(ADCSC2, 0x00); /* Disable HW trigger and autocompare */
EnUser = TRUE; /* Enable device */
OutFlg = 0; /* No measured value */
SumChan = 0;
ModeFlg = STOP; /* Device isn't running */
/* ADCCFG: ADLPC=0,ADIV1=1,ADIV0=1,ADLSMP=0,MODE1=0,MODE0=1,ADICLK1=0,ADICLK0=1 */
setReg8(ADCCFG, 0x65); /* Set prescaler bits */
Sensor_HWEnDi(); /* Enable/disable device according to the status flags */
}
/* END Sensor. */
/*
** ###################################################################
**
** This file was created by Processor Expert 3.07 [04.34]
** for the Freescale HCS08 series of microcontrollers.
**
** ###################################################################
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -