//PIC16F685 ADC 采样(0-5V),AD值始终<700,反复参考数据手册未了原因,食不知味,睡不安寝
,请高手不吝指教,感谢!
#include
__CONFIG(0x0034);
#define LED1 RC6
#define LED2 RC7
UINT POT_AD;
void adc_init(void)
{
ADCON0 = 0x81;
ADCON1 = 0b00100000;
ADCON0bits.ADON = 1;
ADCON0bits.GO_nDONE = 0;
}
UINT adc_polling(UINT8 channel)
{
unsigned int rlt = 0;
ADCON0 &= 0x83;
ADCON0 |= (channel << 2);
ADCON0bits.GO_nDONE = 1;
while(ADCON0bits.GO_nDONE);
rlt =ADRESH;
rlt = rlt <<8;
rlt += ADRESL;
return rlt;
}
void io_init(void) //端口初始化
{
ANSEL = 0b01100100;
ANSELH = 0b11111000;
PORTC = 0b00000000;
TRISC = 0b00000111;
}
void system_init(void)
{
OSCCON = 0b01100111;
io_init(); //端口初始化
adc_init(); //ADC初始化
LED1= 0;
LED2= 0;
}
void main(void) //主函数
{
system_init(); //初始化端口
while(1)
{
POT_AD = adc_polling(5); //读取AD
if(POT_AD>700)
{
LED1 = 0;
LED2 = 1;
}
else
{
LED1 =1;
LED2 =0;
}
}
}