拿走,不谢。void InitADC(void)
{
ADCON = 0x80; //允许ADC模块
ADT = 0xee;
/* ADC时钟为系统时钟的32分频,T(ad) = 32*(1/16M) = 2us
ADC采样时间为15个ADC时钟,T(s)= (14+1)*T(ad) = 30us
ADC转换时间为12*T(ad) + T(s) = 54us
*/
ADCH = (1<<7)|(1<<6); //通道配置 AN7 AN6
}
//调用前ch对应pin一定要设置在ad模式
uint8 GetADC_8(uint8 ch)
{
ADCON = 0x80+ch*2; //选择通道
ADCON |= 0x01; //开始ADC转换
while(ADCON&0x01);
return ADDH; //8bit
}
//调用前ch对应pin一定要设置在ad模式
uint16 GetADC_16(uint8 ch)
{
ADCON = 0x80+ch*2; //选择通道
ADCON |= 0x01; //开始ADC转换
while(ADCON&0x01);
return ADDH*4+ADDL; //10bit
}
|