pic 18f45k80 AD转换模块代码

2019-04-15 12:18发布

void Adc_Init(void) { TRISA |= 0x02; // RA1设置为输入 ANCON0bits.ANSEL1 = 1; // RA1设置值模拟信号 ADCON2bits.ADFM = 1; // 右对齐 ADCON2bits.ACQT = 1; // Acquisition time 7 = 20TAD 2 = 4TAD 1=2TAD ADCON2bits.ADCS = 2; // Clock conversion bits 6= FOSC/64 2=FOSC/32 ADCON1bits.VCFG0 = 0; // Vref+ = AVdd ADCON1bits.VCFG1 = 0; // Vref+ = AVdd ADCON1bits.VNCFG = 0; // Vref- = AVss // Select ADC channel-1 ADCON0bits.CHS0 = 1; ADCON0bits.CHS1 = 0; ADCON0bits.CHS2 = 0; ADCON0bits.CHS3 = 0; ADCON0bits.CHS4 = 0; ADCON0bits.ADON = 1; // Turn on ADC } u16 GetAdc_Value(void) { u8 i; u16AdValueH1 = 0; ADCON0bits.GO_NOT_DONE = 1; //// GO/DONE = 1; while(ADCON0bits.GO_NOT_DONE); /// u16AdValueH = ADRESH; u16AdValueL = ADRESL; /// 移位运算易出现未知错误,用以下方式提取高位数据 while(u16AdValueH > 0) { u16AdValueH1 += 256; u16AdValueH--; } u16AdValue = (u16AdValueH1 + u16AdValueL); return (u16AdValue); }