This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Mesh1.0 多通道ADC采集



使用端口:P0_0和P0_1

代码部分:

应用层:

APCFG |= 0x03; //模拟 I/O 使能
HalAdcInit(); // 初始化ADC
HalAdcSetReference ( HAL_ADC_REF_AVDD ); //设置基准电压

ADCTdata= HalAdcRead (HAL_ADC_CHN_AIN1, HAL_ADC_RESOLUTION_12);//热敏电阻
ADC_TH_R=(ADCTdata*3.3)/2047

ADCOdata = HalAdcRead (HAL_ADC_CHN_AIN0, HAL_ADC_RESOLUTION_12);//热电堆

ADC_OJ_T=ADCOdata*3.3/2047

仿真发现HAL_ADC_CHN_AIN1,是有数据的,但是一会大一会小, 很不稳定。而HAL_ADC_CHN_AIN0是完全没有数据,value显示<unavailable>

hal_adc.c:

uint16 HalAdcRead (uint8 channel, uint8 resolution)
{
int16 reading = 0;

#if (HAL_ADC == TRUE)
uint8 i, resbits;
uint8 adctemp;//xk
volatile uint8 tmp;//xk
uint8 adcChannel = 1;
uint8 reference;//xk

reference = ADCCON3 & HAL_ADC_REF_BITS;//xk

/*
* If Analog input channel is AIN0..AIN7, make sure corresponing P0 I/O pin is enabled. The code
* does NOT disable the pin at the end of this function. I think it is better to leave the pin
* enabled because the results will be more accurate. Because of the inherent capacitance on the
* pin, it takes time for the voltage on the pin to charge up to its steady-state level. If
* HalAdcRead() has to turn on the pin for every conversion, the results may show a lower voltage
* than actuality because the pin did not have time to fully charge.
*/
if (channel < 8)
{
for (i=0; i < channel; i++)
{
adcChannel <<= 1;
}
}

/* Enable channel */
ADCCFG |= adcChannel;

/* 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;
}
tmp = ADCL;//xk
tmp = ADCH;//xk

adctemp = ADCCON3;//xk
adctemp &= ~(HAL_ADC_CHN_BITS | HAL_ADC_DEC_BITS | HAL_ADC_REF_AVDD);//xk
adctemp |= channel | resbits | (reference);//xk
ADCCON3 = adctemp;//xk
/* Wait for the conversion to be done */
while (!(ADCCON1 & HAL_ADC_EOC));

/* Disable channel after done conversion */
ADCCFG &= (adcChannel ^ 0xFF);

/* 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:
reading >>= 2;
break;
}
#else
// unused arguments
(void) channel;
(void) resolution;
#endif

return ((uint16)reading);
}

请问我实现多通道采集这部分代码是否有问题?