您好,
我重新添加了一个.zip的文件,使用IAR开发的,工程在
enddiv_use_adc\enddiv\Projects\zstack\Samples\SampleApp\CC2530DB
路径下
关于昨天的回复我纠正一些东西,我测试的时候根据不同的通道,连接的是相对应的引脚,回复的时候错写成了都连P0.0了,不好意思。
uint16 HalAdcRead (uint8 channel, uint8 resolution)
{
int16 reading = 0;
#if (HAL_ADC == TRUE)
uint8 i, resbits;
uint8 adcChannel = 1;
/*
* 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;
}
/* writing to this register starts the extra conversion */
ADCCON3 = channel | resbits | adcRef;
/* 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);
}
这是API的代码,我分析了一下,在
adc1 = HalAdcRead(HAL_ADC_CHANNEL_0, HAL_ADC_RESOLUTION_10);
的调用中
adcRef=0x40;
adcChannel=0x01;
resbits=0x10;
channel=0x00;
所以代码中对应寄存器设置的结果应该是
ADCCFG =0xFF;
ADCCON3=01 01 0000
AIN7参考电压 9bitENOB 分辨率 P0.0口
我上午做了一些测试,传感器的电压输出一直是稳定在800mV左右,对应不同的的通道,输出结果依旧是不一样的,
我上传的附件大于15m,上传到我的资源里头,我觉得应该是我代码出了问题,谢谢