请教ADC采集中寄存器设置的相关问题
时间:10-02
整理:3721RD
点击:
用的是PT100采集外部温度数据,属于模拟信号输入,选用的是P0.7口为模拟输入口。下面是我对ADC寄存器的设置,但是在线调试的时候,ADCH和ADCL里没有数值,不解,求高手指导!万分感谢!
void HalAdcInit (void)
{
#if (HAL_ADC == TRUE)
volatile uint8 tmp;
/*
* After reset, the first ADC reading of the extra conversion always reads GND level.
* We will do a few dummy conversions to bypass this bug.
*/
tmp = ADCL; /* read ADCL,ADCH to clear EOC */
tmp = ADCH
ADCCON3 = HAL_ADC_REF_VOLT | HAL_ADC_DEC_RATE | HAL_ADC_ECHN;//此处将ADCCON3初始化为0x8C。
while ((ADCCON1 & HAL_ADC_EOC) != HAL_ADC_EOC); /* Wait for conversion */
#endif
}
/**************************************************************************************************
* @fn HalAdcRead
*RNDH
* @brief Read the ADC based on given channel and resolution
*
* @param channel - channel where ADC will be read
* @param resolution - the resolution of the value
*
* @return 16 bit value of the ADC in offset binary format.
* Note that the ADC is "bipolar", which means the GND (0V) level is mid-scale.
**************************************************************************************************/
uint16 HalAdcRead (uint8 channel, uint8 resolution)
{
int16 reading = 0;
#if (HAL_ADC == TRUE)
volatile uint8 tmp;
//Init P0.7
P0SEL |= 0x80; //配置 P0.7为外设
P0DIR |= 0x7F;//配置 P0.7口为输入模式
P0INP |= 0x80; //配置 P0.7为三态
ADCCON1 |= 0x1F; //选择ADC的启动模式为手动
ADCCON2 = HAL_ADC_REF_VOLT | HAL_ADC_DEC_RATE | HAL_ADC_SCHN;
// Enable channel
APCFG |= 0x80;
//Convert resolution to decimation rate
/*
switch (resolution)
{
case HAL_ADC_RESOLUTION_8:
resbits = HAL_ADC_DEC_064;
break;
case HAL_ADC_RESOLUTION_10:
resbits = HAL_ADC_DEC_128;
break;
case HAL_ADC_RESOLUTION_12:
resbits = HAL_ADC_DEC_256;
break;
case HAL_ADC_RESOLUTION_14:
default:
resbits = HAL_ADC_DEC_512;
break;
}
*/
// read ADCL,ADCH to clear EOC
tmp = ADCL;
tmp = ADCH;
// writing to this register starts the extra conversion
ADCCON3 = 0x87;
// Wait for the conversion to be done
while (!(ADCCON1 & HAL_ADC_EOC));
// Disable channel after done conversion
//APCFG = 0x00;
// Read the result
reading = (int16) (ADCL);
reading |= (int16) (ADCH << 8);
// Treat small negative as 0
if (reading < 0)
reading = 0;
switch (resolution)
{
case HAL_ADC_RESOLUTION_8:
reading >>= 8;
break;
case HAL_ADC_RESOLUTION_10:
reading >>= 6;
break;
case HAL_ADC_RESOLUTION_12:
reading >>= 4;
break;
case HAL_ADC_RESOLUTION_14:
default:
break;
}
#endif
return ((uint16)reading);
}
void HalAdcInit (void)
{
#if (HAL_ADC == TRUE)
volatile uint8 tmp;
/*
* After reset, the first ADC reading of the extra conversion always reads GND level.
* We will do a few dummy conversions to bypass this bug.
*/
tmp = ADCL; /* read ADCL,ADCH to clear EOC */
tmp = ADCH
ADCCON3 = HAL_ADC_REF_VOLT | HAL_ADC_DEC_RATE | HAL_ADC_ECHN;//此处将ADCCON3初始化为0x8C。
while ((ADCCON1 & HAL_ADC_EOC) != HAL_ADC_EOC); /* Wait for conversion */
#endif
}
/**************************************************************************************************
* @fn HalAdcRead
*RNDH
* @brief Read the ADC based on given channel and resolution
*
* @param channel - channel where ADC will be read
* @param resolution - the resolution of the value
*
* @return 16 bit value of the ADC in offset binary format.
* Note that the ADC is "bipolar", which means the GND (0V) level is mid-scale.
**************************************************************************************************/
uint16 HalAdcRead (uint8 channel, uint8 resolution)
{
int16 reading = 0;
#if (HAL_ADC == TRUE)
volatile uint8 tmp;
//Init P0.7
P0SEL |= 0x80; //配置 P0.7为外设
P0DIR |= 0x7F;//配置 P0.7口为输入模式
P0INP |= 0x80; //配置 P0.7为三态
ADCCON1 |= 0x1F; //选择ADC的启动模式为手动
ADCCON2 = HAL_ADC_REF_VOLT | HAL_ADC_DEC_RATE | HAL_ADC_SCHN;
// Enable channel
APCFG |= 0x80;
//Convert resolution to decimation rate
/*
switch (resolution)
{
case HAL_ADC_RESOLUTION_8:
resbits = HAL_ADC_DEC_064;
break;
case HAL_ADC_RESOLUTION_10:
resbits = HAL_ADC_DEC_128;
break;
case HAL_ADC_RESOLUTION_12:
resbits = HAL_ADC_DEC_256;
break;
case HAL_ADC_RESOLUTION_14:
default:
resbits = HAL_ADC_DEC_512;
break;
}
*/
// read ADCL,ADCH to clear EOC
tmp = ADCL;
tmp = ADCH;
// writing to this register starts the extra conversion
ADCCON3 = 0x87;
// Wait for the conversion to be done
while (!(ADCCON1 & HAL_ADC_EOC));
// Disable channel after done conversion
//APCFG = 0x00;
// Read the result
reading = (int16) (ADCL);
reading |= (int16) (ADCH << 8);
// Treat small negative as 0
if (reading < 0)
reading = 0;
switch (resolution)
{
case HAL_ADC_RESOLUTION_8:
reading >>= 8;
break;
case HAL_ADC_RESOLUTION_10:
reading >>= 6;
break;
case HAL_ADC_RESOLUTION_12:
reading >>= 4;
break;
case HAL_ADC_RESOLUTION_14:
default:
break;
}
#endif
return ((uint16)reading);
}