使用端口: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);
}
请问我实现多通道采集这部分代码是否有问题?
YiKai Chen:
如果單只有HAL_ADC_CHN_AIN0或是HAL_ADC_CHN_AIN1去做ADC采集會有問題嗎?
user5032796:
回复 YiKai Chen:
没有问题的,我切换过来。0和1都可以读到。目前就是头一条生效,后一条不生效。
YiKai Chen:
回复 user5032796:
中間加個延遲試試
user5032796:
回复 YiKai Chen:
还是不可以的,中间加延时和用定时器分别触发。
user5032796:
回复 YiKai Chen:
好奇怪,找到一点规律:
如果代码这样写:
for(i=0;i<50;i++)
{
ADCOdata+= HalAdcRead (HAL_ADC_CHN_AIN0, HAL_ADC_RESOLUTION_12);//热敏电阻
ADCOdata=ADCOdata/2.0;
}
就有数值;1.1100219722E+3
如果代码这样写:
ADCOdata+= HalAdcRead (HAL_ADC_CHN_AIN0, HAL_ADC_RESOLUTION_12);//热敏电阻
就没有数值;<unavailable>
YiKai Chen:
回复 user5032796:
你如果接上IAR調試每次讀到的HalAdcRead 返回值是正常的嗎?
user5032796:
回复 YiKai Chen:
不正常,我一直都是在断点仿真调试的。这或许也可以解释第一组数据一会大一会小,而第二组根本没数据。因为一开始第一组是有加循环的,而第二组没有。
YiKai Chen:
回复 user5032796:
你是用TI CC2530DK開發版做這個測試的嗎?
user5032796:
回复 YiKai Chen:
不是的,自己画的板子。IO出来直接2条采集线。用万用表量采集线,均有电压稳定输出。
YiKai Chen:
回复 user5032796:
如果用示波器去量采集线也是看到稳定电压?