ADC采样不精准,求解答
uint16 ad_value;
uint8 ad_h,ad_l;
ad_value = HalAdcRead (0x07, HAL_ADC_RESOLUTION_10);
ad_h = ad_value>>8;
ad_l = ad_value&0xff;
HalUARTWrite(HAL_UART_PORT_0,&ad_h,1);
HalUARTWrite(HAL_UART_PORT_0,&ad_l,1);
程序如上,很简单,0V接到P0_7检测读出0x28-0x29,悬空检测读出0x1FFF,什么情况?看了用户手册ADCL最后俩位是无效位读出来会是0,可是实际API里面并没有把俩位移去,而且读出来还是有数值的!是不是要设置成输入才能读?、
如果是使用I/O口模拟时序进行读写,读的时候是要设置为输入的
Hi Carter Liu:
我是使用TI BLE1.32协议栈的API进行读的
函数如下:
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);
}
问题:我刚才试过用输入上拉,可是读出的值还是不准,如下:
void HalKeyInit(void)
{
uint16 ad_value;
uint8 ad_h,ad_l;
P0SEL &= ~0X07; //设置P0_0为普通IO口
P0DIR &= ~0X07; //按键在P0_0 口,设置为输入模式
P0INP &= ~0x07; //打开P0_0上拉电阻,不影响
ad_value = HalAdcRead (0x07, HAL_ADC_RESOLUTION_14);
ad_l = ad_value&0xff;
ad_h = ad_value>>8;
HalUARTWrite(HAL_UART_PORT_0,&ad_h,1);
HalUARTWrite(HAL_UART_PORT_0,&ad_l,1);
}
P0_7接地读出来的是0x028D,接VCC和悬空读出来是1F FF
你用的是哪款芯片啊,把电路部分也贴上看看吧
是CC2540,电路是一个CC2540模块的P0_7直接接地或者3.3V