uint16 ReadLightData( void )
{
uint16 reading = 0;
HalAdcSetReference( HAL_ADC_REF_AVDD );
reading = HalAdcRead (HAL_ADC_CHANNEL_6 , HAL_ADC_RESOLUTION_12);
return reading;
}
char str[9]; uint16 value;
osal_memset(str, 0, 9);
value = ReadLightData();
sprintf(str, "%d ", value);
我接的是p0.6口,输出到串口后一直显示200,不管是接3.3v还是gnd都没变化。
请问怎么调用hal_adc里的uint16 HalAdcRead (uint8 channel, uint8 resolution)函数,该怎么配置才能采集到电压?
gancheng:
你可能还没有使能IO的ADC功能,设置APCFG寄存器。
Viki Shi:
参考这边的详细配置: http://blog.csdn.net/qq_15647227/article/details/53393253
yg wu:
回复 gancheng:
额,我去试试
yg wu:
回复 Viki Shi:
感谢!
yg wu:
回复 Viki Shi:
调用了下面的函数后还要配置那些东西才行啊?
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);}
yg wu:
回复 yg wu:
这两个我也配置了 APCFG |= 0x40; P0SEL |= 0x40;
TI中文支持网